DEV Community

loop77
loop77

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 :
// nav.storage.local.remove(name);

// I use this peace of code in the FocusJUMP extension, Test it and say me what you think about :)
// https://sebastienaubry.pagesperso-orange.fr/focusjump/

Discussion (1)

Collapse
fjones profile image
FJones • Edited on

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.