-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
378 lines (331 loc) · 14.2 KB
/
Copy pathcontent.js
File metadata and controls
378 lines (331 loc) · 14.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
(function() {
'use strict';
if (window.__inboxFilterLoaded) return;
window.__inboxFilterLoaded = true;
console.log('[InboxFilter] Script executing - top level');
// Polyfill for GM_addStyle to ensure compatibility with browsers
// that don't support Tampermonkey's GM_addStyle function
function GM_addStyle(css) {
var head, style;
head = document.getElementsByTagName('head')[0];
if (!head) { return; }
style = document.createElement('style');
style.innerHTML = css.replace(/;/g, ' !important;');
head.appendChild(style);
}
// CSS styles for the project filter UI components
// Includes styles for the container, select dropdown, and refresh button
GM_addStyle(`
#project-filter-container {
display: flex;
align-items: center;
margin: 0 10px;
}
#project-filter {
margin-left: 5px;
padding: 5px 15px;
border: 1px solid #E3E3E3;
border-radius: 8px;
background-color: #F7F7F7;
color: #333333;
font-size: 16px;
box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.1);
transition: all 0.3s ease;
cursor: pointer;
min-width: 200px;
max-width: 350px;
text-overflow: ellipsis;
overflow: hidden;
white-space: nowrap;
outline: none;
}
#project-filter:hover {
background-color: #EFEFEF;
border-color: #DADADA;
}
#refresh-filter {
cursor: pointer;
padding: 5px;
border-radius: 50%;
transition: background-color 0.3s ease;
}
#refresh-filter:hover {
background-color: #EFEFEF;
}
#refresh-filter svg {
width: 20px;
height: 20px;
fill: #6d6d6d;
}
`);
// Global variables to store UI elements
let projectFilter; // Stores the select dropdown element
let refreshIcon; // Stores the refresh button element
const MAX_RETRIES = 40; // Increased to 20 seconds (40 * 500ms)
/**
* Creates the project filter UI elements
* Includes a refresh button and a dropdown select for projects
* @returns {HTMLElement} Container div with the filter elements
*/
function createProjectFilter() {
const container = document.createElement('div');
container.id = 'project-filter-container';
refreshIcon = document.createElement('div');
refreshIcon.id = 'refresh-filter';
refreshIcon.innerHTML = `
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
<path d="M17.65 6.35A7.958 7.958 0 0 0 12 4c-4.42 0-7.99 3.58-7.99 8s3.57 8 7.99 8c3.73 0 6.84-2.55 7.73-6h-2.08A5.99 5.99 0 0 1 12 18c-3.31 0-6-2.69-6-6s2.69-6 6-6c1.66 0 3.14.69 4.22 1.78L13 11h7V4l-2.35 2.35z"/>
</svg>
`;
refreshIcon.addEventListener('click', forceRefresh);
projectFilter = document.createElement('select');
projectFilter.id = 'project-filter';
projectFilter.innerHTML = '<option value="">Projects</option>';
container.appendChild(refreshIcon);
container.appendChild(projectFilter);
return container;
}
/**
* Adds the project filter to the Asana toolbar
* Only adds if the filter doesn't already exist
* Initializes the project list and sets up event listeners
*/
function addProjectFilter() {
if (document.getElementById('project-filter-container')) return;
const toolbar = document.querySelector('.GlobalTopbarStructure-middleChildren, .GlobalTopbarStructure-search');
if (!toolbar) {
console.warn('[InboxFilter] Toolbar not found, cannot add filter UI.');
return;
}
toolbar.style.display = 'flex';
toolbar.style.alignItems = 'center';
toolbar.appendChild(createProjectFilter());
updateProjectList();
projectFilter.addEventListener('change', filterTasks);
}
/**
* Updates the project list in the dropdown
* Scans all visible inbox tasks and collects unique project names
* Maintains the current selection when updating the list
*/
function updateProjectList() {
if (!projectFilter) return;
const tasks = document.querySelectorAll('.InboxExpandableThread');
const projects = new Set();
tasks.forEach(task => {
const projectTag =
task.querySelector('.InboxIconAndNameContext-name--withDefaultColors') ||
task.querySelector('[class*="InboxIconAndNameContext-name"]');
if (projectTag) {
projects.add(projectTag.textContent.trim());
}
});
const currentValue = projectFilter.value;
projectFilter.removeEventListener('change', filterTasks);
projectFilter.innerHTML = '<option value="">Projects</option>';
Array.from(projects).sort().forEach(project => {
const option = document.createElement('option');
option.value = project;
option.textContent = project;
projectFilter.appendChild(option);
});
projectFilter.value = currentValue;
projectFilter.addEventListener('change', filterTasks);
}
/**
* Determina se un thread della inbox appartiene a un progetto specifico
* @param {HTMLElement} thread - L'elemento thread della inbox
* @param {string} projectName - Il nome del progetto da cercare
* @returns {boolean} - True se il thread appartiene al progetto o se non ha progetto
*/
function threadBelongsToProject(thread, projectName) {
// Se non è selezionato alcun progetto, mostra tutto
if (!projectName) return true;
// Cerca il tag del progetto usando diversi selettori possibili
const projectTag =
thread.querySelector('.InboxIconAndNameContext-name--withDefaultColors') ||
thread.querySelector('[class*="InboxIconAndNameContext-name"]');
// Se troviamo un tag di progetto, confrontiamo il suo nome
if (projectTag) {
return projectTag.textContent.trim() === projectName;
}
// Per i thread senza tag di progetto (come i riepiloghi AI),
// controlliamo se il titolo contiene il nome del progetto
const threadTitle = thread.querySelector('.InboxLinkifiedThreadTitle-link');
if (threadTitle) {
// Opzione 1: Nascondi tutti i thread senza tag di progetto esplicito
// return false;
// Opzione 2: Mostra thread senza tag solo se il titolo contiene il nome del progetto
return threadTitle.textContent.includes(projectName);
}
// Se non troviamo né tag né titolo, nascondiamo il thread quando è attivo un filtro
return false;
}
/**
* Filtra i task in base al progetto selezionato
* Mostra/nasconde i task in base al loro tag di progetto o al contenuto del titolo
*/
function filterTasks() {
if (!projectFilter) return;
console.log('[InboxFilter] filterTasks function started.');
const selectedProject = projectFilter.value;
const threads = document.querySelectorAll('.InboxExpandableThread');
let matchedThreads = 0;
let totalThreads = 0;
const now = new Date().toISOString();
console.log(`[InboxFilter] [${now}] filterTasks: Filtering for "${selectedProject}". ${threads.length} threads in DOM.`);
threads.forEach(thread => {
totalThreads++;
const shouldShow = threadBelongsToProject(thread, selectedProject);
if (shouldShow) matchedThreads++;
thread.style.display = shouldShow ? '' : 'none';
});
console.log(`[InboxFilter] [${now}] filterTasks: Finished. Showed ${matchedThreads}/${totalThreads} threads.`);
// Hide/show non-thread elements (e.g. "Le tue attività scadute", "Progetti in corso")
const transitionGroup = document.querySelector('.InboxFeed-transitionGroup');
if (transitionGroup) {
Array.from(transitionGroup.children).forEach(child => {
if (!child.querySelector('.InboxExpandableThread')) {
child.style.display = selectedProject ? 'none' : '';
}
});
}
}
/**
* Manually triggers a refresh of the project list and reapplies filters
* Called when the refresh button is clicked
*/
function forceRefresh() {
updateProjectList();
filterTasks();
}
/**
* Removes the project filter from the DOM
* Called when navigating away from the inbox
*/
function removeProjectFilter() {
document.getElementById('project-filter-container')?.remove();
}
/**
* Debounce utility function to limit the rate of function execution
* @param {Function} func - The function to debounce
* @param {number} wait - The debounce delay in milliseconds
* @returns {Function} Debounced version of the input function
*/
function debounce(func, wait) {
let timeout;
return function executedFunction(...args) {
const later = () => {
clearTimeout(timeout);
func(...args);
};
clearTimeout(timeout);
timeout = setTimeout(later, wait);
};
}
/**
* Debounced function to check the current page and manage filter visibility
* Adds filter on inbox page and removes it on other pages
*/
const debouncedCheckAndAddFilter = debounce(() => {
const isInInbox = window.location.pathname.includes('/inbox');
if (isInInbox) {
addProjectFilter();
} else {
removeProjectFilter();
}
}, 300);
let archiveObserverInstance = null;
let inboxFeedObserverInterval = null;
let inboxFeedRetryCount = 0;
function tryObserveInboxFeed() {
// Early return if not on inbox page - no point looking for InboxFeed
if (!window.location.pathname.includes('/inbox')) {
return;
}
let inboxFeed = document.querySelector('.InboxFeed');
// Fallback: try to find the feed via a thread element if the main class is missing/renamed
if (!inboxFeed) {
const thread = document.querySelector('.InboxExpandableThread');
if (thread && thread.parentElement) {
inboxFeed = thread.parentElement;
console.log('[InboxFilter] tryObserveInboxFeed: Found inbox feed via fallback (thread parent).');
}
}
if (inboxFeed) {
console.log('[InboxFilter] tryObserveInboxFeed: Inbox feed FOUND.', inboxFeed);
if (inboxFeedObserverInterval) {
clearInterval(inboxFeedObserverInterval);
inboxFeedObserverInterval = null;
inboxFeedRetryCount = 0;
}
if (archiveObserverInstance && archiveObserverInstance.targetNode === inboxFeed) {
console.log('[InboxFilter] tryObserveInboxFeed: Observer already attached to this InboxFeed element.');
return; // Already observing the exact same element
}
if (archiveObserverInstance) { // It exists, but targetNode is different or null
archiveObserverInstance.disconnect();
console.log('[InboxFilter] tryObserveInboxFeed: Disconnected existing archiveObserverInstance from old/different target.');
}
archiveObserverInstance = new MutationObserver(debounce(() => {
const now = new Date().toISOString();
console.log(`[InboxFilter] [${now}] inboxFeedObserver (archiveObserverInstance) FIRED. Re-applying updateProjectList and filterTasks.`);
updateProjectList();
filterTasks();
}, 500));
archiveObserverInstance.observe(inboxFeed, {
childList: true,
subtree: true,
attributes: true,
attributeFilter: ['class']
});
archiveObserverInstance.targetNode = inboxFeed; // Store the node we are observing
console.log('[InboxFilter] tryObserveInboxFeed: Attached new archiveObserverInstance to:', inboxFeed);
} else {
inboxFeedRetryCount++;
if (inboxFeedRetryCount > MAX_RETRIES) {
console.warn(`[InboxFilter] Max retries (${MAX_RETRIES}) reached for inboxFeed. Observer not attached. Selector: '.InboxFeed'`);
if (inboxFeedObserverInterval) {
clearInterval(inboxFeedObserverInterval);
inboxFeedObserverInterval = null;
}
return;
}
console.log(`[InboxFilter] tryObserveInboxFeed: Inbox feed not found (attempt ${inboxFeedRetryCount}/${MAX_RETRIES}), will retry. Selector: '.InboxFeed'`);
if (!inboxFeedObserverInterval) {
inboxFeedObserverInterval = setInterval(tryObserveInboxFeed, 500);
}
}
}
function observeDOM() {
const bodyObserver = new MutationObserver(() => {
debouncedCheckAndAddFilter();
tryObserveInboxFeed();
});
bodyObserver.observe(document.body, {
childList: true,
subtree: true
});
window.addEventListener('popstate', () => {
debouncedCheckAndAddFilter();
tryObserveInboxFeed();
});
// Monkey-patch History API to intercept SPA navigation
const originalPushState = history.pushState;
history.pushState = function(...args) {
originalPushState.apply(this, args);
debouncedCheckAndAddFilter();
tryObserveInboxFeed();
};
const originalReplaceState = history.replaceState;
history.replaceState = function(...args) {
originalReplaceState.apply(this, args);
debouncedCheckAndAddFilter();
tryObserveInboxFeed();
};
// Initial attempt
tryObserveInboxFeed();
}
observeDOM();
debouncedCheckAndAddFilter();
})();