aboutsummaryrefslogtreecommitdiff
path: root/background/policy_smuggler.js
diff options
context:
space:
mode:
Diffstat (limited to 'background/policy_smuggler.js')
-rw-r--r--background/policy_smuggler.js64
1 files changed, 64 insertions, 0 deletions
diff --git a/background/policy_smuggler.js b/background/policy_smuggler.js
new file mode 100644
index 0000000..6d0da38
--- /dev/null
+++ b/background/policy_smuggler.js
@@ -0,0 +1,64 @@
+/**
+* Myext smuggling policy to content script through url
+*
+* Copyright (C) 2021 Wojtek Kosior
+*
+* Dual-licensed under:
+* - 0BSD license
+* - GPLv3 or (at your option) any later version
+*/
+
+"use strict";
+
+(() => {
+ const TYPE_PREFIX = window.TYPE_PREFIX;
+ const get_storage = window.get_storage;
+ const browser = window.browser;
+ const url_item = window.url_item;
+
+ var storage;
+
+ function redirect(request)
+ {
+ let url_re = /^([^#]*)((#[^#]*)(#.*)?)?$/;
+ let match = url_re.exec(request.url);
+ let base_url = match[1];
+ let first_target = match[3];
+ let second_target = match[4];
+
+ if (first_target === "#myext-allow") {
+ console.log(["not redirecting"]);
+ return {cancel : false};
+ }
+
+ let url = url_item(request.url);
+ let settings = storage.get(TYPE_PREFIX.PAGE, url);
+ console.log("got", storage.get(TYPE_PREFIX.PAGE, url), "for", url);
+ if (settings === undefined || !settings.allow)
+ return {cancel : false};
+
+ second_target = (first_target || "") + (second_target || "")
+
+ console.log(["redirecting", request.url,
+ (base_url + "#myext-allow" + second_target)]);
+
+ return {
+ redirectUrl : (base_url + "#myext-allow" + second_target)
+ };
+ }
+
+ async function start() {
+ storage = await get_storage();
+
+ chrome.webRequest.onBeforeRequest.addListener(
+ redirect,
+ {
+ urls: ["<all_urls>"],
+ types: ["main_frame", "sub_frame"]
+ },
+ ["blocking"]
+ );
+ }
+
+ window.start_policy_smuggler = start;
+})();