Browse Source

refactor: more reliable SafePromise::then patch

tophf 1 year ago
parent
commit
80ff902dc3
3 changed files with 6 additions and 7 deletions
  1. 1 3
      src/injected/web/bridge.js
  2. 0 2
      src/injected/web/requests.js
  3. 5 2
      src/injected/web/safe-globals.js

+ 1 - 3
src/injected/web/bridge.js

@@ -20,9 +20,7 @@ const bridge = {
       cb = resolve;
     });
     postWithCallback(cmd, data, node, cb);
-    return IS_FIREFOX // In FF 130 a detached Promise's `then` doesn't work with `await`
-      ? setOwnProp(res, 'then', then)
-      : res;
+    return res;
   },
   /** @return {?} synchronous */
   call: postWithCallback,

+ 0 - 2
src/injected/web/requests.js

@@ -224,8 +224,6 @@ export function onRequestCreate(opts, context, fileName) {
     events: EVENTS_TO_NOTIFY::filter(key => isFunction(cb[key] = opts[`on${key}`])),
   }, opts, OPTS_TO_PASS));
   setOwnProp(res, 'abort', () => bridge.post('AbortRequest', id));
-  // In FF 130 a detached Promise's `then` doesn't work with `await`
-  if (IS_FIREFOX && context.async) setOwnProp(res, 'then', then);
   return res;
 }
 

+ 5 - 2
src/injected/web/safe-globals.js

@@ -79,6 +79,7 @@ export const cloneInto = PAGE_MODE_HANDSHAKE ? null : global.cloneInto;
  * or window[0] before our content script runs at document_start, https://crbug.com/1261964 */
 export const VAULT = (() => {
   let tmp;
+  let SafePromiseProto;
   let Reflect;
   let SafeObject;
   let i = -1;
@@ -158,8 +159,8 @@ export const VAULT = (() => {
     parseFromString = res[i += 1] || SafeDOMParser[PROTO].parseFromString,
     reflectOwnKeys = res[i += 1] || Reflect.ownKeys,
     stopImmediatePropagation = res[i += 1] || src.Event[PROTO].stopImmediatePropagation,
-    SafePromiseConstructor = res[i += 1] || (tmp = src.Promise[PROTO]).constructor,
-    then = res[i += 1] || (srcFF || tmp).then,
+    SafePromiseConstructor = res[i += 1] || (SafePromiseProto = src.Promise[PROTO]).constructor,
+    then = res[i += 1] || (srcFF || SafePromiseProto).then,
     urlSearchParamsToString = res[i += 1] || src.URLSearchParams[PROTO].toString,
     // various getters
     getCurrentScript = res[i += 1] || describeProperty(src.Document[PROTO], 'currentScript').get,
@@ -179,5 +180,7 @@ export const VAULT = (() => {
   toStringTagSym = SafeSymbol.toStringTag;
   // Binding Promise to this realm
   SafePromise = safeBind(SafePromiseConstructor, getPrototypeOf(promiseResolve()));
+  // In FF 130+ a detached `then` doesn't work with `await`, so we use an exported `then`
+  if (IS_FIREFOX) SafePromiseProto.then = then;
   return res;
 })();