CRXJS Chrome Extension Tools

repository·main·Indexed 26 days ago

https://github.com/crxjs/chrome-extension-tools

A toolset for building cross-browser extensions with Vite and Rollup. It features true Hot Module Replacement (HMR) for content scripts, zero-configuration setup, and Manifest V3 support via @crxjs/vite-plugin. For Manifest V2 support, it provides rollup-plugin-chrome-extension, which simplifies bundling by using manifest.json as the primary input and offers features like browser polyfills and auto-reloading.

Tokens
15.6K
Snippets
50
Records
111
Agent score
88%

What's inside CRXJS

  1. Overview of CRXJS features

    main

    CRXJS is a toolset for building cross-browser extensions with native Hot Module Replacement (HMR) and zero-config setup. Key features include:

    • Full Vite Plugin Ecosystem: Use any Vite-compatible plugins without extra setup.
    • Zero Configuration: Intelligent defaults for immediate development.
    • Manifest V3 Support: Built for modern Chrome extensions.
    • True Hot Module Replacement (HMR): Instant UI updates that preserve extension state, including support for content scripts.
    • Static Asset Import: Direct referencing of images and fonts in your code.
    • Auto Web-Accessible Resources: Automatic generation of web_accessible_resources manifest entries.
  2. Overview of rollup-plugin-chrome-extension

    main
    The rollup-plugin-chrome-extension (RPCE) is a bundler configuration tool designed to simplify the complexity of building Chrome Extensions. It uses manifest.json as the primary input; every file referenced in the manifest is automatically bundled or copied to the output directory. It supports features like Hot Module Replacement (HMR) within the Chrome Extension environment.
  3. Quick Start with Vue 3 + Vite + CRXJS template

    main

    This template provides a pre-configured environment for developing Chrome extensions using Vue 3, TypeScript, and Vite, integrated with the CRXJS Vite plugin.

    To get started, follow these steps:

    1. Install dependencies using npm.
    2. Run the development server for live reloading.
    3. Build the project for production deployment.
    npm install
    npm run dev
    npm run build
  4. Access images from content scripts

    main

    Content scripts share the origin of the host page. If you attempt to use a relative path like logo.svg, the browser will attempt to load it from the host page's origin (e.g., https://google.com/logo.svg) instead of your extension's directory.

    To correctly load images from your extension within a content script, follow these two steps:

    1. Declare the images in manifest.json: You must add the image paths to the web_accessible_resources field so the browser allows the host page to access them.
    2. Use chrome.runtime.getURL: Reference the image using the chrome.runtime.getURL method to resolve the correct extension-internal URL.
  5. Handle static assets in content scripts

    main

    CRXJS supports importing static assets directly into content scripts. When you import an asset, CRXJS automatically declares it as a web_accessible_resources dependency in the manifest.

    Because content scripts share the origin of the host page, you must convert imported asset paths to the extension origin using chrome.runtime.getURL to ensure they load correctly.

    import logo from './logo.png'
    const url = chrome.runtime.getURL(logo)
  6. Implement a React (Legacy) content script

    main

    For projects using React versions prior to 18, use ReactDOM.render after creating and appending a root element to the document body.

    import React from 'react'
    import ReactDOM from 'react-dom'
    import './index.css'
    import App from './App'
    
    // Create the root element and append it to the DOM
    const root = document.createElement('div')
    root.id = 'crx-root'
    document.body.append(root)
    
    ReactDOM.render(
      <React.StrictMode>
        <App />
      </React.StrictMode>,
      root,
    )
  7. Inspect the popup window for HMR

    main

    To leverage Vite's Hot Module Replacement (HMR) while developing a popup, you must prevent the popup from closing automatically.

    1. Locate the extension icon in your browser.
    2. Right-click the icon.
    3. Select "Inspect popup window".

    This opens the popup alongside its dedicated DevTools window, allowing the popup to remain open and visible while you apply code changes.