summaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authorhackademix2019-10-18 17:55:35 +0200
committerhackademix2019-10-18 22:40:55 +0200
commit1e88b30a209d345240d28d0adb8a28f34ddd889a (patch)
tree0c8cb4a16ebeb0a7c56b1452fa9e64a2830857d1 /src/lib
parent260291e07f7056b9f71013d483f65e48388020d2 (diff)
downloadnoscript-1e88b30a209d345240d28d0adb8a28f34ddd889a.tar.gz
noscript-1e88b30a209d345240d28d0adb8a28f34ddd889a.tar.xz
noscript-1e88b30a209d345240d28d0adb8a28f34ddd889a.zip
Fixed asynchronous onSyncMessage listeners support, on Chromium too.
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/SyncMessage.js31
1 files changed, 30 insertions, 1 deletions
diff --git a/src/lib/SyncMessage.js b/src/lib/SyncMessage.js
index a644507..5b6ac6e 100644
--- a/src/lib/SyncMessage.js
+++ b/src/lib/SyncMessage.js
@@ -19,6 +19,7 @@
}
let tabUrlCache = new Map();
+ let asyncResults = new Map();
let tabRemovalListener = null;
let CANCEL = {cancel: true};
let {TAB_ID_NONE} = browser.tabs;
@@ -28,6 +29,9 @@
let {url, tabId} = request;
let params = new URLSearchParams(url.split("?")[1]);
let msgId = params.get("id");
+ if (asyncResults.has(msgId)) {
+ return asyncRet(msgId);
+ }
let msg = params.get("msg");
let documentUrl = params.get("url");
let sender;
@@ -82,9 +86,34 @@
console.error("%o processing message %o from %o", e, msg, sender);
}
}
- return result instanceof Promise ? (async () => ret(await result)) : ret(result);
+ if (result instanceof Promise) {
+ if (MOZILLA) {
+ // Firefox supports asynchronous webRequest listeners, so we can
+ // just defer the return
+ return (async () => ret(await result))();
+ } else {
+ // On Chromium, if the promise is not resolved yet,
+ // we redirect the XHR to the same URL (hence same msgId)
+ // while the result get cached for asynchronous retrieval
+ result.then(r => {
+ asyncResults.set(msgId, result = r);
+ });
+ return asyncResults.has(msgId)
+ ? asyncRet(msgId) // promise was already resolved
+ : {redirectUrl: url.replace(
+ /&redirects=(\d+)|$/, // redirects count to avoid loop detection
+ (all, count) => `&redirects=${parseInt(count) + 1 || 1}`)};
+ }
+ }
+ return ret(result);
};
+
let ret = r => ({redirectUrl: `data:application/json,${JSON.stringify(r)}`})
+ let asyncRet = msgId => {
+ let result = asyncResults.get(msgId);
+ asyncResults.delete(msgId);
+ return ret(result);
+ }
let listeners = new Set();
browser.runtime.onSyncMessage = {