Просмотр исходного кода

ref: add func for unsafeWindow fallback

Sv443 1 год назад
Родитель
Сommit
b6b2e50e29
3 измененных файлов с 46 добавлено и 27 удалено
  1. 8 1
      src/dev/ytForceShowVideoTime.js
  2. 2 2
      src/features/input.ts
  3. 36 24
      src/utils.ts

+ 8 - 1
src/dev/ytForceShowVideoTime.js

@@ -2,7 +2,14 @@
 // only works once for some reason (should be enough tho)
 
 // needed because otherwise YTM errors out - see https://github.com/Sv443/BetterYTM/issues/18#show_issue
-const view = unsafeWindow ?? window;
+const view = (() => {
+  try {
+    return unsafeWindow;
+  }
+  catch(e) {
+    return window;
+  }
+})();
 
 const player = document.querySelector("#movie_player");
 player.dispatchEvent(new MouseEvent("mouseenter", {

+ 2 - 2
src/features/input.ts

@@ -1,4 +1,4 @@
-import { error, getVideoTime, info, log, warn } from "../utils";
+import { error, getUnsafeWindow, getVideoTime, info, log, warn } from "../utils";
 import type { Domain } from "../types";
 import { getFeatures } from "../config";
 
@@ -36,7 +36,7 @@ function onKeyDown(evt: KeyboardEvent) {
       isTrusted: true,
       repeat: false,
       // needed because otherwise YTM errors out - see https://github.com/Sv443/BetterYTM/issues/18#show_issue
-      view: unsafeWindow ?? window,
+      view: getUnsafeWindow(),
     };
 
     let invalidKey = false;

+ 36 - 24
src/utils.ts

@@ -1,8 +1,6 @@
 import { branch, scriptInfo } from "./constants";
 import type { Domain, LogLevel } from "./types";
 
-//#MARKER BYTM-specific
-
 //#SECTION logging
 
 let curLogLevel: LogLevel = 1;
@@ -118,7 +116,7 @@ function ytForceShowVideoTime() {
 
   const defaultProps = {
     // needed because otherwise YTM errors out - see https://github.com/Sv443/BetterYTM/issues/18#show_issue
-    view: unsafeWindow ?? window,
+    view: getUnsafeWindow(),
     bubbles: true,
     cancelable: false,
   };
@@ -144,27 +142,6 @@ function ytForceShowVideoTime() {
   return true;
 }
 
-/**
- * Creates an invisible anchor with _blank target and clicks it.  
- * This has to be run in relatively quick succession to a user interaction event, else the browser rejects it.
- */
-export function openInNewTab(href: string) {
-  const openElem = document.createElement("a");
-  Object.assign(openElem, {
-    className: "betterytm-open-in-new-tab",
-    target: "_blank",
-    rel: "noopener noreferrer",
-    href,
-    style: {
-      visibility: "hidden",
-    },
-  });
-  document.body.appendChild(openElem);
-  openElem.click();
-  // timeout just to be safe
-  setTimeout(() => openElem.remove(), 200);
-}
-
 //#SECTION DOM
 
 /**
@@ -197,6 +174,41 @@ export function addGlobalStyle(style: string, ref?: string) {
 
 //#SECTION misc
 
+/**
+ * Creates an invisible anchor with _blank target and clicks it.  
+ * This has to be run in relatively quick succession to a user interaction event, else the browser rejects it.
+ */
+export function openInNewTab(href: string) {
+  const openElem = document.createElement("a");
+  Object.assign(openElem, {
+    className: "betterytm-open-in-new-tab",
+    target: "_blank",
+    rel: "noopener noreferrer",
+    href,
+    style: {
+      visibility: "hidden",
+    },
+  });
+  document.body.appendChild(openElem);
+  openElem.click();
+  // timeout just to be safe
+  setTimeout(() => openElem.remove(), 200);
+}
+
+/**
+ * Returns `unsafeWindow` if it is available, otherwise falls back to just `window`  
+ * unsafeWindow is sometimes needed because otherwise YTM errors out - see [this issue](https://github.com/Sv443/BetterYTM/issues/18#show_issue)
+ */
+export function getUnsafeWindow() {
+  try {
+    // throws ReferenceError if the "@grant unsafeWindow" isn't present
+    return unsafeWindow;
+  }
+  catch(e) {
+    return window;
+  }
+}
+
 /**
  * Returns the current domain as a constant string representation
  * @throws Throws if script runs on an unexpected website