-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.js
250 lines (240 loc) · 9.66 KB
/
options.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
var modal = document.getElementById("modal");
var Savebtn = document.getElementById("save");
var span = document.getElementById("close");
var advFilter = document.getElementById('useAdv');
var antiPrnFilter = document.getElementById('useAntiPrn');
var suspFilter = document.getElementById('useSuspBlock');
var updtBtn = document.getElementById('updtchk');
var checkStateChanged = false;
// Saves options to chrome.storage
function save_options() {
var useAdv = document.getElementById('useAdv').checked;
var useAntiprn = document.getElementById('useAntiPrn').checked;
var useSusp = document.getElementById('useSuspBlock').checked;
chrome.storage.sync.set({ advStat: useAdv, antiPrnStat: useAntiprn, suspStat: useSusp }, function () {
// Update status to let user know options were saved.
var status = document.getElementById('status');
status.textContent = 'Options saved.';
setTimeout(function () {
status.textContent = '';
}, 750);
});
var status = document.getElementById('status');
status.textContent = 'Options saved.';
setTimeout(function () {
status.textContent = '';
}, 750);
if (useAdv) {
chrome.declarativeNetRequest.updateEnabledRulesets({
enableRulesetIds: ["advLIST"]
});
}
if (useAntiprn) {
chrome.declarativeNetRequest.updateEnabledRulesets({
enableRulesetIds: ["antiprnLIST"]
});
}
if (useSusp) {
chrome.declarativeNetRequest.updateEnabledRulesets({
enableRulesetIds: ["suspLIST"]
});
}
if (!useAdv) {
chrome.declarativeNetRequest.updateEnabledRulesets({
disableRulesetIds: ["advLIST"]
});
}
if (!useAntiprn) {
chrome.declarativeNetRequest.updateEnabledRulesets({
disableRulesetIds: ["antiprnLIST"]
});
}
if (!useSusp) {
chrome.declarativeNetRequest.updateEnabledRulesets({
disableRulesetIds: ["suspLIST"]
});
}
checkStateChanged = false;
}
advFilter.onclick = function () {
if (advFilter.checked) { modal.style.display = "block"; }
}
var settings = document.querySelectorAll("input[type='checkbox']");
settings.forEach(element => {
element.onchange = () => {
Savebtn.classList.remove('saveBefore');
Savebtn.classList.add('saveAfter');
}
});
function restore_options() {
chrome.storage.sync.get(['advStat', 'antiPrnStat', 'suspStat'], function (items) {
advFilter.checked = items.advStat;
antiPrnFilter.checked = items.antiPrnStat;
suspFilter.checked = items.suspStat;
if (items.advStat) {
chrome.declarativeNetRequest.updateEnabledRulesets({
enableRulesetIds: ["advLIST"]
});
}
if (items.antiPrnStat) {
chrome.declarativeNetRequest.updateEnabledRulesets({
enableRulesetIds: ["antiprnLIST"]
});
}
if (items.suspStat) {
chrome.declarativeNetRequest.updateEnabledRulesets({
enableRulesetIds: ["suspLIST"]
});
}
if (!items.advStat) {
chrome.declarativeNetRequest.updateEnabledRulesets({
disableRulesetIds: ["advLIST"]
});
}
if (!items.antiPrnStat) {
chrome.declarativeNetRequest.updateEnabledRulesets({
disableRulesetIds: ["antiprnLIST"]
});
}
if (!items.suspStat) {
chrome.declarativeNetRequest.updateEnabledRulesets({
disableRulesetIds: ["suspLIST"]
});
}
});
}
span.onclick = function () {
modal.style.display = "none";
}
window.onclick = function (event) {
if (event.target == modal) { modal.style.display = "none"; }
}
if (updtBtn) {
updtBtn.onclick = function () {
var updtUrl = "https://raw.githubusercontent.com/Rutuj-Runwal/RR-Adblocker/main/patch.txt";
try {
fetch(updtUrl)
.then(response => response.text())
.then(function (text) {
let urlPatchDt = text.split("\n");
let patchRules = [];
let patchRulesId = [];
let id = 4950;
if (urlPatchDt.length > 2) {
for (let i = 2; i < urlPatchDt.length; i++) {
patchRulesId.push(id);
if (urlPatchDt[i].includes("U: ")) {
var data = urlPatchDt[i].split(' ');
var blockOrAllow = data[2].trim() === "A" ? "allow" : "block";
var priorityBorA = blockOrAllow === "allow" ? 2 : 1;
patchRules.push({
"id": id++,
"priority": priorityBorA,
"action": {
"type": blockOrAllow,
},
"condition": {
"urlFilter": "||" + data[1].trim() + "^",
"resourceTypes": [
"main_frame",
"sub_frame",
"script",
"xmlhttprequest",
"ping",
"csp_report",
"stylesheet",
"media",
"websocket",
"image",
"webtransport",
"webbundle",
"other"
]
}
})
}
else if (urlPatchDt[i].includes("R: ")) {
console.log(urlPatchDt[i].split(' ')[1]);
patchRules.push({
"id": id++,
"priority": 1,
"action": {
"type": "block",
},
"condition": {
"regexFilter": urlPatchDt[i].split(' ')[1].trim(),
"resourceTypes": [
"main_frame",
"sub_frame",
"script",
"xmlhttprequest",
"ping",
"csp_report",
"stylesheet",
"media",
"websocket",
"image",
"webtransport",
"webbundle",
"other"
]
}
})
}
}
patchRulesId = patchRulesId.slice(0, patchRulesId.length - 1);
chrome.declarativeNetRequest.updateDynamicRules({
addRules: patchRules,
removeRuleIds: patchRulesId,
});
}
console.log(patchRules);
console.log(patchRulesId);
alert("Done! Latest filters have been applied. If you still face issues with certain website report the Bug/Issue");
}).catch(function (err) { console.log(err); alert("Failed to get updates! Ensure you have a stable network connection.") });
} catch (err) {
alert("Failed to get updates! Ensure you have a stable network connection.");
}
}
}
document.addEventListener('DOMContentLoaded', restore_options);
document.getElementById('save').addEventListener('click', save_options);
function add_state_change_listener(element) {
element.addEventListener('change', e => {
checkStateChanged = true;
});
}
add_state_change_listener(advFilter);
add_state_change_listener(antiPrnFilter);
add_state_change_listener(suspFilter);
window.addEventListener('beforeunload', function (event) {
if (checkStateChanged) {
event.preventDefault();
event.returnValue = '';
}
})
window.onload = () => {
let nightMode = document.getElementsByClassName("mode")[0]
let lightMode = document.getElementsByClassName("mode")[1]
nightMode.onclick = () => {
toggleMode()
}
lightMode.onclick = () => {
toggleMode()
}
}
const toggleMode = () => {
let nightMode = document.getElementsByClassName("mode")[0]
let lightMode = document.getElementsByClassName("light-mode")[0]
if (nightMode.style.display == "") {
nightMode.style.display = "block"
} else {
nightMode.style.display = ""
}
if (lightMode.style.display == "") {
lightMode.style.display = "block"
} else {
lightMode.style.display = ""
}
document.getElementsByTagName("body")[0].classList.toggle("night-mode-style")
}