From 5301c8ccda2b4f6ef2d20fc9405b848a93b619b9 Mon Sep 17 00:00:00 2001 From: magnolia1234 <7676006-magnolia1234@users.noreply.gitlab.com> Date: Sun, 19 Feb 2023 12:36:09 +0100 Subject: [PATCH] Update custom sites (add_ext_link) --- README.md | 1 + background.js | 11 +++- changelog.txt | 1 + contentScript.js | 35 ++++++++++++ custom/manifest.json | 2 +- manifest.json | 2 +- options/optin/opt-in.js | 12 +---- options/options_custom.js | 111 ++++++++++++++++++++------------------ options/popup.html | 2 +- options/popup.js | 2 +- 10 files changed, 111 insertions(+), 68 deletions(-) diff --git a/README.md b/README.md index a3bdf5d..2c2bbf8 100644 --- a/README.md +++ b/README.md @@ -903,6 +903,7 @@ Additional custom options: * redirect to amp-page when paywall(selector) * load text from json when paywall|article(selector) * load text from Google webcache when paywall|article(selector) +* add external link to archive-site when paywall|article(selector) * remove/unhide elements in dom (optional for dev; check examples) [Example list of custom sites](https://gitlab.com/magnolia1234/bypass-paywalls-firefox-clean/-/blob/master/custom/sites_custom.json) or [download list (json)](https://gitlab.com/magnolia1234/bypass-paywalls-firefox-clean/-/raw/master/custom/sites_custom.json) diff --git a/background.js b/background.js index 141a19d..4e6a78b 100644 --- a/background.js +++ b/background.js @@ -80,6 +80,8 @@ var cs_code; var ld_json; // load text from Google webcache var ld_google_webcache; +// add external link to article +var add_ext_link; // custom: block javascript var block_js_custom = []; @@ -103,6 +105,7 @@ function initSetRules() { cs_code = {}; ld_json = {}; ld_google_webcache = {}; + add_ext_link = {}; block_js_custom = []; block_js_custom_ext = []; blockedRegexes = {}; @@ -338,6 +341,8 @@ function set_rules(sites, sites_updated, sites_custom) { ld_json[domain] = rule.ld_json; if (rule.ld_google_webcache) ld_google_webcache[domain] = rule.ld_google_webcache; + if (rule.add_ext_link && rule.add_ext_link_type) + add_ext_link[domain] = {css: rule.add_ext_link, type: rule.add_ext_link_type}; } } } @@ -526,7 +531,8 @@ ext_api.storage.onChanged.addListener(function (changes, namespace) { } // Refresh the current tab - if (!['ext_version_new'].includes(key)) + let refresh = (url_loc === 'chrome') || ((typeof storageChange.newValue === 'string') && (storageChange.newValue !== storageChange.oldValue)) || ((typeof storageChange.newValue === 'object') && (Object.keys(storageChange.newValue).length !== Object.keys(storageChange.oldValue).length)); + if (refresh) refreshCurrentTab(); } }); @@ -741,6 +747,9 @@ if (typeof browser !== 'object') { let ld_google_webcache_domain = ''; if (ld_google_webcache_domain = matchUrlDomain(Object.keys(ld_google_webcache), url)) bg2csData.ld_google_webcache = ld_google_webcache[ld_google_webcache_domain]; + let add_ext_link_domain = ''; + if (add_ext_link_domain = matchUrlDomain(Object.keys(add_ext_link), url)) + bg2csData.add_ext_link = add_ext_link[add_ext_link_domain]; let tab_runs = 5; for (let n = 0; n < tab_runs; n++) { setTimeout(function () { diff --git a/changelog.txt b/changelog.txt index 3916904..210ca96 100644 --- a/changelog.txt +++ b/changelog.txt @@ -17,6 +17,7 @@ Fix Grupo Abril (js) Fix Lc.nl Fix Outside (js) Update block general paywall script (FewCents) +Update custom sites (add_ext_link) * v3.0.5.0 (2023-02-12) Add L'Informe (fr) diff --git a/contentScript.js b/contentScript.js index 3dc180c..130b89f 100644 --- a/contentScript.js +++ b/contentScript.js @@ -106,6 +106,37 @@ if (bg2csData.ld_google_webcache) { } } +// custom/updated sites: add link to article +if (bg2csData.add_ext_link) { + if (bg2csData.add_ext_link.css && bg2csData.add_ext_link.css.includes('|') && bg2csData.add_ext_link.type) { + window.setTimeout(function () { + let url = window.location.href; + let add_ext_link_split = bg2csData.add_ext_link.css.split('|'); + let paywall_sel = add_ext_link_split[0]; + let article_sel = add_ext_link_split[1]; + let paywall = document.querySelectorAll(paywall_sel); + if (paywall.length) { + removeDOMElement(...paywall); + let article = document.querySelector(article_sel); + if (article) { + switch (bg2csData.add_ext_link.type) { + case 'archive.is': + article.firstChild.before(archiveLink(url)); + break; + case '12ft.io': + article.firstChild.before(ext_12ftLink(url)); + break; + case 'google_search_tool': + article.firstChild.before(googleSearchToolLink(url)); + break; + } + + } + } + }, 1000); + } +} + // custom/updated sites: try to unhide text on amp-page if (bg2csData.amp_unhide) { window.setTimeout(function () { @@ -4851,6 +4882,10 @@ function googleWebcacheLink(url, text_fail = 'BPC > Full article text:\r\n') { return externalLink(['webcache.googleusercontent.com'], 'https://{domain}/search?q=cache:{url}', url, text_fail); } +function googleSearchToolLink(url, text_fail = 'BPC > Full article text:\r\n') { + return externalLink(['search.google.com'], 'https://search.google.com/test/rich-results?url={url}', encodeURIComponent(url), text_fail); +} + function ext_12ftLink(url, text_fail = 'BPC > Full article text:\r\n') { return externalLink(['12ft.io'], 'https://{domain}/{url}', url, text_fail); } diff --git a/custom/manifest.json b/custom/manifest.json index e816a6b..5590a55 100644 --- a/custom/manifest.json +++ b/custom/manifest.json @@ -51,5 +51,5 @@ "webRequestBlocking", "*://*/*" ], - "version": "3.0.5.7" + "version": "3.0.5.8" } diff --git a/manifest.json b/manifest.json index 9019093..353201c 100644 --- a/manifest.json +++ b/manifest.json @@ -737,5 +737,5 @@ "*://*.wallkit.net/*", "*://webcache.googleusercontent.com/*" ], - "version": "3.0.5.7" + "version": "3.0.5.8" } diff --git a/options/optin/opt-in.js b/options/optin/opt-in.js index c1027b0..e91bca9 100644 --- a/options/optin/opt-in.js +++ b/options/optin/opt-in.js @@ -3,14 +3,10 @@ var ext_api = (typeof browser === 'object') ? browser : chrome; var manifestData = ext_api.runtime.getManifest(); var navigator_ua = navigator.userAgent; var navigator_ua_mobile = navigator_ua.toLowerCase().includes('mobile'); -var custom_switch = manifestData.optional_permissions && manifestData.optional_permissions.length && !navigator_ua_mobile; +var custom_switch = ((manifestData.optional_permissions && manifestData.optional_permissions.length) || (manifestData.optional_host_permissions && manifestData.optional_host_permissions.length)) && !navigator_ua_mobile; window.addEventListener("load", function () { document.getElementById("button-close").addEventListener("click", function () { - ext_api.storage.local.set({ - "optInShown": true, - "customShown": true - }); window.close(); }); @@ -39,9 +35,6 @@ window.addEventListener("load", function () { } else { custom_enabled.innerText = 'NO'; } - ext_api.storage.local.set({ - "customShown": true - }); }); }); @@ -55,9 +48,6 @@ window.addEventListener("load", function () { "customOptIn": false }); } - ext_api.storage.local.set({ - "customShown": true - }); }); }); diff --git a/options/options_custom.js b/options/options_custom.js index 63776e7..ce36ca2 100644 --- a/options/options_custom.js +++ b/options/options_custom.js @@ -4,6 +4,7 @@ var url_loc = (typeof browser === 'object') ? 'firefox' : 'chrome'; var useragent_options = ['', 'googlebot', 'bingbot', 'facebookbot']; var referer_options = ['', 'facebook', 'google', 'twitter']; var random_ip_options = ['', 'all', 'eu']; +var add_ext_link_type_options = ['', 'archive.is', '12ft.io', 'google_search_tool'] function capitalize(str) { return (typeof str === 'string') ? str.charAt(0).toUpperCase() + str.slice(1) : ''; @@ -211,20 +212,22 @@ function edit_options() { var edit_site = sites_custom[title]; document.querySelector('input[data-key="title"]').value = title; document.querySelector('input[data-key="domain"]').value = edit_site.domain; - document.querySelector('select[data-key="useragent"]').selectedIndex = (edit_site.googlebot > 0) ? 1 : useragent_options.indexOf(edit_site.useragent); document.querySelector('input[data-key="allow_cookies"]').checked = (edit_site.allow_cookies > 0); document.querySelector('input[data-key="remove_cookies"]').checked = (edit_site.remove_cookies > 0); + document.querySelector('select[data-key="useragent"]').selectedIndex = (edit_site.googlebot > 0) ? 1 : useragent_options.indexOf(edit_site.useragent); + document.querySelector('select[data-key="referer"]').selectedIndex = referer_options.indexOf(edit_site.referer); + document.querySelector('select[data-key="random_ip"]').selectedIndex = random_ip_options.indexOf(edit_site.random_ip); document.querySelector('input[data-key="block_js"]').checked = (edit_site.block_js > 0 || edit_site.block_javascript > 0); document.querySelector('input[data-key="block_js_ext"]').checked = (edit_site.block_js_ext > 0 || edit_site.block_javascript_ext > 0); document.querySelector('input[data-key="block_js_inline"]').value = edit_site.block_js_inline ? edit_site.block_js_inline : ''; document.querySelector('input[data-key="block_regex"]').value = edit_site.block_regex ? edit_site.block_regex : ''; - document.querySelector('textarea[data-key="cs_code"]').value = edit_site.cs_code ? edit_site.cs_code : ''; document.querySelector('input[data-key="amp_unhide"]').checked = (edit_site.amp_unhide > 0); document.querySelector('input[data-key="amp_redirect"]').value = edit_site.amp_redirect ? edit_site.amp_redirect : ''; document.querySelector('input[data-key="ld_json"]').value = edit_site.ld_json ? edit_site.ld_json : ''; document.querySelector('input[data-key="ld_google_webcache"]').value = edit_site.ld_google_webcache ? edit_site.ld_google_webcache : ''; - document.querySelector('select[data-key="referer"]').selectedIndex = referer_options.indexOf(edit_site.referer); - document.querySelector('select[data-key="random_ip"]').selectedIndex = random_ip_options.indexOf(edit_site.random_ip); + document.querySelector('input[data-key="add_ext_link"]').value = edit_site.add_ext_link ? edit_site.add_ext_link : ''; + document.querySelector('select[data-key="add_ext_link_type"]').selectedIndex = add_ext_link_type_options.indexOf(edit_site.add_ext_link_type); + document.querySelector('textarea[data-key="cs_code"]').value = edit_site.cs_code ? edit_site.cs_code : ''; }); } @@ -283,6 +286,9 @@ function renderOptions() { 'domain': 0, 'allow_cookies': 1, 'remove_cookies': 1, + 'useragent': 0, + 'referer': 0, + 'random_ip': 0, 'block_js (domain)': 1, 'block_js_ext': 1, 'block_js_inline': 0, @@ -291,59 +297,59 @@ function renderOptions() { 'amp_redirect': 0, 'ld_json': 0, 'ld_google_webcache': 0, + 'add_ext_link': 0, + 'add_ext_link_type': 0, 'cs_code': 0, }; - for (var key in add_checkboxes) { - labelEl = document.createElement('label'); - if (!['cs_code'].includes(key)) { - inputEl = document.createElement('input'); - inputEl.size = 25; - } else { - inputEl = document.createElement('textarea'); - inputEl.rows = 5; - inputEl.cols = 25; - } - inputEl.dataset.key = key.split(' (')[0]; - labelEl.appendChild(inputEl); - if (add_checkboxes[key]) { - inputEl.type = 'checkbox'; - inputEl.dataset.value = 1; - } else { - let placeholders = { - title: 'Example', - domain: 'example.com', - block_js_inline: '\\.example\\.com\\/article\\/', - block_regex: '\\.example\\.com\\/js\\/', - amp_redirect: 'div.paywall', - ld_json: 'div.paywall|div.article', - ld_google_webcache: 'div.paywall|div.article', - cs_code: 'for dev: check GitLab examples', - }; - if (placeholders[key]) - inputEl.placeholder = placeholders[key]; - } - labelEl.appendChild(document.createTextNode(' ' + key)); - add_sitesEl.appendChild(labelEl); - } - var add_options = { useragent: useragent_options, referer: referer_options, - random_ip: random_ip_options + random_ip: random_ip_options, + add_ext_link_type: add_ext_link_type_options }; - for (let key in add_options) { + for (var key in add_checkboxes) { + if (add_checkboxes[key]) { + inputEl = document.createElement('input'); + inputEl.type = 'checkbox'; + inputEl.dataset.value = 1; + } else { + if (add_options[key]) { + inputEl = document.createElement('select'); + for (let elem of add_options[key]) { + let option = document.createElement("option"); + option.value = elem; + option.text = elem; + inputEl.appendChild(option); + } + } else { + if (!['cs_code'].includes(key)) { + inputEl = document.createElement('input'); + inputEl.size = 25; + } else { + inputEl = document.createElement('textarea'); + inputEl.rows = 5; + inputEl.cols = 25; + } + let placeholders = { + title: 'Example', + domain: 'example.com', + block_js_inline: '\\.example\\.com\\/article\\/', + block_regex: '\\.example\\.com\\/js\\/', + amp_redirect: 'div.paywall', + ld_json: 'div.paywall|div.article', + ld_google_webcache: 'div.paywall|div.article', + add_ext_link: 'div.paywall|div.article', + cs_code: 'for dev: check GitLab examples', + }; + if (placeholders[key]) + inputEl.placeholder = placeholders[key]; + } + } labelEl = document.createElement('label'); - labelEl.appendChild(document.createTextNode(key + ' ')); - inputEl = document.createElement('select'); + labelEl.style = 'margin: 2px 0px;'; inputEl.dataset.key = key.split(' (')[0]; labelEl.appendChild(inputEl); - - for (let elem of add_options[key]) { - let option = document.createElement("option"); - option.value = elem; - option.text = elem; - inputEl.appendChild(option); - } + labelEl.appendChild(document.createTextNode(' ' + key)); add_sitesEl.appendChild(labelEl); } @@ -363,20 +369,21 @@ function renderOptions() { let isDefaultSite = defaultSites_domains.includes(domain); optionEl.text = isDefaultSite ? '*' : ''; optionEl.text += key + ': ' + domain + - (sites_custom[key]['googlebot'] > 0 ? ' | googlebot' : '') + (sites_custom[key]['allow_cookies'] > 0 ? ' | allow_cookies' : '') + (sites_custom[key]['remove_cookies'] > 0 ? ' | remove_cookies' : '') + + (sites_custom[key]['useragent'] ? ' | useragent: ' + sites_custom[key]['useragent'] : '') + + (sites_custom[key]['googlebot'] > 0 ? ' | googlebot' : '') + + (sites_custom[key]['referer'] ? ' | referer: ' + sites_custom[key]['referer'] : '') + + (sites_custom[key]['random_ip'] ? ' | random_ip: ' + sites_custom[key]['random_ip'] : '') + ((sites_custom[key]['block_js'] > 0 || sites_custom[key]['block_javascript'] > 0) ? ' | block_js' : '') + ((sites_custom[key]['block_js_ext'] > 0 || sites_custom[key]['block_javascript_ext'] > 0) ? ' | block_js_ext' : '') + (sites_custom[key]['block_js_inline'] ? ' | block_js_inline' : '') + (sites_custom[key]['block_regex'] ? ' | block_regex' : '') + - (sites_custom[key]['useragent'] ? ' | useragent: ' + sites_custom[key]['useragent'] : '') + - (sites_custom[key]['referer'] ? ' | referer: ' + sites_custom[key]['referer'] : '') + - (sites_custom[key]['random_ip'] ? ' | random_ip: ' + sites_custom[key]['random_ip'] : '') + (sites_custom[key]['amp_unhide'] > 0 ? ' | amp_unhide' : '') + (sites_custom[key]['amp_redirect'] ? ' | amp_redirect' : '') + (sites_custom[key]['ld_json'] ? ' | ld_json' : '') + (sites_custom[key]['ld_google_webcache'] ? ' | ld_google_webcache' : '') + + (sites_custom[key]['add_ext_link'] && sites_custom[key]['add_ext_link_type'] ? ' | add_ext_link' : '') + (sites_custom[key]['cs_code'] ? ' | cs_code' : ''); optionEl.value = key; selectEl.add(optionEl); diff --git a/options/popup.html b/options/popup.html index 9d519ed..2f2e421 100644 --- a/options/popup.html +++ b/options/popup.html @@ -14,7 +14,7 @@ } -
+