Skip to content

feat: add Firefox support with webextension-polyfill#24

Closed
roshankumar0036singh wants to merge 2 commits intobrowseping:mainfrom
roshankumar0036singh:feat/firefox-support
Closed

feat: add Firefox support with webextension-polyfill#24
roshankumar0036singh wants to merge 2 commits intobrowseping:mainfrom
roshankumar0036singh:feat/firefox-support

Conversation

@roshankumar0036singh
Copy link

@roshankumar0036singh roshankumar0036singh commented Jan 13, 2026

Add Firefox Support with webextension-polyfill

closes #10

Summary

This PR adds Firefox support to the BrowsePing browser extension while maintaining full compatibility with Chromium-based browsers (Chrome, Edge, etc.). The implementation uses webextension-polyfill to provide cross-browser API compatibility without requiring any changes to the existing codebase.

Changes Made

Dependencies

  • webextension-polyfill: Cross-browser API compatibility layer
  • @types/webextension-polyfill: TypeScript type definitions
  • web-ext: Mozilla's official CLI tool for Firefox extension development
  • cross-env: Cross-platform environment variable support
  • @types/node: Node.js type definitions for TypeScript

Build System

webpack.config.js

  • Added webextension-polyfill to entry points (loads before application code)
  • Implemented manifest transformation logic for Firefox-specific fields
  • Automatically adds browser_specific_settings when TARGET_BROWSER=firefox

package.json

New build scripts:

  • build:firefox - Build extension for Firefox
  • build:chrome - Build extension for Chrome/Edge
  • start:firefox - Launch Firefox with extension for testing

Configuration

tsconfig.json

  • Updated to support Node.js types for better TypeScript compatibility

Key Features

✅ Zero Code Changes Required

  • All existing chrome.* API calls work in both browsers
  • webextension-polyfill provides automatic Promise-based wrappers
  • No refactoring needed in service files or components

✅ Browser-Specific Builds

  • Firefox: Includes browser_specific_settings with gecko ID and minimum version (109.0)
  • Chrome: Standard manifest without Firefox-specific fields
  • Automatic manifest transformation based on TARGET_BROWSER environment variable

✅ Manifest V3 Support

  • Both browsers use Manifest V3
  • Firefox 109+ supports MV3 service workers
  • Future-proof implementation

Testing

Build Verification

Both builds tested and working:

npm run build:firefox  # ✅ Success
npm run build:chrome   # ✅ Success

Firefox Manifest Output

{
  "manifest_version": 3,
  "name": "BrowsePing",
  "browser_specific_settings": {
    "gecko": {
      "id": "browseping@example.com",
      "strict_min_version": "109.0"
    }
  }
}

Chrome Manifest Output

{
  "manifest_version": 3,
  "name": "BrowsePing"
  // No browser_specific_settings (correct)
}

How to Test

Firefox Testing

npm run build:firefox
npm run start:firefox

This launches a temporary Firefox instance with the extension loaded.

Deployment Notes

Before Publishing to Firefox Add-ons

Update the gecko ID in webpack.config.js:

gecko: {
  id: 'your-actual-extension-id@example.com',  // Update this
  strict_min_version: '109.0'
}

Chrome Web Store

No changes needed - continue using npm run build:chrome

Technical Implementation

Cross-Browser API Strategy

The webextension-polyfill is bundled as the first entry point in webpack, ensuring it loads before any extension code. This provides:

  • Promise-based API wrappers for chrome.* namespace
  • Consistent behavior across browsers
  • Automatic handling of browser-specific quirks

Build Process Flow

  1. Set TARGET_BROWSER environment variable
  2. Webpack bundles code with polyfill
  3. CopyWebpackPlugin transforms manifest based on target
  4. Output to dist/ directory

Breaking Changes

None - fully backward compatible with existing Chrome builds.

Files Changed

  • package.json - Added dependencies and build scripts
  • package-lock.json - Dependency lock file updates
  • webpack.config.js - Entry points and manifest transformation
  • tsconfig.json - TypeScript configuration updates
  • src/background/index.ts - Minor formatting (no functional changes)
Screenshot 2026-01-13 223223

- Install webextension-polyfill for cross-browser API compatibility
- Add browser-specific build scripts (build:firefox, build:chrome)
- Configure webpack to transform manifest.json for Firefox
- Add browser_specific_settings for Firefox (gecko ID and min version)
- Include web-ext for Firefox testing (start:firefox script)
- No code changes required - existing chrome.* APIs work via polyfill
@roshankumar0036singh
Copy link
Author

@akash-kumar-dev plz review the pr

@akash-kumar-dev
Copy link
Member

Hi @roshankumar0036singh, have you tested the extension in Firefox? I'm encountering issues during installation, needing manifest.json modifications, and afterward, I'm seeing icon and styling problems. Could you please test it on your Firefox browser?

@roshankumar0036singh
Copy link
Author

roshankumar0036singh commented Jan 15, 2026

@akash-kumar-dev its working fine in firefox just there was service workerk issue as mv3 for firefox is strict for service worker so provided a fallback to background script for it
image

…fox builds

- Firefox MV3 support for service_worker can be strict/problematic in some contexts
- Fallback to background.scripts (Event Pages) for Firefox targets via webpack manifest transform
- Resolves 'background.service_worker is currently disabled' error
@roshankumar0036singh
Copy link
Author

@akash-kumar-dev plz review it

@roshankumar0036singh
Copy link
Author

@akash-kumar-dev is there any problem

if (manifest.background && manifest.background.service_worker) {
manifest.background.scripts = [manifest.background.service_worker];
delete manifest.background.service_worker;
delete manifest.background.type;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After this change, will the background service worker function correctly in Firefox? Have you tested the service worker and WebSocket functionality in Firefox?

@roshankumar0036singh
Copy link
Author

@akash-kumar-dev yes i tested that the photo i shared was after adding it it worked fine in my browser plz check once

@roshankumar0036singh
Copy link
Author

@akash-kumar-dev please check once

@akash-kumar-dev
Copy link
Member

I've tested locally and found that the background service isn't starting, which prevents establishing WebSocket connections. Please don't attempt to solve this with AI-generated code. This codebase is carefully managed, and introducing AI-generated solutions could lead to runtime issues. If you're unfamiliar with background services, please refrain from reviewing this PR. We cannot directly merge AI-generated code.

@roshankumar0036singh
Copy link
Author

@akash-kumar-dev Firefox still uses background.scripts, while Chrome uses background.service_worker and to support both browsers, you may need separate manifests or tools like templating.firefox still doesnt support service worker

@roshankumar0036singh
Copy link
Author

@akash-kumar-dev during dev i refffered to this https://stackoverflow.com/questions/75043889/manifest-v3-background-scripts-service-worker-on-firefox from where i got the idea to provide the fallback mechnaism for background scripts to use

@roshankumar0036singh
Copy link
Author

@akash-kumar-dev how do you expect the service worker to be working s firefox doesnt support service worker they are still using scripts we may need two manifest file and switch them check this pr jsnjack/surfly-protocol@82df547

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make Extension Compatible with Firefox (Cross-Browser Support)

2 participants

Comments