简体   繁体   English

使用 chrome.tabs 与 browser.tabs 来实现浏览器兼容性

[英]Using chrome.tabs vs browser.tabs for browser compatibility

I'm porting my Chrome extension to Firefox.我正在将我的 Chrome 扩展程序移植到 Firefox。

According to MDN there is a browser.tabs API which should be supported by chrome.根据 MDN,chrome 应该支持一个browser.tabs API。

However browser is not an object in Chrome stable.但是browser不是 Chrome 稳定版中的对象。 At the same time chrome.tabs works just fine in Firefox.同时chrome.tabs在 Firefox 中工作得很好。

Is it safe to replace browser with chrome when reading the MDN docs?阅读 MDN 文档时,用chrome替换browser是否安全? What is the reason for the docs to write browser ?文档写browser的原因是什么? Is there are planned change coming?有计划的改变吗?

Note: In the last couple days, many of the MDN JavaScript API pages have been changed from using chrome.* to using browser.* and describing the promise which is provided by the browser.* interface.注意:最近几天,许多 MDN JavaScript API 页面已从使用chrome.*更改为使用browser.*并描述了browser.*接口提供的承诺。 Those changes make the pages not mention using the API as chrome.* at all.这些更改使页面根本没有提及使用 API 作为chrome.* The changes are being made by the same person within the project with whom I had discussed the change away from browser.* to chrome.* .这些更改是由项目中的同一个人进行的,我曾与他讨论过从browser.*chrome.*的更改。 I do not yet know what is going on.我还不知道发生了什么。 I have sent him email asking the reason for those changes.我已经给他发了电子邮件,询问这些变化的原因。 As of this edit, 2016-11-02, it is too soon to expect a response from him.截至本次编辑,2016-11-02,现在期待他的回应还为时过早。


I am not involved with working on the API.我不参与 API 的工作。 But, I have made, and will make, some changes to the API documentation specifically in respect to WebExtensions chrome.* vs. browser.* (all pages were changed to show chrome.* as of 2016-09-22).但是,我已经并且将要对 API 文档进行一些更改,特别是关于 WebExtensions chrome.*browser.* (截至 2016 年 9 月 22 日,所有页面都已更改为显示chrome.* )。 I got involved because I, also, found having most of the WebExtensions API pages show browser.* confusing.我参与其中是因为我也发现大多数 WebExtensions API 页面显示browser.*令人困惑。 I wanted to know what the difference was between chrome.* and browser.* .我想知道chrome.*browser.*之间有什么区别。 Once I found out, I wanted to make it less confusing for others.一旦我发现,我想让其他人不那么困惑。

The WebExtensions browser.* API returns a promise : WebExtensions browser.* API 返回一个 promise

The browser.* object implements a version of the API that returns a promise if you omit providing a callback function when you call the API. browser.*对象实现了一个API版本, 如果您在调用 API 时省略提供回调函数,该版本将返回一个承诺 The intent is that the actual functionality is the same for both chrome.* and browser.* .目的是使chrome.*browser.*的实际功能相同。 If you provide a callback function to the browser.* API, it functions identically to the chrome.* API.如果您向browser.* API 提供回调函数,则其功能与chrome.* API 相同。

Is it safe to replace browser with chrome when reading the MDN docs?阅读 MDN 文档时,用chrome替换browser是否安全?

Yes, unless the documentation is specifically discussing promises.是的,除非文档专门讨论承诺。 The only difference between the two is that the browser.* methods will return a promise if the callback function is not provided when you call the API.两者的唯一区别是,如果调用 API 时未提供回调函数,则browser.*方法将返回一个 promise。

What is the reason for the docs to write browser ?文档写browser的原因是什么?

I'm not sure as to why the docs were originally written with browser.* being so prominent.我不确定为什么文档最初是用browser.*编写的browser.*如此突出。 I assume that either it was a configuration choice that was made when the pages were generated, or the specifics of the difference between chrome.* and browser.* changed at some point early in implementing the WebExtensions API.我认为要么是在生成页面时做出的配置选择,要么是chrome.*browser.*之间差异的细节在实现 WebExtensions API 的早期发生了变化。

Is there a technical reason why chrome.* without a callback can't return a promise? chrome.*没有回调不能返回承诺是否有技术原因?

The only reason I have come up with so far is that using chrome.* to return promises would make it impossible to check for the presence of a mandatory callback function when performing parameter checking within the API.到目前为止,我想出的唯一原因是使用chrome.*返回承诺将使在 API 中执行参数检查时无法检查强制回调函数的存在。 With the browser.* namespace, the API can assume the caller is using the returned promise.使用browser.*命名空间,API 可以假设调用者正在使用返回的承诺。 Thus, with browser.* a promise can be returned rather than generating an error when a mandatory callback function is not provided in the call to the API.因此,当调用 API 时未提供强制回调函数时,使用browser.*可以返回承诺而不是生成错误。 This reason is, however, speculation on my part.然而,这个原因是我的推测。

