bypass-paywalls-firefox-clean/background.js

412 lines
11 KiB
JavaScript
Raw Normal View History

2017-07-08 17:40:29 +00:00
'use strict';
var defaultSites = {
'Algemeen Dagblad': 'ad.nl',
2017-07-08 17:40:29 +00:00
'Baltimore Sun': 'baltimoresun.com',
'Barron\'s': 'barrons.com',
2018-05-06 00:53:07 +00:00
'Bloomberg': 'bloomberg.com',
2018-07-07 22:11:09 +00:00
'Business Insider': 'businessinsider.com',
'Caixin': 'caixinglobal.com',
2019-01-05 19:39:45 +00:00
'Chemical & Engineering News': 'cen.acs.org',
2017-07-08 17:40:29 +00:00
'Chicago Tribune': 'chicagotribune.com',
'Central Western Daily': 'centralwesterndaily.com.au',
'Crain\'s Chicago Business': 'chicagobusiness.com',
2018-04-06 06:22:58 +00:00
'Corriere Della Sera': 'corriere.it',
2017-07-08 17:40:29 +00:00
'Daily Press': 'dailypress.com',
2018-07-05 01:46:28 +00:00
'Denver Post': 'denverpost.com',
'De Tijd': 'tijd.be',
'De Groene Amsterdammer': 'groene.nl',
'de Volkskrant': 'volkskrant.nl',
2017-07-08 17:40:29 +00:00
'The Economist': 'economist.com',
'Eindhovens Dagblad': 'ed.nl',
2018-10-02 01:52:35 +00:00
'Examiner': 'examiner.com.au',
2017-07-08 17:40:29 +00:00
'Financial Times': 'ft.com',
'Foreign Policy': 'foreignpolicy.com',
2017-07-08 17:40:29 +00:00
'Glassdoor': 'glassdoor.com',
'Haaretz': 'haaretz.co.il',
2018-04-23 09:20:40 +00:00
'Haaretz English': 'haaretz.com',
'Handelsblatt': 'handelsblatt.com',
2017-07-08 17:40:29 +00:00
'Hartford Courant': 'courant.com',
'Harvard Business Review': 'hbr.org',
'Inc.com': 'inc.com',
2019-03-10 20:03:53 +00:00
'Investors Chronicle': 'investorschronicle.co.uk',
2018-12-06 21:23:04 +00:00
'La Repubblica': 'repubblica.it',
'Le Temps': 'letemps.ch',
2017-07-08 17:40:29 +00:00
'Los Angeles Times': 'latimes.com',
2018-01-31 05:08:11 +00:00
'Medium': 'medium.com',
2017-07-08 17:40:29 +00:00
'Medscape': 'medscape.com',
'MIT Technology Review': 'technologyreview.com',
2018-10-29 04:44:18 +00:00
'Mountain View Voice': 'mv-voice.com',
'National Post': 'nationalpost.com',
2018-07-23 01:55:35 +00:00
'New Statesman': 'newstatesman.com',
2019-02-19 01:25:35 +00:00
'New York Magazine': 'nymag.com',
2017-07-08 17:40:29 +00:00
'Nikkei Asian Review': 'asia.nikkei.com',
'NRC': 'nrc.nl',
2019-06-27 03:26:30 +00:00
'New Zealand Herald': 'nzherald.co.nz',
2018-10-29 04:44:18 +00:00
'Orange County Register': 'ocregister.com',
'Orlando Sentinel': 'orlandosentinel.com',
'Palo Alto Online': 'paloaltoonline.com',
'Parool': 'parool.nl',
2019-09-30 01:18:59 +00:00
'Quartz': 'qz.com',
2018-10-29 04:44:18 +00:00
'Quora': 'quora.com',
2019-07-11 17:48:40 +00:00
'Statista': 'statista.com',
'Telegraaf': 'telegraaf.nl',
2018-10-29 04:44:18 +00:00
'SunSentinel': 'sun-sentinel.com',
2018-10-02 01:52:35 +00:00
'The Advocate': 'theadvocate.com.au',
'The Age': 'theage.com.au',
2019-09-30 00:47:26 +00:00
'The Atlantic': 'theatlantic.com',
'The Australian': 'theaustralian.com.au',
2018-12-18 07:45:33 +00:00
'The Australian Financial Review': 'afr.com',
2019-09-30 00:27:24 +00:00
'The Boston Globe': 'bostonglobe.com',
2018-06-30 19:48:40 +00:00
'The Business Journals': 'bizjournals.com',
2019-05-12 23:40:27 +00:00
'The Diplomat': 'thediplomat.com',
'The Globe and Mail': 'theglobeandmail.com',
'The Herald': 'theherald.com.au',
2018-09-09 20:21:36 +00:00
'The Japan Times': 'japantimes.co.jp',
2018-10-29 04:44:18 +00:00
'TheMarker': 'themarker.com',
2018-01-31 05:08:11 +00:00
'The Mercury News': 'mercurynews.com',
2017-07-08 17:40:29 +00:00
'The Morning Call': 'mcall.com',
'The Nation': 'thenation.com',
'The New York Times': 'nytimes.com',
'The New Yorker': 'newyorker.com',
2019-01-05 19:39:45 +00:00
'The News-Gazette': 'news-gazette.com',
'The Saturday Paper': 'thesaturdaypaper.com.au',
2018-07-23 02:04:27 +00:00
'The Spectator': 'spectator.co.uk',
2017-07-08 17:40:29 +00:00
'The Seattle Times': 'seattletimes.com',
'The Sydney Morning Herald': 'smh.com.au',
'The Times': 'thetimes.co.uk',
2018-08-28 23:15:23 +00:00
'The Toronto Star': 'thestar.com',
2017-07-08 17:40:29 +00:00
'The Washington Post': 'washingtonpost.com',
2018-06-03 18:23:04 +00:00
'The Wall Street Journal': 'wsj.com',
'Towards Data Science': 'towardsdatascience.com',
'Trouw': 'trouw.nl',
'Vanity Fair': 'vanityfair.com',
'Vrij Nederland': 'vn.nl',
2018-10-02 01:52:35 +00:00
'Wired': 'wired.com'
2017-07-08 17:40:29 +00:00
};
2018-01-31 05:08:11 +00:00
const restrictions = {
2017-07-08 17:40:29 +00:00
'barrons.com': 'barrons.com/articles'
}
// Don't remove cookies before page load
2018-01-31 05:08:11 +00:00
const allow_cookies = [
2017-07-08 17:40:29 +00:00
'asia.nikkei.com',
'nytimes.com',
'wsj.com',
'ft.com',
'letemps.ch',
2018-04-06 06:22:58 +00:00
'mercurynews.com',
2018-05-13 00:14:07 +00:00
'economist.com',
2019-09-30 00:27:24 +00:00
'bostonglobe.com',
2018-07-05 01:46:28 +00:00
'denverpost.com',
'hacked.com',
2018-07-23 01:55:35 +00:00
'ocregister.com',
2018-07-23 02:04:27 +00:00
'newstatesman.com',
'spectator.co.uk',
'towardsdatascience.com',
2018-09-28 14:39:45 +00:00
'medium.com',
'theadvocate.com.au',
2018-10-07 05:18:34 +00:00
'examiner.com.au',
2019-01-06 01:59:26 +00:00
'thestar.com',
'washingtonpost.com',
2019-02-10 23:43:12 +00:00
'hbr.org',
2019-02-19 01:25:35 +00:00
'nymag.com',
2019-04-07 04:09:19 +00:00
'theaustralian.com.au',
'ad.nl',
'ed.nl',
'parool.nl',
'telegraaf.nl',
'trouw.nl',
'vn.nl',
'volkskrant.nl'
2017-07-08 17:40:29 +00:00
]
// Removes cookies after page load
2018-01-31 05:08:11 +00:00
const remove_cookies = [
2017-07-08 17:40:29 +00:00
'asia.nikkei.com',
2018-10-19 04:28:29 +00:00
'nytimes.com',
'ft.com',
'letemps.ch',
2018-04-06 06:22:58 +00:00
'mercurynews.com',
2018-04-07 18:02:08 +00:00
'theage.com.au',
2018-05-13 00:14:07 +00:00
'economist.com',
2019-09-30 00:27:24 +00:00
'bostonglobe.com',
2018-07-05 01:46:28 +00:00
'denverpost.com',
'hacked.com',
2018-07-23 01:55:35 +00:00
'ocregister.com',
2018-07-23 02:04:27 +00:00
'newstatesman.com',
'spectator.co.uk',
'towardsdatascience.com',
2018-09-28 14:39:45 +00:00
'medium.com',
'theadvocate.com.au',
2018-10-07 05:18:34 +00:00
'examiner.com.au',
'thestar.com',
'centralwesterndaily.com.au',
'theherald.com.au',
'foreignpolicy.com',
'wsj.com',
2019-01-05 19:39:45 +00:00
'glassdoor.com',
'cen.acs.org',
2019-02-10 23:43:12 +00:00
'hbr.org',
'thenewsrep.com',
2019-02-19 01:25:35 +00:00
'washingtonpost.com',
'nymag.com',
'nationalpost.com',
'handelsblatt.com',
2019-05-12 23:40:27 +00:00
'thediplomat.com',
2019-09-30 00:44:03 +00:00
'latimes.com',
2019-09-30 00:47:26 +00:00
'theatlantic.com',
2019-09-30 01:04:17 +00:00
'chicagobusiness.com',
2019-09-30 01:18:59 +00:00
'qz.com',
'ad.nl',
'ed.nl',
'telegraaf.nl',
'vn.nl'
2017-07-08 17:40:29 +00:00
]
// Override User-Agent with Googlebot
const use_google_bot = [
'theaustralian.com.au',
'telegraph.co.uk',
'thetimes.co.uk',
2019-05-31 04:09:02 +00:00
'barrons.com',
]
2017-07-08 17:40:29 +00:00
function setDefaultOptions() {
browser.storage.sync.set({
sites: defaultSites
}, function() {
browser.runtime.openOptionsPage();
});
}
2018-01-31 05:08:11 +00:00
const blockedRegexes = [
2017-07-08 17:40:29 +00:00
/.+:\/\/.+\.tribdss\.com\//,
2018-10-07 00:53:49 +00:00
/thenation\.com\/.+\/paywall-script\.php/,
2019-06-27 03:26:30 +00:00
/haaretz\.co\.il\/htz\/js\/inter\.js/,
/nzherald\.co\.nz\/.+\/headjs\/.+\.js/
2017-07-08 17:40:29 +00: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 17:40:29 +00:00
var enabledSites = [];
// Get the enabled sites
browser.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
browser.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
browser.runtime.onInstalled.addListener(function(details) {
2017-07-08 17:40:29 +00:00
if (details.reason == "install") {
setDefaultOptions();
} else if (details.reason == "update") {
// User updated extension
}
});
2019-02-10 23:43:12 +00:00
// WSJ bypass
browser.webRequest.onBeforeSendHeaders.addListener(function(details) {
if (!isSiteEnabled(details) || details.url.indexOf("mod=rsswn") !== -1 || details.url.indexOf("/print-edition/") !== -1) {
2019-02-10 23:43:12 +00:00
return;
}
2017-07-08 17:40:29 +00:00
2019-02-10 23:43:12 +00:00
var param;
var updatedUrl;
2017-07-08 17:40:29 +00:00
2019-02-10 23:43:12 +00:00
param = getParameterByName("mod", details.url);
2017-07-08 17:40:29 +00:00
2019-02-10 23:43:12 +00:00
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 17:40:29 +00: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};
},
{
2019-09-30 00:34:16 +00:00
urls: ["*://*.theglobeandmail.com/*", "*://*.economist.com/*", "*://*.thestar.com/*", "*://*.newstatesman.com/*", "*://*.bostonglobe.com/*", "*://*.afr.com/*"],
types: ["script"]
},
["blocking"]
);
2019-02-10 23:43:12 +00:00
browser.webRequest.onBeforeSendHeaders.addListener(function(details) {
if (!isSiteEnabled(details)) {
2017-07-08 17:40:29 +00:00
return;
}
2018-11-24 22:10:21 +00:00
if (blockedRegexes.some(function(regex) { return regex.test(details.url); })) {
return { cancel: true };
}
2017-07-08 17:40:29 +00:00
var requestHeaders = details.requestHeaders;
var tabId = details.tabId;
var useUserAgentMobile = false;
2017-07-08 17:40:29 +00:00
var setReferer = false;
// if referer exists, set it to google
requestHeaders = requestHeaders.map(function(requestHeader) {
2017-07-08 17:40:29 +00:00
if (requestHeader.name === 'Referer') {
if (details.url.indexOf("cooking.nytimes.com/api/v1/users/bootstrap") !== -1) {
// this fixes images not being loaded on cooking.nytimes.com main page
// referrer has to be *nytimes.com otherwise returns 403
requestHeader.value = 'https://cooking.nytimes.com';
2019-02-21 03:37:08 +00:00
} else 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");
}
return requestHeader;
});
2017-07-08 17:40:29 +00: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/'
});
}
}
2017-07-08 17:40:29 +00: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
})
requestHeaders.push({
"name": "X-Forwarded-For",
"value": "66.249.66.1"
})
2017-07-08 17:40:29 +00: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
browser.tabs.executeScript(tabId, {
file: 'contentScript.js',
runAt: 'document_start'
}, function(res) {
if (browser.runtime.lastError || res[0]) {
return;
}
});
}
return { requestHeaders: requestHeaders };
}, {
urls: ['<all_urls>']
}, ['blocking', 'requestHeaders']);
// remove cookies after page load
browser.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
}
browser.cookies.getAll({domain: domainVar}, function(cookies) {
for (var i=0; i<cookies.length; i++) {
2018-10-19 04:28:29 +00:00
var cookie = {
url: (cookies[i].secure ? "https://" : "http://") + cookies[i].domain + cookies[i].path,
name: cookies[i].name,
storeId: cookies[i].storeId
};
if (cookies[i].firstPartyDomain !== undefined) {
cookie.firstPartyDomain = cookies[i].firstPartyDomain;
}
browser.cookies.remove(cookie);
2017-07-08 17:40:29 +00:00
}
});
}
}, {
urls: ["<all_urls>"]
2018-08-28 23:15:23 +00:00
});
2019-02-10 23:43:12 +00:00
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, ' '));
}
function stripQueryStringAndHashFromPath(url) {
return url.split("?")[0].split("#")[0];
2019-06-27 03:26:30 +00:00
}