Convert Figma logo to code with AI

abhijithvijayan logoweb-extension-starter

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

2,020
171
2,020
11

Top Related Projects

A Chrome Extensions boilerplate using React 18 and Webpack 5.

2,693

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,693

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.

🧙‍♂️ React + TypeScript = This branch

😨 React + JavaScript = Checkout react-javascript branch

👶🏼 HTML + JavaScript = Checkout master branch

Features

  • Cross Browser Support (Web-Extensions API)
  • Browser Tailored Manifest generation
  • Automatic build on code changes
  • Auto packs browser specific build files
  • SASS styling
  • TypeScript by default
  • ES6 modules support
  • React UI Library by default
  • Smart reload

Browser Support

ChromeFirefoxOperaEdgeYandexBravevivaldi
49 & later ✔52 & later ✔36 & later ✔79 & later ✔Latest ✔Latest ✔Latest ✔

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

and many more...

Use this template

Create a new directory and run

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

🚀 Quick Start

Ensure you have

Then run the following:

  • npm install to install dependencies.
  • npm run dev:chrome to start the development server for chrome extension
  • npm run dev:firefox to start the development server for firefox addon
  • npm run dev:opera to start the development server for opera extension
  • npm run build:chrome to build chrome extension
  • npm run build:firefox to build firefox addon
  • npm run build:opera to build opera extension
  • npm run build builds and packs extensions all at once to extension/ directory

Development

  • npm install to install dependencies.

  • To watch file changes in development

    • Chrome
      • npm run dev:chrome
    • Firefox
      • npm run dev:firefox
    • Opera
      • npm run dev:opera
  • Load extension in browser

  • Chrome

    • Go to the browser address bar and type chrome://extensions
    • Check the Developer Mode button to enable it.
    • Click on the Load Unpacked Extension… button.
    • Select your browsers folder in extension/.
  • Firefox

    • Load the Add-on via about:debugging as temporary Add-on.
    • Choose the manifest.json file in the extracted directory
  • Opera

    • Load the extension via opera:extensions
    • Check the Developer Mode and load as unpacked from extension’s extracted directory.

Production

  • npm run build builds the extension for all the browsers to extension/BROWSER directory respectively.

Note: By default the manifest.json is set with version 0.0.0. The webpack loader will update the version in the build with that of the package.json version. In order to release a new version, update version in package.json and run script.

If you don't want to use package.json version, you can disable the option here.

Generating browser specific manifest.json

Update source/manifest.json file with browser vendor prefixed manifest keys

{
  "__chrome__name": "SuperChrome",
  "__firefox__name": "SuperFox",
  "__edge__name": "SuperEdge",
  "__opera__name": "SuperOpera"
}

if the vendor is chrome this compiles to:

{
  "name": "SuperChrome",
}

Add keys to multiple vendors by separating them with | in the prefix

{
  __chrome|opera__name: "SuperBlink"
}

if the vendor is chrome or opera, this compiles to:

{
  "name": "SuperBlink"
}

See the original README of wext-manifest-loader package for more details

Bugs

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

Linting & TypeScript Config

License

MIT © Abhijith Vijayan