summaryrefslogtreecommitdiff
path: root/src/bg/RequestUtil.js
diff options
context:
space:
mode:
authorhackademix2018-07-01 01:01:23 +0200
committerhackademix2018-07-01 01:01:23 +0200
commiteceae7187a6f0e9510bc1165f6977256b87f490f (patch)
treed943f1ec73c09efa70954dcedb55eac82a726148 /src/bg/RequestUtil.js
downloadnoscript-eceae7187a6f0e9510bc1165f6977256b87f490f.tar.gz
noscript-eceae7187a6f0e9510bc1165f6977256b87f490f.tar.xz
noscript-eceae7187a6f0e9510bc1165f6977256b87f490f.zip
Initial commit starting at version 10.1.8.3rc4.
Diffstat (limited to 'src/bg/RequestUtil.js')
-rw-r--r--src/bg/RequestUtil.js130
1 files changed, 130 insertions, 0 deletions
diff --git a/src/bg/RequestUtil.js b/src/bg/RequestUtil.js
new file mode 100644
index 0000000..1ebbbaa
--- /dev/null
+++ b/src/bg/RequestUtil.js
@@ -0,0 +1,130 @@
+'use strict';
+{
+ let runningScripts = new Map();
+
+ var RequestUtil = {
+ async executeOnStart(request, details) {
+ let {requestId, tabId, frameId} = request;
+ details = Object.assign({
+ runAt: "document_start",
+ frameId,
+ }, details);
+ browser.tabs.executeScript(tabId, details);
+ return;
+ let filter = browser.webRequest.filterResponseData(requestId);
+ filter.onstart = event => {
+ browser.tabs.executeScript(tabId, details);
+ debug("Execute on start", details);
+ filter.write(new Uint8Array());
+ };
+ filter.ondata = event => {
+ filter.write(event.data);
+ filter.disconnect();
+
+ }
+ },
+ async executeOnStartCS(request, details) {
+ let {url, requestId, tabId, frameId} = request;
+
+ let urlObj = new URL(url);
+ if (urlObj.hash || urlObj.port || urlObj.username) {
+ urlObj.hash = urlObj.port = urlObj.username = "";
+ url = urlObj.toString();
+ }
+ let wr = browser.webRequest;
+ let filter = {
+ urls: [`${urlObj.origin}/*`],
+ types: ["main_frame", "sub_frame", "object"]
+ };
+ let finalize;
+ let cleanup = r => {
+ if (cleanup && r.requestId === requestId) {
+ wr.onCompleted.removeListener(cleanup);
+ wr.onErrorOccurred.removeListener(cleanup);
+ cleanup = null;
+ if (finalize) {
+ finalize();
+ }
+ }
+ };
+ wr.onCompleted.addListener(cleanup, filter);
+ wr.onErrorOccurred.addListener(cleanup, filter);
+
+ details = Object.assign({
+ runAt: "document_start",
+ frameId,
+ }, details);
+
+ if (browser.contentScripts) {
+ let js = [{}];
+ if (details.file) js[0].file = details.file;
+ else if (details.code) js[0].code = details.code;
+ let settings = {
+ "runAt": details.runAt,
+ js,
+ matches: [url],
+ allFrames: frameId !== 0,
+ }
+ // let's try to avoid duplicates
+ let key = JSON.stringify(settings);
+ if (runningScripts.has(key)) {
+ let scriptRef = runningScripts.get(key);
+ scriptRef.count++;
+ return;
+ }
+ if (settings.allFrames) {
+ // let's check whether the same script is registered for top frames:
+ // if it is, let's unregister it first to avoid duplicates
+ settings.allFrames = false;
+ let topKey = JSON.stringify(settings);
+ settings.allFrames = true;
+ if (runningScripts.has(topKey)) {
+ let topScript = runningScripts.get(topKey);
+ try {
+ topScript.unregister();
+ } catch (e) {
+ error(e);
+ } finally {
+ runningScripts.delete(topKey);
+ }
+ }
+ }
+
+ let script = await browser.contentScripts.register(settings);
+ debug("Content script %o registered.", settings);
+ finalize = () => {
+ debug("Finalizing content script %o...", settings);
+ try {
+ script.unregister();
+ runningScripts.delete(key);
+ debug("Content script %o unregistered!", settings);
+ } finally {
+ finalize = null;
+ }
+ }
+ runningScripts.set(key, script);
+ if (!cleanup) { // the request has already been interrupted
+ finalize();
+ }
+
+ return;
+ }
+
+ function listener(r) {
+ if (r.requestId === requestId) {
+ browser.tabs.executeScript(tabId, details);
+ finalize();
+ finalize = null;
+ }
+ }
+ finalize = () => {
+ wr.onResponseStarted.removeListener(listener);
+ }
+ wr.onResponseStarted.addListener(listener, filter);
+ debug("Executing %o", details);
+
+ },
+
+
+ }
+}