-
Notifications
You must be signed in to change notification settings - Fork 16
/
background.js
152 lines (130 loc) · 3.89 KB
/
background.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
'use strict';
var defaultSites = {
'The Age': 'theage.com.au',
'Baltimore Sun': 'baltimoresun.com',
'Barron\'s': 'barrons.com',
'Crain\'s Chicago Business': 'chicagobusiness.com',
'Chicago Tribune': 'chicagotribune.com',
'Daily Press': 'dailypress.com',
'The Economist': 'economist.com',
'Financial Times': 'ft.com',
'Hartford Courant': 'courant.com',
'Harvard Business Review': 'hbr.org',
'Inc.com': 'inc.com',
'Los Angeles Times': 'latimes.com',
'Medscape': 'medscape.com',
'The Morning Call': 'mcall.com',
'The Nation': 'thenation.com',
'The New Yorker': 'newyorker.com',
'OrlandoSentinel': 'orlandosentinel.com',
'Quora': 'quora.com',
'SunSentinel': 'sun-sentinel.com',
'The Sydney Morning Herald': 'smh.com.au',
'The Telegraph': 'telegraph.co.uk',
'The Washington Post': 'washingtonpost.com',
'The Wall Street Journal': 'wsj.com'
};
var restrictions = {
'barrons.com': 'barrons.com/articles'
}
function setDefaultOptions() {
chrome.storage.sync.set({
sites: defaultSites
}, function() {
//console.log('default options saved');
chrome.tabs.create({ 'url': 'chrome://extensions/?options=' + chrome.runtime.id });
});
}
// Check whether new version is installed
chrome.runtime.onInstalled.addListener(function(details){
if (details.reason === "install") {
setDefaultOptions();
} else if (details.reason === "update") {
var thisVersion = chrome.runtime.getManifest().version;
if (['0.0.1','0.0.2','0.0.3','0.0.4'].indexOf(details.previousVersion) !== -1) {
setDefaultOptions();
}
//console.log("Updated from " + details.previousVersion + " to " + thisVersion + "!");
}
});
var blockedRegexes = [
/.+:\/\/.+\.tribdss\.com\//,
/thenation\.com\/.+\/paywall-script\.php/
];
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];
});
}
}
});
chrome.webRequest.onBeforeSendHeaders.addListener(function(details) {
if (blockedRegexes.some(function(regex) { return regex.test(details.url); })) {
return { cancel: true };
}
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;
});
if (!isEnabled) {
return;
}
var requestHeaders = details.requestHeaders;
var tabId = details.tabId;
var setReferer = false;
// if referer exists, set it to google
requestHeaders = requestHeaders.map(function(requestHeader) {
if (requestHeader.name === 'Referer') {
requestHeader.value = 'https://www.google.com/';
setReferer = true;
}
return requestHeader;
});
// otherwise add it
if (!setReferer) {
requestHeaders.push({
name: 'Referer',
value: 'https://www.google.com/'
});
}
// remove cookies
requestHeaders = requestHeaders.map(function(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>']
}, ['blocking', 'requestHeaders']);