-
Notifications
You must be signed in to change notification settings - Fork 0
/
content-script.js
564 lines (455 loc) · 17.5 KB
/
content-script.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
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
'use strict';
(async () => {
const src = chrome.runtime.getURL("filterGlob-bundle.js");
// Adds filterGlob to the global namespace;
await import(src);
/**
* Globals
*/
// Prevents toolbar creation from keep triggering mutation observer.
const WIDGET_ID = 'glob-filter-widget';
const STORAGE_KEY = 'github-pr-filter-saved-filters';
// Because sometimes Github is changing page content and url without triggering a reload, so we need to use a mutation observer.
// If the page reload, the whole content-script will be reloaded.
const filterObserver = new MutationObserver(debounce(run, 500));
filterObserver.observe(document.body, { childList: true, subtree: true });
class Widget {
widget;
trigger;
dropdown;
filterGlobToggleDOM;
filterRegexToggleDOM;
filterType = "glob";
filterInputDOM;
filterRegexFlagsTitleDOM;
filterRegexFlagsDOM;
filterRegexFlags = "";
filterValue = "";
savedFiltersDOM;
constructor() {
this.widget = this.constructWidget();
this.trigger = this.constructTrigger();
this.dropdown = this.constructDropdown();
this.widget.appendChild(this.trigger);
this.widget.appendChild(this.dropdown);
this.setupSavedFiltersListener();
}
constructGithubContainer() {
const container = document.createElement('div');
container.style.padding = "12px";
container.style.borderBottom = "1px solid var(--color-select-menu-border-secondary)";
return container;
}
constructWidget() {
const widget = document.createElement('details');
widget.className = "details-reset details-overlay diffbar-item toc-select select-menu ml-0 ml-sm-3"
widget.id = WIDGET_ID;
return widget;
}
constructTrigger() {
const trigger = document.createElement('summary');
trigger.className = "btn-link Link--muted select-menu-button";
trigger.ariaHasPopup = "menu";
trigger.role = "button";
const text = document.createElement('strong');
text.textContent = "Advanced Filter";
trigger.appendChild(text);
return trigger;
}
constructDropdown() {
const dropdown = document.createElement('details-menu');
dropdown.className = "select-menu-modal position-absolute";
dropdown.style.zIndex = 99;
dropdown.role = "menu";
dropdown.preload = true;
const header = this.constructHeader();
const filterOptions = this.constructFilterOptions();
const filterInput = this.constructFilterInput();
const filterSubmit = this.constructFilterSubmit();
const savedFilters = this.constructSavedFilters();
dropdown.appendChild(header);
dropdown.appendChild(filterOptions);
dropdown.appendChild(filterInput);
dropdown.appendChild(filterSubmit);
dropdown.appendChild(savedFilters);
return dropdown;
}
constructHeader() {
const header = document.createElement('div');
header.className = "select-menu-header";
const text = document.createElement('span');
text.className = "select-menu-title";
text.textContent = "Filter changed files";
header.appendChild(text);
return header;
}
constructFilterOptions() {
const container = this.constructGithubContainer();
container.style.display = "flex";
container.style.justifyContent = "space-around";
const globContainer = document.createElement('div');
const globToggle = document.createElement('input');
globToggle.type = "radio";
globToggle.id = "glob";
globToggle.name = "filter-type";
globToggle.value = "glob";
globToggle.checked = true;
globToggle.style.marginRight = '4px';
globToggle.addEventListener('change', () => {
this.filterType = "glob";
this.resetFilterInput();
this.filterRegexFlagsTitleDOM.style.display = "none";
this.filterRegexFlagsDOM.style.display = "none";
});
this.filterGlobToggleDOM = globToggle;
const globLabel = document.createElement('label');
globLabel.for = 'glob';
globLabel.textContent = 'Glob';
globContainer.appendChild(globToggle);
globContainer.appendChild(globLabel);
const regexContainer = document.createElement('div');
const regexToggle = document.createElement('input');
regexToggle.type = "radio";
regexToggle.id = "regex";
regexToggle.name = "filter-type";
regexToggle.value = "regex";
regexToggle.style.marginRight = '4px';
regexToggle.addEventListener('change', () => {
this.filterType = "regex";
this.resetFilterInput();
this.filterRegexFlagsTitleDOM.style.display = "block";
this.filterRegexFlagsDOM.style.display = "block";
});
this.filterRegexToggleDOM = regexToggle;
const regexLabel = document.createElement('label');
regexLabel.for = 'regex';
regexLabel.textContent = 'Regex';
regexContainer.appendChild(regexToggle);
regexContainer.appendChild(regexLabel);
container.appendChild(globContainer);
container.appendChild(regexContainer);
return container;
}
constructFilterInput() {
const container = this.constructGithubContainer();
container.style.display = "flex";
container.style.justifyContent = "space-around";
container.style.alignItems = "center";
const filterInput = document.createElement('input');
filterInput.type = 'text';
filterInput.className = "form-control";
filterInput.placeholder = "Filter changed files";
filterInput.style.width = "100%";
filterInput.addEventListener('change', () => {
this.filterValue = filterInput.value;
});
this.filterInputDOM = filterInput;
const flagsTitle = document.createElement('strong');
flagsTitle.textContent = "Flags";
flagsTitle.style.marginLeft = "8px";
flagsTitle.style.marginRight = "4px";
flagsTitle.style.display = "none";
const filterRegexFlags = document.createElement('input');
filterRegexFlags.type = 'text';
filterRegexFlags.className = "form-control";
filterRegexFlags.placeholder = "Regex flags";
// hide on initial since glob is default.
filterRegexFlags.style.display = "none";
filterRegexFlags.addEventListener('change', () => {
this.filterRegexFlags = filterRegexFlags.value;
});
this.filterRegexFlagsTitleDOM = flagsTitle;
this.filterRegexFlagsDOM = filterRegexFlags;
container.appendChild(filterInput);
container.appendChild(flagsTitle);
container.appendChild(filterRegexFlags);
return container;
}
constructFilterSubmit() {
const container = this.constructGithubContainer();
const submit = document.createElement('button');
// Copying Github's class names
submit.className = "btn btn-lg btn-primary";
submit.textContent = "Run filter";
submit.style.width = "100%";
submit.addEventListener('click', () => {
this.filterFiles(this.filterType);
});
const savedFilterButton = this.constructSaveFilterButton();
container.appendChild(savedFilterButton);
container.appendChild(submit);
return container;
}
constructSaveFilterButton() {
const save = document.createElement('button');
// Copying Github's class names
save.className = "btn btn-lg";
save.textContent = "Save Filter";
save.style.width = "100%";
save.style.backgroundColor = "blue";
save.addEventListener('click', () => {
this.saveFilter();
});
return save;
}
constructSavedFilters() {
const container = document.createElement('div');
const header = document.createElement('div');
header.className = "select-menu-header";
header.style.display = "flex";
header.style.alignItems = "center";
header.style.justifyContent = "center";
const text = document.createElement('span');
text.className = "select-menu-title";
text.textContent = "Saved filters (<name>|<value>|<flags>|<type>)";
header.appendChild(text);
const selection = document.createElement('div');
selection.style.maxHeight = "300px";
selection.style.overflow = "auto";
container.appendChild(header);
container.appendChild(selection);
this.savedFiltersDOM = selection;
this.updateSavedFilters(selection);
return container;
}
updateSavedFilters(container) {
let savedFiltersFragment = document.createDocumentFragment();
chrome.storage.sync.get([ STORAGE_KEY ], (result) => {
const savedFilters = result[STORAGE_KEY];
if (!savedFilters || savedFilters.length <= 0) {
container.innerHTML = "";
return;
}
savedFilters.forEach(filter => {
const savedFilter = document.createElement('div');
savedFilter.className = "select-menu-item";
savedFilter.role = "menuitem";
savedFilter.style.display = "flex";
savedFilter.style.justifyContent = "space-between";
const metaContainer = document.createElement('div');
metaContainer.style.display = "flex";
const filterName = document.createElement('span');
filterName.textContent = filter.name + ' | ';
filterName.style.whiteSpace = "pre-wrap";
const filterValue = document.createElement('span');
filterValue.textContent = filter.value + ' | ';
filterValue.style.whiteSpace = "pre-wrap";
const filterType = document.createElement('span');
filterType.textContent = filter.type;
filterType.style.whiteSpace = "pre-wrap";
metaContainer.appendChild(filterName);
metaContainer.appendChild(filterValue);
if (filter.flags) {
const filterFlags = document.createElement('span');
filterFlags.textContent = filter.flags + ' | ';
filterFlags.style.whiteSpace = "pre-wrap";
metaContainer.appendChild(filterFlags);
}
metaContainer.appendChild(filterType);
const controlsContainer = document.createElement('div');
controlsContainer.style.display = "flex";
const applyButton = document.createElement('button');
applyButton.className = "btn btn-sm btn-primary";
applyButton.style.marginRight = "8px";
applyButton.textContent = "Apply";
const deleteButton = document.createElement('button');
deleteButton.className = "btn btn-sm btn-danger";
deleteButton.textContent = "Delete";
applyButton.addEventListener('click', () => {
this.applySavedFilter(filter);
});
deleteButton.addEventListener('click', () => {
this.deleteSavedFilter(filter.name);
});
controlsContainer.appendChild(applyButton);
controlsContainer.appendChild(deleteButton);
savedFilter.appendChild(metaContainer);
savedFilter.appendChild(controlsContainer);
savedFiltersFragment.appendChild(savedFilter);
});
container.innerHTML = "";
container.appendChild(savedFiltersFragment)
});
}
/**
* Switch toggle to the right type, hopefully that resets everything.
* Replace filter input value;
*/
applySavedFilter({ value, type, flags}) {
switch (type) {
case "glob":
this.filterGlobToggleDOM.click();
this.filterInputDOM.value = value;
this.filterValue = value;
break;
case "regex":
this.filterRegexToggleDOM.click();
this.filterInputDOM.value = value;
this.filterValue = value;
this.filterRegexFlagsDOM.value = flags;
this.filterRegexFlags = flags;
break;
default:
window.alert('Apply failed');
break;
}
}
deleteSavedFilter(name) {
if (window.confirm(`Are you sure you want to delete ${name}`)) {
chrome.storage.sync.get([ STORAGE_KEY ] , (result) => {
const savedFilters = result[STORAGE_KEY];
const index = savedFilters.findIndex((filter) => filter.name === name);
savedFilters.splice(index, 1);
chrome.storage.sync.set({ [STORAGE_KEY]: [ ...savedFilters ]});
});
}
}
saveFilter() {
if (!this.filterValue) {
window.alert('No value provided. Saved failed');
return;
}
const name = window.prompt('Enter a name for this filter');
if (!name) {
window.alert("No name provided. Save failed");
return;
}
chrome.storage.sync.get([ STORAGE_KEY ] , (result) => {
const savedFilters = result[STORAGE_KEY];
const newFilter = {
name,
value: this.filterValue,
type: this.filterType,
flags: this.filterRegexFlags,
}
if (!savedFilters) {
chrome.storage.sync.set({ [STORAGE_KEY]: [ newFilter ]});
} else {
chrome.storage.sync.set({ [STORAGE_KEY]: [ ...savedFilters, newFilter ]});
}
});
}
resetFilterInput() {
this.filterInputDOM.value = "";
this.filterValue = "";
this.filterRegexFlagsDOM.value = "";
this.filterRegexFlags = "";
}
filterFiles(type) {
let filteredFiles;
try {
const changedFiles = [...this._getAllChangedFiles()];
switch(type) {
case "regex":
filteredFiles = this._filterByRegex(changedFiles);
break;
case "glob":
default:
filteredFiles = this._filterByGlob(changedFiles);
break;
}
// We first, collapse everything. This is good for also handling resetting the initial state when the filter changes.
this._collapseFiles(changedFiles);
filteredFiles.forEach(file => {
const button = this._getCollapseButton(file);
// If file is currently collapsed, click to expand it.
if (button.getAttribute('aria-expanded') === 'false') {
button.click();
}
});
} catch (error) {
window.alert(error);
}
}
setupSavedFiltersListener() {
chrome.storage.onChanged.addListener((changes) => {
for (let [key, _] of Object.entries(changes)) {
if (key === STORAGE_KEY) {
this.updateSavedFilters(this.savedFiltersDOM);
}
}
});
}
/**
* TODO:
* 1. I think we can optimize this time complexity by using a dictionary.
* 2. I can we think can support multiple glob pattern in one string, similar to vscode search by splitting the string by "," and run `filterGlob` on each splitted output.
* Then we can merge the output filenames back together and remove duplicates by leveraging dictionary from 1). (e.g, globString = "globPattern1, globPattern2")
*/
_filterByGlob(changedFiles) {
const changedFilenames = changedFiles.map(this._getFilename);
const filteredFilenames = filterGlob(changedFilenames, this.filterValue);
// Get files that match the glob.
const filteredFiles = changedFiles.filter(file => {
const name = this._getFilename(file);
return filteredFilenames.indexOf(name) >= 0;
});
return filteredFiles;
}
_filterByRegex(changedFiles) {
const changedFilenames = changedFiles.map(this._getFilename);
// TODO: Implement a way to allow user to change regex flag.
const re = this.filterRegexFlags ? new RegExp(this.filterValue, this.filterRegexFlags) : new RegExp(this.filterValue);
const filteredFilenames = changedFilenames.filter(filename => re.test(filename));
// Get files that match the glob.
const filteredFiles = changedFiles.filter(file => {
const name = this._getFilename(file);
return filteredFilenames.indexOf(name) >= 0;
});
return filteredFiles;
}
_collapseFiles(changedFiles) {
changedFiles.forEach(changedFile => {
const button = this._getCollapseButton(changedFile);
if (button.getAttribute('aria-expanded') === 'true') {
button.click();
}
})
}
_getFilename(fileDOM) {
return fileDOM.querySelector('a').getAttribute('title');
}
_getAllChangedFiles() {
const filesContainer = document.querySelector('#files');
const files = filesContainer.querySelectorAll('[data-details-container-group]');
return files;
}
_getCollapseButton(fileDOM) {
return fileDOM.querySelector('button');
}
}
function debounce(fn, rate) {
let timer;
return function() {
if (timer) {
clearTimeout(timer);
timer = null;
}
timer = setTimeout(() => {
fn.apply(this, arguments)
}, rate);
}
}
function isPRPage() {
// location.href doesn't seem to capture 'www.', so this pattern here is a bit different from the manifest.json
return filterGlob([location.href], '*://*github.com/**/pull/**/files*').length > 0 || filterGlob([location.href], '*://*githubprivate.com/**/pull/**/files*').length > 0;
}
function run() {
if (!isPRPage()) {
return;
}
if (document?.querySelector(`#${WIDGET_ID}`)) {
return;
}
const toolbar = getPRToolbar();
const widget = new Widget();
toolbar.appendChild(widget.widget);
function getPRToolbar() {
const toolbarContainer = document.querySelector('.pr-toolbar');
const actions = toolbarContainer.querySelector('details');
const toolbar = actions.parentNode;
return toolbar;
}
}
run();
})();