forked from ThomazPom/Moz-Ext-Ignore-X-Frame-Options
-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
64 lines (63 loc) · 1.85 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
var defaultRgx = ["http://*/*", "https://*/*"].join('\n')
var defaultRgx_fancestor = ["http://*","http://*:*", "https://*","https://*:*", "file://*"].join('\n')
function updateRegexpes()
{
browser.storage.local.get("regstr_fancestor", function(res) {
regstr_fancestor = (res.regstr_fancestor || defaultRgx_fancestor).split("\n").join(" ");
});
browser.storage.local.get("regstr", function(res) {
var regstr = (res.regstr || defaultRgx);
var regexpesarray = regstr.split("\n");
browser.webRequest.onHeadersReceived.removeListener(setHeader)
browser.webRequest.onHeadersReceived.addListener(
setHeader,
{urls : regexpesarray},
["blocking", "responseHeaders"]
);
});
}
function setHeader(e) {
var headersdelete = ["content-security-policy","x-frame-options"]
var cspval="";
e.responseHeaders= e.responseHeaders.filter(x=>{
var lowername = x.name.toLowerCase();
cspval = lowername === headersdelete[0]?x.value:cspval
return !headersdelete.includes(lowername)
})
e.responseHeaders.push({
name: "x-frame-options",
value: "ALLOW"
});
e.responseHeaders.push({
name: "content-security-policy",
value: cspval.includes("frame-ancestors")?
cspval.replace(/frame-ancestors[^;]*;?/, "frame-ancestors "+regstr_fancestor+";")
:
"frame-ancestors "+regstr_fancestor+";"+cspval
});
return {responseHeaders: e.responseHeaders};
}
// Listen for onHeaderReceived for the target page.
// Set "blocking" and "responseHeaders".
updateRegexpes();
var portFromCS;
function connected(p) {
portFromCS = p;
portFromCS.onMessage.addListener(function(m) {
if(m.updateRegexpes)
{
browser.storage.local.set(
{
"regstr":m.updateRegexpes,
},
()=>{
browser.storage.local.set(
{
"regstr_fancestor":m.updateRegexpes_fancestor
},updateRegexpes);
}
);
}
});
}
browser.runtime.onConnect.addListener(connected);