diff --git a/src/js/bScript.js b/src/js/bScript.js index ee25537..9516c59 100644 --- a/src/js/bScript.js +++ b/src/js/bScript.js @@ -1,247 +1,317 @@ -var style, _style, enabled = false, prefix = 'ujs_bScript', none = '{display: none !important;}'; - -var getValue = function(name){ - if(window.localStorage){ - return window.localStorage[name] || ''; - }else{ - var nameEQ = name+'='; - var ca = document.cookie.split(';'); - for(var i = 0, c; c = ca[i]; i++){ - while(c.charAt(0) == ' ')c = c.substring(1, c.length); - if(c.indexOf(nameEQ) == 0)return unescape(c.substring(nameEQ.length, c.length)); - } - return ''; - } -}; +var style, + _style, + enabled = false, + prefix = "ujs_bScript", + none = "{display: none !important;}"; -var setValue = function(name, value, days){ - if(window.localStorage){ - window.localStorage[name] = value; - }else{ - var date = new Date(); - date.setTime(date.getTime()+((days || 10*365)*24*60*60*1000)); - if(document.cookie.split(';').length < 30 && document.cookie.length-escape(getValue(name)).length+escape(value).length < 4000){ - document.cookie = name+'='+escape(value)+'; expires='+date.toGMTString()+'; path=/'; - }else{ - alert(browser.i18n.getMessage("cookies")); - } - } +var getValue = function (name) { + if (window.localStorage) { + return window.localStorage[name] || ""; + } else { + var nameEQ = name + "="; + var ca = document.cookie.split(";"); + for (var i = 0, c; (c = ca[i]); i++) { + while (c.charAt(0) == " ") c = c.substring(1, c.length); + if (c.indexOf(nameEQ) == 0) + return unescape(c.substring(nameEQ.length, c.length)); + } + return ""; + } }; -var addStyle = function(css){ - var s = document.createElement('style'); - s.setAttribute('type', 'text/css'); - s.setAttribute('style', 'display: none !important;'); - s.appendChild(document.createTextNode(css)); - return (document.getElementsByTagName('head')[0] || document.documentElement).appendChild(s); +var setValue = function (name, value, days) { + if (window.localStorage) { + window.localStorage[name] = value; + } else { + var date = new Date(); + date.setTime(date.getTime() + (days || 10 * 365) * 24 * 60 * 60 * 1000); + if ( + document.cookie.split(";").length < 30 && + document.cookie.length - + escape(getValue(name)).length + + escape(value).length < + 4000 + ) { + document.cookie = + name + + "=" + + escape(value) + + "; expires=" + + date.toGMTString() + + "; path=/"; + } else { + alert(browser.i18n.getMessage("cookies")); + } + } }; -var addScript = function(css){ - var s = document.createElement('script'); - s.setAttribute('type', 'text/javascript'); - s.setAttribute('style', 'display: none !important;'); - s.appendChild(document.createTextNode(css)); - if(document.getElementsByTagName('head').length){ - return document.getElementsByTagName('head')[0].appendChild(s); - } +var addStyle = function (css) { + var s = document.createElement("style"); + s.setAttribute("type", "text/css"); + s.setAttribute("style", "display: none !important;"); + s.appendChild(document.createTextNode(css)); + return ( + document.getElementsByTagName("head")[0] || document.documentElement + ).appendChild(s); }; -var replaceStyle = function(element, css){ - if(element){ - if(element.firstChild)element.removeChild(element.firstChild); - element.appendChild(document.createTextNode(css)); - } +var addScript = function (css) { + var s = document.createElement("script"); + s.setAttribute("type", "text/javascript"); + s.setAttribute("style", "display: none !important;"); + s.appendChild(document.createTextNode(css)); + if (document.getElementsByTagName("head").length) { + return document.getElementsByTagName("head")[0].appendChild(s); + } }; -var splitCss = function(css){ - var rez = []; - css.replace(/(([\w#:.~>+()\s-]+|\*|\[.*?\])+)\s*(,|$)/g, function(s, m){rez.push(m.replace(/^\s+|\s+$/g, ''))}); - return rez; +var replaceStyle = function (element, css) { + if (element) { + if (element.firstChild) element.removeChild(element.firstChild); + element.appendChild(document.createTextNode(css)); + } }; -var clearCss = function(css){ - var a = splitCss(css); - for(var i = a.length-1; i >= 0; i--){ - var rule = a[i]+'>'; - for(var j = a.length-1; j >= 0; j--){ - if(a[j].indexOf(rule) == 0)a.splice(j, 1); - } - } - return a.join(','); +var splitCss = function (css) { + var rez = []; + css.replace(/(([\w#:.~>+()\s-]+|\*|\[.*?\])+)\s*(,|$)/g, function (s, m) { + rez.push(m.replace(/^\s+|\s+$/g, "")); + }); + return rez; }; -var delCss = function(css, del){ - var a = splitCss(css); - if(del){ - for(var i = a.length-1; i >= 0; i--){ - if(del.indexOf(a[i]) == 0)a.splice(i, 1); - } - }else{ - a.pop(); - } - return a.join(','); +var clearCss = function (css) { + var a = splitCss(css); + for (var i = a.length - 1; i >= 0; i--) { + var rule = a[i] + ">"; + for (var j = a.length - 1; j >= 0; j--) { + if (a[j].indexOf(rule) == 0) a.splice(j, 1); + } + } + return a.join(","); }; -var getAtt = function(element, tags){ - var rez = ''; - if(element.attributes){ - var r = new RegExp('^('+tags+')$'); - for(var i = 0, a; a = element.attributes[i]; i++){ - var n = a.nodeName.toLowerCase(); - if(r.test(n))rez += '['+n+'=\x22'+a.nodeValue.replace(/[\x22\x5C]/g, '\\$&')+'\x22]'; - } - } - return rez; +var delCss = function (css, del) { + var a = splitCss(css); + if (del) { + for (var i = a.length - 1; i >= 0; i--) { + if (del.indexOf(a[i]) == 0) a.splice(i, 1); + } + } else { + a.pop(); + } + return a.join(","); }; -var getNth = function(element){ - var nth, n = 0, p = element.parentNode; - for(var i = 0, c; c = p.childNodes[i]; i++){if(c.nodeType == 1){n++; if(c == element)nth = n}}; - return (!nth || n < 2) ? '' : ':nth-child('+nth+')'; +var getAtt = function (element, tags) { + var rez = ""; + if (element.attributes) { + var r = new RegExp("^(" + tags + ")$"); + for (var i = 0, a; (a = element.attributes[i]); i++) { + var n = a.nodeName.toLowerCase(); + if (r.test(n)) + rez += + "[" + + n + + "=\x22" + + a.nodeValue.replace(/[\x22\x5C]/g, "\\$&") + + "\x22]"; + } + } + return rez; }; -var getCssRules = function(element, wide){ - var att, tag, rez = []; - while(element){ - if(element.nodeType == 1){ - att = getAtt(element, 'src') || getAtt(element, 'href'); - tag = element.nodeName; - if(att){ - rez.unshift(tag+(wide ? att.replace(/^(\[\w+)(=\x22[^?#]+\/).*(\x22\])$/, '$1^$2$3') : att)); - break; - } - else{ - rez.unshift(tag+getAtt(element, 'id|class|height|width|color|bgcolor|align|type')+((wide || /^(html|body)$/i.test(tag)) ? '' : getNth(element))); - } - } - element = element.parentNode; - } - return rez.join('>'); +var getNth = function (element) { + var nth, + n = 0, + p = element.parentNode; + for (var i = 0, c; (c = p.childNodes[i]); i++) { + if (c.nodeType == 1) { + n++; + if (c == element) nth = n; + } + } + return !nth || n < 2 ? "" : ":nth-child(" + nth + ")"; }; -var setBlockStyle = function(){ - if(document.documentElement instanceof HTMLHtmlElement){ - var css = getValue(prefix); - if(css)style = addStyle(css+none); - return true; - } -} - -function unblockElement(latest){ -var css = getValue(prefix); -if(enabled || !style || !css)return; -var remove = function(){ - document.removeEventListener('click', click, false); - document.removeEventListener('keyup', press, false); - enabled = false; +var getCssRules = function (element, wide) { + var att, + tag, + rez = []; + while (element) { + if (element.nodeType == 1) { + att = getAtt(element, "src") || getAtt(element, "href"); + tag = element.nodeName; + if (att) { + rez.unshift( + tag + + (wide + ? att.replace(/^(\[\w+)(=\x22[^?#]+\/).*(\x22\])$/, "$1^$2$3") + : att) + ); + break; + } else { + rez.unshift( + tag + + getAtt(element, "id|class|height|width|color|bgcolor|align|type") + + (wide || /^(html|body)$/i.test(tag) ? "" : getNth(element)) + ); + } + } + element = element.parentNode; + } + return rez.join(">"); }; -var click = function(event){ - event.preventDefault(); - var oldCss = getValue(prefix); - var css = delCss(oldCss, getCssRules(event.target, false)); - if(css == oldCss)css = delCss(oldCss, getCssRules(event.target, true)); - if(css != oldCss)setValue(prefix, css); - replaceStyle(style, css ? css+none : ''); - remove(); +var setBlockStyle = function () { + if (document.documentElement instanceof HTMLHtmlElement) { + var css = getValue(prefix); + if (css) style = addStyle(css + none); + return true; + } }; -var press = function(event){ - if(event.keyCode == 27){ - var css = getValue(prefix); - replaceStyle(style, css ? css+none : ''); - remove(); - } -}; +function unblockElement(latest) { + var css = getValue(prefix); + if (enabled || !style || !css) return; + var remove = function () { + document.removeEventListener("click", click, false); + document.removeEventListener("keyup", press, false); + enabled = false; + }; -if(latest){ - css = delCss(css); - setValue(prefix, css); - replaceStyle(style, css ? css+none : ''); -}else{ - enabled = true; - replaceStyle(style, css+'{background-color: red !important; border: 2px solid #FF1111 !important; opacity: 0.7 !important;}'); - document.addEventListener('click', click, false); - document.addEventListener('keyup', press, false); -} + var click = function (event) { + event.preventDefault(); + var oldCss = getValue(prefix); + var css = delCss(oldCss, getCssRules(event.target, false)); + if (css == oldCss) css = delCss(oldCss, getCssRules(event.target, true)); + if (css != oldCss) setValue(prefix, css); + replaceStyle(style, css ? css + none : ""); + remove(); + }; + + var press = function (event) { + if (event.keyCode == 27) { + var css = getValue(prefix); + replaceStyle(style, css ? css + none : ""); + remove(); + } + }; + + if (latest) { + css = delCss(css); + setValue(prefix, css); + replaceStyle(style, css ? css + none : ""); + } else { + enabled = true; + replaceStyle( + style, + css + + "{background-color: red !important; border: 2px solid #FF1111 !important; opacity: 0.7 !important;}" + ); + document.addEventListener("click", click, false); + document.addEventListener("keyup", press, false); + } } -function blockElement(wide){ -if(enabled)return; -var element = '', outline = '', border = '', bgColor = '', title = '', reObjects = /^(iframe|object|embed)$/i; +function blockElement(wide) { + if (enabled) return; + var element = "", + outline = "", + border = "", + bgColor = "", + title = "", + reObjects = /^(iframe|object|embed)$/i; -var remove = function(){ - document.removeEventListener('mouseover', over, false); - document.removeEventListener('mouseout', out, false); - document.removeEventListener('click', click, false); - document.removeEventListener('keyup', press, false); - enabled = false; -}; + var remove = function () { + document.removeEventListener("mouseover", over, false); + document.removeEventListener("mouseout", out, false); + document.removeEventListener("click", click, false); + document.removeEventListener("keyup", press, false); + enabled = false; + }; -var over = function(event){ - element = event.target; - title = element.title; - if(reObjects.test(element.nodeName)){ - border = element.style.border; - element.style.border = '2px solid #306EFF'; - }else{ - outline = element.style.outline; - element.style.outline = '1px solid #306EFF'; - bgColor = element.style.backgroundColor; - element.style.backgroundColor = '#C6DEFF'; - } -}; + var over = function (event) { + element = event.target; + title = element.title; + if (reObjects.test(element.nodeName)) { + border = element.style.border; + element.style.border = "2px solid #306EFF"; + } else { + outline = element.style.outline; + element.style.outline = "1px solid #306EFF"; + bgColor = element.style.backgroundColor; + element.style.backgroundColor = "#C6DEFF"; + } + }; -var out = function(){ - if(element){ - element.title = title; - if(reObjects.test(element.nodeName)){ - element.style.border = border; - }else{ - element.style.outline = outline; - element.style.backgroundColor = bgColor; - } - } -}; + var out = function () { + if (element) { + element.title = title; + if (reObjects.test(element.nodeName)) { + element.style.border = border; + } else { + element.style.outline = outline; + element.style.backgroundColor = bgColor; + } + } + }; -var click = function(event){ - if(element){ - event.preventDefault(); - out(); - remove(); - var css = getCssRules(element, wide || event.altKey); - var tmpCss = addStyle(css+'{background-color: red !important; border: 1px solid #FF1111 !important; opacity: 0.7 !important;}'); - css = prompt(browser.i18n.getMessage("blockElementFromThePage"), css); - if(css){ - var oldCss = getValue(prefix); - if(oldCss)css = clearCss(oldCss+','+css); - setValue(prefix, css); - if(style){replaceStyle(style, css+none)}else{style = addStyle(css+none)}; - }; - tmpCss.parentNode.removeChild(tmpCss); - } -}; + var click = function (event) { + if (element) { + event.preventDefault(); + out(); + remove(); + var css = getCssRules(element, wide || event.altKey); + var tmpCss = addStyle( + css + + "{background-color: red !important; border: 1px solid #FF1111 !important; opacity: 0.7 !important;}" + ); + css = prompt(browser.i18n.getMessage("blockElementFromThePage"), css); + if (css) { + var oldCss = getValue(prefix); + if (oldCss) css = clearCss(oldCss + "," + css); + setValue(prefix, css); + if (style) { + replaceStyle(style, css + none); + } else { + style = addStyle(css + none); + } + } + tmpCss.parentNode.removeChild(tmpCss); + } + }; -var press = function(event){ - if(event.keyCode == 27){ - out(); - remove(); - } -}; + var press = function (event) { + if (event.keyCode == 27) { + out(); + remove(); + } + }; -enabled = true; -document.addEventListener('mouseover', over, false); -document.addEventListener('mouseout', out, false); -document.addEventListener('click', click, false); -document.addEventListener('keyup', press, false); + enabled = true; + document.addEventListener("mouseover", over, false); + document.addEventListener("mouseout", out, false); + document.addEventListener("click", click, false); + document.addEventListener("keyup", press, false); } -document.addEventListener('keydown', function(e){ - if(!e.shiftKey && !e.ctrlKey && e.altKey){ - switch(e.keyCode){ - case 85: unblockElement(); break; - case 66: blockElement(); break; - case 76: unblockElement(true); break; - } - } -}, false); \ No newline at end of file +document.addEventListener( + "keydown", + function (e) { + if (!e.shiftKey && !e.ctrlKey && e.altKey) { + switch (e.keyCode) { + case 85: + unblockElement(); + break; + case 66: + blockElement(); + break; + case 76: + unblockElement(true); + break; + } + } + }, + false +); diff --git a/src/js/background.js b/src/js/background.js index 77b9de2..5866f56 100644 --- a/src/js/background.js +++ b/src/js/background.js @@ -1,84 +1,103 @@ -var imported = document.createElement('bScript.js'); -const javascriptEnabled = {32: "../res/icons/bScript_enabled-32.png"}; -const javascriptDisabled = {32: "../res/icons/bScript_disabled-32.png"}; -var style, _style, enabled = false, prefix = 'ujs_bScript', none = '{display: none !important;}'; +var imported = document.createElement("bScript.js"); +const javascriptEnabled = { 32: "../res/icons/bScript_enabled-32.png" }; +const javascriptDisabled = { 32: "../res/icons/bScript_disabled-32.png" }; +var style, + _style, + enabled = false, + prefix = "ujs_bScript", + none = "{display: none !important;}"; function is_whitelisted(dict, host) { - let whitelist_js = true; - if (dict[host] !== undefined) { - whitelist_js = dict[host]; - } - return whitelist_js; + let whitelist_js = true; + if (dict[host] !== undefined) { + whitelist_js = dict[host]; + } + return whitelist_js; } function add_csp_nojs_header(response) { - let host = new URL(response.url).hostname; - let headers = response.responseHeaders; - return new Promise( (resolve) => { - 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(new_csp); - } - resolve({responseHeaders: headers}); - }); - }); + let host = new URL(response.url).hostname; + let headers = response.responseHeaders; + return new Promise((resolve) => { + 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(new_csp); + } + resolve({ responseHeaders: headers }); + }); + }); } -browser.webRequest.onHeadersReceived.addListener(add_csp_nojs_header,{urls: [""], types: ["main_frame"]},["blocking", "responseHeaders"]); +browser.webRequest.onHeadersReceived.addListener( + add_csp_nojs_header, + { urls: [""], types: ["main_frame"] }, + ["blocking", "responseHeaders"] +); browser.tabs.onUpdated.addListener((id, changeInfo) => { - if (changeInfo.url) { - 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: status_title, tabId:id}); - }); - } - browser.pageAction.show(id); + if (changeInfo.url) { + 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: status_title, tabId: id }); + }); + } + browser.pageAction.show(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() { - browser.tabs.reload(); - }); - }); +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 () { + browser.tabs.reload(); + }); + }); } // Context menu function onCreated() { - if (browser.runtime.lastError) { - console.log(`Error: ${browser.runtime.lastError}`); - } + if (browser.runtime.lastError) { + console.log(`Error: ${browser.runtime.lastError}`); + } } -function startContextMenu(){ - if(localStorage.getItem('contextMenu') == "True" || localStorage.getItem('contextMenu') == null){ - browser.menus.create({ - id: "bScript", - title: "bScript", - contexts: ["all"] - }, onCreated); - } else { - browser.menus.remove("bScript"); - } +function startContextMenu() { + if ( + localStorage.getItem("contextMenu") == "True" || + localStorage.getItem("contextMenu") == null + ) { + browser.menus.create( + { + id: "bScript", + title: "bScript", + contexts: ["all"], + }, + onCreated + ); + } else { + browser.menus.remove("bScript"); + } } browser.menus.onClicked.addListener((info, tab) => { - switch (info.menuItemId) { - case "bScript": - activateOrNotbScript(tab); - break; - } -}); \ No newline at end of file + switch (info.menuItemId) { + case "bScript": + activateOrNotbScript(tab); + break; + } +}); diff --git a/src/js/language.js b/src/js/language.js index c372d9b..834b17e 100644 --- a/src/js/language.js +++ b/src/js/language.js @@ -1,9 +1,9 @@ -let textElements = document.querySelectorAll('[data-manifest]'); +let textElements = document.querySelectorAll("[data-manifest]"); for (let element of textElements) { - element.textContent = Manifest[element.dataset.manifest]; + element.textContent = Manifest[element.dataset.manifest]; } -textElements = document.querySelectorAll('[data-i18n]'); +textElements = document.querySelectorAll("[data-i18n]"); for (let element of textElements) { - element.textContent = browser.i18n.getMessage(element.dataset.i18n); -} \ No newline at end of file + element.textContent = browser.i18n.getMessage(element.dataset.i18n); +} diff --git a/src/js/preferences.js b/src/js/preferences.js index 7ad7996..01514cb 100644 --- a/src/js/preferences.js +++ b/src/js/preferences.js @@ -4,18 +4,21 @@ var radios = document.getElementsByName("contextMenu"); // version var version = document.getElementById("version"); -version.textContent = browser.runtime.getManifest().name + " (v"+ browser.runtime.getManifest().version + ")"; +version.textContent = + browser.runtime.getManifest().name + + " (v" + + browser.runtime.getManifest().version + + ")"; - -$(document).ready(function(){ - var val = localStorage.getItem('contextMenu'); - for(var i=0;i