WebExtension Polyfill Globalizer

0.10.1-1 · active · verified Tue Apr 21

webextension-polyfill-global is a utility package designed to make Mozilla's `webextension-polyfill` readily available as a global `browser` object within web extensions, even when using bundlers like Webpack or Parcel. It addresses a known issue where the upstream `webextension-polyfill` can be challenging to import as a true global polyfill, often requiring manual `import()` calls or complex bundler configurations. This package simplifies that process by directly making the polyfill globally accessible upon import. The current stable version is 0.10.1-1. As a wrapper, its release cadence is typically tied to updates in the core `webextension-polyfill` or improvements in its global registration mechanism, though it has seen consistent maintenance. Its key differentiator is its singular focus on providing a simple, fire-and-forget global registration for the browser API, streamlining development for extension authors who prefer the `browser` global over explicit imports everywhere.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to import the polyfill and immediately use the global `browser` API.

import 'webextension-polyfill-global';

// Now, the 'browser' global API is available

async function getActiveTabUrl() {
  if (typeof browser !== 'undefined' && browser.tabs) {
    try {
      const tabs = await browser.tabs.query({ active: true, currentWindow: true });
      if (tabs.length > 0) {
        console.log('Active tab URL:', tabs[0].url);
        // Example: Send a message to the active tab's content script
        await browser.tabs.sendMessage(tabs[0].id, { greeting: 'hello from background' });
      }
    } catch (error) {
      console.error('Error getting active tab URL:', error);
    }
  } else {
    console.warn('WebExtension polyfill or browser API not available.');
  }
}

getActiveTabUrl();

view raw JSON →