diff options
author | Wojtek Kosior <koszko@koszko.org> | 2021-07-02 11:54:34 +0200 |
---|---|---|
committer | Wojtek Kosior <koszko@koszko.org> | 2021-07-02 11:54:34 +0200 |
commit | 8708ddd3fc564c0af3d66b50ead77e211a911254 (patch) | |
tree | 680493573f9b1cd224b3ccdfcca73042c3ad281c | |
parent | b428239892337553afb2b7236e9323c3e4d33d63 (diff) | |
download | browser-extension-8708ddd3fc564c0af3d66b50ead77e211a911254.tar.gz browser-extension-8708ddd3fc564c0af3d66b50ead77e211a911254.zip |
move parsing of url with targets to misc.js
-rw-r--r-- | common/misc.js | 22 | ||||
-rw-r--r-- | content/main.js | 17 |
2 files changed, 29 insertions, 10 deletions
diff --git a/common/misc.js b/common/misc.js index 5754bd0..0a3a425 100644 --- a/common/misc.js +++ b/common/misc.js @@ -41,6 +41,27 @@ function url_item(url) return match[1]; } +/* + * Assume a url like: https://example.com/green?illuminati=confirmed#tinky#winky + * This function will make it into an object like: + * { + * "base_url" : "https://example.com/green?illuminati=confirmed", + * "target" : "#tinky", + * "target2" : "#winky" + * } + * In case url doesn't have 2 #'s, target2 and target can be set to undefined. + */ +function url_extract_target(url) +{ + let url_re = /^([^#]*)((#[^#]*)(#.*)?)?$/; + let match = url_re.exec(url); + return { + base_url : match[1], + target : match[3], + target2 : match[4] + }; +} + /* csp rule that blocks all scripts except for those injected by us */ function csp_rule(nonce) { @@ -54,6 +75,7 @@ function csp_rule(nonce) * EXPORTS_START * EXPORT gen_unique * EXPORT url_item + * EXPORT url_extract_target * EXPORT csp_rule * EXPORTS_END */ diff --git a/content/main.js b/content/main.js index 8525961..65ac008 100644 --- a/content/main.js +++ b/content/main.js @@ -9,6 +9,7 @@ * IMPORTS_START * IMPORT handle_page_actions * IMPORT url_item + * IMPORT url_extract_target * IMPORT gen_unique * IMPORT csp_rule * IMPORT sanitize_attributes @@ -39,18 +40,14 @@ function needs_blocking() if (url.startsWith("https://") || url.startsWith("http://")) return false; - let url_re = /^([^#]*)((#[^#]*)(#.*)?)?$/; - let match = url_re.exec(document.URL); - let base_url = match[1]; - let first_target = match[3]; - let second_target = match[4]; + const parsed_url = url_extract_target(document.URL); - if (first_target !== undefined && - first_target === '#' + unique) { - if (second_target !== undefined) - window.location.href = base_url + second_target; + if (parsed_url.target !== undefined && + parsed_url.target === '#' + unique) { + if (parsed_url.target2 !== undefined) + window.location.href = parsed_url.base_url + parsed_url.target2; else - history.replaceState(null, "", base_url); + history.replaceState(null, "", parsed_url.base_url); console.log(["allowing whitelisted", document.URL]); return false; |