Skip to content

Commit 6fc2099

Browse files
authored
Merge pull request #65 from Sv443-Network/develop
2 parents fcb77a7 + 390110e commit 6fc2099

3 files changed

Lines changed: 13 additions & 2 deletions

File tree

.changeset/purple-hats-argue.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@sv443-network/userutils": patch
3+
---
4+
5+
Throw an error when calling `interceptEvent()` on `window` or `unsafeWindow` on FireMonkey instead of crashing the entire page

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,7 @@ If no predicate is specified, all events will be discarded.
622622
Calling this function will set the `Error.stackTraceLimit` to 100 (if it's not already higher) to ensure the stack trace is preserved.
623623

624624
⚠️ This function should be called as soon as possible (I recommend using `@run-at document-start`), as it will only intercept events that are *attached* after this function is called.
625+
⚠️ Due to this function modifying the `addEventListener` prototype, it might break execution of the page's main script if the userscript is running in an isolated context (like it does in FireMonkey). In that case, calling this function will throw an error.
625626

626627
<details><summary><b>Example - click to view</b></summary>
627628

@@ -655,7 +656,8 @@ If no predicate is specified, all events will be discarded.
655656
This is essentially the same as [`interceptEvent()`](#interceptevent), but automatically uses the `unsafeWindow` (or falls back to regular `window`).
656657

657658
⚠️ This function should be called as soon as possible (I recommend using `@run-at document-start`), as it will only intercept events that are *attached* after this function is called.
658-
⚠️ In order for all events to be interceptable, the directive `@grant unsafeWindow` should be set.
659+
⚠️ In order to have the best chance at intercepting events, the directive `@grant unsafeWindow` should be set.
660+
⚠️ Due to this function modifying the `addEventListener` prototype, it might break execution of the page's main script if the userscript is running in an isolated context (like it does in FireMonkey). In that case, calling this function will throw an error.
659661

660662
<details><summary><b>Example - click to view</b></summary>
661663

lib/dom.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ export function interceptEvent<
9797
eventName: Parameters<TEvtObj["addEventListener"]>[0],
9898
predicate: (event: TPredicateEvt) => boolean = () => true,
9999
): void {
100+
// @ts-ignore
101+
if((eventObject === window || eventObject === getUnsafeWindow()) && GM?.info?.scriptHandler && GM.info.scriptHandler === "FireMonkey")
102+
throw new Error("Intercepting window events is not supported on FireMonkey due to the isolated context the userscript runs in.");
103+
100104
// default is 25 on FF so this should hopefully be more than enough
101105
// @ts-ignore
102106
Error.stackTraceLimit = Math.max(Error.stackTraceLimit, 100);
@@ -128,7 +132,7 @@ export function interceptEvent<
128132
export function interceptWindowEvent<TEvtKey extends keyof WindowEventMap>(
129133
eventName: TEvtKey,
130134
predicate: (event: WindowEventMap[TEvtKey]) => boolean = () => true,
131-
): void {
135+
): void {
132136
return interceptEvent(getUnsafeWindow(), eventName, predicate);
133137
}
134138

0 commit comments

Comments
 (0)