-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackground.js
81 lines (70 loc) · 1.96 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
76
77
78
79
80
81
let useSync;
function restoreOptions() {
function setCurrentChoice(result) {
useSync = result.useSync;
}
function onError(error) {
console.log('Error: ${error}');
}
browser.storage.local.get("useSync").then(setCurrentChoice, onError);
}
document.addEventListener("DOMContentLoaded", restoreOptions);
function isBlock(dict, host) {
if (dict[host] !== undefined) {
return dict[host];
}
return false;
}
function getHost(url) {
return new URL(url).hostname;
}
function getStorage() {
//return useSync ? browser.storage.sync : browser.storage.local;
return browser.storage.local;
}
function pushNoJsHeader(response) {
let host = getHost(response.url);
let headers = response.responseHeaders;
return new Promise((resolve) => {
getStorage().get(host).then(item => {
if (isBlock(item, host)) {
headers.push({name: "Content-Security-Policy", value: "script-src 'none';"});
}
resolve({responseHeaders: headers});
});
});
}
browser.webRequest.onHeadersReceived.addListener(pushNoJsHeader,
{
urls: ["<all_urls>"],
types: ["main_frame", "sub_frame"]
},
["blocking", "responseHeaders"]
);
browser.tabs.onUpdated.addListener((id, inf) => {
if (inf.url && !inf.url.startsWith('moz-extension') && !inf.url.startsWith('about')) {
let host = getHost(inf.url);
getStorage().get(host).then(item => {
let blocked = isBlock(item, host);
browser.pageAction.setIcon({tabId: id, path: blocked ? "js_off.svg" : "js_on.svg"});
browser.pageAction.setTitle({tabId: id, title: 'Javascript ' + (blocked ? 'Disabled' : 'Enabled')});
});
browser.pageAction.show(id);
}
});
browser.pageAction.onClicked.addListener(function (tab) {
let host = getHost(tab.url);
getStorage().get(host).then(item => {
if (!isBlock(item, host)) {
let to_store = {};
to_store[host] = true;
getStorage().set(to_store).then(function () {
browser.tabs.reload();
});
} else {
getStorage().remove(host).then(function () {
browser.tabs.reload();
});
}
});
});