DEV Community


Posted on


web-extension - cross-browser compatibility - Firefox, Chrome, Opera in one line

// this line make your web-extension compatible with Firefox, Chrome/Chromium and Opera 
const nav = chrome?chrome:browser;

// you can now use the nav namespace object and use callback functions, for examle :

// I use this peace of code in the FocusJUMP extension, Test it and say me what you think about :)

Top comments (1)

fjones profile image
FJones • Edited

Even shorter, thanks to short-circuiting, is const nav = chrome || browser; which also allows for more legible chaining past two items - such as falling back to a polyfill or nullobject.

Particularly useful if certain functions may require optional permissions.