|
| 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 | +}) |
0 commit comments