From f137e990324510c34c830e8c89733a7fbb406344 Mon Sep 17 00:00:00 2001 From: Jhonatas Machado Date: Wed, 24 Apr 2024 21:49:09 -0300 Subject: [PATCH] This update addresses an issue related to a blacklist that disabled website JavaScript upon first access. Moving forward, we plan to include an option to manage this feature. --- src/js/background.js | 97 +++++++++++++++++++++++++++----------------- src/manifest.json | 2 +- 2 files changed, 60 insertions(+), 39 deletions(-) diff --git a/src/js/background.js b/src/js/background.js index c65dfd2..74fb1d0 100644 --- a/src/js/background.js +++ b/src/js/background.js @@ -1,23 +1,32 @@ +var imported = document.createElement("bScript.js"); const javascriptEnabled = { 32: "../res/icons/bScript_enabled-32.png" }; const javascriptDisabled = { 32: "../res/icons/bScript_disabled-32.png" }; -const prefix = "ujs_bScript"; +var style, + _style, + enabled = false, + prefix = "ujs_bScript", + none = "{display: none !important;}"; -async function isWhitelisted(host) { - const item = await browser.storage.local.get(host); - return !!item[host]; +function is_whitelisted(dict, host) { + let whitelist_js = true; + if (dict[host] !== undefined) { + whitelist_js = dict[host]; + } + return whitelist_js; } -function addCspNoJsHeader(response) { - const host = new URL(response.url).hostname; - const headers = response.responseHeaders; +function add_csp_nojs_header(response) { + let host = new URL(response.url).hostname; + let headers = response.responseHeaders; return new Promise((resolve) => { - isWhitelisted(host).then((whitelistJs) => { - if (!whitelistJs) { - const newCsp = { + browser.storage.local.get(host).then((item) => { + let whitelist_js = is_whitelisted(item, host); + if (!whitelist_js) { + var new_csp = { name: "Content-Security-Policy", value: "script-src 'none';", }; - headers.push(newCsp); + headers.push(new_csp); } resolve({ responseHeaders: headers }); }); @@ -25,42 +34,58 @@ function addCspNoJsHeader(response) { } browser.webRequest.onHeadersReceived.addListener( - addCspNoJsHeader, + add_csp_nojs_header, { urls: [""], types: ["main_frame"] }, ["blocking", "responseHeaders"] ); -browser.tabs.onUpdated.addListener((id, changeInfo, tab) => { +browser.tabs.onUpdated.addListener((id, changeInfo) => { if (changeInfo.url) { - const host = new URL(changeInfo.url).hostname; - isWhitelisted(host).then((whitelistJs) => { - const statusIcon = whitelistJs ? javascriptEnabled : javascriptDisabled; - browser.pageAction.setIcon({ path: statusIcon, tabId: id }); - const statusTitle = whitelistJs + let host = new URL(changeInfo.url).hostname; + browser.storage.local.get(host).then((item) => { + let whitelist_js = is_whitelisted(item, host); + let status_icon = whitelist_js ? javascriptEnabled : javascriptDisabled; + browser.pageAction.setIcon({ path: status_icon, tabId: id }); + let status_title = whitelist_js ? browser.i18n.getMessage("javascriptEnabled") : browser.i18n.getMessage("javascriptDisabled"); - browser.pageAction.setTitle({ title: statusTitle, tabId: id }); + browser.pageAction.setTitle({ title: status_title, tabId: id }); }); } browser.pageAction.show(id); }); -browser.pageAction.onClicked.addListener((tab) => { - const host = new URL(tab.url).hostname; - isWhitelisted(host).then((whitelistJs) => { - const toStore = {}; - toStore[host] = !whitelistJs; - browser.storage.local.set(toStore).then(() => { - browser.tabs.reload(tab.id); +browser.pageAction.onClicked.addListener(activateOrNotbScript); + +function activateOrNotbScript(tab) { + let host = new URL(tab.url).hostname; + browser.storage.local.get(host).then((item) => { + let whitelist_js = is_whitelisted(item, host); + let to_store = {}; + to_store[host] = !whitelist_js; + browser.storage.local.set(to_store).then(function () { + let status_icon = !whitelist_js ? javascriptEnabled : javascriptDisabled; + let status_title = !whitelist_js + ? browser.i18n.getMessage("javascriptEnabled") + : browser.i18n.getMessage("javascriptDisabled"); + browser.pageAction.setIcon({ path: status_icon, tabId: tab.id }); + browser.pageAction.setTitle({ title: status_title, tabId: tab.id }); }); }); -}); +} // Context menu +function onCreated() { + if (browser.runtime.lastError) { + console.log(`Error: ${browser.runtime.lastError}`); + } +} + function startContextMenu() { - const contextMenuEnabled = - localStorage.getItem("contextMenu") !== "False"; - if (contextMenuEnabled || localStorage.getItem("contextMenu") === null) { + if ( + localStorage.getItem("contextMenu") == "True" || + localStorage.getItem("contextMenu") == null + ) { browser.menus.create( { id: "bScript", @@ -74,14 +99,10 @@ function startContextMenu() { } } -function onCreated() { - if (browser.runtime.lastError) { - console.error(`Error: ${browser.runtime.lastError}`); - } -} - browser.menus.onClicked.addListener((info, tab) => { - if (info.menuItemId === "bScript") { - activateOrNotbScript(tab); + switch (info.menuItemId) { + case "bScript": + activateOrNotbScript(tab); + break; } }); \ No newline at end of file diff --git a/src/manifest.json b/src/manifest.json index 8eaf359..9ad2d84 100644 --- a/src/manifest.json +++ b/src/manifest.json @@ -2,7 +2,7 @@ "manifest_version": 2, "name": "__MSG_extensionName__", "description": "__MSG_extensionDescription__", - "version": "1.0.6", + "version": "1.0.7", "homepage_url": "https://github.com/jhonatasrm/bScript", "default_locale": "en", "author": "Jhonatas Machado",