1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
---
title: permissions.contains()
slug: Mozilla/Add-ons/WebExtensions/API/permissions/contains
tags:
- Contains
- permissions.contains()
translation_of: Mozilla/Add-ons/WebExtensions/API/permissions/contains
---
<div>{{AddonSidebar()}}</div>
<p>检查扩展名是否具有给定 {{WebExtAPIRef("permissions.Permissions")}} 对象中列出的权限。</p>
<p>The <code>Permissions</code> argument may contain either an origins property, which is an array of <a href="/en-US/Add-ons/WebExtensions/manifest.json/permissions#Host_permissions">host permissions</a>, or a <code>permissions</code> property, which is an array of <a href="/en-US/Add-ons/WebExtensions/manifest.json/permissions#API_permissions">API permissions</a>, or both.</p>
<p>This is an asynchronous function that returns a <code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise">Promise</a></code>. The promise is fulfilled with true only if all the extension currently has all the given permissions. For host permissions, if the extension's permissions <a href="/en-US/docs/Mozilla/Add-ons/WebExtensions/Match_patterns">pattern-match</a> the permissions listed in <code>origins</code>, then they are considered to match.</p>
<h2 id="Syntax">Syntax</h2>
<pre class="syntaxbox brush:js notranslate">var getContains = browser.permissions.contains(
permissions // Permissions object
)
</pre>
<h3 id="Parameters">Parameters</h3>
<dl>
<dt><code>permissions</code></dt>
<dd>A {{WebExtAPIRef("permissions.Permissions")}} object.</dd>
</dl>
<h3 id="Return_value">Return value</h3>
<p>A <code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise">Promise</a></code> that will be fulfilled with <code>true</code> if the extension already has all the permissions listed in the <code>permissions</code> argument, or <code>false</code> otherwise.</p>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p>{{Compat("webextensions.api.permissions.contains")}}</p>
<h2 id="Examples">Examples</h2>
<pre class="brush: js notranslate">// Extension permissions are:
// "webRequest", "tabs", "*://*.mozilla.org/*"
var testPermissions1 = {
origins: ["*://mozilla.org/"],
permissions: ["tabs"]
};
browser.permissions.contains(testPermissions1).then((result) => {
console.log(result); // true
});
var testPermissions2 = {
origins: ["*://mozilla.org/"],
permissions: ["tabs", "alarms"]
};
browser.permissions.contains(testPermissions2).then((result) => {
console.log(result); // false, "alarms" doesn't match
});
var testPermissions3 = {
origins: ["https://developer.mozilla.org/"],
permissions: ["tabs", "webRequest"]
};
browser.permissions.contains(testPermissions3).then((result) => {
console.log(result); // true: "https://developer.mozilla.org/"
}); // matches: "*://*.mozilla.org/*"
var testPermissions4 = {
origins: ["https://example.org/"]
};
browser.permissions.contains(testPermissions4).then((result) => {
console.log(result); // false, "https://example.org/"
}); // does not match
</pre>
<p>{{WebExtExamples}}</p>
<div class="note"><strong>Acknowledgements</strong>
<p>This API is based on Chromium's <a href="https://developer.chrome.com/extensions/permissions"><code>chrome.permissions</code></a> API.</p>
<p>Microsoft Edge compatibility data is supplied by Microsoft Corporation and is included here under the Creative Commons Attribution 3.0 United States License.</p>
</div>
<div id="gtx-trans" style="position: absolute; left: 677px; top: 46px;">
<div class="gtx-trans-icon"></div>
</div>
|