https://github.com/binki/binki-userscript-when-event-dispatched-async
Utility to turn an event into an awaitable Promise for userscripts
https://github.com/binki/binki-userscript-when-event-dispatched-async
Last synced: 4 months ago
JSON representation
Utility to turn an event into an awaitable Promise for userscripts
- Host: GitHub
- URL: https://github.com/binki/binki-userscript-when-event-dispatched-async
- Owner: binki
- License: mit
- Created: 2024-04-06T22:26:20.000Z (about 2 years ago)
- Default Branch: master
- Last Pushed: 2025-11-13T16:25:28.000Z (7 months ago)
- Last Synced: 2025-11-13T18:19:20.081Z (7 months ago)
- Language: JavaScript
- Size: 8.79 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Usage
Include this in your userscript using [`@require`](https://wiki.greasespot.net/Metadata_Block#.40require). It is recommended to [use a permalink](https://docs.github.com/repositories/working-with-files/using-files/getting-permanent-links-to-files) instead of referring to `master`.
```js
// ==UserScript==
// @name example
// @version 1.0
// @require ttps://github.com/binki/binki-userscript-when-event-dispatched-async/raw/master/binki-userscript-when-event-dispatched-async.js
// ==UserScript==
(async () => {
while (true) {
await whenEventDispatchedAsync(window, 'hashchange');
console.log(`The URI is now ${window.location.href}`);
}
})();
```
# API
```js
whenEventDispatchedAsync(eventTarget, type, handleEvent, options);
```
Parameters:
* `eventTarget` is the [`EventTarget`](https://dom.spec.whatwg.org/#interface-eventtarget) whose [`addEventListener`](https://dom.spec.whatwg.org/#dom-eventtarget-addeventlistener) and [`removeEventListener`](https://dom.spec.whatwg.org/#dom-eventtarget-removeeventlistener) methods are called.
* `type` is the name of the event.
* `handleEvent` (optional) is a function. If supplied, this is passed the event. The function can return `false` to indicate that the wait should continue. Since the `Event` object may only be inspected when handling the event, this must be done here.
* `options` (optional) is an [`AddEventListenerOptions`](https://dom.spec.whatwg.org/#dictdef-addeventlisteneroptions) object. This can be used to specify things like [`capture`](https://dom.spec.whatwg.org/#dom-eventlisteneroptions-capture). [`passive`](https://dom.spec.whatwg.org/#dom-addeventlisteneroptions-passive) is automatically set to `true` if no `eventHandler` is supplied. [`signal`](https://dom.spec.whatwg.org/#dom-addeventlisteneroptions-signal) is supported and will result in the returned `Promise` being rejected if the signal is fired.