From c12c7073a120b83b8a50b5523980813cc6fc91f3 Mon Sep 17 00:00:00 2001 From: magnolia1234 <7676006-magnolia1234@users.noreply.gitlab.com> Date: Sun, 23 May 2021 20:42:28 +0200 Subject: [PATCH] Fix-update Medium/Towards Data Science --- background.js | 39 +++++++++++++++++++++++++++++++-------- changelog.txt | 1 + contentScript.js | 20 +++++++++++++++----- manifest.json | 2 +- 4 files changed, 48 insertions(+), 14 deletions(-) diff --git a/background.js b/background.js index c5cbad5..c2d2800 100644 --- a/background.js +++ b/background.js @@ -621,6 +621,7 @@ ext_api.storage.local.get({ } } disableJavascriptOnListedSites(); + disableJavascriptInlineOnMediumSites(); }); // Listen for changes to options @@ -755,7 +756,7 @@ ext_api.storage.onChanged.addListener(function (changes, namespace) { active: true, currentWindow: true }, function (tabs) { - if (tabs.length > 0 && tabs[0].url && tabs[0].url.indexOf("http") !== -1) { + if (tabs && tabs[0] && tabs[0].url.startsWith('http')) { ext_api.tabs.update(tabs[0].id, { url: tabs[0].url }); @@ -910,7 +911,7 @@ ext_api.webRequest.onHeadersReceived.addListener(function (details) { ['blocking', 'responseHeaders']); // block inline script -var block_js_inline = ["*://elviajero.elpais.com/*", "*://retina.elpais.com/*", "*://verne.elpais.com/*", "*://*.medianama.com/*"] +var block_js_inline = ["*://elviajero.elpais.com/*", "*://retina.elpais.com/*", "*://verne.elpais.com/*", "*://*.medianama.com/*"]; ext_api.webRequest.onHeadersReceived.addListener(function (details) { if (!isSiteEnabled(details)) { return; @@ -929,6 +930,28 @@ ext_api.webRequest.onHeadersReceived.addListener(function (details) { }, ['blocking', 'responseHeaders']); +// medium sites: block inline script +var medium_block_js_inline = ["*://*.medium.com/*", "*://*.towardsdatascience.com/*"]; +function disableJavascriptInlineOnMediumSites() { + ext_api.webRequest.onHeadersReceived.addListener(function (details) { + if (!isSiteEnabled(details) || !details.url.split('?')[0].match(/((\w)+(\-)+){3,}/)) { + return; + } + var headers = details.responseHeaders; + headers.push({ + 'name': 'Content-Security-Policy', + 'value': "script-src *;" + }); + return { + responseHeaders: headers + }; + }, { + 'types': ['main_frame', 'sub_frame'], + 'urls': medium_block_js_inline + }, + ['blocking', 'responseHeaders']); +} + var block_js_default = ["*://cdn.tinypass.com/*", "*://*.piano.io/*", "*://*.poool.fr/*", "*://cdn.ampproject.org/v*/amp-access-*.js", "*://*.blueconic.net/*", "*://*.cxense.com/*", "*://*.evolok.net/*", "*://js.matheranalytics.com/*", "*://*.newsmemory.com/*", "*://*.onecount.net/*", "*://js.pelcro.com/*", "*://*.qiota.com/*", "*://*.tribdss.com/*"]; var block_js_custom = []; var block_js_custom_ext = []; @@ -1055,7 +1078,7 @@ ext_api.webRequest.onBeforeSendHeaders.addListener(function(details) { active: true, currentWindow: true }, function (tabs) { - if (tabs.length > 0 && tabs[0].url && tabs[0].url.indexOf("http") !== -1) { + if (tabs && tabs[0] && tabs[0].url.startsWith('http')) { ext_api.tabs.executeScript({ file: 'options/toggleIcon.js', runAt: 'document_start' @@ -1216,7 +1239,7 @@ if (matchUrlDomain(change_headers, details.url) && (['main_frame', 'sub_frame', active: true, currentWindow: true }, function (tabs) { - if (tabs.length > 0 && tabs[0].url && tabs[0].url.indexOf("http") !== -1) { + if (tabs && tabs[0] && tabs[0].url.startsWith('http')) { if (isSiteEnabled({url: tabs[0].url})) { ext_api.tabs.executeScript({ file: 'contentScript.js', @@ -1308,7 +1331,7 @@ function site_switch() { active: true, currentWindow: true }, function (tabs) { - if (tabs.length > 0 && tabs[0].url && tabs[0].url.indexOf("http") !== -1) { + if (tabs && tabs[0] && tabs[0].url.startsWith('http')) { let currentUrl = tabs[0].url; let isDefaultSite = matchUrlDomain(defaultSites_grouped_domains, currentUrl); if (!isDefaultSite) { @@ -1354,7 +1377,7 @@ function remove_cookies_fn(domainVar, exclusions = false) { active: true, currentWindow: true }, function (tabs) { - if (tabs.length > 0 && tabs[0].url && tabs[0].url.indexOf("http") !== -1) { + if (tabs && tabs[0] && tabs[0].url.startsWith('http')) { let tabId = tabs[0].id; let storeId = '0'; for (let store of cookieStores) { @@ -1414,7 +1437,7 @@ function clear_cookies() { active: true, currentWindow: true }, function (tabs) { - if (tabs.length > 0 && tabs[0].url && tabs[0].url.indexOf("http") !== -1) { + if (tabs && tabs[0] && tabs[0].url.startsWith('http')) { ext_api.tabs.executeScript({ file: 'options/clearCookies.js', runAt: 'document_start' @@ -1457,7 +1480,7 @@ ext_api.runtime.onMessage.addListener(function (message, sender) { active: true, currentWindow: true }, function (tabs) { - if (tabs.length > 0 && tabs[0].url && tabs[0].url.indexOf("http") !== -1) { + if (tabs && tabs[0] && tabs[0].url.startsWith('http')) { let currentUrl = tabs[0].url; let domain; let isExcludedSite = matchUrlDomain(excludedSites, currentUrl); diff --git a/changelog.txt b/changelog.txt index 9eb005b..7c0e271 100644 --- a/changelog.txt +++ b/changelog.txt @@ -4,6 +4,7 @@ Changelog Bypass Paywalls Clean - Firefox Post-release Add MediaNama (India) Remove Mitteldeutsche Zeitung (obsolete) +Fix-update Medium/Towards Data Science Fix-update National Geographic USA Fix-update Prensa Iberica Fix-update Saechsische Zeitung (amp) diff --git a/contentScript.js b/contentScript.js index 7431283..1ecad03 100644 --- a/contentScript.js +++ b/contentScript.js @@ -74,6 +74,18 @@ if (matchDomain(['medium.com', 'towardsdatascience.com']) || document.querySelec if (meter) meter.hidden = true; }, 500); // Delay (in milliseconds) + let hidden_images = document.querySelectorAll('img:not([src])'); + let parser = new DOMParser(); + for (let hidden_image of hidden_images) { + let noscript = hidden_image.parentElement.parentElement.querySelector('noscript'); + if (noscript && noscript.innerHTML) { + let html = parser.parseFromString(noscript.innerHTML, 'text/html'); + if (html.querySelector('img')) { + hidden_image.parentNode.replaceChild(html.querySelector('img'), hidden_image); + removeDOMElement(noscript); + } + } + } } else if (window.location.hostname.match(/\.(com|net)\.au$/) || matchDomain(['afr.com'])) {//australia @@ -2028,13 +2040,11 @@ else if (matchDomain('newyorker.com')) { for (let asset_invisible of invisible_assets) asset_invisible.classList.remove('responsive-asset--invisible'); let overlays = document.querySelectorAll('.aspect-ratio--overlay-container, .asset-embed__asset-container'); - let noscript, - html; let parser = new DOMParser(); for (let overlay of overlays) { - noscript = overlay.querySelector('noscript'); - if (noscript) { - html = parser.parseFromString(noscript.innerHTML, 'text/html'); + let noscript = overlay.querySelector('noscript'); + if (noscript && noscript.innerHTML) { + let html = parser.parseFromString(noscript.innerHTML, 'text/html'); overlay.appendChild(html.querySelector('img')); removeDOMElement(noscript); } diff --git a/manifest.json b/manifest.json index 5469728..e0170f7 100644 --- a/manifest.json +++ b/manifest.json @@ -517,5 +517,5 @@ "*://*.wallkit.net/*", "*://*.wsj.net/*" ], - "version": "2.2.1.8" + "version": "2.2.1.9" } \ No newline at end of file