-
Notifications
You must be signed in to change notification settings - Fork 73
/
background.js
98 lines (77 loc) · 2.61 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
var accessControlRequestHeaders;
var exposedHeaders;
var requestListener = function(details){
var flag = false,
rule = {
name: "Origin",
value: "http://evil.com/"
};
var i;
for (i = 0; i < details.requestHeaders.length; ++i) {
if (details.requestHeaders[i].name.toLowerCase() === rule.name.toLowerCase()) {
flag = true;
details.requestHeaders[i].value = rule.value;
break;
}
}
if(!flag) details.requestHeaders.push(rule);
for (i = 0; i < details.requestHeaders.length; ++i) {
if (details.requestHeaders[i].name.toLowerCase() === "access-control-request-headers") {
accessControlRequestHeaders = details.requestHeaders[i].value
}
}
return {requestHeaders: details.requestHeaders};
};
var responseListener = function(details){
var flag = false,
rule = {
"name": "Access-Control-Allow-Origin",
"value": "*"
};
for (var i = 0; i < details.responseHeaders.length; ++i) {
if (details.responseHeaders[i].name.toLowerCase() === rule.name.toLowerCase()) {
flag = true;
details.responseHeaders[i].value = rule.value;
break;
}
}
if(!flag) details.responseHeaders.push(rule);
if (accessControlRequestHeaders) {
details.responseHeaders.push({"name": "Access-Control-Allow-Headers", "value": accessControlRequestHeaders});
}
if(exposedHeaders) {
details.responseHeaders.push({"name": "Access-Control-Expose-Headers", "value": exposedHeaders});
}
details.responseHeaders.push({"name": "Access-Control-Allow-Methods", "value": "GET, PUT, POST, DELETE, HEAD, OPTIONS"});
return {responseHeaders: details.responseHeaders};
};
/*On install*/
chrome.runtime.onInstalled.addListener(function(){
chrome.storage.local.set({'active': false});
chrome.storage.local.set({'urls': ["<all_urls>"]});
chrome.storage.local.set({'exposedHeaders': ''});
reload();
});
/*Reload settings*/
function reload() {
chrome.storage.local.get({'active': false, 'urls': ["<all_urls>"], 'exposedHeaders': ''}, function(result) {
exposedHeaders = result.exposedHeaders;
/*Remove Listeners*/
chrome.webRequest.onHeadersReceived.removeListener(responseListener);
chrome.webRequest.onBeforeSendHeaders.removeListener(requestListener);
if(result.active) {
chrome.browserAction.setIcon({path: "on.png"});
if(result.urls.length) {
/*Add Listeners*/
chrome.webRequest.onHeadersReceived.addListener(responseListener, {
urls: result.urls
},["blocking", "responseHeaders"]);
chrome.webRequest.onBeforeSendHeaders.addListener(requestListener, {
urls: result.urls
},["blocking", "requestHeaders"]);
}
} else {
chrome.browserAction.setIcon({path: "off.png"});
}
});
}