TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Ask HN: Browser-extension creators, how do you write for multiple browsers?

90 pointsby falafeliteover 3 years ago
How do you avoid repeating code for, say, both a Firefox and Chrome extension? Chrome recently mandated that only manifest V3 extensions can be submit to their online store, and that introduces quite the number of breaking changes from V2 (even with the polyfill package). As far as I can tell, Firefox does not yet support V3 (I could be quite mistaken there).<p>As someone new to writing extensions and trying to write something for at least both Firefox and Chrome, how should I go about it? Or do I have to accept writing separate background.js files, separate manifest.json files, etc.?

19 comments

bambaxover 3 years ago
I could be very wrong about this, and yes, it&#x27;s also mostly wishful thinking, but my guess is Chrome will have to walk back V3 at some point. The reasoning is that V3 makes uBlock Origin impossible, and Chrome without uBlock (or any browser really) is unusable.<p>So maybe you can just wait? Although it may take a long time.<p>Also, FF said in May of last year (2021) that they would start accepting V3 extensions in &quot;early 2022&quot; [0] but I don&#x27;t know if they have kept their schedule.<p>[0] <a href="https:&#x2F;&#x2F;blog.mozilla.org&#x2F;addons&#x2F;2021&#x2F;05&#x2F;27&#x2F;manifest-v3-update&#x2F;" rel="nofollow">https:&#x2F;&#x2F;blog.mozilla.org&#x2F;addons&#x2F;2021&#x2F;05&#x2F;27&#x2F;manifest-v3-updat...</a>
评论 #30314764 未加载
评论 #30316822 未加载
评论 #30314475 未加载
评论 #30314771 未加载
评论 #30315513 未加载
klntskyover 3 years ago
I used WebExtension polyfill[0] when adapting my FF addon to Chrome and admittedly all the intricate differences between APIs still costed me half a day of work.<p>I managed to have it done with only a few places where I branch on navigator.vendor, but If I wanted to ship different versions to AMO and CWS, I&#x27;d make use of something like DefinePlugin[1] for webpack to include&#x2F;exclude code based on build target.<p>[0] <a href="https:&#x2F;&#x2F;github.com&#x2F;mozilla&#x2F;webextension-polyfill&#x2F;" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;mozilla&#x2F;webextension-polyfill&#x2F;</a><p>[1] <a href="https:&#x2F;&#x2F;github.com&#x2F;webpack&#x2F;docs&#x2F;wiki&#x2F;list-of-plugins#defineplugin" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;webpack&#x2F;docs&#x2F;wiki&#x2F;list-of-plugins#definep...</a>
评论 #30314581 未加载
bunya017over 3 years ago
If you&#x27;re familiar with Vue js, you should consider using Quasar[0]. It&#x27;ll handle the compatability across browsers while you work on the extension.<p>[0] <a href="https:&#x2F;&#x2F;quasar.dev&#x2F;quasar-cli&#x2F;developing-browser-extensions&#x2F;introduction" rel="nofollow">https:&#x2F;&#x2F;quasar.dev&#x2F;quasar-cli&#x2F;developing-browser-extensions&#x2F;...</a>
评论 #30314780 未加载
评论 #30314571 未加载
gnicholasover 3 years ago
My company&#x27;s main products are browser extensions. At first, our Chrome extension was the most popular, but I anticipate with the upcoming changes we may say goodbye to Chrome and just be on Firefox and Safari, depending on how they deal with V3 issue. We simply don&#x27;t make enough money from our Chrome extension to deal with all of their big changes. Last year they ditched the Webstore Payments, last summer they migrated Google Docs (one of our most popular use cases) from DOM to canvas-based rendering, and now this. We can&#x27;t keep rewriting major parts of our codebase at the whim of big G.
评论 #30316471 未加载
isodevover 3 years ago
I usually keep a common source folder for all browsers. This includes a common manifest (V2, still experimenting with V3). At build time a script generates a browser specific flavour of the manifest and copies all resources into a target folder (e.g &#x2F;dist-chrome, &#x2F;dist-safari, &#x2F;dist-firefox) to create a bundle per browser and apply browser specific tweaks (like copying resources into an Xcode project for Safari etc).
评论 #30315958 未加载
评论 #30314681 未加载
azeirahover 3 years ago
Unless something changed in the past 3 weeks, you can still upload manifest V2 plugins to chrome, I uploaded one a few weeks ago.<p>Otherwise, what I did to support v2 for firefox and v3 for chrome is fairly straightforward.<p>You write your code and compile it all with webpack into a single file, all you have to do is write two manifest files which are 70% the same.<p>There are some incompatibilities between v2 and v3, like v3 having promises for the extension api. I wrote wrappers for the non-promise versions myself (I&#x27;m sure there are packages that do this for you). Webpack takes care of a lot of the other inconsistencies like module support.<p>Also note that you should 100% make sure that you follow the guidelines for how to structure a plugin for the v3 version, ie that it is event based. Your background scripts will be shut down randomly every now and then and you will get insanely buggy code when running in v3 if you don&#x27;t follow the guidelines strictly.<p>For now, I just ship v2 to both firefox and chrome, it&#x27;s easiest for me. Note though I have had this plugin for a couple years now, maybe it&#x27;s not possible to create _new_ plugins with v2.
评论 #30314415 未加载
dotprotoover 3 years ago
Hey, Simeon from the Chrome Extensions team here. A lot of other replies talk about libraries and tools; I&#x27;d like to focus more on some general guidance.<p>First up, no, you don&#x27;t need to maintain two completely different code bases. I&#x27;d recommend having a separate manifest.json for each browser or having a build step generate the per-browser manifest.json. A lot of the manifest changes are relatively small, like splitting &quot;host_permissions&quot; out from &quot;permissions&quot; and should be automatable. For example, a build step would merge these two arrays for the MV2 manifest.<p>As for your background scripts, if possible I&#x27;d recommend limiting the JS capabilities you use to the intersection of what all browsers support.<p>- Chrome supports background service workers - Safari supports event pages and I believe has service worker support in beta builds of their OSs - Firefox supports persistent background pages<p>In other words, target the capabilities available in both worker and page contexts. You&#x27;ll also want to design your background logic to embrace ephemerality. This is required by Chrome, but it also benefits Firefox users in that it helps keep the browser responsive.<p>Best of luck!
评论 #30373721 未加载
Glenchover 3 years ago
I made ExtensionPay[1] (a service to help you take payments in extensions) and I used Mozilla&#x27;s `browser` shim in the extension library itself. MV3 is not available on Firefox yet so I support both MV2 and MV3, but yeah it&#x27;s a pain in the butt to have one MV2 version and one MV3 version.<p>[1] <a href="https:&#x2F;&#x2F;extensionpay.com" rel="nofollow">https:&#x2F;&#x2F;extensionpay.com</a>
L1quidover 3 years ago
For our browser extension, The Camelizer, I wrote Spader: <a href="https:&#x2F;&#x2F;github.com&#x2F;cosmic-shovel&#x2F;spader" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;cosmic-shovel&#x2F;spader</a><p>It lets me use Ruby in my html&#x2F;css&#x2F;js files, and also sass in the css, letting us use one codebase for all target browsers.<p>Definitely a work in progress, and lacks the exciting auto-reload functionality of other similar projects, but it works for us. Issuing one build command and having an extension for all our target browsers is pretty nice.
onassarover 3 years ago
I ended up writing a build script that makes the required changes for chrome&#x2F;ff extension.<p>This will become harder when mv3 gets pushed out broadly, but it&#x27;s def not pretty :(
deweyover 3 years ago
I just wrote my first extensions for a new side project (<a href="https:&#x2F;&#x2F;getbirdfeeder.com" rel="nofollow">https:&#x2F;&#x2F;getbirdfeeder.com</a>) a few weeks ago. Initially I thought that now after a few years everything will be mostly standardized with &quot;web extensions&quot; and I just upload the extension to the three big extension stores. Turns out it&#x27;s nothing like that and I wrote a little bit about my misconceptions in my blog [1]. It&#x27;s still not a great development workflow. If there&#x27;s any good strategies I&#x27;ll gladly give that a try.<p>My initial plan to have a private package that I just import in my extensions but turns out you can&#x27;t really do that as imports don&#x27;t work. Right now I&#x27;m kinda stuck in copy pasting code back and fort hand keeping a V2 Extension for Firefox (and Safari) and a V3 Extension for Chrome. I also keep separate manifest files as they have different features.<p>[1] <a href="https:&#x2F;&#x2F;blog.notmyhostna.me&#x2F;posts&#x2F;building-web-extensions-for-chrome-firefox-and-safari&#x2F;" rel="nofollow">https:&#x2F;&#x2F;blog.notmyhostna.me&#x2F;posts&#x2F;building-web-extensions-fo...</a>
porkbrainover 3 years ago
I worked with extensions for roughly 4 years on a handful of separate projects. I had managed a framework which abstracted over all APIs (including the insane old Safari one).<p>While I can&#x27;t share the framework, I have designed a code base architecture which allows incremental abstraction of APIs, uses typed message passing between scripts (with errors being propagated too), unit and integration tests or automation for compilation of the extension for each store according to their respective rules. This architecture is my IP and although I don&#x27;t actively work with extensions on day to day basis anymore, I still do take on contracts to lay foundations or migrate existing projects.<p>If this is something you&#x27;re interested in, reach out to me via email in my bio.
BugWatchover 3 years ago
Could anyone suggest a good resources&#x2F;guides&#x2F;tutorials&#x2F;books for writing Firefox add-ons?<p>I remember making a cursory search a year ago, and hardly found anything remarkable. (Might have been a case of Google&#x27;s-bad-search-results syndrome.)
评论 #30319809 未加载
评论 #30321400 未加载
kiru_ioover 3 years ago
Since this thread is full of Browser-extension creator. I have a related question: How do you make money with Browser-Extension (without selling data)?
评论 #30316316 未加载
Bilal_ioover 3 years ago
&gt; Chrome recently mandated that only manifest V3 extensions can be submit to their online store<p>Can someone provide a source for this please?<p>My company has an extension with the old manifest V1 and I&#x27;d like to propose upgrading or getting rid of the extension all together.
评论 #30316786 未加载
zerrover 3 years ago
Also interesting, how do you monetize? (Besides acquiring a lot of users and selling out)
gnicholasover 3 years ago
Does anyone know how Brave will handle manifest V3?
评论 #30329652 未加载
Animatsover 3 years ago
I gave up and deleted my plug-in for Chrome.
nextlevelwizardover 3 years ago
I just don&#x27;t support Chrome.