Преглед изворни кода

feat: remind users to reload tab if browser API fails

tophf пре 4 година
родитељ
комит
e278cfc7c1
1 измењених фајлова са 13 додато и 1 уклоњено
  1. 13 1
      src/common/browser.js

+ 13 - 1
src/common/browser.js

@@ -70,7 +70,19 @@ if (!IS_FIREFOX && !global.browser?.runtime) {
           reject(stackInfo);
         }
       };
-      func::apply(thisArg, args);
+      if (process.env.IS_INJECTED) {
+        try {
+          func::apply(thisArg, args);
+        } catch (e) {
+          if (e[MESSAGE] === 'Extension context invalidated.') {
+            console.error('Please reload the tab to restore Violentmonkey API for userscripts.');
+          } else {
+            throw e;
+          }
+        }
+      } else {
+        func::apply(thisArg, args);
+      }
       if (process.env.DEBUG) promise.catch(err => console.warn(args, err?.[MESSAGE] || err));
       return promise;
     }