summaryrefslogtreecommitdiff
path: root/src/bg/ContentScriptOnce.js
diff options
context:
space:
mode:
authorhackademix2018-08-18 03:16:54 +0200
committerhackademix2018-08-18 03:20:33 +0200
commit2f9c5299afe0d612deff7f89c948bb44332f2abf (patch)
tree343fe4435bef995b4029656563ce6194829ea641 /src/bg/ContentScriptOnce.js
parente959accb70ea00f99fa611c9f1af40e1be5bde92 (diff)
downloadnoscript-2f9c5299afe0d612deff7f89c948bb44332f2abf.tar.gz
noscript-2f9c5299afe0d612deff7f89c948bb44332f2abf.tar.xz
noscript-2f9c5299afe0d612deff7f89c948bb44332f2abf.zip
Removed all references to RequestUtil.js and dependancies.
Diffstat (limited to 'src/bg/ContentScriptOnce.js')
-rw-r--r--src/bg/ContentScriptOnce.js52
1 files changed, 52 insertions, 0 deletions
diff --git a/src/bg/ContentScriptOnce.js b/src/bg/ContentScriptOnce.js
new file mode 100644
index 0000000..5be602e
--- /dev/null
+++ b/src/bg/ContentScriptOnce.js
@@ -0,0 +1,52 @@
+var ContentScriptOnce = (() => {
+ "use strict";
+
+ let requestMap = new Map();
+
+ {
+ let cleanup = r => {
+ let {requestId} = r;
+ let scripts = requestMap.get(requestId);
+ if (scripts) {
+ window.setTimeout(() => {
+ requestMap.delete(requestId);
+ for (let s of scripts) s.unregister();
+ }, 0);
+ }
+ }
+
+ let filter = {
+ urls: ["<all_urls>"],
+ types: ["main_frame", "sub_frame", "object"]
+ };
+ let wr = browser.webRequest;
+ for (let event of ["onCompleted", "onErrorOccurred"]) {
+ wr[event].addListener(cleanup, filter);
+ }
+ }
+
+ return {
+ async execute(request, options) {
+ let {requestId, url} = request;
+ let scripts = requestMap.get(requestId);
+ if (!scripts) requestMap.set(requestId, scripts = new Set());
+ try {
+ let urlObj = new URL(url);
+ if (urlObj.port) {
+ urlObj.port = "";
+ url = urlObj.toString();
+ }
+ } catch (e) {}
+ let defOpts = {
+ runAt: "document_start",
+ matchAboutBlank: true,
+ matches: [url],
+ allFrames: true,
+ };
+
+ scripts.add(await browser.contentScripts.register(
+ Object.assign(defOpts, options)
+ ));
+ }
+ }
+})();