Skip to content

Latest commit

 

History

History
59 lines (39 loc) · 1.12 KB

one-event.md

File metadata and controls

59 lines (39 loc) · 1.12 KB

oneEvent

Utility method to capture a single event and resolve a promise after it's received:

import {oneEvent} from 'webext-events';

// Wait until the user opens a new tab
await oneEvent(chrome.tabs.onCreated);

console.log('Hurray, a new tab was created')

It also supports filtering:

import {oneEvent} from 'webext-events';

// Wait until the user opens a new tab, but it has it be HTTPS
await oneEvent(chrome.tabs.onCreated, {
	filter: (tab) => tab.pendingUrl?.startsWith('https'),
});

console.log('Hurray, a new HTTPS tab was created')

And abort signals:

import {oneEvent} from 'webext-events';

// Wait until the user opens a new tab, but only for one second
const timeout = AbortSignal.timeout(1000);
await oneEvent(chrome.tabs.onCreated, {
	signal: timeout,
});

if (timeout.aborted) {
	console.log('No tab was created in time')
} else {
	console.log('Hurray, a new tab was created')
}

Compatibility

  • Any browser

Permissions

  • No special permissions

Context

  • Any context

Related

  • one-event - The same thing, but for regular browser events.