Skip to content

Commit

Permalink
ref: window event interceptor
Browse files Browse the repository at this point in the history
  • Loading branch information
Sv443 committed Nov 16, 2024
1 parent 573bd95 commit 5ca7f83
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions src/features/behavior.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@
import { clamp, interceptWindowEvent, pauseFor } from "@sv443-network/userutils";
import { domLoaded, error, getDomain, getVideoTime, getWatchId, info, log, waitVideoElementReady, clearNode, getCurrentMediaType, getVideoElement, warn } from "../utils/index.js";
import { domLoaded, error, getDomain, getVideoTime, getWatchId, info, log, waitVideoElementReady, clearNode, getCurrentMediaType, getVideoElement } from "../utils/index.js";
import { getFeature } from "../config.js";
import { addSelectorListener } from "../observers.js";
import { initialParams } from "../constants.js";
import { LogLevel } from "../types.js";

//#region beforeunload popup

let beforeUnloadEnabled = true;
let discardBeforeUnload = false;

/** Disables the popup before leaving the site */
export function disableBeforeUnload() {
beforeUnloadEnabled = false;
discardBeforeUnload = true;
info("Disabled popup before leaving the site");
}

/** (Re-)enables the popup before leaving the site */
export function enableBeforeUnload() {
beforeUnloadEnabled = true;
discardBeforeUnload = false;
info("Enabled popup before leaving the site");
}

/** Adds a spy function into `window.__proto__.addEventListener` to selectively discard `beforeunload` event listeners before they can be called by the site */
export async function initBeforeUnloadHook() {
if(GM?.info?.scriptHandler && GM.info.scriptHandler !== "FireMonkey")
interceptWindowEvent("beforeunload", () => !beforeUnloadEnabled);
else
warn(`Event intercepting is not available in ${GM.info.scriptHandler}, please use a different userscript extension`);
try {
interceptWindowEvent("beforeunload", () => discardBeforeUnload);
}
catch(err) {
error("Error in beforeunload hook:", err);
}
}

//#region auto close toasts
Expand Down

0 comments on commit 5ca7f83

Please sign in to comment.