Parcourir la source

fix: detect script files

close violentmonkey/violentmonkey#114
Gerald il y a 8 ans
Parent
commit
63e3c7d556
2 fichiers modifiés avec 27 ajouts et 1 suppressions
  1. 3 1
      src/background/utils/requests.js
  2. 24 0
      src/injected/index.js

+ 3 - 1
src/background/utils/requests.js

@@ -217,7 +217,9 @@ export function confirmInstall(info) {
 }
 }
 
 
 browser.webRequest.onBeforeRequest.addListener(req => {
 browser.webRequest.onBeforeRequest.addListener(req => {
-  // onBeforeRequest is fired for local files too
+  // onBeforeRequest fired for `file:`
+  // - works on Chrome
+  // - does not work on Firefox
   if (req.method === 'GET' && /\.user\.js([?#]|$)/.test(req.url)) {
   if (req.method === 'GET' && /\.user\.js([?#]|$)/.test(req.url)) {
     // {cancel: true} will redirect to a blocked view
     // {cancel: true} will redirect to a blocked view
     const noredirect = { redirectUrl: 'javascript:history.back()' };  // eslint-disable-line no-script-url
     const noredirect = { redirectUrl: 'javascript:history.back()' };  // eslint-disable-line no-script-url

+ 24 - 0
src/injected/index.js

@@ -78,4 +78,28 @@ import webBridgeObj from './web';
     });
     });
   }
   }
   initBridge();
   initBridge();
+
+  // For installation
+  // Firefox does not support `onBeforeRequest` for `file:`
+  function checkJS() {
+    if (!document.querySelector('title')) {
+      // plain text
+      sendMessage({
+        cmd: 'ConfirmInstall',
+        data: {
+          code: document.body.textContent,
+          url: location.href,
+          from: document.referrer,
+        },
+      })
+      .then(() => {
+        if (history.length > 1) history.go(-1);
+        else sendMessage({ cmd: 'TabClose' });
+      });
+    }
+  }
+  if (/\.user\.js$/.test(location.pathname)) {
+    if (document.readyState === 'complete') checkJS();
+    else window.addEventListener('load', checkJS, false);
+  }
 }());
 }());