1
|
/**
|
2
|
* Myext miscellaneous operations refactored to a separate file
|
3
|
*
|
4
|
* Copyright (C) 2021 Wojtek Kosior
|
5
|
* Redistribution terms are gathered in the `copyright' file.
|
6
|
*/
|
7
|
|
8
|
/*
|
9
|
* IMPORTS_START
|
10
|
* IMPORT sha256
|
11
|
* IMPORT browser
|
12
|
* IMPORT is_chrome
|
13
|
* IMPORT TYPE_NAME
|
14
|
* IMPORTS_END
|
15
|
*/
|
16
|
|
17
|
/*
|
18
|
* generating unique, per-site value that can be computed synchronously
|
19
|
* and is impossible to guess for a malicious website
|
20
|
*/
|
21
|
function gen_unique(url)
|
22
|
{
|
23
|
return sha256(get_secure_salt() + url);
|
24
|
}
|
25
|
|
26
|
function get_secure_salt()
|
27
|
{
|
28
|
if (is_chrome)
|
29
|
return browser.runtime.getManifest().key.substring(0, 50);
|
30
|
else
|
31
|
return browser.runtime.getURL("dummy");
|
32
|
}
|
33
|
|
34
|
/*
|
35
|
* stripping url from query and target (everything after `#' or `?'
|
36
|
* gets removed)
|
37
|
*/
|
38
|
function url_item(url)
|
39
|
{
|
40
|
let url_re = /^([^?#]*).*$/;
|
41
|
let match = url_re.exec(url);
|
42
|
return match[1];
|
43
|
}
|
44
|
|
45
|
/*
|
46
|
* Assume a url like: https://example.com/green?illuminati=confirmed#tinky#winky
|
47
|
* This function will make it into an object like:
|
48
|
* {
|
49
|
* "base_url" : "https://example.com/green?illuminati=confirmed",
|
50
|
* "target" : "#tinky",
|
51
|
* "target2" : "#winky"
|
52
|
* }
|
53
|
* In case url doesn't have 2 #'s, target2 and target can be set to undefined.
|
54
|
*/
|
55
|
function url_extract_target(url)
|
56
|
{
|
57
|
let url_re = /^([^#]*)((#[^#]*)(#.*)?)?$/;
|
58
|
let match = url_re.exec(url);
|
59
|
return {
|
60
|
base_url : match[1],
|
61
|
target : match[3],
|
62
|
target2 : match[4]
|
63
|
};
|
64
|
}
|
65
|
|
66
|
/* csp rule that blocks all scripts except for those injected by us */
|
67
|
function csp_rule(nonce)
|
68
|
{
|
69
|
let rule = `script-src 'nonce-${nonce}';`;
|
70
|
if (is_chrome)
|
71
|
rule += `script-src-elem 'nonce-${nonce}';`;
|
72
|
return rule;
|
73
|
}
|
74
|
|
75
|
/*
|
76
|
* Print item together with type, e.g.
|
77
|
* nice_name("s", "hello") → "hello (script)"
|
78
|
*/
|
79
|
function nice_name(prefix, name)
|
80
|
{
|
81
|
return `${name} (${TYPE_NAME[prefix]})`;
|
82
|
}
|
83
|
|
84
|
/* Open settings tab with given item's editing already on. */
|
85
|
function open_in_settings(prefix, name)
|
86
|
{
|
87
|
name = encodeURIComponent(name);
|
88
|
const url = browser.runtime.getURL("html/options.html#" + prefix + name);
|
89
|
window.open(url, "_blank");
|
90
|
}
|
91
|
|
92
|
/* Check if url corresponds to a browser's special page */
|
93
|
function is_privileged_url(url)
|
94
|
{
|
95
|
return !!/^(chrome(-extension)?|moz-extension):\/\/|^about:/i.exec(url);
|
96
|
}
|
97
|
|
98
|
/*
|
99
|
* EXPORTS_START
|
100
|
* EXPORT gen_unique
|
101
|
* EXPORT url_item
|
102
|
* EXPORT url_extract_target
|
103
|
* EXPORT csp_rule
|
104
|
* EXPORT nice_name
|
105
|
* EXPORT open_in_settings
|
106
|
* EXPORT is_privileged_url
|
107
|
* EXPORTS_END
|
108
|
*/
|