bypass-paywalls-chrome-clean/background.js

461 lines
13 KiB
JavaScript
Raw Normal View History

/* Please respect alphabetical order when adding a site in any list */
2017-07-08 14:16:27 -07:00
'use strict';
// Cookies from this list are blocked by default
2017-07-08 14:16:27 -07:00
var defaultSites = {
'Algemeen Dagblad': 'ad.nl',
2019-05-12 22:27:15 -07:00
'American Banker': 'americanbanker.com',
2017-07-08 14:16:27 -07:00
'Baltimore Sun': 'baltimoresun.com',
'Barron\'s': 'barrons.com',
2018-05-05 02:08:25 -07:00
'Bloomberg': 'bloomberg.com',
2019-10-06 10:27:44 -07:00
'Bloomberg Quint': 'bloombergquint.com',
'Business Insider': 'businessinsider.com',
2017-07-08 14:16:27 -07:00
'Crain\'s Chicago Business': 'chicagobusiness.com',
'Chicago Tribune': 'chicagotribune.com',
2018-04-05 23:34:36 -07:00
'Corriere Della Sera': 'corriere.it',
2017-07-08 14:16:27 -07:00
'Daily Press': 'dailypress.com',
2018-04-05 23:34:36 -07:00
'Dagens Nyheter': 'dn.se',
'De Groene Amsterdammer': 'groene.nl',
'De Volkskrant': 'volkskrant.nl',
2019-05-25 14:41:10 -07:00
'DeMorgen': 'demorgen.be',
2018-07-04 18:02:08 -07:00
'Denver Post': 'denverpost.com',
'Dynamed Plus': 'dynamed.com',
2019-06-02 04:38:20 -07:00
'Eindhovens Dagblad': 'ed.nl',
2019-03-20 19:03:37 -07:00
'Encyclopedia Britannica': 'britannica.com',
2018-10-01 19:20:22 -07:00
'Examiner': 'examiner.com.au',
2019-09-23 11:21:21 -07:00
'First Things': 'firstthings.com',
'Financial News': 'fnlondon.com',
2017-07-08 14:16:27 -07:00
'Financial Times': 'ft.com',
'Foreign Policy': 'foreignpolicy.com',
2017-07-08 14:16:27 -07:00
'Glassdoor': 'glassdoor.com',
'Haaretz': 'haaretz.co.il',
2018-04-23 02:21:18 -07:00
'Haaretz English': 'haaretz.com',
2017-07-08 14:16:27 -07:00
'Hartford Courant': 'courant.com',
2019-01-04 10:28:16 -08:00
'Harper\'s Magazine': 'harpers.org',
2017-07-08 14:16:27 -07:00
'Harvard Business Review': 'hbr.org',
'Inc.com': 'inc.com',
2019-03-10 14:15:18 -07:00
'Irish Times': 'irishtimes.com',
'La Nacion': 'lanacion.com.ar',
2018-12-07 04:43:16 -08:00
'La Repubblica': 'repubblica.it',
2019-10-16 00:41:57 -07:00
'La Tercera': 'latercera.com',
'L\'Echo': 'lecho.be',
2019-10-29 03:29:53 -07:00
'Le Devoir': 'ledevoir.com',
'Le Monde': 'lemonde.fr',
2019-11-01 10:39:23 -07:00
'Le Parisien': 'leparisien.fr',
'Les Echos': 'lesechos.fr',
'Liberation': 'liberation.fr',
2019-10-15 05:11:26 -07:00
'Loeb Classical Library': 'loebclassics.com',
'Los Angeles Business Journal': 'labusinessjournal.com',
2017-07-08 14:16:27 -07:00
'Los Angeles Times': 'latimes.com',
2018-01-30 21:26:21 -08:00
'Medium': 'medium.com',
'Mexico News Daily': 'mexiconewsdaily.com',
2019-06-20 22:28:29 -07:00
'MIT Sloan Management Review': 'sloanreview.mit.edu',
2017-07-08 14:16:27 -07:00
'MIT Technology Review': 'technologyreview.com',
2019-02-17 13:43:21 -08:00
'Newsrep': 'thenewsrep.com',
'New York Magazine': 'nymag.com',
2017-07-08 14:16:27 -07:00
'Nikkei Asian Review': 'asia.nikkei.com',
'NRC': 'nrc.nl',
2019-09-22 17:10:17 -07:00
'New Zealand Herald': 'nzherald.co.nz',
2019-09-25 09:54:25 -07:00
'OrlandoSentinel': 'orlandosentinel.com',
'Parool': 'parool.nl',
2019-05-25 14:25:56 -07:00
'Quartz': 'qz.com',
2018-08-25 17:03:12 -07:00
'Quora': 'quora.com',
2019-10-16 00:41:57 -07:00
'San Diego Union Tribune': 'sandiegouniontribune.com',
'San Francisco Chronicle': 'sfchronicle.com',
2019-04-28 13:43:51 -07:00
'Scientific American': 'scientificamerican.com',
2018-08-25 17:03:12 -07:00
'SunSentinel': 'sun-sentinel.com',
2019-07-11 10:43:44 -07:00
'Statista':'statista.com',
2019-06-02 04:33:52 -07:00
'Telegraaf': 'telegraaf.nl',
2018-10-01 19:11:16 -07:00
'The Advocate': 'theadvocate.com.au',
'The Age': 'theage.com.au',
2019-09-16 11:07:04 -07:00
'The American Interest': 'the-american-interest.com',
2019-09-18 02:04:03 -07:00
'The Atlantic': 'theatlantic.com',
'The Australian': 'theaustralian.com.au',
'The Australian Financial Review': 'afr.com',
2018-01-30 21:26:21 -08:00
'The Boston Globe': 'bostonglobe.com',
'The Business Journals': 'bizjournals.com',
2019-10-27 17:21:46 -07:00
'The Canberra Times': 'canberratimes.com.au',
'The Economist': 'economist.com',
'The Globe and Mail': 'theglobeandmail.com',
2019-10-29 03:29:53 -07:00
'The Hindu': 'thehindu.com',
2019-03-20 18:55:00 -07:00
'The Japan Times': 'japantimes.co.jp',
'TheMarker': 'themarker.com',
2018-01-30 21:26:21 -08:00
'The Mercury News': 'mercurynews.com',
2017-07-08 14:16:27 -07:00
'The Morning Call': 'mcall.com',
'The Nation': 'thenation.com',
'The News-Gazette': 'news-gazette.com',
2018-08-25 16:59:06 -07:00
'The New Statesman': 'newstatesman.com',
2017-07-08 14:16:27 -07:00
'The New York Times': 'nytimes.com',
'The New Yorker': 'newyorker.com',
2019-10-16 02:11:22 -07:00
'The Philadelphia Inquirer': 'inquirer.com',
2017-07-08 14:16:27 -07:00
'The Seattle Times': 'seattletimes.com',
2018-08-25 17:00:48 -07:00
'The Spectator': 'spectator.co.uk',
2017-07-08 14:16:27 -07:00
'The Sydney Morning Herald': 'smh.com.au',
2019-06-21 20:42:35 -07:00
'The Telegraph': 'telegraph.co.uk',
2019-09-22 17:03:15 -07:00
'The Times': 'thetimes.co.uk',
'The Toronto Star': 'thestar.com',
2017-07-08 14:16:27 -07:00
'The Washington Post': 'washingtonpost.com',
2018-06-26 23:09:29 -07:00
'The Wall Street Journal': 'wsj.com',
'Trouw': 'trouw.nl',
'Winston-Salem Journal': 'journalnow.com',
'Vanity Fair': 'vanityfair.com',
'Vrij Nederland': 'vn.nl',
2019-06-21 20:53:21 -07:00
'Wired': 'wired.com',
'Zeit Online': 'zeit.de'
2017-07-08 14:16:27 -07:00
};
2018-01-30 21:26:21 -08:00
const restrictions = {
2017-07-08 14:16:27 -07:00
'barrons.com': 'barrons.com/articles'
}
// Don't remove cookies before page load
2018-01-30 21:26:21 -08:00
const allow_cookies = [
'ad.nl',
'asia.nikkei.com',
2018-07-04 18:02:08 -07:00
'bostonglobe.com',
2019-10-27 17:21:46 -07:00
'canberratimes.com.au',
2018-09-30 00:33:18 -07:00
'chicagobusiness.com',
'demorgen.be',
'denverpost.com',
'economist.com',
'ed.nl',
'examiner.com.au',
'ft.com',
2019-10-23 13:29:38 -07:00
'harpers.org',
'hbr.org',
'lemonde.fr',
'lesechos.fr',
2019-01-05 18:01:37 -08:00
'medium.com',
'mercurynews.com',
'mexiconewsdaily.com',
'nrc.nl',
'nymag.com',
'nytimes.com',
'parool.nl',
2019-10-23 04:45:10 -07:00
'qz.com',
'scientificamerican.com',
'seattletimes.com',
'telegraaf.nl',
'the-american-interest.com',
'theadvocate.com.au',
'theage.com.au',
'theatlantic.com',
'theaustralian.com.au',
'trouw.nl',
'vn.nl',
'volkskrant.nl',
'washingtonpost.com',
'wsj.com'
2017-07-08 14:16:27 -07:00
]
// Removes cookies after page load
2018-01-30 21:26:21 -08:00
const remove_cookies = [
'ad.nl',
'asia.nikkei.com',
2018-06-08 14:14:57 -07:00
'bostonglobe.com',
2019-10-27 17:21:46 -07:00
'canberratimes.com.au',
'chicagobusiness.com',
'demorgen.be',
'denverpost.com',
'economist.com',
'ed.nl',
'examiner.com.au',
'ft.com',
2019-10-23 13:29:38 -07:00
'harpers.org',
'hbr.org',
'lesechos.fr',
'medium.com',
'mercurynews.com',
'mexiconewsdaily.com',
'nrc.nl',
'nymag.com',
2019-10-23 04:45:10 -07:00
'qz.com',
'scientificamerican.com',
'seattletimes.com',
2019-06-02 04:33:52 -07:00
'telegraaf.nl',
'theadvocate.com.au',
'theage.com.au',
'theatlantic.com',
'vn.nl',
'washingtonpost.com',
'wsj.com'
2017-07-08 14:16:27 -07:00
]
// select specific cookie(s) to hold from remove_cookies domains
const remove_cookies_select_hold = {
'nrc.nl': ['nmt_closed_cookiebar'],
'washingtonpost.com': ['wp_gdpr'],
'wsj.com': ['wsjregion']
}
// select only specific cookie(s) to drop from remove_cookies domains
const remove_cookies_select_drop = {
'ad.nl': ['temptationTrackingId'],
'demorgen.be': ['TID_ID'],
'ed.nl': ['temptationTrackingId'],
'nrc.nl': ['counter']
}
2019-05-12 14:00:27 -07:00
// Override User-Agent with Googlebot
const use_google_bot = [
2019-05-25 14:18:22 -07:00
'barrons.com',
2019-11-01 10:39:23 -07:00
'lemonde.fr',
2019-09-22 16:54:54 -07:00
'mexiconewsdaily.com',
'nytimes.com',
'quora.com',
'telegraph.co.uk',
'theaustralian.com.au',
2019-09-22 17:03:15 -07:00
'thetimes.co.uk',
'wsj.com',
'zeit.de',
2019-05-12 14:00:27 -07:00
]
2017-07-08 14:16:27 -07:00
function setDefaultOptions() {
chrome.storage.sync.set({
sites: defaultSites
}, function() {
chrome.tabs.create({ 'url': 'chrome://extensions/?options=' + chrome.runtime.id });
});
}
var blockedRegexes = [
/.+:\/\/.+\.tribdss\.com\//,
/thenation\.com\/.+\/paywall-script\.php/,
2019-09-22 17:10:17 -07:00
/haaretz\.co\.il\/htz\/js\/inter\.js/,
/nzherald\.co\.nz\/.+\/headjs\/.+\.js/
2017-07-08 14:16:27 -07:00
];
const userAgentDesktop = "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"
const userAgentMobile = "Chrome/41.0.2272.96 Mobile Safari/537.36 (compatible ; Googlebot/2.1 ; +http://www.google.com/bot.html)"
2017-07-08 14:16:27 -07:00
var enabledSites = [];
// Get the enabled sites
chrome.storage.sync.get({
sites: {}
}, function(items) {
var sites = items.sites;
enabledSites = Object.keys(items.sites).map(function(key) {
return items.sites[key];
});
});
// Listen for changes to options
chrome.storage.onChanged.addListener(function(changes, namespace) {
var key;
for (key in changes) {
var storageChange = changes[key];
if (key === 'sites') {
var sites = storageChange.newValue;
enabledSites = Object.keys(sites).map(function(key) {
return sites[key];
});
}
}
});
// Set and show default options on install
chrome.runtime.onInstalled.addListener(function (details) {
if (details.reason == "install") {
setDefaultOptions();
} else if (details.reason == "update") {
// User updated extension
}
});
/**
// WSJ bypass
chrome.webRequest.onBeforeRequest.addListener(function (details) {
if (!isSiteEnabled(details) || details.url.indexOf("mod=rsswn") !== -1) {
return;
}
2019-02-07 19:23:57 -08:00
var param;
var updatedUrl;
param = getParameterByName("mod", details.url);
if (param === null) {
updatedUrl = stripQueryStringAndHashFromPath(details.url);
updatedUrl += "?mod=rsswn";
} else {
updatedUrl = details.url.replace(param, "rsswn");
}
return { redirectUrl: updatedUrl};
},
{urls:["*://*.wsj.com/*"], types:["main_frame"]},
["blocking"]
);
**/
2017-07-08 14:16:27 -07:00
// Disable javascript for these sites
chrome.webRequest.onBeforeRequest.addListener(function(details) {
if (!isSiteEnabled(details) || details.url.indexOf("mod=rsswn") !== -1) {
return;
}
return {cancel: true};
},
{
urls: ["*://*.thestar.com/*", "*://*.economist.com/*", "*://*.theglobeandmail.com/*", "*://*.afr.com/*", "*://*.bizjournals.com/*", "*://*.businessinsider.com/*", "*://*.bostonglobe.com/*"],
types: ["script"]
},
["blocking"]
);
2017-07-08 14:16:27 -07:00
chrome.webRequest.onBeforeSendHeaders.addListener(function(details) {
if (!isSiteEnabled(details)) {
2017-07-08 14:16:27 -07:00
return;
}
if (blockedRegexes.some(function(regex) { return regex.test(details.url); })) {
return { cancel: true };
}
2017-07-08 14:16:27 -07:00
var requestHeaders = details.requestHeaders;
var tabId = details.tabId;
var useUserAgentMobile = false;
2017-07-08 14:16:27 -07:00
var setReferer = false;
// if referer exists, set it to google
requestHeaders = requestHeaders.map(function (requestHeader) {
2017-07-08 14:16:27 -07:00
if (requestHeader.name === 'Referer') {
2019-02-20 19:42:13 -08:00
if (details.url.indexOf("wsj.com") !== -1 || details.url.indexOf("ft.com") !== -1) {
requestHeader.value = 'https://www.facebook.com/';
} else {
requestHeader.value = 'https://www.google.com/';
}
setReferer = true;
}
if (requestHeader.name === 'User-Agent') {
useUserAgentMobile = requestHeader.value.toLowerCase().includes("mobile");
}
2017-07-08 14:16:27 -07:00
return requestHeader;
});
2017-07-08 14:16:27 -07:00
// otherwise add it
if (!setReferer) {
if (details.url.indexOf("wsj.com") !== -1) {
requestHeaders.push({
name: 'Referer',
value: 'https://www.facebook.com/'
});
} else {
requestHeaders.push({
name: 'Referer',
value: 'https://www.google.com/'
});
}
}
2019-05-12 14:00:27 -07:00
// override User-Agent to use Googlebot
var useGoogleBot = use_google_bot.filter(function(item) {
return typeof item == 'string' && details.url.indexOf(item) > -1;
}).length > 0;
if (useGoogleBot) {
requestHeaders.push({
"name": "User-Agent",
"value": useUserAgentMobile ? userAgentMobile : userAgentDesktop
})
2019-05-12 14:00:27 -07:00
requestHeaders.push({
"name": "X-Forwarded-For",
"value": "66.249.66.1"
})
}
2017-07-08 14:16:27 -07:00
// remove cookies before page load
requestHeaders = requestHeaders.map(function(requestHeader) {
for (var siteIndex in allow_cookies) {
if (details.url.indexOf(allow_cookies[siteIndex]) !== -1) {
return requestHeader;
}
}
if (requestHeader.name === 'Cookie') {
requestHeader.value = '';
}
return requestHeader;
});
if (tabId !== -1) {
// run contentScript inside tab
chrome.tabs.executeScript(tabId, {
file: 'contentScript.js',
runAt: 'document_start'
}, function(res) {
if (chrome.runtime.lastError || res[0]) {
return;
}
});
}
return { requestHeaders: requestHeaders };
}, {
urls: ['<all_urls>']
2019-02-02 15:30:42 -08:00
}, ['blocking', 'requestHeaders', 'extraHeaders']);
2017-07-08 14:16:27 -07:00
// remove cookies after page load
chrome.webRequest.onCompleted.addListener(function(details) {
for (var domainIndex in remove_cookies) {
var domainVar = remove_cookies[domainIndex];
if (!enabledSites.includes(domainVar) || details.url.indexOf(domainVar) === -1) {
continue; // don't remove cookies
}
chrome.cookies.getAll({domain: domainVar}, function(cookies) {
for (var i=0; i<cookies.length; i++) {
var cookie_domain = cookies[i].domain;
var rc_domain = cookie_domain.replace(/^(\.?www\.|\.)/, '');
// hold specific cookie(s) from remove_cookies domains
if ((rc_domain in remove_cookies_select_hold) && remove_cookies_select_hold[rc_domain].includes(cookies[i].name)){
continue; // don't remove specific cookie
}
// drop only specific cookie(s) from remove_cookies domains
if ((rc_domain in remove_cookies_select_drop) && !(remove_cookies_select_drop[rc_domain].includes(cookies[i].name))){
continue; // only remove specific cookie
}
chrome.cookies.remove({url: (cookies[i].secure ? "https://" : "http://") + cookies[i].domain + cookies[i].path, name: cookies[i].name});
}
2017-07-08 14:16:27 -07:00
});
}
}, {
urls: ["<all_urls>"]
});
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-69824169-2']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = 'https://ssl.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
function isSiteEnabled(details) {
var isEnabled = enabledSites.some(function(enabledSite) {
var useSite = details.url.indexOf("." + enabledSite) !== -1;
if (enabledSite in restrictions) {
return useSite && details.url.indexOf(restrictions[enabledSite]) !== -1;
}
return useSite;
});
return isEnabled;
}
function getParameterByName(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, '\\$&');
var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, ' '));
2019-02-07 19:23:57 -08:00
}
function stripQueryStringAndHashFromPath(url) {
return url.split("?")[0].split("#")[0];
2019-06-05 04:46:20 -07:00
}