-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
39 lines (36 loc) · 968 Bytes
/
background.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
function dnrRules(rawRules) {
return rawRules
.split("\n")
.map((rule) => rule.split(">"))
.filter((parts) => parts.length === 2)
.map(([oldPrefix, newPrefix], i) => ({
id: i + 1,
priority: i + 1,
action: {
type: "redirect",
redirect: {
regexSubstitution: newPrefix.trim() + "\\1",
},
},
condition: {
regexFilter: "^" + oldPrefix.trim() + "(.*)$",
resourceTypes: ["main_frame"],
},
}));
}
function updateRules(rawRules) {
chrome.declarativeNetRequest.getDynamicRules((oldRules) => {
chrome.declarativeNetRequest.updateDynamicRules({
removeRuleIds: oldRules.map((rule) => rule.id),
addRules: dnrRules(rawRules),
});
});
}
chrome.storage.sync.get({ rules: "" }, (storage) => {
updateRules(storage.rules);
});
chrome.storage.onChanged.addListener((changes) => {
if (changes.rules) {
updateRules(changes.rules.newValue);
}
});