Promise support for Chrome Extensions API?

荒凉一梦 提交于 2019-11-30 02:42:29

问题


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.


回答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.




回答2:


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));

Edit: the author of chrome-promise is no longer maintaining it, other than fixing bugs. They list some alternative libraries here, including the Mozilla polyfill rejected by the OP.




回答3:


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



来源:https://stackoverflow.com/questions/42191030/promise-support-for-chrome-extensions-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!