创建跨浏览器 Gmail 扩展程序的最佳方式是什么?

What is the best way to create a cross browser Gmail extension?

我想创建一个适用于 Chrome 和 Firefox 的 Gmail 扩展程序。最好的方法是什么?

只需使用 chrome.* 进行扩展 API 调用即可编写适用于两者的代码。

在很大程度上,Chrome 和 Firefox(使用 WebExtensions) are directly code compatible. As long as you are using APIs which are supported in both browsers (Chrome, Firefox),您可以在两个浏览器中使用 chrome.* 命名空间进行 API 调用。许多扩展将直接代码兼容。

但是,存在一些功能上的不兼容性。查找相关信息的最佳位置是每个 API 的 Mozilla 开发者网络 (MDN) 页面。如果您发现不兼容,您可以将它们写下来并提交 PR 到 browser compatibility JSON file maintained by Mozilla, or directly modify the appropriate MDN API page (e.g. issues where developers need to see something is actually broken). For some things, you will need to 并为每个执行稍微不同的代码。然而,对于大多数事情,这些将是运行时选择。您不需要两套不同的代码。

Chrome:您应该先阅读 Chrome extension overview (perhaps along with the pages linked from the overview). The architecture section has overall architecture information which should help your understanding of how things are generally organized/done. You will should also read Content Scripts

Firefox: 你应该从阅读开始 Anatomy of a WebExtension page (perhaps work through reading the pages linked from there). It has overall architecture information which should help your understanding of how things are generally organized/done. Here, also, reading about content scripts 会非常有益。

备注:

  1. Firefox 使用 browser.*(基于 Promise)和 chrome.*(基于回调,如 Chrome)原生支持其 API。如果你想在 Chrome 中进行基于 API 的调用,Mozilla 有一个 browser.* pollyfillchrome.* 命名空间支持回调的事实没有很好的记录(它是,但 Mozilla 选择更改文档以仅在几个地方提及它)。因此,您会发现他们所有的 API 页面都显示了 browser.* 命名空间和 Promises。不要被吓倒。支持 chrome.* 命名空间,特别是使用 Chrome 进行跨浏览器扩展(特别是从 Chrome 到 Firefox 的移植)更容易。

  2. 每个浏览器都有 API 不被其他浏览器支持。 Mozilla 仍然非常积极地开发 WebExtensions APIs。在很大程度上,这是在实施 Chrome 中已经可用的 APIs。在某种程度上,这提供了一些在其他类型的 Firefox 附加组件中可用的附加功能。但是,这始终是 非常有限 的功能子集,are/were 可用于其他类型的 Firefox 附加组件。如果您不知道,从 Firefox 57 (2017-11-14) 的发布版本开始,Firefox 将禁用所有类型的扩展,除了 WebExtensions

特别是 Gmail

你需要什么很大程度上取决于你真正想做什么。有问题How to develop Chrome extension for Gmail? which provides some information. You should also investigate the Gmail API. There are also a large number of questions/answers related to making a Gmail Google Chrome extension.