-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
63 lines (57 loc) · 1.95 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
var wr = chrome.webRequest;
var maps = {};
var cb = function(details) {
var url = details.url;
var dstUrl = "";
var matched = "";
var protoExp = new RegExp('(http://|https://)','i');
for (var id in maps) {
var online = maps[id].online.replace(protoExp, ''),
local = maps[id].local;
var pattern = new RegExp(online, "i");
matched = url.match(pattern);
if (!matched)
continue;
var protocol = local.match(protoExp);
if (protocol)
protocol = protocol[1];
else
protocol = "http://";
local = local.replace(protoExp, '');
url = url.replace(protoExp, '');
dstUrl = protocol + url.replace(pattern, local);
console.log(pattern, local, url, dstUrl)
return {redirectUrl: dstUrl};
}
}
var filter = {urls: []}; // {urls: ["<all_urls>"]};
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
maps[request.id] = {online: request.online, local: request.local};
// update filter
filter.urls = [];
for (var id in maps) {
var matched = maps[id].online.match(new RegExp("(?:http://|https://|^)(.*?)/", "i"));
if (matched && matched[1]) {
var host = matched[1];
if (host.indexOf(':') != -1)
host = host.substr(0, host.indexOf(':'));
filter.urls.push("*://" + host + "/*");
}
}
if (request.doBlock) {
chrome.webRequest.onBeforeRequest.addListener(cb, filter, ["blocking"]);
chrome.webRequest.onHeadersReceived.addListener(onResponse,
{urls: ["*://www.2think.com/*"]}, ["blocking"]);
} else {
chrome.webRequest.onBeforeRequest.removeListener(cb);
chrome.webRequest.onHeadersReceived.removeListener(onResponse);
}
sendResponse({filter: filter});
});
function onResponse(details) {
console.log(details)
var accessControl = {name: 'Access-Control-Allow-Origin', value: '*'};
details.responseHeaders = details.responseHeaders || [];
details.responseHeaders.push(accessControl);
return {responseHeaders: details.responseHeaders};
}