-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.js
142 lines (131 loc) · 4.08 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
let previewCount = null;
let lastUrl = null;
let interval;
let redirect = {
channelNames: [],
targets: []
};
let keywords = [];
let embeds = [];
/**
* Scans DOM elments from the selectors array for
* provided keywords.
* @param selectors array
* @param keywords array
*/
function antiBonk(selectors, keywords) {
selectors.forEach(element => {
const text = element.innerText;
keywords.every(keyword => {
if (text.toLowerCase().includes(keyword.toLowerCase())) {
element.parentNode.innerHTML = getRandom(embeds);
return false;
}
return true;
});
});
}
/**
* Scans the url for keywords and redirects the user
* on a match.
* @param url string
*/
function runBoyRun(url) {
url = url.split('/').reverse()[0];
if (
!redirect ||
!Array.isArray(redirect.channelNames) ||
!Array.isArray(redirect.targets)
) {
return;
}
redirect.channelNames.every(keyword => {
if (url.toLowerCase().includes(keyword)) {
window.location.href = getRandom(redirect.targets);
return false;
}
return true;
});
}
/**
* Get random element from array.
*/
function getRandom(list) {
return list[Math.floor((Math.random()*list.length))];
}
/**
* Scans known DOM elements which contain channel previews.
*/
function scanForBonks() {
const url = window.location.href;
let selectors = ['.shelf-element__impression-wrapper', '.tw-flex.tw-flex-column.tw-mg-0 .tw-hover-accent-effect .tw-aspect', '.tw-flex.tw-flex-column.tw-mg-0', '.sc-AxjAm.dBWNsP'];
selectors = selectors.map(s => [...document.querySelectorAll(s)]).flat(2);
if (
previewCount === null ||
(previewCount != selectors.length) ||
lastUrl === null ||
(lastUrl != url)
) {
runBoyRun(url);
antiBonk(selectors, keywords);
}
previewCount = selectors.length;
}
function loadListsFromStorage() {
chrome.storage.sync.get("filterLists", (data) => {
let lists = data.filterLists;
if (lists && lists.length > 0) {
lists.forEach((list, index) => {
fetch(list.url)
.then((r) => r.json())
.then(e => {
lists[index] = e;
keywords = [...keywords, ...e.keywords];
embeds = [...embeds, ...e.embeds];
redirect.channelNames = [...redirect.channelNames, ...e.redirect.channelNames];
redirect.targets = [...redirect.targets, ...e.redirect.targets];
if (index === (lists.length-1)) {
// save lists in storage
chrome.storage.sync.set({'filterLists': lists});
// remove duplicates from keywords
keywords = [...new Set(keywords)];
// start scan
setTimeout(burst, 500);
interval = setInterval(scanForBonks, 5000);
}
// load localList
if ((index+1) == lists.length) {
loadLocalListFromStorage();
}
})
});
} else {
loadLocalListFromStorage();
// start scan
setTimeout(burst, 500);
interval = setInterval(scanForBonks, 5000);
}
});
}
function loadLocalListFromStorage() {
chrome.storage.sync.get("localList", (data) => {
let list = data.localList;
if (Array.isArray(list) && list[0]) {
keywords = [...keywords, ...(list[0].keywords)];
if (!embeds || (embeds.length === 0)) {
embeds = [''];
}
}
});
}
/**
* Run three scans in quick succession.
*/
function burst() {
setTimeout(scanForBonks, 500);
setTimeout(scanForBonks, 1000);
setTimeout(scanForBonks, 1500);
}
window.onload = function() {
loadListsFromStorage();
}