Skip to content

Commit

Permalink
Merge pull request #5 from jhonatasrm/update-v1.0.7
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jhonatasrm authored Apr 25, 2024
2 parents 8df3a0b + f137e99 commit cfe9fb0
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 39 deletions.
97 changes: 59 additions & 38 deletions src/js/background.js
Original file line number Diff line number Diff line change
@@ -1,66 +1,91 @@
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 });
});
});
}

browser.webRequest.onHeadersReceived.addListener(
addCspNoJsHeader,
add_csp_nojs_header,
{ urls: ["<all_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",
Expand All @@ -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;
}
});
2 changes: 1 addition & 1 deletion src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit cfe9fb0

Please sign in to comment.