-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
79 lines (66 loc) · 1.98 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
var VIA_TWITTER = ["wsj.com"]
function changeRefer(details) {
foundReferer = false;
foundUA = false;
var useTwitter = VIA_TWITTER.map(function(url) {
if (details.url.includes(url)) {
return true;
}
return false;
})
.reduce(function(a, b) { return a || b}, false);
var reqHeaders = details.requestHeaders.filter(function(header) {
// block cookies by default
if (header.name !== "Cookie") {
return header;
}
}).map(function(header) {
if (header.name === "Referer") {
header.value = setRefer(useTwitter);
foundReferer = true;
}
if (header.name === "User-Agent") {
header.value = setUserAgent(useTwitter);
foundUA = true;
}
return header;
})
// append referer
if (!foundReferer) {
reqHeaders.push({
"name": "Referer",
"value": setRefer(useTwitter)
})
}
if (!foundUA) {
reqHeaders.push({
"name": "User-Agent",
"value": setUserAgent(useTwitter)
})
}
return {requestHeaders: reqHeaders};
}
function blockCookies(details) {
for (var i = 0; i < details.responseHeaders.length; ++i) {
if (details.responseHeaders[i].name === "Set-Cookie") {
details.responseHeaders.splice(i, 1);
}
}
return {responseHeaders: details.responseHeaders};
}
function setRefer(useTwitter) {
if (useTwitter) return "https://t.co/T1323aaaa";
else return "https://www.google.com/";
}
function setUserAgent(useTwitter) {
if (useTwitter) return "Mozilla/5.0 (iPhone; CPU iPhone OS 10_2 like Mac OS X) AppleWebKit/602.1.32 (KHTML, like Gecko) Mobile/14C92 Twitter for iPhone";
else return "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)";
}
chrome.webRequest.onBeforeSendHeaders.addListener(changeRefer, {
urls: ["<all_urls>"],
types: ["main_frame"],
}, ["requestHeaders", "blocking"]);
chrome.webRequest.onHeadersReceived.addListener(blockCookies, {
urls: ["<all_urls>"],
types: ["main_frame"],
}, ["responseHeaders", "blocking"]);