From fd83e53acfaf0ae548313024699665b343823a56 Mon Sep 17 00:00:00 2001 From: magnolia1234 <7676006-magnolia1234@users.noreply.gitlab.com> Date: Sun, 7 Nov 2021 08:47:42 +0100 Subject: [PATCH] Check for update rules at startup (opt-in) --- README.md | 3 ++- background.js | 57 ++++++++++++++++++++++++++++++++++------------ changelog.txt | 1 + manifest.json | 2 +- options/options.js | 2 +- options/version.js | 6 +++-- sites.js | 35 +++++++++++++++------------- sites_updated.json | 2 ++ 8 files changed, 72 insertions(+), 36 deletions(-) create mode 100644 sites_updated.json diff --git a/README.md b/README.md index d968352..b02d096 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,8 @@ Or download and install the latest xpi-version from [GitLab](https://gitlab.com/ By default BPC has limited permissions, but you can opt-in to enable custom sites (and also clear cookies/block general paywall-scripts for non-listed sites). ### Update -Check for updates (in about:addons) and allow permissions for newly supported sites (else no update will be installed). +Check for updates (in about:addons) and allow permissions for newly supported sites (else no update will be installed).\ +You can also check for update of site rules at startup (opt-in). #### Android On Android this add-on doesn't work with latest Firefox v84 (Fenix); it only supports a number of 'recommended' add-ons (for now).\ diff --git a/background.js b/background.js index 07bfe28..cc5e3a9 100644 --- a/background.js +++ b/background.js @@ -2,6 +2,7 @@ 'use strict'; var ext_api = (typeof browser === 'object') ? browser : chrome; +var url_loc = (typeof browser === 'object') ? 'firefox' : 'chrome'; var manifestData = ext_api.runtime.getManifest(); var ext_name = manifestData.name; var ext_version = manifestData.version; @@ -88,6 +89,7 @@ var disabledSites = []; var optionSites = {}; var customSites = {}; var customSites_domains = []; +var updatedSites = {}; var excludedSites = []; function setDefaultOptions() { @@ -103,7 +105,24 @@ function setDefaultOptions() { }); } -function set_rules(sites, sites_custom) { +function check_sites_updated() { + var sites_updated_json = 'https://gitlab.com/magnolia1234/bypass-paywalls-' + url_loc + '-clean/-/raw/master/sites_updated.json'; + fetch(sites_updated_json) + .then(response => { + if (response.ok) { + response.json().then(json => { + expandSiteRules(json); + ext_api.storage.local.set({ + sites_updated: json + }); + }) + } + }).catch(function (err) { + false; + }); +} + +function set_rules(sites, sites_updated, sites_custom) { initSetRules(); for (let site in sites) { let site_domain = sites[site].toLowerCase(); @@ -112,7 +131,9 @@ function set_rules(sites, sites_custom) { let rule = {}; if (defaultSites.hasOwnProperty(site)) { rule = defaultSites[site]; - } else { // custom sites + if (sites_updated.hasOwnProperty(site)) + rule = sites_updated[site]; + } else { // custom sites rule = sites_custom[site]; custom = true; } @@ -145,8 +166,9 @@ function set_rules(sites, sites_custom) { let block_regex_default = ''; if (rule.hasOwnProperty('block_regex')) block_regex_default = rule.block_regex; - - rule = sites_custom[customSite_title]; + rule = {}; + for (let key in sites_custom[customSite_title]) + rule[key] = sites_custom[customSite_title][key]; if (block_regex_default) { if (rule.hasOwnProperty('block_regex')) { if (block_regex_default instanceof RegExp) @@ -221,6 +243,7 @@ function set_rules(sites, sites_custom) { disableJavascriptOnListedSites(); } +// add grouped sites to en/disabledSites (and exclude sites) function add_grouped_enabled_domains(groups) { for (let key in groups) { if (enabledSites.includes(key)) @@ -236,24 +259,21 @@ function add_grouped_enabled_domains(groups) { } } -// add grouped sites to en/disabledSites (and exclude sites) -function add_grouped_sites(grouped_sites, sites, sites_custom) { - add_grouped_enabled_domains(grouped_sites); - set_rules(sites, sites_custom); -} - // Get the enabled sites (from local storage) & set_rules for sites ext_api.storage.local.get({ sites: {}, sites_default: Object.keys(defaultSites).filter(x => !defaultSites[x].domain.match(/^(#options_|###$)/)), sites_custom: {}, + sites_updated: {}, sites_excluded: [], ext_version_old: '2.3.9.0', optIn: false }, function (items) { var sites = items.sites; + optionSites = sites; var sites_default = items.sites_default; var sites_custom = items.sites_custom; + var sites_updated = items.sites_updated; var ext_version_old = items.ext_version_old; optin_setcookie = items.optIn; excludedSites = items.sites_excluded; @@ -284,9 +304,12 @@ ext_api.storage.local.get({ customSites = sites_custom; customSites_domains = Object.values(sites_custom).map(x => x.domain); + updatedSites = sites_updated; disabledSites = defaultSites_domains.concat(customSites_domains).filter(x => !enabledSites.includes(x) && x !== '###'); add_grouped_enabled_domains(grouped_sites); - set_rules(sites, customSites); + set_rules(sites, updatedSites, customSites); + if (enabledSites.includes('#options_optin_update_rules')) + check_sites_updated(); }); // Listen for changes to options @@ -305,7 +328,7 @@ ext_api.storage.onChanged.addListener(function (changes, namespace) { }); disabledSites = defaultSites_domains.concat(customSites_domains).filter(x => !enabledSites.includes(x) && x !== '###'); add_grouped_enabled_domains(grouped_sites); - set_rules(sites, customSites); + set_rules(sites, updatedSites, customSites); } if (key === 'sites_custom') { var sites_custom = storageChange.newValue ? storageChange.newValue : {}; @@ -333,10 +356,14 @@ ext_api.storage.onChanged.addListener(function (changes, namespace) { true; }); } else - set_rules(sites, customSites); + set_rules(sites, updatedSites, customSites); }); - } + if (key === 'sites_updated') { + var sites_updated = storageChange.newValue ? storageChange.newValue : {}; + updatedSites = sites_updated; + set_rules(optionSites, updatedSites, customSites); + } if (key === 'sites_excluded') { var sites_excluded = storageChange.newValue ? storageChange.newValue : []; var sites_excluded_old = storageChange.oldValue ? storageChange.oldValue : []; @@ -959,7 +986,7 @@ function updateBadge(activeTab) { var ext_version_new; check_update(); function check_update() { - var manifest_new = 'https://bitbucket.org/magnolia1234/bypass-paywalls-firefox-clean/raw/master/manifest.json'; + var manifest_new = 'https://gitlab.com/magnolia1234/bypass-paywalls-' + url_loc + '-clean/-/raw/master/manifest.json'; fetch(manifest_new) .then(response => { if (response.ok) { diff --git a/changelog.txt b/changelog.txt index 4c6a0b1..f2d6aee 100644 --- a/changelog.txt +++ b/changelog.txt @@ -9,6 +9,7 @@ Fix Lee Enterprises Group Fix LesEchos.fr (json) Fix WaPo (images) Fix WSJ (timing) +Check for update rules at startup (opt-in) Refactor (custom) site rules * v2.4.2.0 (2021-10-31) diff --git a/manifest.json b/manifest.json index 13a8646..09ac70b 100644 --- a/manifest.json +++ b/manifest.json @@ -559,5 +559,5 @@ "*://*.wallkit.net/*", "*://*.wsj.net/*" ], - "version": "2.4.2.6" + "version": "2.4.2.7" } \ No newline at end of file diff --git a/options/options.js b/options/options.js index d8aa14f..a4d417a 100644 --- a/options/options.js +++ b/options/options.js @@ -96,7 +96,7 @@ function renderOptions() { function selectAll() { var inputEls = Array.from(document.querySelectorAll('input')); inputEls = inputEls.filter(function (input) { - return (!input.dataset.value.includes('#options_disable_')); + return (!input.dataset.value.match(/^#options_(disable|optin)_/)); }); inputEls.forEach(function (inputEl) { inputEl.checked = true; diff --git a/options/version.js b/options/version.js index 71dfd85..2910426 100644 --- a/options/version.js +++ b/options/version.js @@ -8,8 +8,8 @@ versionString_new.setAttribute('style', 'font-weight: bold;'); var anchorEl; const proxyurl = "https://bpc-cors-anywhere.herokuapp.com/"; -//const manifest_new = 'https://gitlab.com/magnolia1234/bypass-paywalls-firefox-clean/-/raw/master/manifest.json'; -const manifest_new = 'https://bitbucket.org/magnolia1234/bypass-paywalls-firefox-clean/raw/master/manifest.json'; +const manifest_new = 'https://gitlab.com/magnolia1234/bypass-paywalls-firefox-clean/-/raw/master/manifest.json'; +//const manifest_new = 'https://bitbucket.org/magnolia1234/bypass-paywalls-firefox-clean/raw/master/manifest.json'; //fetch(proxyurl + manifest_new, { headers: { "Content-Type": "application/json", "X-Requested-With": "XMLHttpRequest" } }) fetch(manifest_new) .then(response => { @@ -54,4 +54,6 @@ fetch(manifest_new) anchorEl.target = '_blank'; versionString_new.appendChild(anchorEl); } +}).catch(function (err) { + false; }); diff --git a/sites.js b/sites.js index 07c72b3..3f751fe 100644 --- a/sites.js +++ b/sites.js @@ -30,7 +30,7 @@ var defaultSites = { "destentor.nl", "gelderlander.nl" ], - remove_cookies_select_drop: ['temptationTrackingId'] + remove_cookies_select_drop: ["temptationTrackingId"] }, "Alma Talent (Finland)": { domain: "###_fi_alma_talent", @@ -60,7 +60,7 @@ var defaultSites = { }, "Ámbito": { domain: "ambito.com", - remove_cookies_select_drop: ['TDNotesRead'] + remove_cookies_select_drop: ["TDNotesRead"] }, "American Affairs": { domain: "americanaffairsjournal.org" @@ -138,7 +138,7 @@ var defaultSites = { "Barron's": { domain: "barrons.com", block_regex: /(cdn\.cxense\.com\/.+|cdn\.ampproject\.org\/v\d\/amp-(access|ad|consent|subscriptions)-.+\.js)/, - remove_cookies_select_hold: ['wsjregion'], + remove_cookies_select_hold: ["wsjregion"], useragent: "googlebot" }, "BBC History Extra": { @@ -167,7 +167,7 @@ var defaultSites = { "Bloomberg": { domain: "bloomberg.com", block_regex: /\.tinypass\.com\//, - remove_cookies_select_hold: ['bb_geo_info'] + remove_cookies_select_hold: ["bb_geo_info"] }, "Bloomberg Quint (free articles only)": { domain: "bloombergquint.com" @@ -186,7 +186,7 @@ var defaultSites = { }, "Caixin Global": { domain: "caixinglobal.com", - remove_cookies_select_drop: ['CAIXINGLB_LOGIN_UUID'] + remove_cookies_select_drop: ["CAIXINGLB_LOGIN_UUID"] }, "Challenges": { domain: "challenges.fr", @@ -406,7 +406,7 @@ var defaultSites = { "Financieele Dagblad (fd.nl)": { domain: "fd.nl", referer: "facebook", - remove_cookies_select_drop: ['socialread'] + remove_cookies_select_drop: ["socialread"] }, "First Things": { domain: "firstthings.com" @@ -496,11 +496,11 @@ var defaultSites = { "Griffith Review": { domain: "griffithreview.com", block_regex: /\.griffithreview\.com\/.+\/leaky-paywall\//, - remove_cookies_select_drop: ['issuem_lp'] + remove_cookies_select_drop: ["issuem_lp"] }, "Groene Amsterdammer": { domain: "groene.nl", - remove_cookies_select_hold: ['accept-cookies', 'popunder-hidden'] + remove_cookies_select_hold: ["accept-cookies", "popunder-hidden"] }, "Groupe EBRA (France)": { domain: "###_fr_groupe_ebra", @@ -1007,7 +1007,7 @@ var defaultSites = { "thecut.com", "vulture.com" ], - remove_cookies_select_drop: ['nymcid'], + remove_cookies_select_drop: ["nymcid"], block_regex: /fosse\.nymag\.com\/fosse\/.+\/scripts\/.+\.js/ }, "New Zealand Herald": { @@ -1049,7 +1049,7 @@ var defaultSites = { }, "NRC Handelsblad": { domain: "nrc.nl", - remove_cookies_select_drop: ['counter'] + remove_cookies_select_drop: ["counter"] }, "NyTeknik": { domain: "nyteknik.se", @@ -1108,7 +1108,7 @@ var defaultSites = { "Quartz (free articles only)": { domain: "qz.com", block_regex: /\.tinypass\.com\//, - remove_cookies_select_hold: ['gdpr'] + remove_cookies_select_hold: ["gdpr"] }, "Quora": { domain: "quora.com", @@ -1295,7 +1295,7 @@ var defaultSites = { }, "The Atlantic": { domain: "theatlantic.com", - remove_cookies_select_drop: ['articleViews'] + remove_cookies_select_drop: ["articleViews"] }, "The Australian Financial Review": { domain: "afr.com", @@ -1405,7 +1405,7 @@ var defaultSites = { }, "The New Statesman": { domain: "newstatesman.com", - remove_cookies_select_hold: ['STYXKEY_nsversion'] + remove_cookies_select_hold: ["STYXKEY_nsversion"] }, "The New York Review of Books": { domain: "nybooks.com", @@ -1426,7 +1426,7 @@ var defaultSites = { }, "The Point Magazine": { domain: "thepointmag.com", - remove_cookies_select_drop: ['monthly_history'] + remove_cookies_select_drop: ["monthly_history"] }, "The Saturday Paper": { domain: "thesaturdaypaper.com.au", @@ -1434,7 +1434,7 @@ var defaultSites = { }, "The Seattle Times": { domain: "seattletimes.com", - remove_cookies_select_hold: ['st_newsletter_splash_seen'] + remove_cookies_select_hold: ["st_newsletter_splash_seen"] }, "The Spectator (UK)": { domain: "spectator.co.uk", @@ -1480,7 +1480,7 @@ var defaultSites = { "The Wall Street Journal": { domain: "wsj.com", block_regex: /(cdn\.ampproject\.org\/v\d\/amp-(access|ad|consent|subscriptions)-.+\.js|cdn\.cxense\.com\/)/, - remove_cookies_select_hold: ['wsjregion', 'ResponsiveConditional_initialBreakpoint'], + remove_cookies_select_hold: ["wsjregion", "ResponsiveConditional_initialBreakpoint"], useragent: "googlebot" }, "The Washington Post": { @@ -1663,6 +1663,9 @@ var defaultSites = { "Enable new sites by default": { domain: "#options_enable_new_sites" }, + "Check for update rules at startup": { + domain: "#options_optin_update_rules" + }, "Restore opt-in for custom sites (on reload; Chrome-only)": { domain: "#options_restore_custom" }, diff --git a/sites_updated.json b/sites_updated.json new file mode 100644 index 0000000..2c63c08 --- /dev/null +++ b/sites_updated.json @@ -0,0 +1,2 @@ +{ +}