Revision 4c6a2323
Added by koszko over 1 year ago
| common/misc.js | ||
|---|---|---|
| 42 | 42 |
* proprietary program, I am not going to enforce this in court. |
| 43 | 43 |
*/ |
| 44 | 44 |
|
| 45 |
#FROM common/browser.js IMPORT browser |
|
| 46 |
#FROM common/stored_types.js IMPORT TYPE_NAME, TYPE_PREFIX |
|
| 47 |
|
|
| 48 | 45 |
/* uint8_to_hex is a separate function used in cryptographic functions. */ |
| 49 | 46 |
const uint8_to_hex = |
| 50 | 47 |
array => [...array].map(b => ("0" + b.toString(16)).slice(-2)).join("");
|
| ... | ... | |
| 83 | 80 |
*/ |
| 84 | 81 |
#EXPORT (prefix, name) => `${name} (${TYPE_NAME[prefix]})` AS nice_name
|
| 85 | 82 |
|
| 86 |
/* Open settings tab with given item's editing already on. */ |
|
| 87 |
function open_in_settings(prefix, name) |
|
| 88 |
{
|
|
| 89 |
name = encodeURIComponent(name); |
|
| 90 |
const url = browser.runtime.getURL("html/options.html#" + prefix + name);
|
|
| 91 |
window.open(url, "_blank"); |
|
| 92 |
} |
|
| 93 |
#EXPORT open_in_settings |
|
| 94 |
|
|
| 95 | 83 |
/* |
| 96 | 84 |
* Check if url corresponds to a browser's special page (or a directory index in |
| 97 | 85 |
* case of `file://' protocol). |
| ... | ... | |
| 102 | 90 |
const priv_reg = /^chrome(-extension)?:\/\/|^about:|^file:\/\/[^?#]*\/([?#]|$)/; |
| 103 | 91 |
#ENDIF |
| 104 | 92 |
#EXPORT url => priv_reg.test(url) AS is_privileged_url |
| 105 |
|
|
| 106 |
/* Parse a CSP header */ |
|
| 107 |
function parse_csp(csp) {
|
|
| 108 |
let directive, directive_array; |
|
| 109 |
let directives = {};
|
|
| 110 |
for (directive of csp.split(';')) {
|
|
| 111 |
directive = directive.trim(); |
|
| 112 |
if (directive === '') |
|
| 113 |
continue; |
|
| 114 |
|
|
| 115 |
directive_array = directive.split(/\s+/); |
|
| 116 |
directive = directive_array.shift(); |
|
| 117 |
/* The "true" case should never occur; nevertheless... */ |
|
| 118 |
directives[directive] = directive in directives ? |
|
| 119 |
directives[directive].concat(directive_array) : |
|
| 120 |
directive_array; |
|
| 121 |
} |
|
| 122 |
return directives; |
|
| 123 |
} |
|
| 124 |
|
|
| 125 |
/* Regexes and objects to use as/in schemas for parse_json_with_schema(). */ |
|
| 126 |
const nonempty_string_matcher = /.+/; |
|
| 127 |
|
|
| 128 |
const matchers = {
|
|
| 129 |
sha256: /^[0-9a-f]{64}$/,
|
|
| 130 |
nonempty_string: nonempty_string_matcher, |
|
| 131 |
component: [ |
|
| 132 |
new RegExp(`^[${TYPE_PREFIX.SCRIPT}${TYPE_PREFIX.BAG}]$`),
|
|
| 133 |
nonempty_string_matcher |
|
| 134 |
] |
|
| 135 |
}; |
|
| 136 |
#EXPORT matchers |
|
| 137 |
|
|
| 138 |
/* |
|
| 139 |
* Facilitates checking if there aren't any keys in object. This does *NOT* |
|
| 140 |
* account for pathological cases like redefined properties of Object prototype. |
|
| 141 |
*/ |
|
| 142 |
function is_object_empty(object) |
|
| 143 |
{
|
|
| 144 |
for (const key in object) |
|
| 145 |
return false; |
|
| 146 |
return true; |
|
| 147 |
} |
|
| 148 |
#EXPORT is_object_empty |
|
Also available in: Unified diff
make Haketilo buildable again (for Mozilla)
How cool it is to throw away 5755 lines of code...