API
WebExtension JavaScript API 可以在附加组件的后台脚本和附加组件定义的任何浏览器动作或页面动作中使用。这里的部分API也可以通过附加组件的内容脚本访问(见内容脚本指南列表)。
要使用更强大的 API,您需要在您的 manifest.json 中申请权限。
您可以使用 browser
命名空间访问这些 API。
function logTabs(tabs) {
console.log(tabs);
}
browser.tabs.query({currentWindow: true}, logTabs);
许多 API 为异步,返回一个 Promise
:
function logCookie(c) {
console.log(c);
}
function logError(e) {
console.error(e);
}
var setCookie = browser.cookies.set(
{url: "https://developer.mozilla.org/"}
);
setCookie.then(logCookie, logError);
请注意,这不同于 Google Chrome 的扩展系统,它使用 chrome
而非 browser
名字空间,并且对异步函数使用回调而不是 promises。为辅助移植,Firefox 实现的 WebExtensions 支持 chrome
和回调以及 browser
和 promises。Mozilla 也写了一个 polyfill 使使用 browser
和 promises 的代码能不经修改的在 Chrome 中使用:https://github.com/mozilla/webextension-polyfill。
微软 Edge 使用 browser
名字空间,但尚不支持基于 promise 的异步API。目前在 Edge 中,异步 API 必须使用回调。
并非所有浏览器都支持这里的所有 API:详情见浏览器对 JavaScript API 的支持。
- alarms
- 在未来一个特定的时间运行的计划任务代码。这很像
setTimeout()
和setInterval()
,不过这些函数仅可以按需使用而不能在后台页面工作。 - bookmarks
- 此WebExtensions
bookmarks
API允许一个附加组件和浏览器的书签系统交互和操作。您可以用它给页面加书签,获取已有的书签,以及编辑,移除和管理书签。 - browserAction
- 添加按钮到浏览器的工具栏。
- captivePortal
- Determine the captive portal state of the user’s connection. A captive portal is a web page displayed when a user first connects to a Wi-Fi network. The user provides information or acts on the captive portal web page to gain broader access to network resources, such as accepting terms and conditions or making a payment.
- clipboard
- WebExtention 的
clipboard
API 增加了一个将图像复制到剪贴板的函数。目前,这个 API 仅支持复制图像,但我们期望它未来支持复制文本和 HTML(译者注:原文如此,可能是指被支持复制富内容之后的标准剪贴板 API 取代)。 - contentScripts
- 通过
contentScripts
API,拓展可以在运行时动态地注册或取消注册脚本。 - contextMenus
- 使用
menus.create()
方法创建一个菜单项。你需要传递一个包含条目选项的对象,它包括条目的id,类型,和需要显示出来的文本值。 - cookies
- 使用 WebExtensions 获取或设置 cookies, 并且在修改时能够获得通知。
- downloads
- 为使用此 API,你需要在你的 manifest.json 文件中声明 "downloads" API 权限。
- find
- 在网页中查找文本,并突出显示匹配项。
- history
- 使用
history
API与浏览器历史记录进行交互。 - i18n
- 国际化扩展的函数。您可以使用这些 api 从与扩展打包在一起的本地化文件中获取本地化字符串,查找浏览器的当前语言,并查找其 Accept-Language header头的值。
- idle
- 找出用户系统何时处于空闲,锁定或活动状态。
- permissions
- Extensions need permissions to access more powerful WebExtension APIs. They can ask for permissions at install time, by including the permissions they need in the
permissions
manifest.json key. The main advantages of asking for permissions at install time are: - proxy
- 使用proxy API来代理web请求。你可以使用
事件监听器来拦截web请求,并且返回一个可以描述是否代理并且怎样代理它们的对象。proxy.onRequest
- runtime
- 该模块提供关于附加组件以及运行环境的信息。
- search
- Retrieves search engines and executes a search with a specific search engine.
- sessions
- Use the sessions API to list, and restore, tabs and windows that have been closed while the browser has been running.
- storage
- 此存储系统API基于 Web Storage API, 并有少许不同。
- tabs
- 与浏览器标签系统进行交互。
- types
- Defines the
BrowserSetting
type, which is used to represent a browser setting. - webNavigation
- Add event listeners for the various stages of a navigation. A navigation consists of a frame in the browser transitioning from one URL to another, usually (but not always) in response to a user action like clicking a link or entering a URL in the location bar.
- webRequest
- 每个事件都会在请求的特定阶段触发。事件的顺序大概是这样的:
- windows
- 与浏览器窗口互动。您可以使用此 API 获取有关已打开窗口的信息,以及打开、修改和关闭窗口。您也可以监听窗口的打开、关闭和其激活事件。