Compare commits

..

2 Commits

Author SHA1 Message Date
GitHub Actions 50f6289ac5 chore(release): v0.6.3 2023-09-26 22:28:00 +00:00
Aaron a0e1b4741e feat(client): Add block and addEventListener utils to ContentScriptContext (#128) 2023-09-26 17:19:56 -05:00
3 changed files with 53 additions and 1 deletions
+12
View File
@@ -1,5 +1,17 @@
# Changelog
## v0.6.3
[compare changes](https://github.com/wxt-dev/wxt/compare/v0.6.2...v0.6.3)
### 🚀 Enhancements
- **client:** Add `block` and `addEventListener` utils to `ContentScriptContext` ([#128](https://github.com/wxt-dev/wxt/pull/128))
### ❤️ Contributors
- Aaron <aaronklinker1@gmail.com>
## v0.6.2
[compare changes](https://github.com/wxt-dev/wxt/compare/v0.6.1...v0.6.2)
+1 -1
View File
@@ -1,7 +1,7 @@
{
"name": "wxt",
"type": "module",
"version": "0.6.2",
"version": "0.6.3",
"description": "Next gen framework for developing web extensions",
"engines": {
"node": ">=18.16.0",
+40
View File
@@ -54,6 +54,23 @@ export class ContentScriptContext extends AbortController {
return () => this.signal.removeEventListener('abort', cb);
}
/**
* Return a promise that never resolves. Useful if you have an async function that shouldn't run
* after the context is expired.
*
* @example
* const getValueFromStorage = async () => {
* if (ctx.isInvalid) return ctx.block();
*
* // ...
* }
*/
block<T>(): Promise<T> {
return new Promise(() => {
// noop
});
}
/**
* Wrapper around `window.setInterval` that automatically clears the interval when invalidated.
*/
@@ -105,6 +122,29 @@ export class ContentScriptContext extends AbortController {
return id;
}
/**
* Call `target.addEventListener` and remove the event listener when the context is invalidated.
*
* @example
* ctx.addEventListener(window, "mousemove", () => {
* // ...
* });
* ctx.addEventListener(document, "visibilitychange", () => {
* // ...
* });
*/
addEventListener(
target: any,
type: string,
handler: (event: Event) => void,
options?: AddEventListenerOptions,
) {
target.addEventListener?.(type, handler, options);
this.onInvalidated(
() => target.removeEventListener?.(type, handler, options),
);
}
/**
* Abort the abort controller and execute all `onInvalidated` listeners.
*/