Browse Source

fix #1891: always set window.external in sleazyfork

...with a dummy isInstalled if `expose` option is not enabled for the site
tophf 2 years ago
parent
commit
697683e471
3 changed files with 9 additions and 6 deletions
  1. 3 2
      src/background/utils/preinject.js
  2. 2 2
      src/injected/content/index.js
  3. 4 2
      src/injected/web/index.js

+ 3 - 2
src/background/utils/preinject.js

@@ -336,7 +336,8 @@ function prepareXhrBlob({ [kResponseHeaders]: responseHeaders, [kFrameId]: frame
 
 function prepare(cacheKey, url, isTop) {
   const shouldExpose = isTop && url.startsWith('https://') && expose[url.split('/', 3)[2]];
-  const bagNoOp = shouldExpose ? BAG_NOOP_EXPOSE : BAG_NOOP;
+  const bagNoOp = shouldExpose != null ? BAG_NOOP_EXPOSE : BAG_NOOP;
+  BAG_NOOP_EXPOSE[INJECT][EXPOSE] = shouldExpose;
   if (!isApplied) {
     return bagNoOp;
   }
@@ -345,7 +346,7 @@ function prepare(cacheKey, url, isTop) {
   const env = getScriptsByURL(url, isTop, errors);
   if (env) {
     env[PROMISE] = prepareBag(cacheKey, url, isTop,
-      env, shouldExpose ? { [EXPOSE]: true } : {}, errors);
+      env, shouldExpose != null ? { [EXPOSE]: shouldExpose } : {}, errors);
   }
   return cache.put(cacheKey, env || bagNoOp);
 }

+ 2 - 2
src/injected/content/index.js

@@ -39,9 +39,9 @@ async function init() {
     Run(SKIP_SCRIPTS);
     return;
   }
-  if (data[EXPOSE] && !isXml && injectPageSandbox(data)) {
+  if (data[EXPOSE] != null && !isXml && injectPageSandbox(data)) {
     addHandlers({ GetScriptVer: true });
-    bridge.post('Expose');
+    bridge.post('Expose', data[EXPOSE]);
   }
   if (data[SCRIPTS]) {
     onScripts.forEach(fn => fn(data));

+ 4 - 2
src/injected/web/index.js

@@ -102,11 +102,13 @@ addHandlers({
     if (!PAGE_MODE_HANDSHAKE) toRunNow::forEach(onCodeSet);
     else if (IS_FIREFOX) bridge.post('InjectList', items[0][RUN_AT]);
   },
-  Expose() {
+  Expose(allowGetScriptVer) {
     external[VIOLENTMONKEY] = {
       version: process.env.VM_VER,
       isInstalled: (name, namespace) => (
-        bridge.send('GetScriptVer', { meta: { name, namespace } })
+        allowGetScriptVer
+          ? bridge.send('GetScriptVer', { meta: { name, namespace } })
+          : promiseResolve()
       ),
     };
   },