Synching Chrome/Firefox scripts

Now identical cross-browser Javascript-code for easier maintenance/updating.
No changes in functionality.
merge-requests/1/head
magnolia1234 5 years ago committed by GitHub
parent b8a4bcd5ee
commit aaf04ba08c

@ -1,6 +1,7 @@
/* Please respect alphabetical order when adding a site in any list */ /* Please respect alphabetical order when adding a site in any list */
'use strict'; 'use strict';
var ext_api = chrome || browser;
// Cookies from this list are blocked by default (obsolete) // Cookies from this list are blocked by default (obsolete)
// defaultSites are loaded from sites.js at installation extension // defaultSites are loaded from sites.js at installation extension
@ -184,15 +185,16 @@ const userAgentMobile = "Chrome/41.0.2272.96 Mobile Safari/537.36 (compatible ;
var enabledSites = []; var enabledSites = [];
function setDefaultOptions() { function setDefaultOptions() {
browser.storage.sync.set({ ext_api.storage.sync.set({
sites: defaultSites sites: defaultSites
}, function() { }, function() {
browser.runtime.openOptionsPage(); ext_api.runtime.openOptionsPage();
}); });
} }
// Get the enabled sites (from local storage) & add to allow/remove_cookies (if not already in one of these arrays) // Get the enabled sites (from local storage) & add to allow/remove_cookies (if not already in one of these arrays)
// Add googlebot- and block_javascript-settings for custom sites // Add googlebot- and block_javascript-settings for custom sites
browser.storage.sync.get({ ext_api.storage.sync.get({
sites: {}, sites: {},
sites_custom: {} sites_custom: {}
}, function (items) { }, function (items) {
@ -238,7 +240,7 @@ browser.storage.sync.get({
}); });
// Listen for changes to options // Listen for changes to options
browser.storage.onChanged.addListener(function (changes, namespace) { ext_api.storage.onChanged.addListener(function (changes, namespace) {
for (var key in changes) { for (var key in changes) {
var storageChange = changes[key]; var storageChange = changes[key];
if (key === 'sites') { if (key === 'sites') {
@ -283,7 +285,7 @@ browser.storage.onChanged.addListener(function (changes, namespace) {
}); });
// Set and show default options on install // Set and show default options on install
browser.runtime.onInstalled.addListener(function (details) { ext_api.runtime.onInstalled.addListener(function (details) {
if (details.reason == "install") { if (details.reason == "install") {
setDefaultOptions(); setDefaultOptions();
} else if (details.reason == "update") { } else if (details.reason == "update") {
@ -292,7 +294,7 @@ browser.runtime.onInstalled.addListener(function (details) {
}); });
// repubblica.it bypass // repubblica.it bypass
browser.webRequest.onBeforeRequest.addListener(function (details) { ext_api.webRequest.onBeforeRequest.addListener(function (details) {
if (!isSiteEnabled(details)) { if (!isSiteEnabled(details)) {
return; return;
} }
@ -307,7 +309,7 @@ var block_js_default = ["*://*.tinypass.com/*", "*://*.poool.fr/*", "*://*.piano
var block_js_custom = []; var block_js_custom = [];
var block_js = block_js_default.concat(block_js_custom); var block_js = block_js_default.concat(block_js_custom);
// Disable javascript for these sites/general paywall-scripts // Disable javascript for these sites/general paywall-scripts
browser.webRequest.onBeforeRequest.addListener(function(details) { ext_api.webRequest.onBeforeRequest.addListener(function(details) {
if (!isSiteEnabled(details)) { if (!isSiteEnabled(details)) {
return; return;
} }
@ -320,9 +322,13 @@ browser.webRequest.onBeforeRequest.addListener(function(details) {
}, },
["blocking"]); ["blocking"]);
var extraInfoSpec = ['blocking', 'requestHeaders'];
if (ext_api.webRequest.OnBeforeSendHeadersOptions.hasOwnProperty('EXTRA_HEADERS'))
extraInfoSpec.push('extraHeaders');
// list of regional ad.nl sites // list of regional ad.nl sites
const ad_region_domains = ['bd.nl', 'ed.nl', 'tubantia.nl', 'bndestem.nl', 'pzc.nl', 'destentor.nl', 'gelderlander.nl']; const ad_region_domains = ['bd.nl', 'ed.nl', 'tubantia.nl', 'bndestem.nl', 'pzc.nl', 'destentor.nl', 'gelderlander.nl'];
browser.webRequest.onBeforeSendHeaders.addListener(function(details) { ext_api.webRequest.onBeforeSendHeaders.addListener(function(details) {
var requestHeaders = details.requestHeaders; var requestHeaders = details.requestHeaders;
var header_referer = ''; var header_referer = '';
@ -336,9 +342,9 @@ browser.webRequest.onBeforeSendHeaders.addListener(function(details) {
// remove cookies for sites medium platform (mainfest.json needs in permissions: <all_urls>) // remove cookies for sites medium platform (mainfest.json needs in permissions: <all_urls>)
if (isSiteEnabled({url: '.medium.com'}) && details.url.indexOf('cdn-client.medium.com') !== -1 && header_referer.indexOf('.medium.com') === -1) { if (isSiteEnabled({url: '.medium.com'}) && details.url.indexOf('cdn-client.medium.com') !== -1 && header_referer.indexOf('.medium.com') === -1) {
var domainVar = new URL(header_referer).hostname; var domainVar = new URL(header_referer).hostname;
browser.cookies.getAll({domain: domainVar}, function(cookies) { ext_api.cookies.getAll({domain: domainVar}, function(cookies) {
for (var i=0; i<cookies.length; i++) { for (var i=0; i<cookies.length; i++) {
browser.cookies.remove({url: (cookies[i].secure ? "https://" : "http://") + cookies[i].domain + cookies[i].path, name: cookies[i].name}); ext_api.cookies.remove({url: (cookies[i].secure ? "https://" : "http://") + cookies[i].domain + cookies[i].path, name: cookies[i].name});
} }
}); });
} }
@ -347,10 +353,10 @@ browser.webRequest.onBeforeSendHeaders.addListener(function(details) {
if (isSiteEnabled({url: '.ad.nl'})) { if (isSiteEnabled({url: '.ad.nl'})) {
var domainVar = new URL(details.url).hostname.replace('www.', ''); var domainVar = new URL(details.url).hostname.replace('www.', '');
if (ad_region_domains.includes(domainVar)) { if (ad_region_domains.includes(domainVar)) {
browser.cookies.getAll({domain: domainVar}, function(cookies) { ext_api.cookies.getAll({domain: domainVar}, function(cookies) {
for (var i=0; i<cookies.length; i++) { for (var i=0; i<cookies.length; i++) {
if (remove_cookies_select_drop['ad.nl'].includes(cookies[i].name)){ if (remove_cookies_select_drop['ad.nl'].includes(cookies[i].name)){
browser.cookies.remove({url: (cookies[i].secure ? "https://" : "http://") + cookies[i].domain + cookies[i].path, name: cookies[i].name}); ext_api.cookies.remove({url: (cookies[i].secure ? "https://" : "http://") + cookies[i].domain + cookies[i].path, name: cookies[i].name});
} }
} }
}); });
@ -364,10 +370,10 @@ browser.webRequest.onBeforeSendHeaders.addListener(function(details) {
// allow BG paywall-script to set cookies in homepage/sections (else no article-text) // allow BG paywall-script to set cookies in homepage/sections (else no article-text)
if (details.url.indexOf('meter.bostonglobe.com/js/') !== -1 && (header_referer === 'https://www.bostonglobe.com/' if (details.url.indexOf('meter.bostonglobe.com/js/') !== -1 && (header_referer === 'https://www.bostonglobe.com/'
|| header_referer.indexOf('/?p1=BGHeader_') !== -1 || header_referer.indexOf('/?p1=BGMenu_') !== -1)) { || header_referer.indexOf('/?p1=BGHeader_') !== -1 || header_referer.indexOf('/?p1=BGMenu_') !== -1)) {
browser.webRequest.handlerBehaviorChanged(function () {}); ext_api.webRequest.handlerBehaviorChanged(function () {});
break; break;
} else if (header_referer.indexOf('theglobeandmail.com') !== -1 && !(header_referer.indexOf('/article-') !== -1)) { } else if (header_referer.indexOf('theglobeandmail.com') !== -1 && !(header_referer.indexOf('/article-') !== -1)) {
browser.webRequest.handlerBehaviorChanged(function () {}); ext_api.webRequest.handlerBehaviorChanged(function () {});
break; break;
} }
return { cancel: true }; return { cancel: true };
@ -451,11 +457,11 @@ browser.webRequest.onBeforeSendHeaders.addListener(function(details) {
if (tabId !== -1) { if (tabId !== -1) {
// run contentScript inside tab // run contentScript inside tab
browser.tabs.executeScript(tabId, { ext_api.tabs.executeScript(tabId, {
file: 'contentScript.js', file: 'contentScript.js',
runAt: 'document_start' runAt: 'document_start'
}, function(res) { }, function(res) {
if (browser.runtime.lastError || res[0]) { if (ext_api.runtime.lastError || res[0]) {
return; return;
} }
}); });
@ -464,13 +470,14 @@ browser.webRequest.onBeforeSendHeaders.addListener(function(details) {
return { requestHeaders: requestHeaders }; return { requestHeaders: requestHeaders };
}, { }, {
urls: ['<all_urls>'] urls: ['<all_urls>']
}, ['blocking', 'requestHeaders']); }, extraInfoSpec);
// extraInfoSpec is ['blocking', 'requestHeaders'] + possible 'extraHeaders'
browser.tabs.onUpdated.addListener(updateBadge); ext_api.tabs.onUpdated.addListener(updateBadge);
browser.tabs.onActivated.addListener(updateBadge); ext_api.tabs.onActivated.addListener(updateBadge);
function updateBadge() { function updateBadge() {
browser.tabs.query({ ext_api.tabs.query({
active: true, active: true,
currentWindow: true currentWindow: true
}, function (arrayOfTabs) { }, function (arrayOfTabs) {
@ -478,8 +485,8 @@ function updateBadge() {
if (!activeTab) if (!activeTab)
return; return;
var textB = getTextB(activeTab.url); var textB = getTextB(activeTab.url);
browser.browserAction.setBadgeBackgroundColor({color: "red"}); ext_api.browserAction.setBadgeBackgroundColor({color: "red"});
browser.browserAction.setBadgeText({text: textB}); ext_api.browserAction.setBadgeText({text: textB});
}); });
} }
@ -494,23 +501,16 @@ function getTextB(currentUrl) {
} }
// remove cookies after page load // remove cookies after page load
browser.webRequest.onCompleted.addListener(function(details) { ext_api.webRequest.onCompleted.addListener(function (details) {
for (var domainIndex in remove_cookies) { for (var domainIndex in remove_cookies) {
var domainVar = remove_cookies[domainIndex]; var domainVar = remove_cookies[domainIndex];
if (!enabledSites.includes(domainVar) || details.url.indexOf(domainVar) === -1) { if (!enabledSites.includes(domainVar) || details.url.indexOf(domainVar) === -1) {
continue; // don't remove cookies continue; // don't remove cookies
} }
browser.cookies.getAll({domain: domainVar}, function(cookies) { ext_api.cookies.getAll({
domain: domainVar
}, function (cookies) {
for (var i = 0; i < cookies.length; i++) { for (var i = 0; i < cookies.length; i++) {
var cookie = {
url: (cookies[i].secure ? "https://" : "http://") + cookies[i].domain + cookies[i].path,
name: cookies[i].name,
storeId: cookies[i].storeId
};
if (cookies[i].firstPartyDomain !== undefined) {
cookie.firstPartyDomain = cookies[i].firstPartyDomain;
}
var cookie_domain = cookies[i].domain; var cookie_domain = cookies[i].domain;
var rc_domain = cookie_domain.replace(/^(\.?www\.|\.)/, ''); var rc_domain = cookie_domain.replace(/^(\.?www\.|\.)/, '');
// hold specific cookie(s) from remove_cookies domains // hold specific cookie(s) from remove_cookies domains
@ -521,7 +521,10 @@ browser.webRequest.onCompleted.addListener(function(details) {
if ((rc_domain in remove_cookies_select_drop) && !(remove_cookies_select_drop[rc_domain].includes(cookies[i].name))) { if ((rc_domain in remove_cookies_select_drop) && !(remove_cookies_select_drop[rc_domain].includes(cookies[i].name))) {
continue; // only remove specific cookie continue; // only remove specific cookie
} }
browser.cookies.remove(cookie); ext_api.cookies.remove({
url: (cookies[i].secure ? "https://" : "http://") + cookies[i].domain + cookies[i].path,
name: cookies[i].name
});
} }
}); });
} }

@ -1,7 +1,7 @@
// defaultSites are loaded from sites.js at installation extension (and are saved to local storage) // defaultSites are loaded from sites.js at installation extension (and are saved to local storage)
// var defaultSites = {}; // var defaultSites = {};
// Saves options to browser.storage // Saves options to ext_api.storage
function save_options() { function save_options() {
var gh_url = document.getElementById('bypass_sites').value; var gh_url = document.getElementById('bypass_sites').value;
var inputEls = document.querySelectorAll('#bypass_sites input'); var inputEls = document.querySelectorAll('#bypass_sites input');
@ -14,7 +14,7 @@ function save_options() {
return memo; return memo;
}, {}); }, {});
browser.storage.sync.set({ ext_api.storage.sync.set({
sites: sites sites: sites
}, function() { }, function() {
// Update status to let user know options were saved. // Update status to let user know options were saved.
@ -27,22 +27,21 @@ function save_options() {
}); });
// Refresh the current tab // Refresh the current tab
browser.tabs.query({ ext_api.tabs.query({
active: true, active: true,
currentWindow: true currentWindow: true
}, function (tabs) { }, function (tabs) {
if (tabs[0].url.indexOf("http") !== -1) { if (tabs[0].url && tabs[0].url.indexOf("http") !== -1) {
browser.tabs.update(tabs[0].id, { ext_api.tabs.update(tabs[0].id, {
url: tabs[0].url url: tabs[0].url
}); });
} }
}); });
} }
// Restores checkbox input states using the preferences stored in browser.storage. // Restores checkbox input states using the preferences stored in ext_api.storage.
function renderOptions() { function renderOptions() {
browser.storage.sync.get({ ext_api.storage.sync.get({
sites: {}, sites_custom: {} sites: {}, sites_custom: {}
}, function(items) { }, function(items) {
var sites = items.sites; var sites = items.sites;

@ -1,12 +1,13 @@
var ext_api = chrome || browser;
// Saves options to browser.storage // Saves options to ext_api.storage
function save_options() { function save_options() {
var gh_url = document.getElementById('bypass_sites').value; var gh_url = document.getElementById('bypass_sites').value;
var textareaEl = document.querySelector('#bypass_sites textarea'); var textareaEl = document.querySelector('#bypass_sites textarea');
var sites_custom = {}; var sites_custom = {};
if (textareaEl.value !== '') if (textareaEl.value !== '')
var sites_custom = JSON.parse(textareaEl.value); var sites_custom = JSON.parse(textareaEl.value);
browser.storage.sync.set({ ext_api.storage.sync.set({
sites_custom: sites_custom sites_custom: sites_custom
}, function () { }, function () {
// Update status to let user know custom sites were saved. // Update status to let user know custom sites were saved.
@ -22,7 +23,7 @@ function save_options() {
// Export custom sites to file // Export custom sites to file
function export_options() { function export_options() {
browser.storage.sync.get({ ext_api.storage.sync.get({
sites_custom: {} sites_custom: {}
}, function (items) { }, function (items) {
var result = JSON.stringify(items.sites_custom); var result = JSON.stringify(items.sites_custom);
@ -46,7 +47,7 @@ function import_options(e) {
function _imp() { function _imp() {
let sites_custom = JSON.parse(this.result); let sites_custom = JSON.parse(this.result);
browser.storage.sync.set({ ext_api.storage.sync.set({
sites_custom: sites_custom sites_custom: sites_custom
}, function () { }, function () {
// Update status to let user know custom sites were imported. // Update status to let user know custom sites were imported.
@ -60,7 +61,7 @@ function _imp() {
}); });
} }
// Add custom site to browser.storage // Add custom site to ext_api.storage
function add_options() { function add_options() {
var gh_url = document.getElementById('add_site').value; var gh_url = document.getElementById('add_site').value;
var inputEls = document.querySelectorAll('#add_site input'); var inputEls = document.querySelectorAll('#add_site input');
@ -86,7 +87,7 @@ function add_options() {
sites_custom[title]['domain'] = sites_custom[title]['domain'].replace('www.', ''); sites_custom[title]['domain'] = sites_custom[title]['domain'].replace('www.', '');
// add new site to local storage // add new site to local storage
browser.storage.sync.get({ ext_api.storage.sync.get({
sites_custom: {} sites_custom: {}
}, function (items) { }, function (items) {
var sites_custom_old = items.sites_custom; var sites_custom_old = items.sites_custom;
@ -95,7 +96,7 @@ function add_options() {
sites_custom_old[key] = sites_custom[key]; sites_custom_old[key] = sites_custom[key];
} }
browser.storage.sync.set({ ext_api.storage.sync.set({
sites_custom: sites_custom_old sites_custom: sites_custom_old
}, function () { }, function () {
// Update status to let user know new custom site was added. // Update status to let user know new custom site was added.
@ -109,7 +110,7 @@ function add_options() {
}); });
} }
// Delete custom site from browser.storage // Delete custom site from ext_api.storage
function delete_options() { function delete_options() {
var gh_url = document.getElementById('custom_sites').value; var gh_url = document.getElementById('custom_sites').value;
var selectEl = document.querySelector('#custom_sites select'); var selectEl = document.querySelector('#custom_sites select');
@ -117,13 +118,13 @@ function delete_options() {
var remove_key = selectEl.value; var remove_key = selectEl.value;
// delete site from local storage // delete site from local storage
browser.storage.sync.get({ ext_api.storage.sync.get({
sites_custom: {} sites_custom: {}
}, function (items) { }, function (items) {
var sites_custom_old = items.sites_custom; var sites_custom_old = items.sites_custom;
delete sites_custom_old[remove_key]; delete sites_custom_old[remove_key];
browser.storage.sync.set({ ext_api.storage.sync.set({
sites_custom: sites_custom_old sites_custom: sites_custom_old
}, function () { }, function () {
// Update status to let user know custom site was deleted. // Update status to let user know custom site was deleted.
@ -137,9 +138,9 @@ function delete_options() {
}); });
} }
// Restores checkbox input states using the preferences stored in browser.storage. // Restores checkbox input states using the preferences stored in ext_api.storage.
function renderOptions() { function renderOptions() {
browser.storage.sync.get({ ext_api.storage.sync.get({
sites_custom: {} sites_custom: {}
}, function (items) { }, function (items) {
var sites_custom = items.sites_custom; var sites_custom = items.sites_custom;

@ -1,4 +1,6 @@
var manifestData = browser.runtime.getManifest(); var ext_api = chrome || browser;
var manifestData = ext_api.runtime.getManifest();
var versionString = 'v' + manifestData.version; var versionString = 'v' + manifestData.version;
document.getElementById('version').innerText = versionString; document.getElementById('version').innerText = versionString;

Loading…
Cancel
Save