Project

General

Profile

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

haketilo / background / policy_smuggler.js @ 6bae771d

1
/**
2
 * Myext smuggling policy to content script through url
3
 *
4
 * Copyright (C) 2021 Wojtek Kosior
5
 *
6
 * This code is dual-licensed under:
7
 * - Asshole license 1.0,
8
 * - GPLv3 or (at your option) any later version
9
 *
10
 * "dual-licensed" means you can choose the license you prefer.
11
 *
12
 * This code is released under a permissive license because I disapprove of
13
 * copyright and wouldn't be willing to sue a violator. Despite not putting
14
 * this code under copyleft (which is also kind of copyright), I do not want
15
 * it to be made proprietary. Hence, the permissive alternative to GPL is the
16
 * Asshole license 1.0 that allows me to call you an asshole if you use it.
17
 * This means you're legally ok regardless of how you utilize this code but if
18
 * you make it into something nonfree, you're an asshole.
19
 *
20
 * You should have received a copy of both GPLv3 and Asshole license 1.0
21
 * together with this code. If not, please see:
22
 * - https://www.gnu.org/licenses/gpl-3.0.en.html
23
 * - https://koszko.org/asshole-license.txt
24
 */
25

    
26
"use strict";
27

    
28
(() => {
29
    const TYPE_PREFIX = window.TYPE_PREFIX;
30
    const get_storage = window.get_storage;
31
    const browser = window.browser;
32
    const url_item = window.url_item;
33
    const gen_unique = window.gen_unique;
34
    const get_query_best = window.get_query_best;
35

    
36
    var storage;
37
    var query_best;
38

    
39
    function redirect(request)
40
    {
41
	let url_re = /^([^#]*)((#[^#]*)(#.*)?)?$/;
42
	let match = url_re.exec(request.url);
43
	let base_url = match[1];
44
	let first_target = match[3];
45
	let second_target = match[4];
46

    
47
	let url = url_item(request.url);
48
	let unique = gen_unique(url);
49

    
50
	if (first_target === unique) {
51
	    console.log(["not redirecting"]);
52
	    return {cancel : false};
53
	}
54

    
55
	let [pattern, settings] = query_best(url);
56
	if (settings === undefined || !settings.allow)
57
	    return {cancel : false};
58

    
59
	second_target = (first_target || "") + (second_target || "");
60

    
61
	console.log(["redirecting", request.url,
62
		     (base_url + unique + second_target)]);
63

    
64
	return {
65
	    redirectUrl : (base_url + unique + second_target)
66
	};
67
    }
68

    
69
    async function start() {
70
	storage = await get_storage();
71
	query_best = await get_query_best();
72

    
73
	chrome.webRequest.onBeforeRequest.addListener(
74
	    redirect,
75
	    {
76
		urls: ["<all_urls>"],
77
		types: ["main_frame", "sub_frame"]
78
	    },
79
	    ["blocking"]
80
	);
81
    }
82

    
83
    window.start_policy_smuggler = start;
84
})();
(5-5/9)