-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
54 lines (41 loc) · 1.3 KB
/
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
'use strict';
let KEYWORD = '';
window.onscroll = function () {
excludeAll(KEYWORD);
}
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.message == 'replaceAll') {
console.log('EVENT: manual');
const { keyword } = request;
replaceAll({ keyword });
sendResponse({ message: 'done ' });
}
});
window.addEventListener('load', () => {
console.log('EVENT: load');
chrome.runtime.sendMessage({ message: 'tabLoaded' }, replaceAll);
});
function replaceAll({ keyword }) {
KEYWORD = keyword;
excludeAll(keyword);
}
function escapeRegExp(string) {
return string.replace(/[.*+\-?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}
function includes(string, keyword) {
const regex = new RegExp(`${escapeRegExp(keyword)}`, 'i');
return regex.test(string);
}
function excludeAll(keyword) {
console.log(`Excluding all << ${keyword} >>`);
if (keyword == '') return;
document.querySelectorAll('div .g').forEach((div) => {
includes(div.innerHTML, keyword) && div.remove();
});
document.querySelectorAll('ytd-video-renderer').forEach((div) => {
includes(div.innerHTML, keyword) && div.remove();
});
document.querySelectorAll('div').forEach((div) => {
div.dataset.rank && includes(div.innerHTML, keyword) && div.remove();
});
}