1
|
/**
|
2
|
* Helper functions for blocking scripts in pages, based off NoScript's lib/DocumentFreezer.js
|
3
|
*
|
4
|
* Copyright (C) 2005-2021 Giorgio Maone - https://maone.net
|
5
|
* Copyright (C) 2021 jahoti
|
6
|
* Redistribution terms are gathered in the `copyright' file.
|
7
|
*/
|
8
|
|
9
|
const loaderAttributes = ["href", "src", "data"];
|
10
|
const jsOrDataUrlRx = /^(?:data:(?:[^,;]*ml|unknown-content-type)|javascript:)/i;
|
11
|
|
12
|
function sanitize_attributes(element) {
|
13
|
if (element._frozen)
|
14
|
return;
|
15
|
let fa = [];
|
16
|
let loaders = [];
|
17
|
let attributes = element.attributes || [];
|
18
|
|
19
|
for (let a of attributes) {
|
20
|
let name = a.localName.toLowerCase();
|
21
|
if (loaderAttributes.includes(name))
|
22
|
if (jsOrDataUrlRx.test(a.value))
|
23
|
loaders.push(a);
|
24
|
|
25
|
else if (name.startsWith("on")) {
|
26
|
console.debug("Removing", a, element.outerHTML);
|
27
|
fa.push(a.cloneNode());
|
28
|
a.value = "";
|
29
|
element[name] = null;
|
30
|
}
|
31
|
}
|
32
|
if (loaders.length) {
|
33
|
for (let a of loaders) {
|
34
|
fa.push(a.cloneNode());
|
35
|
a.value = "javascript://frozen";
|
36
|
}
|
37
|
if ("contentWindow" in element)
|
38
|
element.replaceWith(element = element.cloneNode(true));
|
39
|
|
40
|
}
|
41
|
if (fa.length)
|
42
|
element._frozenAttributes = fa;
|
43
|
element._frozen = true;
|
44
|
}
|
45
|
|
46
|
function script_suppressor(nonce) {
|
47
|
const blockExecute = e => {
|
48
|
if (document.readyState === 'complete') {
|
49
|
removeEventListener('beforescriptexecute', blockExecute, true);
|
50
|
return;
|
51
|
}
|
52
|
else if (e.isTrusted && e.target.getAttribute('nonce') !== nonce) { // Prevent blocking of injected scripts
|
53
|
e.preventDefault();
|
54
|
console.log('Suppressed script', e.target);
|
55
|
}
|
56
|
};
|
57
|
return blockExecute;
|
58
|
};
|
59
|
|
60
|
/*
|
61
|
* EXPORTS_START
|
62
|
* EXPORT script_suppressor
|
63
|
* EXPORT sanitize_attributes
|
64
|
* EXPORTS_END
|
65
|
*/
|