Skip to content

Commit 7f3e05a

Browse files
committed
fix: docs & typing for intercept functions
1 parent 095b534 commit 7f3e05a

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

README.md

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -529,11 +529,12 @@ Usage:
529529
interceptEvent(
530530
eventObject: EventTarget,
531531
eventName: string,
532-
predicate: (event: Event) => boolean
532+
predicate?: (event: Event) => boolean
533533
): void
534534
```
535535
536536
Intercepts all events dispatched on the `eventObject` and prevents the listeners from being called as long as the predicate function returns a truthy value.
537+
If no predicate is specified, all events will be discarded.
537538
Calling this function will set the `Error.stackTraceLimit` to 1000 (if it's not already higher) to ensure the stack trace is preserved.
538539
539540
⚠️ 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.
@@ -544,8 +545,12 @@ Calling this function will set the `Error.stackTraceLimit` to 1000 (if it's not
544545
import { interceptEvent } from "@sv443-network/userutils";
545546

546547
interceptEvent(document.body, "click", (event) => {
547-
console.log("Intercepting click event:", event);
548-
return true; // prevent all click events on the body element
548+
// prevent all click events on <a> elements within the entire <body>
549+
if(event.target instanceof HTMLAnchorElement) {
550+
console.log("Intercepting click event:", event);
551+
return true;
552+
}
553+
return false; // allow all other click events through
549554
});
550555
```
551556
@@ -558,11 +563,12 @@ Usage:
558563
```ts
559564
interceptWindowEvent(
560565
eventName: string,
561-
predicate: (event: Event) => boolean
566+
predicate?: (event: Event) => boolean
562567
): void
563568
```
564569
565570
Intercepts all events dispatched on the `window` object and prevents the listeners from being called as long as the predicate function returns a truthy value.
571+
If no predicate is specified, all events will be discarded.
566572
This is essentially the same as [`interceptEvent()`](#interceptevent), but automatically uses the `unsafeWindow` (or falls back to regular `window`).
567573
568574
⚠️ 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.
@@ -573,9 +579,9 @@ This is essentially the same as [`interceptEvent()`](#interceptevent), but autom
573579
```ts
574580
import { interceptWindowEvent } from "@sv443-network/userutils";
575581

576-
interceptWindowEvent("beforeunload", (event) => {
577-
return true; // prevent the pesky "Are you sure you want to leave this page?" popup
578-
});
582+
// prevent the pesky "Are you sure you want to leave this page?" popup
583+
// as no predicate is specified, all events will be discarded by default
584+
interceptWindowEvent("beforeunload");
579585
```
580586
581587
</details>

lib/dom.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ export function openInNewTab(href: string) {
8787

8888
/**
8989
* Intercepts the specified event on the passed object and prevents it from being called if the called {@linkcode predicate} function returns a truthy value.
90+
* If no predicate is specified, all events will be discarded.
9091
* This function should be called as soon as possible (I recommend using `@run-at document-start`), as it will only intercept events that are added after this function is called.
9192
* Calling this function will set the `Error.stackTraceLimit` to 1000 to ensure the stack trace is preserved.
9293
*/
@@ -120,6 +121,7 @@ export function interceptEvent<TEvtObj extends EventTarget, TPredicateEvt extend
120121

121122
/**
122123
* Intercepts the specified event on the window object and prevents it from being called if the called {@linkcode predicate} function returns a truthy value.
124+
* If no predicate is specified, all events will be discarded.
123125
* This function should be called as soon as possible (I recommend using `@run-at document-start`), as it will only intercept events that are added after this function is called.
124126
* Calling this function will set the `Error.stackTraceLimit` to 1000 to ensure the stack trace is preserved.
125127
*/

0 commit comments

Comments
 (0)