Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zh-CN: create management.install() #24938

Merged
merged 4 commits into from
Dec 18, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
---
title: management.install()
slug: Mozilla/Add-ons/WebExtensions/API/management/install
l10n:
sourceCommit: b8a0743ca8b1e1b1b1a95cc93a4413c020f11262
yin1999 marked this conversation as resolved.
Show resolved Hide resolved
---

{{AddonSidebar}}

安装并启用给定 URL 的主题扩展。
yin1999 marked this conversation as resolved.
Show resolved Hide resolved

此 API 需要“management”[API 权限](/zh-CN/docs/Mozilla/Add-ons/WebExtensions/manifest.json/permissions),并且只能用于安装已签名的主题。
yin1999 marked this conversation as resolved.
Show resolved Hide resolved

这是一个异步函数,返回一个 [`Promise`](/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Promise)。
yin1999 marked this conversation as resolved.
Show resolved Hide resolved

## 语法

```js-nolint
browser.management.install(options)
```

### 参数

- options
- : 包含 [addons.mozilla.org](https://addons.mozilla.org) 上的主题 XPI 文件的 URL 和可选的 XPI 文件散列值(使用 sha256 或更强的散列算法)的对象。

### 返回值

[Promise](/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Promise),将用包含对应主题在其 manifest.json 中定义的 `ExtensionID` 的对象兑现。
yin1999 marked this conversation as resolved.
Show resolved Hide resolved

## 浏览器兼容性

{{Compat}}

## 示例

遍历一系列的主题:

```js
"use strict";

const themes = [
"https://addons.mozilla.org/zh-CN/firefox/downloads/file/1063216/insightscare-1.0-fx.xpi",
"https://addons.mozilla.org/zh-CN/firefox/downloads/file/1063419/orange_roses-1.0-fx.xpi",
"https://addons.mozilla.org/zh-CN/firefox/downloads/file/1062647/sticktoyourguns-2.0-fx.xpi",
"https://addons.mozilla.org/zh-CN/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(`安装了如下的主题:${id}`);
yin1999 marked this conversation as resolved.
Show resolved Hide resolved
} catch (e) {
console.error(`主题安装失败:${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"],
});
}
```

{{WebExtExamples}}
Loading