Web Extension Utility Functions

4.0.0 · active · verified Sun Apr 19

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.

Common errors

Warnings

Install

Imports

Quickstart

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);

view raw JSON →