-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
48 lines (39 loc) · 1.33 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
function debounce(time, callback) {
let handle;
return (...args) => {
clearTimeout(handle);
handle = setTimeout(() => callback(...args), time);
};
}
function convertFiltersToRegex(filterStrings) {
return filterStrings.filter(Boolean).map(filter => new RegExp(filter, 'i'));
}
function getFiltersFromStorage() {
return new Promise(resolve => {
chrome.storage.sync.get({ filters: [] }, ({ filters }) => resolve(filters));
});
}
function filterAll(filters) {
for (const node of document.querySelectorAll('.stream-thumbnail')) {
const content = node.textContent;
node.hidden = filters.some(filter => content.match(filter));
}
}
function start(filtersFromStorage) {
let filters = convertFiltersToRegex(filtersFromStorage);
chrome.storage.onChanged.addListener(() => {
getFiltersFromStorage().then(newFiltersFromStorage => {
filters = convertFiltersToRegex(newFiltersFromStorage);
filterAll(filters);
});
});
const observer = new MutationObserver(debounce(50, () => filterAll(filters)));
observer.observe(document, {
childList: true,
subtree: true
});
}
Promise.all([
new Promise(resolve => document.addEventListener('DOMContentLoaded', resolve)),
getFiltersFromStorage()
]).then(([, filtersFromStorage]) => start(filtersFromStorage));