Registry / http-networking / webext-tools

webext-tools

JSON →
library4.0.0jsnpmunverified

webext-tools provides a collection of modular utility functions designed to simplify common tasks when developing Web Extensions for browsers like Chrome, Firefox, and Safari. The current stable version is 4.0.0, which exclusively targets Manifest V3. The package emphasizes modularity, with each utility function available via its own entry point, allowing developers to import only the specific tools they need and reducing bundle size. This approach differentiates it from monolithic utility libraries. While there isn't an explicit release cadence mentioned, the project sees active development and releases major versions periodically with breaking changes, consistently adapting to browser API changes.

npm install webext-tools
INSTALL
IMPORT
SIG · WEBEXT-TOOLS
W
webext-tools
http-networkingjavascriptv4.0.0
Install
—
Import
—
Disk
—
Pass rate
0/ 6
Env Coverage0 / 6
glibc
18–22
musl
18–22
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18–226 runs
build_error
glibc
node 18–226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

doesTabExist
✓ import doesTabExist from 'webext-tools/does-tab-exist.js';
✗ import { doesTabExist } from 'webext-tools';
Since v4.0.0, each utility is a default export from its specific file path. The old single-entry point named import no longer works.
createContextMenu
✓ import createContextMenu from 'webext-tools/create-context-menu.js';
✗ const createContextMenu = require('webext-tools/create-context-menu');
Introduced in v3.0.0. As of v4.0.0, it must be imported from its dedicated file. Primarily designed for ESM usage in modern browser extensions.
setActionPopup
✓ import setActionPopup from 'webext-tools/set-action-popup.js';
✗ import { setActionPopup } from 'webext-tools';
Introduced in v1.2.0, supports MV3 since v1.2.3. Requires specific file import since v4.0.0.
getExtensionUrl
✓ import getExtensionUrl from 'webext-tools/get-extension-url.js';
✗ import * as tools from 'webext-tools'; tools.getExtensionUrl();
Added in v4.0.0. Follows the per-file default export pattern. Requires a valid extension context to use `browser.runtime.getURL` internally.

This quickstart demonstrates how to import and use several `webext-tools` utilities within a Web Extension's background script, including creating a context menu and setting the browser action popup. It includes a mock `browser` API for basic local execution understanding, though it requires a real extension environment.

import createContextMenu from 'webext-tools/create-context-menu.js'; import getExtensionUrl from 'webext-tools/get-extension-url.js'; import setActionPopup from 'webext-tools/set-action-popup.js'; // This polyfill/mock ensures `browser` API is available for demonstration purposes // In a real Web Extension, `browser` is globally available. const browser = globalThis.browser || { tabs: { create: (options) => console.log('Creating tab:', options.url) }, contextMenus: { create: (options) => { console.log(`Creating context menu: ID='${options.id}', Title='${options.title}', Contexts='${options.contexts}'`); // Simulate adding an onclick listener if (options.onclick) { console.log('Context menu handler registered.'); // In a real extension, this would react to actual clicks. } } }, action: { setPopup: (options) => console.log('Setting action popup:', options.popup) }, runtime: { getURL: (path) => `chrome-extension://your_extension_id/${path}`, onInstalled: { addListener: (cb) => cb() }, // Mock event listener onStartup: { addListener: (cb) => cb() } // Mock event listener } }; async function setupExtensionUtilities() { // Create a context menu item that opens an extension page createContextMenu({ id: 'open-webext-page', title: 'Open Example Page (webext-tools)', contexts: ['page', 'selection'], onclick: (info, tab) => { if (tab?.url) { console.log(`Context menu clicked on URL: ${tab.url}`); browser.tabs.create({ url: getExtensionUrl('pages/example.html'), active: true }); } } }); // Dynamically set the browser action popup to an HTML file within the extension setActionPopup('pages/popup.html'); console.log('webext-tools utilities initialized in background script.'); } // Ensure setup runs when the extension loads or updates browser.runtime.onInstalled.addListener(setupExtensionUtilities); browser.runtime.onStartup.addListener(setupExtensionUtilities);
Debug
Known issues
breakingThe package no longer offers a single entry point. All utilities must now be imported individually from their specific file paths.
fix
Update all imports from `import { someTool } from 'webext-tools';` to `import someTool from 'webext-tools/some-tool.js';`.
affects: >=4.0.0
breakingSupport for Manifest V2 (MV2) has been completely dropped. The package now exclusively targets Manifest V3 (MV3).
fix
Ensure your extension's `manifest.json` specifies `manifest_version: 3`. If still on MV2, you must migrate your extension or use an older version of `webext-tools`.
affects: >=4.0.0
breakingThe `createContextMenu` function now enforces stricter typing for its `contexts` parameter.
fix
Review all calls to `createContextMenu` and ensure the `contexts` array contains only valid, type-safe values as per the updated API definitions.
affects: >=4.0.0
breakingThe `addOptionsContextMenu` functionality has been moved out of `webext-tools` and into the `webext-bugs` package.
fix
If you used `addOptionsContextMenu`, you need to install and import it from `webext-bugs` directly or implement the equivalent logic yourself.
affects: >=3.0.0
breakingThe `canAccessTab` function was removed in favor of directly using functionality from the `webext-content-scripts` package.
fix
Replace calls to `canAccessTab` with direct usage of the relevant functions from `webext-content-scripts` or implement custom tab access logic.
affects: >=2.0.0
Errors
Common errors & fixes
TypeError: Cannot read properties of undefined (reading 'doesTabExist')
Attempting to use a utility function like `doesTabExist` via a named import or after incorrect CommonJS `require` syntax, which is no longer supported for individual tools since v4.0.0.
fix
Change the import statement from `import { doesTabExist } from 'webext-tools';` to `import doesTabExist from 'webext-tools/does-tab-exist.js';`
Extension is running in Manifest V2, but requires Manifest V3.
Using `webext-tools` version 4.0.0 or higher in an extension configured with `manifest_version: 2`.
fix
Update your extension's `manifest.json` to specify `"manifest_version": 3` and migrate any other MV2-specific APIs to their MV3 equivalents.
Error: Invalid value for property 'contexts'. (Received: "all").
Passing an invalid or deprecated string value to the `contexts` array of `createContextMenu` after the type strictness increase in v4.0.0.
fix
Review the Web Extension API documentation for `contextMenus.create` and the `webext-tools` types for `createContextMenu` to use valid `contexts` strings (e.g., 'page', 'selection', 'link').
Upgrade
Version history
4.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
25 hits · last 30 days
node
22
Amazon
1
OpenAI (training)
1
Resources