-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
257 lines (229 loc) · 8.61 KB
/
content.js
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
(function() {
'use strict';
// 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.type = 'text/css';
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;
width: 200px;
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
/**
* 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('Asana Inbox Filter: Toolbar not found');
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() {
const tasks = document.querySelectorAll('.InboxExpandableThread');
const projects = new Set();
tasks.forEach(task => {
const projectTag = task.querySelector('.InboxIconAndNameContext-name--withDefaultColors');
if (projectTag) {
projects.add(projectTag.textContent.trim());
}
});
const currentValue = projectFilter.value;
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;
}
/**
* Filters tasks based on the selected project
* Shows/hides tasks based on their project tag matching the selection
* Shows all tasks if no project is selected
*/
function filterTasks() {
const selectedProject = projectFilter.value;
const tasks = document.querySelectorAll('.InboxExpandableThread');
tasks.forEach(task => {
const projectTag = task.querySelector('.InboxIconAndNameContext-name--withDefaultColors');
if (projectTag) {
task.style.display = (selectedProject === '' || projectTag.textContent.trim() === 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);
/**
* Sets up DOM observers to watch for changes in Asana's interface
* Monitors:
* - General DOM changes for page navigation
* - Inbox container for new tasks
* - Inbox feed for archived items
* Updates the filter accordingly when changes are detected
*/
function observeDOM() {
const observer = new MutationObserver(debouncedCheckAndAddFilter);
observer.observe(document.body, {
childList: true,
subtree: true
});
window.addEventListener('popstate', debouncedCheckAndAddFilter);
window.addEventListener('pushState', debouncedCheckAndAddFilter);
window.addEventListener('replaceState', debouncedCheckAndAddFilter);
const inboxContainer = document.querySelector('.InboxPage .SinglePaneScrollableContents');
if (inboxContainer) {
const taskObserver = new MutationObserver(() => {
updateProjectList();
filterTasks();
});
taskObserver.observe(inboxContainer, {
childList: true,
subtree: true
});
}
const inboxFeed = document.querySelector('.InboxFeed');
if (inboxFeed) {
const archiveObserver = new MutationObserver(debounce(() => {
updateProjectList();
filterTasks();
}, 500));
archiveObserver.observe(inboxFeed, {
childList: true,
subtree: true,
attributes: true,
attributeFilter: ['class']
});
}
}
// Initialize the script when the page loads
window.addEventListener('load', () => {
observeDOM();
debouncedCheckAndAddFilter();
});
})();