Convert Figma logo to code with AI

mdn logowebextensions-examples

Example Firefox add-ons created using the WebExtensions API

4,091
2,620
4,091
20

Top Related Projects

A lightweight polyfill library for Promise-based WebExtension APIs in Chrome

Chrome Extensions Samples

A curated list of awesome resources for WebExtensions development.

🛠️ A Vue CLI 3+ preset (previously a Vue CLI 2 boilerplate) for quickly starting a web extension with Vue, Babel, ESLint and more!

🖥🔋Web Extension starter to build "Write Once Run on Any Browser" extension

Quick Overview

The mdn/webextensions-examples repository is a collection of sample WebExtensions maintained by Mozilla Developer Network (MDN). It provides a wide range of examples demonstrating various features and capabilities of browser extensions, primarily for Firefox but also applicable to other browsers supporting the WebExtensions API.

Pros

  • Comprehensive collection of examples covering many WebExtension APIs
  • Well-documented code with comments explaining functionality
  • Regularly updated to reflect changes in WebExtensions standards
  • Serves as a valuable learning resource for both beginners and experienced developers

Cons

  • Some examples may become outdated as browser APIs evolve
  • Primarily focused on Firefox, which may limit applicability to other browsers
  • Not a complete reference for all possible WebExtension features
  • Some examples may be overly simplistic for complex real-world scenarios

Code Examples

Here are a few short code examples from different extensions in the repository:

  1. Listening for browser action clicks (from beastify example):
browser.browserAction.onClicked.addListener(function() {
  console.log("Turning the page red!");
  browser.tabs.executeScript({
    code: 'document.body.style.backgroundColor = "red"'
  });
});
  1. Creating a context menu item (from context-menu-demo example):
browser.contextMenus.create({
  id: "eat-page",
  title: "Eat this page"
});

browser.contextMenus.onClicked.addListener((info, tab) => {
  if (info.menuItemId === "eat-page") {
    browser.tabs.executeScript({
      file: "page-eater.js"
    });
  }
});
  1. Using storage API (from stored-credentials example):
function storeCredentials(credentials) {
  browser.storage.local.set(credentials).then(() => {
    console.log("Credentials stored");
  });
}

function getCredentials() {
  return browser.storage.local.get(["username", "password"]);
}

Getting Started

To use these examples:

  1. Clone the repository:

    git clone https://github.com/mdn/webextensions-examples.git
    
  2. Navigate to a specific example directory.

  3. Load the extension in Firefox:

    • Open about:debugging
    • Click "This Firefox"
    • Click "Load Temporary Add-on"
    • Select the manifest.json file in the example directory
  4. Experiment with the extension and explore its source code to learn how it works.

Competitor Comparisons

A lightweight polyfill library for Promise-based WebExtension APIs in Chrome

Pros of webextension-polyfill

  • Provides a unified Promise-based API for cross-browser extension development
  • Simplifies code by abstracting browser-specific implementations
  • Actively maintained and updated to support latest browser APIs

Cons of webextension-polyfill

  • Limited to polyfilling browser APIs, not a comprehensive example collection
  • May introduce a small performance overhead due to Promise wrapping
  • Requires additional setup and integration into extension projects

Code Comparison

webextension-polyfill:

browser.tabs.query({active: true, currentWindow: true}).then(tabs => {
  browser.tabs.sendMessage(tabs[0].id, {greeting: "Hello"});
});

webextensions-examples:

chrome.tabs.query({active: true, currentWindow: true}, tabs => {
  chrome.tabs.sendMessage(tabs[0].id, {greeting: "Hello"});
});

Summary

webextension-polyfill focuses on providing a consistent API across browsers, making it easier to write cross-browser extensions. It uses Promises for asynchronous operations, resulting in cleaner code. However, it's not a comprehensive resource for learning extension development.

webextensions-examples, on the other hand, offers a wide range of practical examples covering various extension features. It's an excellent learning resource but doesn't address cross-browser compatibility issues directly.

Developers may choose to use both repositories in conjunction: webextensions-examples for learning and reference, and webextension-polyfill for streamlining cross-browser development in their projects.

Chrome Extensions Samples

Pros of chrome-extensions-samples

  • More extensive collection of examples, covering a wider range of Chrome extension APIs
  • Regularly updated with new features and best practices for Chrome extensions
  • Includes complex, real-world examples that demonstrate advanced functionality

Cons of chrome-extensions-samples

  • Specific to Chrome, limiting cross-browser compatibility
  • May include deprecated or experimental APIs that are not stable
  • Less beginner-friendly, with fewer explanations and documentation within the examples

Code Comparison

webextensions-examples (manifest.json):

{
  "manifest_version": 2,
  "name": "Example Extension",
  "version": "1.0",
  "description": "A simple example extension"
}

chrome-extensions-samples (manifest.json):

{
  "manifest_version": 3,
  "name": "Chrome Extension Example",
  "version": "1.0",
  "description": "An example Chrome extension",
  "permissions": ["activeTab"]
}

The main difference in the code is the manifest version, with chrome-extensions-samples using the newer Manifest V3, while webextensions-examples still uses Manifest V2 for broader compatibility across browsers.

A curated list of awesome resources for WebExtensions development.

Pros of Awesome-WebExtensions

  • Comprehensive collection of resources, including tutorials, libraries, and tools
  • Regularly updated with community contributions
  • Covers a wide range of topics beyond basic examples

Cons of Awesome-WebExtensions

  • Lacks hands-on code examples for immediate implementation
  • May be overwhelming for beginners due to the sheer amount of information
  • Requires additional research to find the most relevant resources

Code Comparison

While Awesome-WebExtensions doesn't provide direct code examples, webextensions-examples offers practical implementations. Here's a comparison of how each repository might approach a simple browser action:

webextensions-examples:

browser.browserAction.onClicked.addListener(() => {
  console.log("Browser action clicked");
});

Awesome-WebExtensions: (No direct code, but might link to resources explaining browser actions)

Summary

webextensions-examples is ideal for developers seeking hands-on, ready-to-use code examples for specific extension features. It's particularly useful for beginners or those looking for quick implementation references.

Awesome-WebExtensions serves as a comprehensive resource hub, offering a broader perspective on extension development. It's better suited for developers looking to explore various tools, libraries, and advanced concepts in browser extension development.

The choice between the two depends on the developer's needs: immediate code examples (webextensions-examples) or a wide-ranging collection of resources (Awesome-WebExtensions).

🛠️ A Vue CLI 3+ preset (previously a Vue CLI 2 boilerplate) for quickly starting a web extension with Vue, Babel, ESLint and more!

Pros of vue-web-extension

  • Built specifically for Vue.js, offering a streamlined development experience for Vue developers
  • Includes hot-reloading, making the development process faster and more efficient
  • Provides a modern build setup with webpack, allowing for better code organization and optimization

Cons of vue-web-extension

  • Limited to Vue.js framework, less flexible for developers using other technologies
  • Fewer examples and less comprehensive documentation compared to webextensions-examples
  • May have a steeper learning curve for developers not familiar with Vue.js

Code Comparison

webextensions-examples (JavaScript):

browser.browserAction.onClicked.addListener(() => {
  console.log("Button clicked!");
});

vue-web-extension (Vue.js):

<template>
  <button @click="handleClick">Click me</button>
</template>

<script>
export default {
  methods: {
    handleClick() {
      console.log("Button clicked!");
    }
  }
}
</script>

The vue-web-extension example demonstrates the use of Vue.js components and methods, while the webextensions-examples code shows a more traditional JavaScript approach to handling browser actions.

🖥🔋Web Extension starter to build "Write Once Run on Any Browser" extension

Pros of web-extension-starter

  • Provides a modern development setup with TypeScript, React, and Webpack
  • Includes cross-browser compatibility out of the box (Chrome, Firefox, Opera)
  • Offers a well-structured project template with hot-reloading for faster development

Cons of web-extension-starter

  • Less comprehensive in terms of API usage examples compared to webextensions-examples
  • May have a steeper learning curve for beginners due to its use of advanced tools and frameworks
  • Focuses more on project structure and build process rather than demonstrating various WebExtension APIs

Code Comparison

webextensions-examples (JavaScript):

browser.browserAction.onClicked.addListener(() => {
  console.log("Button clicked!");
});

web-extension-starter (TypeScript):

import { browser } from 'webextension-polyfill-ts';

browser.browserAction.onClicked.addListener(() => {
  console.log("Button clicked!");
});

The web-extension-starter example uses TypeScript and imports the browser API from a polyfill library, providing better type checking and cross-browser compatibility. The webextensions-examples repository uses plain JavaScript and relies on the browser's native API, which is simpler but may require additional work for cross-browser support.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

webextensions-examples Build Status

https://github.com/mdn/webextensions-examples

Maintained by Mozilla's Add-ons team.

WebExtensions are a way to write browser extensions: that is, programs installed inside a web browser that modify the behavior of the browser or web pages loaded by the browser. WebExtensions are built on a set of cross-browser APIs, so WebExtensions written for Google Chrome, Opera, or Edge will, in most cases, run in Firefox too.

The "webextensions-examples" repository is a collection of simple, complete, and installable WebExtensions. The examples show how to use the WebExtensions APIs, and you can use them as a starting point for your WebExtensions.

For an index of all the examples, see the "Example extensions" page on MDN.

The examples are made available under the Mozilla Public License 2.0.

How to use "webextensions-examples"

To use the repository, first clone it.

Each example is in a top-level folder and includes a short README explaining what it does. To see how an example works, install it in Firefox by following the installation instructions.

To find your way around a WebExtension's internal structure, have a look at the Anatomy of a WebExtension page on MDN.

To use these examples in Firefox, use the most recent release of Firefox. However, most examples work with earlier releases.

A few examples rely on APIs that are only available in pre-release versions of Firefox. Where this is the case, the example declares the minimum version that it needs in the strict_min_version attribute of the browser_specific_settings key in the extension's manifest.json file.

Installing an example

Some examples work only on specific domains or pages. Details of any restrictions are provided in each example's README file. None of the examples work in private browsing windows by default, see Extensions in Private Browsing for details.

To run an example extension:

  1. Open Firefox and load the about:debugging page. Click Load Temporary Add-on and select the manifest.json file within the folder of an example extension. Here is a video that demonstrates how to do this.
  2. Install the web-ext tool. At the command line, open the example extension's folder and type web-ext run. This launches Firefox and installs the extension automatically. This tool provides some additional development features, such as automatic reloading.

Support for other browsers

These examples are tested in Firefox. They may work in other browsers, if the browser supports the APIs used. Note that these examples all use the browser namespace and promises to work with asynchronous functions. This means the examples won't work in Chrome unless you use the polyfill provided by Mozilla. See the overview of WebExtension APIs for more information.

Learn more

To learn more about developing WebExtensions, see the WebExtensions documentation on MDN for getting started guides, tutorials, and full API reference docs.

Problems?

If you encounter an issue:

If you cannot resolve the issue, file a bug.

Contributing

We welcome contributions, whether they are new examples, new features, bug fixes, or translations of localizable strings. Please see the CONTRIBUTING.md file for more details.