Promise Support For Chrome Extensions Api?
Solution 1:
...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, but no visible work has been done. 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.
Solution 2:
Official promise support is finally coming and has already been implemented on some of the APIs. At the time of writing:
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.
Solution 3:
I haven't looked at the browser
API, so I may be off-base, but if the only difference is that the Firefox APIs return promises instead of using callbacks, the chrome-promise library might be able to help. It wraps all the API calls that require callbacks in functions that return promises instead.
You could then do something like this in Chrome:
var browser = new ChromePromise();
And then make promise calls:
browser.storage.local.get(null).then(data => console.log(data));
If you define the browser
variable as a global and include that script before any others, you can just use it as a global and not have to include the polyfill in every file where you want to use it.
Edit: the author of chrome-promise
is no longer maintaining it, other than fixing bugs. They list some alternative libraries here, including the Mozilla webextension-polyfill rejected by the OP. That library's readme explains how to make it available as a global variable in the various extension contexts where it's needed.
Solution 4:
I created this library https://github.com/lawlietmester/webextension to make this without one general rule like in webextension-polyfill.
My library is crossbrowser way to make one Browser
object without modification of original chrome/browser. Like jQuery in old time.
To use it only one time - import it in background process and make it global for background, then for other imports (from popup for example) use import from
( typeof browser === 'undefined' ? chrome : browser ).extension.getBackgroundPage().Browser
Post a Comment for "Promise Support For Chrome Extensions Api?"