Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 755
Forivin Avatar asked Feb 12 '17 17:02

Forivin


People also ask

Is a Chrome extension an API?

The chrome. extension API has utilities that can be used by any extension page. It includes support for exchanging messages between an extension and its content scripts or between extensions, as described in detail in Message Passing.

Can chrome extensions communicate with each other?

In addition to sending messages between different components in your extension, you can use the messaging API to communicate with other extensions.

How do I communicate with chrome extensions?

Chrome has documentation on sending messages from webpages to chrome extensions. You add an “externally_connectable” object to your manifest. This will specify which webpages you want to allow to communicate with your extension (in this case, localhost, since I was developing on my machine).


2 Answers

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

  • browserAction
  • cookies
  • action
  • windows
  • storage
  • tabs
  • management

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.

like image 104
Forivin Avatar answered Sep 19 '22 00:09

Forivin


...until today I thought that a WebExtension for Firefox should work pretty much automatically in Chrome.

WebExtensions were created with backward compatibility with Chrome extensions in mind. chrome.* namespace is available for supported APIs. The goal here is to ease porting existing extensions to FF to quickly bootstrap the ecosystem.

However, Mozilla disregards forward compatibility with browser.* namespace. Mozilla decided to go with a promise-based approach for the API, but only for that new namespace.

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?

No; they behave differently and have different signatures. Chrome will reject calls without required explicit callbacks. browser.* variants will emit a Promise instead.

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?

As mentioned in comments, Promise-based rewrite of the API is considered by Chrome, and it has been implemented, or started to be implemented, for MV3 extensions. However, there exist polyfills, including the one you mentioned. Other than wrapping methods yourself to create your own polyfill, there's no other solution.

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

Then you can't properly test your ports anyway; that's not a good approach for your potential users. You're better off not porting in this case.

like image 44
Xan Avatar answered Sep 19 '22 00:09

Xan