Skip to content

Commit b186a95

Browse files
Add files via upload
1 parent 77eab4e commit b186a95

4 files changed

Lines changed: 94 additions & 0 deletions

File tree

‎background.js‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
const icon_disabled = {128: "js_disabled.png"}
2+
const icon_whitelisted = {128: "js.png"}
3+
4+
function is_whitelisted(dict, host) {
5+
let whitelist_js = true
6+
if (dict[host] !== undefined) {
7+
whitelist_js = dict[host]
8+
}
9+
return whitelist_js
10+
}
11+
12+
function add_csp_nojs_header(response) {
13+
let host = new URL(response.url).hostname
14+
let headers = response.responseHeaders
15+
return new Promise( (resolve) => {
16+
browser.storage.local.get(host).then(item => {
17+
let whitelist_js = is_whitelisted(item, host)
18+
if (!whitelist_js) {
19+
var new_csp = {name: "Content-Security-Policy", value: "script-src 'none';"}
20+
headers.push(new_csp)
21+
}
22+
resolve({responseHeaders: headers})
23+
})
24+
})
25+
}
26+
27+
browser.webRequest.onHeadersReceived.addListener(
28+
add_csp_nojs_header,
29+
{urls: ["<all_urls>"],
30+
types: ["main_frame", "sub_frame"]},
31+
["blocking", "responseHeaders"]
32+
)
33+
34+
browser.tabs.onUpdated.addListener((id, changeInfo) => {
35+
if (changeInfo.url) {
36+
let host = new URL(changeInfo.url).hostname
37+
browser.storage.local.get(host).then(item => {
38+
let whitelist_js = is_whitelisted(item, host)
39+
40+
let path_icon = whitelist_js ? icon_whitelisted : icon_disabled
41+
browser.pageAction.setIcon({
42+
path: path_icon,
43+
tabId: id
44+
})
45+
46+
let word = whitelist_js ? "Disable" : "Enable"
47+
browser.pageAction.setTitle({
48+
title: word + " Javascript",
49+
tabId: id
50+
})
51+
})
52+
}
53+
browser.pageAction.show(id)
54+
})
55+
56+
browser.pageAction.onClicked.addListener(function(tab) {
57+
let host = new URL(tab.url).hostname
58+
browser.storage.local.get(host).then(item => {
59+
let whitelist_js = is_whitelisted(item, host)
60+
let to_store = {}
61+
to_store[host] = !whitelist_js
62+
browser.storage.local.set(to_store).then( function() {
63+
browser.tabs.reload()
64+
})
65+
})
66+
})

‎js.png‎

25 KB
Loading

‎js_disabled.png‎

14.4 KB
Loading

‎manifest.json‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"author": "Erwan Ameil",
3+
"background": {
4+
"scripts": [
5+
"background.js"
6+
]
7+
},
8+
"description": "Add a button in the url bar to block or enable javascript per-site.\n",
9+
"homepage_url": "https://github.com/idlewan/javascript-control",
10+
"icons": {
11+
"128": "js.png"
12+
},
13+
"manifest_version": 2,
14+
"name": "Javascript Control",
15+
"page_action": {
16+
"browser_style": true,
17+
"default_icon": "js_disabled.png",
18+
"default_title": "Enable Javascript"
19+
},
20+
"permissions": [
21+
"<all_urls>",
22+
"webRequest",
23+
"webRequestBlocking",
24+
"storage",
25+
"tabs"
26+
],
27+
"version": "1.0"
28+
}

0 commit comments

Comments
 (0)