Search code examples
javascriptgoogle-chrome-extensionfirefox-addon-webextensions

Promise support for Chrome Extensions API?


I've been writing some browser extensions in the last few weeks and until today I thought that a WebExtension for Firefox should work pretty much automatically in Chrome. So I tried to write my code according to Mozilla's examples.
But today I realized that there is no mention of Promises in the API documentation for Chrome Extensions.
I have strictly used Promises throughout the code for all my Extensions.

So now my question is, will my code work in Chrome? Or would it work if I add a var browser = chrome declaration at the very top?
Or does Chrome not support Promises on the API at all?
If Chrome doesn't support Promises on the API functions yet, will it support them in the future?

Note, I am aware of this project: https://github.com/mozilla/webextension-polyfill
But I'm not willing to go through the hassle of including that library everywhere. Also, it has annoying bugs in it.

And besides that I don't have Chrome or Chromium and I can't install them for privacy and security reasons.

Edit: They're finally starting to implement support for promises.


Solution

  • Official promise support is finally coming and has already been implemented on some of the APIs. At the time of writing:

    On GoogleChrome's Github you can already find a Hello world example using the chrome.tabs API with Promises (await):

    chrome.runtime.onInstalled.addListener(async () => {
      let url = chrome.runtime.getURL("hello.html");
      let tab = await chrome.tabs.create({ url });
      console.log(`Created tab ${tab.id}`);
    });
    

    As you can see, the promise support has simply been implemented on the chrome object, which now supports both Promises and callbacks. Firefox still uses the browser object for the promise API.