aboutsummaryrefslogtreecommitdiff
path: root/content/freezer.js
blob: 0ea362e391788bb505c26ad07cf6f32da2fe954e (about) (plain)
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
/**
 * Helper functions for blocking scripts in pages, based off NoScript's lib/DocumentFreezer.js
 *
 * Copyright (C) 2005-2021 Giorgio Maone - https://maone.net
 * Copyright (C) 2021 jahoti
 * Redistribution terms are gathered in the `copyright' file.
 */

const loaderAttributes = ["href", "src", "data"];
const jsOrDataUrlRx = /^(?:data:(?:[^,;]*ml|unknown-content-type)|javascript:)/i;

function sanitize_attributes(element) {
    if (element._frozen)
	return;
    let fa = [];
    let loaders = [];
    let attributes = element.attributes || [];

    for (let a of attributes) {
	let name = a.localName.toLowerCase();
	if (loaderAttributes.includes(name))
	    if (jsOrDataUrlRx.test(a.value))
		loaders.push(a);

	else if (name.startsWith("on")) {
	    console.debug("Removing", a, element.outerHTML);
	    fa.push(a.cloneNode());
	    a.value = "";
	    element[name] = null;
	}
    }
    if (loaders.length) {
	for (let a of loaders) {
	    fa.push(a.cloneNode());
	    a.value = "javascript://frozen";
	}
	if ("contentWindow" in element)
	    element.replaceWith(element = element.cloneNode(true));

    }
    if (fa.length)
	element._frozenAttributes = fa;
    element._frozen = true;
}

function mozilla_suppress_scripts(e) {
    if (document.readyState === 'complete') {
	removeEventListener('beforescriptexecute', blockExecute, true);
	console.log('Script suppressor has detached.');
	return;
    }
    console.log("script event", e);
    if (e.isTrusted && !e.target._hachette_payload) {
	e.preventDefault();
	console.log('Suppressed script', e.target);
    }
};

/*
 * EXPORTS_START
 * EXPORT mozilla_suppress_scripts
 * EXPORT sanitize_attributes
 * EXPORTS_END
 */