Remove Cors Anywhere (cached articles)

merge-requests/4/head
magnolia1234 2 years ago
parent 5163233012
commit 9c732dcf04

@ -1328,6 +1328,41 @@ ext_api.runtime.onMessage.addListener(function (message, sender) {
if (message.request === 'refreshCurrentTab') {
refreshCurrentTab();
}
if (message.request === 'getExtSrc' && message.data) {
fetch(message.data.url)
.then(response => {
if (response.ok) {
response.text().then(html => {
if (message.data.base64) {
html = decode_utf8(atob(html));
message.data.selector_source = 'body';
}
let parser = new DOMParser();
let doc = parser.parseFromString(html, 'text/html');
let article_new = doc.querySelector(message.data.selector_source);
message.data.html = article_new.outerHTML;
ext_api.tabs.query({
active: true,
currentWindow: true
}, function (tabs) {
if (tabs && tabs[0] && tabs[0].url && tabs[0].url.startsWith('http')) {
ext_api.tabs.sendMessage(sender.tab.id, {msg: "showExtSrc", data: message.data});
}
});
});
}
}).catch(function (err) {
message.data.html = '';
ext_api.tabs.query({
active: true,
currentWindow: true
}, function (tabs) {
if (tabs && tabs[0] && tabs[0].url && tabs[0].url.startsWith('http')) {
ext_api.tabs.sendMessage(sender.tab.id, {msg: "showExtSrc", data: message.data});
}
});
});
}
if (message.scheme && (![chrome_scheme, 'undefined'].includes(message.scheme) || focus_changed)) {
let icon_path = {path: {'128': 'bypass.png'}};
if (message.scheme === 'dark')
@ -1433,6 +1468,10 @@ function stripQueryStringAndHashFromPath(url) {
return url.split("?")[0].split("#")[0];
}
function decode_utf8(str) {
return decodeURIComponent(escape(str));
}
function randomInt(max) {
return Math.floor(Math.random() * Math.floor(max));
}

@ -9,6 +9,7 @@ Remove NK News (fix obsolete)
Fix The Globe and Mail (sections)
Fix The Hill Times (text)
Fix (custom) group-sites (limit identify on image)
Remove Cors Anywhere (cached articles)
* v2.6.3.0 (2022-04-03)
Add Diario Cordoba

@ -80,6 +80,14 @@ if ((bg2csData !== undefined) && bg2csData.ld_json && dompurify_loaded) {
var div_bpc_done = document.querySelector('div#bpc_done');
if (!div_bpc_done) {
ext_api.runtime.onMessage.addListener(
function(request, sender) {
if (request.msg === 'showExtSrc') {
replaceDomElementExtSrc(request.data.url, request.data.html, true, false, request.data.selector, request.data.text_fail, request.data.selector_source);
}
}
);
// check for opt-in confirmation (from background.js)
if ((bg2csData !== undefined) && bg2csData.optin_setcookie) {
if (domain = matchDomain(['belfasttelegraph.co.uk', 'independent.ie'])) {
@ -2022,6 +2030,7 @@ else if (matchDomain('prospectmagazine.co.uk')) {
let paywall = document.querySelector('div.paywall_overlay_blend, div.paywall');
if (paywall) {
removeDOMElement(paywall);
csDoneOnce = true;
let url_cache = 'https://webcache.googleusercontent.com/search?q=cache:' + url;
replaceDomElementExt(url_cache, true, false, 'main');
}
@ -2255,6 +2264,7 @@ else if (matchDomain('valor.globo.com')) {
let paywall = document.querySelector('div.paywall');
if (paywall) {
removeDOMElement(paywall);
csDoneOnce = true;
let url_cache = 'https://webcache.googleusercontent.com/search?q=cache:' + url;
replaceDomElementExt(url_cache, true, false, 'div.protected-content');
}
@ -3011,6 +3021,7 @@ else if (matchDomain('newleftreview.org')) {
let paywall = document.querySelector('div.promo-wrapper');
if (paywall) {
removeDOMElement(paywall);
csDoneOnce = true;
let url_cache = 'https://webcache.googleusercontent.com/search?q=cache:' + url;
replaceDomElementExt(url_cache, true, false, 'div.article-page');
}
@ -3785,47 +3796,67 @@ function matchDomain(domains, hostname = window.location.hostname) {
}
function replaceDomElementExt(url, proxy, base64, selector, text_fail = '', selector_source = selector) {
let proxyurl = proxy ? 'https://bpc-cors-anywhere.herokuapp.com/' : '';
fetch(proxyurl + url, {headers: {"Content-Type": "text/plain", "X-Requested-With": "XMLHttpRequest"} })
.then(response => {
let article = document.querySelector(selector);
if (response.ok) {
response.text().then(html => {
if (base64) {
html = decode_utf8(atob(html));
selector_source = 'body';
}
let parser = new DOMParser();
let doc = parser.parseFromString(DOMPurify.sanitize(html, {ADD_ATTR: ['layout'], ADD_TAGS: ['amp-img']}), 'text/html');
//console.log(DOMPurify.removed);
let article_new = doc.querySelector(selector_source);
if (article_new) {
if (article && article.parentNode)
article.parentNode.replaceChild(article_new, article);
if (proxy) {
if (!text_fail) {
if (url.includes('webcache.googleusercontent.com'))
text_fail = 'BPC > failed to load from Google webcache: '
}
ext_api.runtime.sendMessage({request: 'getExtSrc', data: {url: url, selector: selector, selector_source: selector_source, base64: base64, text_fail: text_fail}});
} else {
fetch(url)
.then(response => {
let article = document.querySelector(selector);
if (response.ok) {
response.text().then(html => {
replaceDomElementExtSrc(url, html, false, base64, selector, text_fail, selector_source);
});
} else {
if (text_fail && article) {
replaceTextFail(article, proxy, text_fail)
}
});
} else {
if (!text_fail) {
if (url.includes('webcache.googleusercontent.com'))
text_fail = 'BPC > failed to load from Google webcache: '
}
if (text_fail && article) {
let text_fail_div = document.createElement('div');
text_fail_div.setAttribute('style', 'margin: 0px 50px; font-weight: bold; color: red;');
text_fail_div.appendChild(document.createTextNode(text_fail));
if (proxy) {
let a_link = document.createElement('a');
a_link.innerText = url;
a_link.href = url;
a_link.target = '_blank';
text_fail_div.appendChild(a_link);
}
article.insertBefore(text_fail_div, article.firstChild);
}).catch(function (err) {
false;
});
}
}
function replaceDomElementExtSrc(url, html, proxy, base64, selector, text_fail = '', selector_source = selector) {
let article = document.querySelector(selector);
if (html) {
if (base64) {
html = decode_utf8(atob(html));
selector_source = 'body';
}
let parser = new DOMParser();
window.setTimeout(function () {
let doc = parser.parseFromString(DOMPurify.sanitize(html, {ADD_ATTR: ['layout'], ADD_TAGS: ['amp-img']}), 'text/html');
//console.log(DOMPurify.removed);
let article_new = doc.querySelector(selector_source);
if (article_new) {
if (article && article.parentNode)
article.parentNode.replaceChild(article_new, article);
}
}, 200);
} else {
replaceTextFail(url, article, proxy, text_fail);
}
}
function replaceTextFail(url, article, proxy, text_fail) {
if (text_fail && article) {
let text_fail_div = document.createElement('div');
text_fail_div.setAttribute('style', 'margin: 0px 50px; font-weight: bold; color: red;');
text_fail_div.appendChild(document.createTextNode(text_fail));
if (proxy) {
let a_link = document.createElement('a');
a_link.innerText = url;
a_link.href = url;
a_link.target = '_blank';
text_fail_div.appendChild(a_link);
}
}).catch(function (err) {
false;
});
article.insertBefore(text_fail_div, article.firstChild);
}
}
function archiveLink(url) {

@ -661,7 +661,8 @@
"*://*.repstatic.it/*",
"*://*.sanoma-sndp.fi/*",
"*://*.wallkit.net/*",
"*://*.wsj.net/*"
"*://*.wsj.net/*",
"*://webcache.googleusercontent.com/*"
],
"version": "2.6.3.4"
"version": "2.6.3.5"
}

Loading…
Cancel
Save