-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
75 lines (65 loc) · 2.01 KB
/
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
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
const AVAILABLE_RESOURCE_TYPES = [
"main_frame",
"sub_frame",
"stylesheet",
"script",
"image",
"font",
"object",
"xmlhttprequest",
"ping",
"csp_report",
"media",
"websocket",
"other",
];
async function takeOverDNT(bool) {
chrome.privacy.websites.doNotTrackEnabled.get({}, (dnt) => {
if (dnt.levelOfControl === "controllable_by_this_extension") {
chrome.privacy.websites.doNotTrackEnabled.set( {"value": bool}, () => {
if (chrome.runtime.lastError === undefined && dnt === true) {
console.log("Could not overwrite enabled DNT");
} else {
console.log("Successfully took control over the browser DNT setting");
}
});
} else {
if (dnt === true) {
console.log("Could not overwrite enabled DNT");
}
}
});
}
async function updateDefaultPolicy(policy) {
chrome.action.setTitle({"title": "Policy " + policy});
await chrome.declarativeNetRequest.updateDynamicRules({
removeRuleIds: [1],
addRules:[{
"id": 1,
"priority": 1,
"action": {
"type": "modifyHeaders",
"requestHeaders": [
{ "header": "DNT", "operation": "set", "value": policy},
]
},
"condition": { "resourceTypes": AVAILABLE_RESOURCE_TYPES }
}]
});
}
chrome.declarativeNetRequest.onRuleMatchedDebug.addListener(() => {
console.log("matched rule");
});
chrome.runtime.onInstalled.addListener((details) => {
if(details.reason == "install"){
takeOverDNT(false);
chrome.storage.local.set({"defaultPolicy": "1"}, () => {
console.log("Default Policy set to 1");
});
}
});
chrome.storage.onChanged.addListener((changes, namespace) => {
if ("defaultPolicy" in changes) {
updateDefaultPolicy(changes.defaultPolicy.newValue);
}
});