forked from nextgens/anti-paywall
-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
139 lines (127 loc) · 3.49 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
const websites = [
"*://*.adelaidenow.com.au/*",
"*://*.baltimoresun.com/*",
"*://*.barrons.com/*",
"*://*.chicagobusiness.com/*",
"*://*.chicagotribune.com/*",
"*://*.chip.de/*",
"*://*.clarin.com/*",
"*://*.courant.com/*",
"*://*.couriermail.com.au/*",
"*://*.cricketarchive.com/*",
"*://*.dailypress.com/*",
"*://*.dailytelegraph.com.au/*",
"*://*.durangoherald.com/*",
"*://*.economist.com/*",
"*://*.fd.nl/*",
"*://*.forbes.com/*",
"*://*.ft.com/*",
"*://*.geelongadvertiser.com.au/*",
"*://*.goldcoastbulletin.com.au/*",
"*://*.haaretz.co.il/*",
"*://*.haaretz.com/*",
"*://*.hbr.org/*",
"*://*.heraldsun.com.au/*",
"*://*.inc.com/*",
"*://*.independent.co.uk/*",
"*://*.investingdaily.com/*",
"*://*.irishtimes.com/*",
"*://*.kansas.com/*",
"*://*.kansascity.com/*",
"*://*.latimes.com/*",
"*://*.lanacion.com.ar/*",
"*://*.letemps.ch/*",
"*://*.mcall.com/*",
"*://*.medscape.com/*",
"*://*.medium.com/*",
"*://*.nationalpost.com/*",
"*://*.newsweek.com/*",
"*://*.newyorker.com/*",
"*://*.nikkei.com/*",
"*://*.nrc.nl/*",
"*://*.nyt.com/*",
"*://*.nytimes.com/*",
"*://*.ocregister.com/*",
"*://*.orlandosentinel.com/*",
"*://*.quora.com/*",
"*://*.scmp.com/*",
"*://*.seattletimes.com/*",
"*://*.slashdot.org/*",
"*://*.smh.com.au/*",
"*://*.sun-sentinel.com/*",
"*://*.technologyreview.com/*",
"*://*.theage.com.au/*",
"*://*.theaustralian.com.au/*",
"*://*.thenation.com/*",
"*://*.thestreet.com/*",
"*://*.thesundaytimes.co.uk/*",
"*://*.thetimes.co.uk/*",
"*://*.washingtonpost.com/*",
"*://*.wsj.com/*",
"*://*.wsj.net/*"
]
const cookies = ([
// theaustralian.com.au
'open_token=anonymous',
'sr=true',
'FreedomCookie=true',
'n_regis=123456789'
]).join('; ').trim()
// from https://support.google.com/webmasters/answer/1061943
const UA_Desktop = "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"
const UA_Mobile = "Chrome/41.0.2272.96 Mobile Safari/537.36 (compatible ; Googlebot/2.1 ; +http://www.google.com/bot.html)"
function evadePaywalls(details) {
const shouldDropUA = !details.url.includes("medium.com");
var useMobileUA = false;
var reqHeaders = details.requestHeaders.filter(function(header) {
// drop cookies, referer and UA
switch(header.name) {
case "User-Agent":
useMobileUA = header.value.toLowerCase().includes("mobile")
return !shouldDropUA;
case "Cookie":
case "Referer":
return false;
break;
default:
return true;
}
})
// Add the spoofed ones back
reqHeaders.push({
"name": "Referer",
"value": "https://www.google.com/"
})
if (shouldDropUA) {
reqHeaders.push({
"name": "User-Agent",
"value": useMobileUA ? UA_Mobile : UA_Desktop
})
}
reqHeaders.push({
"name": "Cookie",
"value": cookies
})
reqHeaders.push({
"name": "X-Forwarded-For",
"value": "66.249.66.1"
})
return {requestHeaders: reqHeaders};
}
function blockCookies(details) {
var responseHeaders = details.responseHeaders.filter(function(header) {
if (header.name === "Cookie") {
return false;
}
return true;
})
return {responseHeaders: responseHeaders};
}
chrome.webRequest.onBeforeSendHeaders.addListener(evadePaywalls, {
urls: [...websites],
types: ["main_frame", "script"],
}, ["requestHeaders", "blocking"]);
chrome.webRequest.onHeadersReceived.addListener(blockCookies, {
urls: [...websites],
types: ["main_frame", "script"],
}, ["responseHeaders", "blocking"]);