Firefox / Chrome / MS Edge extensions using chrome.* or browser.*

旧城冷巷雨未停 提交于 2019-12-12 16:15:57

问题


So I couldn't find anything that talked about using chrome.* or browser.* specifically. In some of the WebExtension examples it uses browser.* (browser.runtime.getManifest();) https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/runtime/getManifest, and in others it uses chrome.* (chrome.notifications.create), https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/notifications.

I'm not entirely sure what's the difference. Is it contextual? Both chrome.* and browser.* are available in my content script and in the background script in Firefox. I looked at IEs docs as well and they use browser.* ( didn't see chrome.* in their docs)

I'd like to know what the difference is between and do Chrome extensions only use chrome.* or does it have browser.* as well (does IE only have browser.*)?


回答1:


Chrome only has chrome.apis. Edge only has browser.apis. Firefox has both browser.apis and chrome.apis for compatibility with existing Chrome extensions.

The main difference is that in Firefox the browser.apis use promises but the chrome.apis use callbacks.




回答2:


Both browser and chrome APIs work in Edge, but make sure you don't mix and match. Use all of one or the other.




回答3:


I think your best solution for now is to use callbacks instead of promises as they work for chrome, firefox and edge. In addition you can use something like browser = browser || chrome; to solve the chrome vs browser issue and browser.runtime.lastError; for error handling. Firefox support both the callback and the promise version of the api, callback version will work for both the chrome and browser objects.



来源:https://stackoverflow.com/questions/40776942/firefox-chrome-ms-edge-extensions-using-chrome-or-browser

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