Convert Figma logo to code with AI

abhijithvijayan logoweb-extension-starter

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

2,133
177
2,133
5

Top Related Projects

A Chrome Extensions boilerplate using React 18 and Webpack 5.

2,983

A command line tool to help build, run, and test web extensions

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

Quick Overview

Web Extension Starter is a modern, cross-browser compatible boilerplate for developing browser extensions. It provides a solid foundation for creating extensions that work across Chrome, Firefox, and other Chromium-based browsers, using TypeScript and React for a robust development experience.

Pros

  • Cross-browser compatibility out of the box
  • Built with modern technologies (TypeScript, React, Webpack)
  • Includes hot-reloading for faster development
  • Comprehensive documentation and examples

Cons

  • May have a steeper learning curve for beginners
  • Might be overkill for simple extensions
  • Requires knowledge of React and TypeScript

Code Examples

  1. Accessing browser APIs:
import Browser from 'webextension-polyfill';

// Get all open tabs
Browser.tabs.query({}).then(tabs => {
  console.log('Open tabs:', tabs);
});
  1. Creating a popup component:
import React from 'react';

const Popup: React.FC = () => {
  return (
    <div>
      <h1>Extension Popup</h1>
      <button onClick={() => console.log('Button clicked')}>
        Click me
      </button>
    </div>
  );
};

export default Popup;
  1. Sending a message from content script to background script:
import Browser from 'webextension-polyfill';

// In content script
Browser.runtime.sendMessage({ action: 'getData' })
  .then(response => {
    console.log('Received data:', response);
  });

// In background script
Browser.runtime.onMessage.addListener((message, sender, sendResponse) => {
  if (message.action === 'getData') {
    sendResponse({ data: 'Some data' });
  }
});

Getting Started

  1. Clone the repository:

    git clone https://github.com/abhijithvijayan/web-extension-starter.git
    
  2. Install dependencies:

    cd web-extension-starter
    npm install
    
  3. Start development server:

    npm run dev
    
  4. Build for production:

    npm run build
    
  5. Load the extension in your browser:

    • Chrome: Go to chrome://extensions/, enable "Developer mode", and click "Load unpacked". Select the extension directory from the build output.
    • Firefox: Go to about:debugging#/runtime/this-firefox, click "Load Temporary Add-on", and select the manifest.json file from the extension directory.

Competitor Comparisons

A Chrome Extensions boilerplate using React 18 and Webpack 5.

Pros of chrome-extension-boilerplate-react

  • Built with React, offering a more modern and component-based approach
  • Includes hot-reloading for faster development
  • Provides a more comprehensive project structure with separate folders for components, pages, and hooks

Cons of chrome-extension-boilerplate-react

  • Larger bundle size due to React dependencies
  • Steeper learning curve for developers not familiar with React
  • Less focus on cross-browser compatibility compared to web-extension-starter

Code Comparison

web-extension-starter:

import Browser from 'webextension-polyfill';

export async function getCurrentTab() {
  const tabs = await Browser.tabs.query({currentWindow: true, active: true});
  return tabs[0];
}

chrome-extension-boilerplate-react:

import React from 'react';
import { createRoot } from 'react-dom/client';

const root = createRoot(document.getElementById('root'));
root.render(<Popup />);

The code snippets highlight the different approaches:

  • web-extension-starter uses a more traditional JavaScript approach with browser APIs
  • chrome-extension-boilerplate-react leverages React for rendering components

Both repositories provide solid starting points for developing browser extensions, but they cater to different preferences and use cases. web-extension-starter offers a more lightweight and cross-browser compatible solution, while chrome-extension-boilerplate-react provides a modern React-based development experience with additional features like hot-reloading.

2,983

A command line tool to help build, run, and test web extensions

Pros of web-ext

  • Officially maintained by Mozilla, ensuring compatibility with Firefox
  • Robust CLI tool for building, running, and testing web extensions
  • Supports auto-reloading and watching for file changes during development

Cons of web-ext

  • Primarily focused on Firefox, may require additional setup for cross-browser development
  • Less opinionated about project structure and build processes
  • Steeper learning curve for beginners compared to web-extension-starter

Code Comparison

web-ext:

web-ext run
web-ext lint
web-ext build

web-extension-starter:

"scripts": {
  "dev": "cross-env NODE_ENV=development webpack --config webpack.config.js",
  "build": "cross-env NODE_ENV=production webpack --config webpack.config.js",
  "lint": "eslint . --ext .ts,.tsx"
}

Summary

web-ext is a powerful tool for Firefox extension development, offering robust CLI features and official Mozilla support. However, it may require additional setup for cross-browser compatibility. web-extension-starter provides a more opinionated and beginner-friendly approach, with a pre-configured project structure and build process. The choice between the two depends on the developer's experience level and specific project requirements.

