bypass-paywalls-chrome-clean/background.js

359 lines
9.7 KiB
JavaScript
Raw Normal View History

2017-07-08 14:16:27 -07:00
'use strict';
var defaultSites = {
'Baltimore Sun': 'baltimoresun.com',
'Barron\'s': 'barrons.com',
2018-05-05 02:08:25 -07:00
'Bloomberg': 'bloomberg.com',
'Caixin': 'caixinglobal.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',
2018-07-04 18:02:08 -07:00
'Denver Post': 'denverpost.com',
'Dynamed Plus': 'dynamed.com',
2017-07-08 14:16:27 -07:00
'The Economist': 'economist.com',
'Les Echos': 'lesechos.fr',
2019-03-20 19:03:37 -07:00
'Encyclopedia Britannica': 'britannica.com',
2018-10-01 19:20:22 -07:00
'Examiner': 'examiner.com.au',
'Financial News': 'fnlondon.com',
2017-07-08 14:16:27 -07:00
'Financial Times': 'ft.com',
'Foreign Affairs': 'foreignaffairs.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',
2018-12-07 04:43:16 -08:00
'La Repubblica': 'repubblica.it',
'Liberation': 'liberation.fr',
2017-07-08 14:16:27 -07:00
'Los Angeles Times': 'latimes.com',
2018-01-30 21:26:21 -08:00
'Medium': 'medium.com',
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',
2018-08-25 17:03:12 -07:00
'OrlandoSentinel': 'orlandosentinel.com',
'Quora': 'quora.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',
2018-10-01 19:11:16 -07:00
'The Advocate': 'theadvocate.com.au',
'The Age': 'theage.com.au',
'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 Globe and Mail': 'theglobeandmail.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',
'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',
'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',
'Winston-Salem Journal': 'journalnow.com',
'Vanity Fair': 'vanityfair.com',
2018-10-01 19:11:16 -07:00
'Wired': 'wired.com'
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 = [
2017-07-08 14:16:27 -07:00
'asia.nikkei.com',
'nytimes.com',
'wsj.com',
2018-01-30 21:26:21 -08:00
'ft.com',
'fd.nl',
2018-04-05 23:34:36 -07:00
'mercurynews.com',
2018-04-07 11:01:10 -07:00
'theage.com.au',
2018-05-12 17:14:39 -07:00
'economist.com',
2018-07-04 18:02:08 -07:00
'bostonglobe.com',
2018-09-09 18:54:40 -07:00
'denverpost.com',
2018-09-30 00:33:18 -07:00
'chicagobusiness.com',
2018-09-30 02:34:59 -07:00
'theadvocate.com.au',
'examiner.com.au',
'hbr.org',
2019-01-05 18:01:37 -08:00
'medium.com',
2019-03-10 14:15:18 -07:00
'washingtonpost.com',
'nymag.com',
'theaustralian.com.au',
2017-07-08 14:16:27 -07:00
]
// Removes cookies after page load
2018-01-30 21:26:21 -08:00
const remove_cookies = [
2017-07-08 14:16:27 -07:00
'asia.nikkei.com',
2018-01-30 21:26:21 -08:00
'ft.com',
'fd.nl',
2018-04-05 23:34:36 -07:00
'mercurynews.com',
2018-04-07 11:01:10 -07:00
'theage.com.au',
2018-05-12 17:14:39 -07:00
'economist.com',
2018-06-08 14:14:57 -07:00
'bostonglobe.com',
2018-07-04 18:02:08 -07:00
'wired.com',
2018-09-09 18:54:40 -07:00
'denverpost.com',
'chicagobusiness.com',
2019-01-04 10:28:16 -08:00
'harpers.org',
2018-09-30 02:34:59 -07:00
'theadvocate.com.au',
'examiner.com.au',
'lesechos.fr',
'liberation.fr',
'hbr.org',
'medium.com',
'foreignpolicy.com',
2018-12-09 20:32:54 -08:00
'wsj.com',
2019-02-17 13:43:21 -08:00
'seattletimes.com',
'thenewsrep.com',
'washingtonpost.com',
'sfchronicle.com',
'nymag.com',
'foreignaffairs.com',
2019-04-28 13:43:51 -07:00
'scientificamerican.com',
'thestar.com',
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/,
/haaretz\.co\.il\/htz\/js\/inter\.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/*"],
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/'
});
}
}
// override User-Agent except on medium.com
if (details.url.indexOf("medium.com") === -1) {
requestHeaders.push({
"name": "User-Agent",
"value": useUserAgentMobile ? userAgentMobile : userAgentDesktop
})
}
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++) {
2018-09-18 12:24:21 -07:00
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];
}