From 4ed4d8d8ed4c38b1c0e04676e4cf74f5b3a87975 Mon Sep 17 00:00:00 2001 From: magnolia1234 <7676006-magnolia1234@users.noreply.gitlab.com> Date: Thu, 18 Nov 2021 17:55:25 +0100 Subject: [PATCH] Updated sites: add code to contentScript --- background.js | 8 ++++++++ changelog.txt | 2 +- contentScript.js | 29 +++++++++++++++++++++++++++-- 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/background.js b/background.js index a9d6971..3e9507d 100644 --- a/background.js +++ b/background.js @@ -61,6 +61,8 @@ var blockedRegexes = {}; var amp_unhide; // redirect to amp-page var amp_redirect; +// code for contentScript +var cs_code; // custom: block javascript var block_js_custom = []; @@ -79,6 +81,7 @@ function initSetRules() { change_headers = []; amp_unhide = []; amp_redirect = {}; + cs_code = {}; block_js_custom = []; block_js_custom_ext = []; blockedRegexes = {}; @@ -241,6 +244,8 @@ function set_rules(sites, sites_updated, sites_custom) { // updated if (rule.amp_redirect) amp_redirect[domain] = rule.amp_redirect; + if (rule.cs_code) + cs_code[domain] = rule.cs_code; // custom if (rule.googlebot > 0) use_google_bot.push(domain); @@ -929,6 +934,9 @@ if (matchUrlDomain(change_headers, details.url) && (!['font', 'image', 'styleshe let amp_redirect_domain = ''; if (amp_redirect_domain = matchUrlDomain(Object.keys(amp_redirect), currentTabUrl)) bg2csData.amp_redirect = amp_redirect[amp_redirect_domain]; + let cs_code_domain = ''; + if (cs_code_domain = matchUrlDomain(Object.keys(cs_code), currentTabUrl)) + bg2csData.cs_code = cs_code[cs_code_domain]; ext_api.tabs.executeScript(tabId, { code: 'var bg2csData = ' + JSON.stringify(bg2csData) + ';' }, function () { diff --git a/changelog.txt b/changelog.txt index ca06c91..9c95cc8 100644 --- a/changelog.txt +++ b/changelog.txt @@ -6,7 +6,7 @@ Add Advance Local (local USA) Add Italian.tech Add Spectator World Fix Medium (faster) -Updated sites: add amp-redirect +Updated sites: add amp-redirect/code to contentScript * v2.4.4.0 (2021-11-14) Fix Nikkei Asian Review (cookies) diff --git a/contentScript.js b/contentScript.js index c342726..1a08892 100644 --- a/contentScript.js +++ b/contentScript.js @@ -46,7 +46,7 @@ if ((bg2csData !== undefined) && bg2csData.optin_setcookie) { // custom sites: try to unhide text on amp-page if ((bg2csData !== undefined) && bg2csData.amp_unhide) { window.setTimeout(function () { - let amp_page = document.querySelector('script[src^="https://cdn.ampproject.org/"]'); + let amp_page = document.querySelector('script[src^="https://cdn.ampproject.org/"]') && (window.location.hostname.match(/\/amp(\d)?\./) || window.location.pathname.match(/(\/amp\/|(_|-|\.)amp\.html$|-amp\d|\.amp$)/) || window.location.search.match(/\?(output(Type)?=)?amp/)); if (amp_page) { let preview = document.querySelector('[subscriptions-section="content-not-granted"]'); removeDOMElement(preview); @@ -65,7 +65,7 @@ if ((bg2csData !== undefined) && bg2csData.amp_unhide) { // updated sites: amp-redirect if ((bg2csData !== undefined) && bg2csData.amp_redirect) { window.setTimeout(function () { - let amp_page = document.querySelector('script[src^="https://cdn.ampproject.org/"]'); + let amp_page = document.querySelector('script[src^="https://cdn.ampproject.org/"]') && (window.location.hostname.match(/\/amp(\d)?\./) || window.location.pathname.match(/(\/amp\/|(_|-|\.)amp\.html$|-amp\d|\.amp$)/) || window.location.search.match(/\?(output(Type)?=)?amp/)); if (!amp_page) { let paywall = true; if (bg2csData.amp_redirect.paywall) @@ -79,6 +79,31 @@ if ((bg2csData !== undefined) && bg2csData.amp_redirect) { }, 500); // Delay (in milliseconds) } +function cs_code_elems(elems) { + for (let elem of elems) { + let elem_dom = document.querySelectorAll(elem.cond); + for (let item of elem_dom) { + if (elem.rm_elem) + removeDOMElement(item); + if (elem.rm_class) { + let rm_class = elem.rm_class.split(',').map(x => x.trim()); + item.classList.remove(...rm_class); + } + if (elem.rm_attrib) + item.removeAttribute(elem.rm_attrib); + if (elem.elems) + cs_code_elems(elem.elems); + } + } +} + +// updated sites: cs_code +if ((bg2csData !== undefined) && bg2csData.cs_code) { + window.setTimeout(function () { + cs_code_elems(bg2csData.cs_code); + }, 1000); // Delay (in milliseconds) +} + // Content workarounds/domain if (matchDomain(['medium.com', 'towardsdatascience.com']) || document.querySelector('script[src^="https://cdn-client.medium.com/"]')) {