🛠️ 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, providing a more tailored experience for Vue developers
  • Includes Vue Router and Vuex out of the box, offering a complete Vue ecosystem
  • Uses Vue CLI 3, which provides a more modern and flexible build setup

Cons of vue-web-extension

  • Less frequently updated compared to web-extension-starter
  • Focuses solely on Vue.js, limiting flexibility for developers who might want to use other frameworks
  • May have a steeper learning curve for developers not familiar with Vue.js ecosystem

Code Comparison

web-extension-starter:

import React from 'react';
import ReactDOM from 'react-dom';
import Popup from './Popup';

ReactDOM.render(<Popup />, document.getElementById('popup-root'));

vue-web-extension:

import Vue from 'vue';
import App from './App.vue';
import store from './store';

/* eslint-disable no-new */
new Vue({
  el: '#app',
  store,
  render: h => h(App),
});

The code comparison highlights the different approaches: web-extension-starter uses React, while vue-web-extension is built around Vue.js. The vue-web-extension example also showcases the integration with Vuex (store), which is included by default.

Both projects aim to simplify web extension development, but they cater to different ecosystems and developer preferences. The choice between them largely depends on the developer's familiarity with React or Vue.js and the specific requirements of the project.

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

🚀 web-extension-starter

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

🙋‍♂️ Made by @abhijithvijayan

Donate: PayPal, Patreon

Buy Me a Coffee


❤️ it? ⭐️ it on GitHub or Tweet about it.

Features

  • Cross Browser Support (Web-Extensions API)
  • Browser Tailored Manifest generation
  • Vite for fast builds and HMR
  • Automatic build on code changes
  • Auto packs browser specific build files
  • SASS/SCSS styling with CSS Modules
  • TypeScript by default
  • ES6 modules support
  • React 19 with automatic JSX runtime
  • ESLint 9 flat config with Prettier

Tech Stack

Browser Support

This starter uses Manifest V3 for all browsers.

ChromeFirefoxOperaEdgeBrave
88+ (Jan 2021)109+ (Jan 2023)74+ (Chromium-based)88+ (Chromium-based)Latest (Chromium-based)

Note: Firefox 109+ is required for Manifest V3 support with ES modules in background scripts.

Need to support older Firefox versions? See Firefox MV2 Guide for using Manifest V2 with Firefox.

Used by extensions in production that has over 100,000+ users.

Use this template

Create a new directory and run

curl -fsSL https://github.com/abhijithvijayan/web-extension-starter/archive/react-typescript-vite.tar.gz | tar -xz --strip-components=1

🚀 Quick Start

Ensure you have Node.js 20 or later installed.

Then run the following:

# Install dependencies
npm install

# Start development server
npm run dev:chrome    # For Chrome
npm run dev:firefox   # For Firefox

# Build for production
npm run build:chrome  # Build Chrome extension
npm run build:firefox # Build Firefox addon
npm run build         # Build for all browsers

Project Structure

source/
├── Background/        # Service worker (Chrome MV3) / Background script (Firefox)
├── ContentScript/     # Content scripts (injected into web pages)
├── Popup/             # Extension popup UI
├── Options/           # Options page UI
├── components/        # Shared React components
├── styles/            # Global styles and variables
├── types/             # TypeScript type definitions
├── utils/             # Utility functions
├── public/            # Static assets (icons, etc.)
└── manifest.json      # Extension manifest template

Development

Loading the Extension

Chrome

  1. Navigate to chrome://extensions
  2. Enable "Developer mode"
  3. Click "Load unpacked"
  4. Select extension/chrome directory

Firefox

  1. Navigate to about:debugging
  2. Click "This Firefox"
  3. Click "Load Temporary Add-on"
  4. Select extension/firefox/manifest.json

Content Scripts

Content scripts are automatically bundled as IIFE (Immediately Invoked Function Expression) to ensure compatibility with the browser's content script execution environment, which doesn't support ES modules.

Browser-Specific Manifest

The manifest uses vendor prefixes to generate browser-specific configurations:

{
  "__chrome__name": "My Chrome Extension",
  "__firefox__name": "My Firefox Addon",
  "__chrome|firefox__description": "Works on both!"
}

See vite-plugin-wext-manifest for more details.

Scripts

ScriptDescription
npm run dev:chromeStart dev server for Chrome
npm run dev:firefoxStart dev server for Firefox
npm run build:chromeBuild production Chrome extension
npm run build:firefoxBuild production Firefox addon
npm run buildBuild for all browsers
npm run lintRun ESLint
npm run lint:fixRun ESLint with auto-fix

Linting & TypeScript Config

Bugs

Please file an issue here for bugs, missing documentation, or unexpected behavior.

License

MIT © Abhijith Vijayan