Project

General

Profile

Download (1.64 KB) Statistics
| Branch: | Tag: | Revision:

haketilo / content / freezer.js @ 53837634

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 mozilla_suppress_scripts(e) {
47
    if (document.readyState === 'complete') {
48
	removeEventListener('beforescriptexecute', blockExecute, true);
49
	console.log('Script suppressor has detached.');
50
	return;
51
    }
52
    console.log("script event", e);
53
    if (e.isTrusted && !e.target._hachette_payload) {
54
	e.preventDefault();
55
	console.log('Suppressed script', e.target);
56
    }
57
};
58

    
59
/*
60
 * EXPORTS_START
61
 * EXPORT mozilla_suppress_scripts
62
 * EXPORT sanitize_attributes
63
 * EXPORTS_END
64
 */
(2-2/6)