Changing the documentation:更改文档:

I agree that having the API documentation primarily show browser.* in the Syntax section and elsewhere in each API page is confusing (eg in examples).我同意 API 文档主要在语法部分和每个 API 页面的其他地方显示browser.*令人困惑(例如在示例中)。 I have been discussing changing this across the entire API documentation with the person primarily in charge of the WebExtensions documentation for the last 3 weeks.在过去的 3 周里,我一直在与主要负责 WebExtensions 文档的人讨论在整个 API 文档中更改这一点。 He agrees that it should be changed.他同意应该改变它。

There are plans to make the change.有计划进行更改。 The documentation will change in at least two phases.文档将至少分两个阶段进行更改。 The first will be to go through all the WebExtensions API pages and change the Syntax section to say chrome.* .首先是浏览所有 WebExtensions API 页面并将语法部分更改为chrome.* A note at the end of the Syntax section will say that the "API is also available as browser.* in a version that returns a promise ."语法部分末尾的注释会说“API 也可作为browser.*返回 promise版本中使用。” In addition, all usage of browser.* throughout the page will be changed to chrome.* except those portions which are specifically showing the API being used as returning a promise.此外,整个页面中所有对browser.*都将更改为chrome.*除了那些专门显示 API 被用作返回承诺的部分。

The current expectation is that the page will look much like alarms.clear() does now.当前的期望是该页面看起来与现在的alarms.clear()非常相似。 I had changed that page to show what I was suggesting the change will be, including suggestions from the person in charge of the WebExtensions documentation.我已经更改了该页面以显示我建议的更改内容,包括负责 WebExtensions 文档的人员的建议。 The alarms.clear() Syntax section currently looks like: alarms.clear()语法部分目前看起来像:

使用新格式的 alarms.clear()

Initially (3 weeks ago, 2016-08-25), I had changed the API pages from alarms.clear() through browserAction.enable() with the primary change of moving the content from saying browser.* to chrome.* when my access to making changes on MDN was disabled due to their automatic SPAM filtering.最初(3 周前, alarms.clear() 8 月 25 日),我将 API 页面从alarms.clear()更改为browserAction.enable() ,主要更改是将内容从说browser.*chrome.*当我的由于其自动垃圾邮件过滤,因此无法访问对 MDN 进行更改。 This lead to the discussion of the format that the changes will take.这导致了对更改将采用的格式的讨论。 I paused making changes with the hope that making the initial changes could be done in one pass once the intermediate format for the Syntax box was chosen.我暂停了更改,希望一旦选择了语法框的中间格式,就可以一次性完成初始更改。 Due to real life, on both sides, the discussion has taken longer than I expected.由于现实生活,双方的讨论时间比我预期的要长。

It looks like the discussion about the exact format which will be used is going to broaden to include the dev-mdc mailing list due to the "correct" format for Syntax boxes not being clear in the Mozilla documentation for how to write an API method page .看起来关于将使用的确切格式的讨论将扩大到包括dev-mdc 邮件列表,因为语法框的“正确”格式在Mozilla 文档中不清楚如何编写 API 方法页面.

However, I believe I can move forward with making the change of showing chrome.* instead of browser.* .但是,我相信我可以继续更改显示chrome.*而不是browser.* Doing so will probably result in the pages having to be edited again to get the format to whatever is eventually decided as the official formatting.这样做可能会导致必须再次编辑页面才能将格式设置为最终决定为官方格式的任何内容。 Making the browser.* to chrome.* change prior to finalizing the Syntax block format will probably result in more overall work, but will reduce confusion while the final version of the format change is discussed.在最终确定语法块格式之前将browser.*更改为chrome.*可能会导致更多的整体工作,但在讨论格式更改的最终版本时会减少混淆。 While I have to make these changes on a page by page basis (there is an API for making bulk changes, but I do not have access), I have already implemented a script that makes the majority of the needed changes.虽然我必须逐页进行这些更改(有一个用于进行批量更改的 API,但我没有访问权限),但我已经实现了一个脚本,可以进行大部分所需的更改。 Thus, these initial ones should not take too long.因此,这些初始的不应花费太长时间。

Update:更新:
I have changed all of the WebExtensions JavaScript API pages to show chrome.* instead of browser.* .我已将所有 WebExtensions JavaScript API 页面更改为显示chrome.*而不是browser.* You may need to press Ctrl - F5 on each page to see the changes.您可能需要在每个页面上按Ctrl - F5才能查看更改。

Further changes, more clearly providing documentation on browser.* :进一步的变化,更清楚地提供关于browser.*文档browser.*
After the full changes to Syntax block formatting are made, there is an desire that there be additional changes, at a later time, that provide more examples on a per-API-page basis of the differences between using chrome.* and browser.* .在对语法块格式进行全面更改后,希望稍后进行其他更改,以便在每个 API 页面的基础上提供更多示例,说明使用chrome.*browser.*之间的差异browser.* . This is almost certainly a longer term project.这几乎可以肯定是一个长期项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM