Project

General

Profile

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

haketilo / content / main.js @ d09b7ee1

1
/**
2
 * Hachette main content script run in all frames
3
 *
4
 * Copyright (C) 2021 Wojtek Kosior
5
 * Copyright (C) 2021 jahoti
6
 * Redistribution terms are gathered in the `copyright' file.
7
 */
8

    
9
/*
10
 * IMPORTS_START
11
 * IMPORT handle_page_actions
12
 * IMPORT extract_signed
13
 * IMPORT gen_nonce
14
 * IMPORT is_privileged_url
15
 * IMPORT mozilla_suppress_scripts
16
 * IMPORT is_chrome
17
 * IMPORT is_mozilla
18
 * IMPORT start_activity_info_server
19
 * IMPORT modify_on_the_fly
20
 * IMPORTS_END
21
 */
22

    
23
function accept_node(node, parent)
24
{
25
    const clone = document.importNode(node, false);
26
    node.hachette_corresponding = clone;
27
    /*
28
     * TODO: Stop page's own issues like "Error parsing a meta element's
29
     * content:" from appearing as extension's errors.
30
     */
31
    parent.hachette_corresponding.appendChild(clone);
32
}
33

    
34
if (!is_privileged_url(document.URL)) {
35
    const reductor =
36
	  (ac, [_, sig, pol]) => ac[0] && ac || [extract_signed(sig, pol), sig];
37
    const matches = [...document.cookie.matchAll(/hachette-(\w*)=([^;]*)/g)];
38
    let [policy, signature] = matches.reduce(reductor, []);
39

    
40
    if (!policy || policy.url !== document.URL) {
41
	console.log("WARNING! Using default policy!!!");
42
	policy = {allow: false, nonce: gen_nonce()};
43
    }
44

    
45
    if (signature)
46
	document.cookie = `hachette-${signature}=; Max-Age=-1;`;
47

    
48
    handle_page_actions(policy.nonce);
49

    
50
    if (!policy.allow && is_mozilla)
51
	addEventListener('beforescriptexecute', mozilla_suppress_scripts, true);
52

    
53
    if (!policy.allow && is_chrome) {
54
	const old_html = document.documentElement;
55
	const new_html = document.createElement("html");
56
	old_html.replaceWith(new_html);
57
	old_html.hachette_corresponding = new_html;
58

    
59
	const modify_end =
60
	      modify_on_the_fly(old_html, policy, {node_eater: accept_node});
61
	document.addEventListener("DOMContentLoaded", modify_end);
62
    }
63

    
64
    start_activity_info_server();
65
}
(3-3/6)