瀏覽代碼

ref: beforeUnloadHook now intercepts

Sv443 1 年之前
父節點
當前提交
3de912e972
共有 3 個文件被更改,包括 15 次插入27 次删除
  1. 8 14
      dist/BetterYTM.user.js
  2. 6 13
      src/features/input.ts
  3. 1 0
      src/utils.ts

+ 8 - 14
dist/BetterYTM.user.js

@@ -486,7 +486,7 @@ const scriptInfo = Object.freeze({
     name: GM.info.script.name,
     version: GM.info.script.version,
     namespace: GM.info.script.namespace,
-    lastCommit: "4d46e00", // assert as generic string instead of union
+    lastCommit: "841c8c3", // assert as generic string instead of union
 });
 
 
@@ -671,6 +671,7 @@ function onKeyDown(evt) {
             cancelable: true,
             isTrusted: true,
             repeat: false,
+            // needed because otherwise YTM errors out - see https://github.com/Sv443/BetterYTM/issues/18#show_issue
             view: unsafeWindow !== null && unsafeWindow !== void 0 ? unsafeWindow : window,
         };
         let invalidKey = false;
@@ -755,24 +756,16 @@ function enableBeforeUnload() {
     beforeUnloadEnabled = true;
     (0,_utils__WEBPACK_IMPORTED_MODULE_0__.info)("Enabled popup before leaving the site");
 }
-/** Adds a spy function into `window.__proto__.addEventListener` to selectively discard events before they can be captured by the original site's listeners */
+/**
+ * Adds a spy function into `window.__proto__.addEventListener` to selectively discard beforeunload event listeners before they can be attached by the site
+ */
 function initBeforeUnloadHook() {
     Error.stackTraceLimit = Infinity;
     (function (original) {
         // @ts-ignore
         window.__proto__.addEventListener = function (...args) {
-            const [type, listener, ...rest] = args;
-            if (type === "beforeunload") {
-                return original.apply(this, [
-                    type,
-                    // @ts-ignore
-                    (...a) => {
-                        if (beforeUnloadEnabled)
-                            listener(...a);
-                    },
-                    ...rest,
-                ]);
-            }
+            if (beforeUnloadEnabled && args[0] === "beforeunload")
+                return (0,_utils__WEBPACK_IMPORTED_MODULE_0__.log)("Prevented beforeunload event listener from attaching");
             else
                 return original.apply(this, args);
         };
@@ -1682,6 +1675,7 @@ function ytForceShowVideoTime() {
     if (!player)
         return false;
     const defaultProps = {
+        // needed because otherwise YTM errors out - see https://github.com/Sv443/BetterYTM/issues/18#show_issue
         view: unsafeWindow !== null && unsafeWindow !== void 0 ? unsafeWindow : window,
         bubbles: true,
         cancelable: false,

+ 6 - 13
src/features/input.ts

@@ -35,6 +35,7 @@ function onKeyDown(evt: KeyboardEvent) {
       cancelable: true,
       isTrusted: true,
       repeat: false,
+      // needed because otherwise YTM errors out - see https://github.com/Sv443/BetterYTM/issues/18#show_issue
       view: unsafeWindow ?? window,
     };
 
@@ -141,25 +142,17 @@ export function enableBeforeUnload() {
   info("Enabled popup before leaving the site");
 }
 
-/** Adds a spy function into `window.__proto__.addEventListener` to selectively discard events before they can be captured by the original site's listeners */
+/**
+ * Adds a spy function into `window.__proto__.addEventListener` to selectively discard beforeunload event listeners before they can be attached by the site
+ */
 export function initBeforeUnloadHook() {
   Error.stackTraceLimit = Infinity;
 
   (function(original) {
     // @ts-ignore
     window.__proto__.addEventListener = function(...args) {
-      const [type, listener, ...rest] = args;
-      if(type === "beforeunload") {
-        return original.apply(this, [
-          type,
-          // @ts-ignore
-          (...a) => {
-            if(beforeUnloadEnabled)
-              listener(...a);
-          },
-          ...rest,
-        ]);
-      }
+      if(beforeUnloadEnabled && args[0] === "beforeunload")
+        return log("Prevented beforeunload event listener from attaching");
       else
         return original.apply(this, args);
     };

+ 1 - 0
src/utils.ts

@@ -115,6 +115,7 @@ function ytForceShowVideoTime() {
     return false;
 
   const defaultProps = {
+    // needed because otherwise YTM errors out - see https://github.com/Sv443/BetterYTM/issues/18#show_issue
     view: unsafeWindow ?? window,
     bubbles: true,
     cancelable: false,