management.install()

从给定的 URL 安装并启用主题扩展。

此 API 需要“management” API 权限,并且只能与已签名的主题一起使用。

这是一个异步函数,它返回一个 Promise

语法

js
browser.management.install(options)

参数

options

一个对象,其中包含 addons.mozilla.org 上的主题 XPI 文件的 URL,以及可选的 XPI 文件哈希(使用 sha256 或更强)。

返回值

一个 Promise,它将以一个包含 manifest.json 中为主题定义的 ExtensionID 的对象来 fulfilled。

示例

循环浏览主题列表

js
"use strict";

const themes = [
  "https://addons.mozilla.org/en-US/firefox/downloads/file/1063216/insightscare-1.0-fx.xpi",
  "https://addons.mozilla.org/en-US/firefox/downloads/file/1063419/orange_roses-1.0-fx.xpi",
  "https://addons.mozilla.org/en-US/firefox/downloads/file/1062647/sticktoyourguns-2.0-fx.xpi",
  "https://addons.mozilla.org/en-US/firefox/downloads/file/0/bad_url.xpi",
];

let current;

async function install(url) {
  try {
    current = url;
    const { id } = await browser.management.install({ url });
    console.log(`Theme installed: ${id}`);
  } catch (e) {
    console.error(`Installation failed: ${e}`);
  }
}

browser.browserAction.onClicked.addListener(() => {
  const id = themes.indexOf(current);
  install(themes[(id + 1) % themes.length]);
});

for (const url of themes) {
  browser.menus.create({
    title: url,
    onclick: () => install(url),
    contexts: ["browser_action"],
  });
}

浏览器兼容性