-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
162 lines (139 loc) · 4.15 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
153
154
155
156
157
158
159
160
161
162
var urlList = {},
icons = {
green: 'icons/sansursavan-browseraction-g-64.png',
red: 'icons/sansursavan-browseraction-r-64.png',
};
chrome.storage.local.get('urlList', setWebRequestListener);
chrome.storage.local.get('notRunningForTheFirstTime', (result) => {
if (!result.notRunningForTheFirstTime) {
chrome.tabs.create({
url: 'welcome.html',
active: true,
});
} else {
chrome.storage.local.set({
notRunningForTheFirstTime: true
});
}
});
function setWebRequestListener(result) {
if (!result.urlList) {
getNewList();
return;
}
if (chrome.webRequest.onBeforeRequest.hasListener(redirect)) {
chrome.webRequest.onBeforeRequest.removeListener(redirect);
}
urlList = result.urlList;
var urlListArray = [];
for (var i in urlList) {
urlListArray.push('http://' + i + '/*');
urlListArray.push('https://' + i + '/*');
urlListArray.push('http://www.' + i + '/*');
urlListArray.push('https://www.' + i + '/*');
}
chrome.webRequest.onBeforeRequest.addListener(
redirectToAsk, {
urls: urlListArray,
types: ['main_frame'],
}, ['blocking']
);
chrome.webRequest.onBeforeRequest.addListener(
redirectToUncensored, {
urls: urlListArray,
types: [
'sub_frame',
'stylesheet',
'script',
'image',
'object',
'xmlhttprequest',
'xbl',
'xslt',
'ping',
'beacon',
'xml_dtd',
'font',
'media',
'websocket',
'csp_report',
'imageset',
'web_manifest',
'other',
],
}, ['blocking']
);
}
function redirectToAsk(requestDetails) {
redirect(requestDetails, true);
}
function redirectToUncensored(requestDetails) {
redirect(requestDetails, false)
}
function redirect(requestDetails, askFirst) {
let url = requestDetails.url,
a = document.createElement('a'),
newUrl,
type;
a.href = url;
let censoredDomain = a.host;
if (!urlList[censoredDomain]) {
// how could it happen?
return;
} else if (urlList[censoredDomain] === 'proxy') {
newUrl = 'http://proxy.sansursavan.org/index.php?url=' + url;
type = 'proxy';
chrome.browserAction.setIcon({
path: icons.red,
tabId: requestDetails.tabId,
});
chrome.browserAction.setTitle({
title: 'Bu sayfayı vekil sunucu (proxy) bağlantısı ile geziyorsunuz',
tabId: requestDetails.tabId,
});
chrome.browserAction.setBadgeText({
text: 'prxy',
tabId: requestDetails.tabId,
});
} else {
newUrl = url.replace(censoredDomain, urlList[censoredDomain]);
type = 'redirect';
chrome.browserAction.setBadgeText({
text: 'rdct',
tabId: requestDetails.tabId,
});
}
if (askFirst) {
var redirectUrl = '/' + type + '.html?newUrl=' + encodeURIComponent(newUrl) + '&oldUrl=' + encodeURIComponent(url);
chrome.tabs.update(requestDetails.tabId, {
url: redirectUrl,
});
return {
cancel: true,
};
} else {
return {
redirectUrl: newUrl,
};
}
}
function reqListener() {
urlList = JSON.parse(this.responseText);
setWebRequestListener({
urlList: urlList
});
// write to storage
chrome.storage.local.set({
urlList: urlList,
});
// reload extension to use new data
// chrome.runtime.reload();
// no we cannot reload. it is not supported FF < 51
}
function getNewList() {
var oReq = new XMLHttpRequest();
oReq.addEventListener("load", reqListener);
oReq.open("GET", "https://raw.githubusercontent.com/kolektif/sansur-listesi/master/liste.json");
oReq.send();
}
setTimeout(getNewList, 10 * 60 * 1000); // check new list every ten minutes