aboutsummaryrefslogtreecommitdiff
path: root/files/zh-tw/webapi/permissions/index.html
blob: d58d8466beb4fa2224688a4758a487bdc5ddcb47 (plain)
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
---
title: 權限 (Permissions)
slug: WebAPI/Permissions
translation_of: Web/API/Permissions_API
---
<p>{{ draft() }}</p>
<p>{{ non-standard_header }}</p>
<p>{{ B2GOnlyHeader2('certified') }}</p>
<h2 id="摘要">摘要</h2>
<p>Permissions API 可顯示 Apps 所要求的所有權限,以利使用者管理。Apps 可透過此 API 而讀取其他 Apps 的權限並進一步變更。</p>
<p>透過 <a href="https://developer.mozilla.org/en-US/docs/DOM/PermissionSettings" title="/en-US/docs/DOM/PermissionSettings"><code>PermissionSettings</code></a> 介面的 <a href="https://developer.mozilla.org/en-US/docs/DOM/window.navigator.mozPermissionSettings" title="/en-US/docs/DOM/window.navigator.mozPermissionSettings"><code>navigator.mozPermissionSettings</code></a> 屬性,即可存取 Permission Manager。</p>
<h2 id="已安裝_Apps_的權限">已安裝 Apps 的權限</h2>
<p>所有 Apps 均需透過自己的 manifest 檔案而要求權限。Apps 每次所使用的 API,均以「請求顯性權限 (Explicit Permission)」的 API 為主,並提示使用者是否通過權限。如果使用者選擇「不要再提示」,大概也就不太可能改變決定了。API 則能提供介面,以利使用者管理已發出的權限。</p>
<p>透過 <a href="https://developer.mozilla.org/en-US/docs/DOM/PermissionSettings.get" title="/en-US/docs/DOM/PermissionSettings.get"><code>PermissionSettings.get()</code></a><a href="https://developer.mozilla.org/en-US/docs/DOM/PermissionSettings.set" title="/en-US/docs/DOM/PermissionSettings.set"><code>set()</code></a><a href="https://developer.mozilla.org/en-US/docs/DOM/PermissionSettings.isExplicit" title="/en-US/docs/DOM/PermissionSettings.isExplicit"><code>isExplicit()</code></a> 函式即可達到上述作業。</p>
<h3 id="讀取權限">讀取權限</h3>
<p>若要知道已發出權限的目前狀態,可使用 <a href="https://developer.mozilla.org/en-US/docs/DOM/PermissionSettings.get" title="/en-US/docs/DOM/PermissionSettings.get"><code>PermissionSettings.get()</code></a> 函式。此函式可回傳字串,以顯示特定 Apps 權限的目前狀態。可能的數值有:</p>
<dl>
 <dt>
  <code>allow</code></dt>
 <dd>
  已通過該權限,且不需使用者的進一步互動。</dd>
 <dt>
  <code>denied</code></dt>
 <dd>
  已否決該權限;可能是系統或使用者所否決。</dd>
 <dt>
  <code>prompt</code></dt>
 <dd>
  代表該權限將以明顯的提示方法,詢問使用者是否給予權限。</dd>
 <dt>
  <code>unknown</code></dt>
 <dd>
  代表該 Apps 並未詢問此權限,也不會提示使用者是否給予權限。</dd>
</dl>
<pre class="brush: js">// Let's check all installed apps
var apps = navigator.mozApps.mgmt.getAll();

apps.onsuccess = function () {
  var permission = navigator.mozPermissionSettings;

  // Let's check the permission of each app
  apps.result.forEach(function (app) {
    var request, appName = app.manifest.name;

    for (request in app.manifest.permission) {
      // Let's get the current permission for each permission request by the application
      var p = permission.get(request, app.manifestUrl, app.origine, false);

      console.log(appName + ' asked for "' + request + '" permission, which is "' + p + '"')
    }
  });
}

</pre>
<h3 id="設定權限">設定權限</h3>
<p>只要使用 <a href="https://developer.mozilla.org/en-US/docs/DOM/PermissionSettings.set" title="/en-US/docs/DOM/PermissionSettings.set"><code>PermissionSettings.set()</code></a> 函式即可設定權限。可能的數值均與 <a href="https://developer.mozilla.org/en-US/docs/DOM/PermissionSettings.get" title="/en-US/docs/DOM/PermissionSettings.get"><code>get</code></a> 函式所存取的相同。</p>
<div class="note">
 <p><strong>注意:</strong>根據 Apps 權限的不同,某些可能為隱性 (Implicit) 權限。若因為某種理由,Apps 嘗試將權限變更為隱性權限,就會產生錯誤。為了避免這種錯誤,可透過 <a href="https://developer.mozilla.org/en-US/docs/DOM/PermissionSettings.isExplicit" title="/en-US/docs/DOM/PermissionSettings.isExplicit"><code>PermissionSettings.isExplicit()</code></a> 函式而檢查是否為顯性權限。</p>
</div>
<pre class="brush: js">// Let's check all installed apps
var apps = navigator.mozApps.mgmt.getAll();

apps.onsuccess = function () {
  var permission = navigator.mozPermissionSettings;

  // Let's grant the permission of each app
  apps.result.forEach(function (app) {
    var request, appName = app.manifest.name;

    for (request in app.manifest.permission) {
      // If the permission is not explicit
      if (!permission.isExplicit(request, app.manifestUrl, app.origine, false) {
        // Let's ask the user for all permissions requested by the application
        permission.set(request, app.manifestUrl, app.origine, false);
      }
    }
  });
}</pre>
<h2 id="規格">規格</h2>
<p>不屬於任何規格。</p>
<h2 id="另可參閱">另可參閱</h2>
<ul>
 <li>{{domxref("window.navigator.mozPermissionSettings","navigator.mozPermissionSettings")}}</li>
 <li>{{domxref("PermissionSettings")}}</li>
</ul>