-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshared.js
73 lines (62 loc) · 1.77 KB
/
shared.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
var saveSettings = async () => {
console.log("save settings");
// load redirect url from local storage
const ytUrlStore = await chrome.storage.local.get(["yt_url"]);
const redirYtHomeStore = await chrome.storage.local.get(["redir_yt_home"]);
var urlGroups = ytUrlStore.yt_url.match(/(\S+):\/\/([^:]+):?(\d*)/)
console.log(urlGroups);
const yt_vid_rule = {
"id": 1,
"priority": 1,
"action": {
"type": "redirect",
"redirect": {
"transform": {
"host": urlGroups[2],
"scheme": urlGroups[1]
}
}
},
"condition": {
"regexFilter": "^https://.*\\.youtube\\.com/.+",
"excludedInitiatorDomains": [urlGroups[2]],
"resourceTypes": ["main_frame"]
}
}
const yt_home_rule = {
"id": 2,
"priority": 1,
"action": {
"type": "redirect",
"redirect": {
"transform": {
"host": urlGroups[2],
"scheme": urlGroups[1]
}
}
},
"condition": {
"regexFilter": "^https://.*\\.youtube\\.com/?$",
"resourceTypes": ["main_frame"]
}
}
if (urlGroups[2] != "") {
yt_vid_rule.action.redirect.transform["port"] = urlGroups[3]
yt_home_rule.action.redirect.transform["port"] = urlGroups[3]
}
// Get arrays containing new and old rules
const newRules = [yt_vid_rule]
if (redirYtHomeStore.redir_yt_home) {
newRules.push(yt_home_rule);
}
const oldRules = await chrome.declarativeNetRequest.getDynamicRules();
const oldRuleIds = oldRules.map(rule => rule.id);
console.log({oldRuleIds});
console.log({newRules});
// Use the arrays to update the dynamic rules
await chrome.declarativeNetRequest.updateDynamicRules({
removeRuleIds: oldRuleIds,
addRules: newRules
});
}
export default saveSettings;