Step 1: Project Setup
Start with a basic browser extension structure:Step 2: Create a Temporary Bundling Directory
Create a temporary directory to install Mellowtel and generate the bundles:Step 3: Bundle Mellowtel with esbuild
From inside themellowtel-bundling
directory, create a single bundle that works in both service workers and content scripts:
- Takes the Mellowtel source from
node_modules/mellowtel/dist/index.js
- Bundles all dependencies into a single file
- Uses IIFE format with a global name for universal compatibility
- Adds a footer to make the default export available globally
- Optimizes for browser environment and minifies the output
- Creates
mellowtel.js
in the bundling directory
Step 4: Copy the Bundle to Your Extension
After running the esbuild command, you’ll have one file in themellowtel-bundling
directory:
mellowtel.js
- For use in both service workers and content scripts
Step 5: Configure Your Manifest
Configure yourmanifest.json
to include the Mellowtel bundle in content scripts:
- The
"type": "module"
field enables ES module imports in your service worker. - In the content scripts array,
mellowtel.js
must be loaded beforecontent_script.js
so that the Mellowtel global variable is available when your content script runs.
Step 6: Use Mellowtel in Your Extension
For your service worker (background.js), import the bundled file:Step 7: Testing Your Extension
To test your extension:- Go to
chrome://extensions/
in Chrome (or equivalent in other browsers) - Enable “Developer mode”
- Click “Load unpacked” and select your extension folder