aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/mozilla/add-ons/webextensions/manifest.json/index.html
blob: 281382d38278582ec1301afa325c182710094325 (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
---
title: manifest.json
slug: Mozilla/Add-ons/WebExtensions/manifest.json
tags:
  - JSON
  - manifest.json
translation_of: Mozilla/Add-ons/WebExtensions/manifest.json
---
<div>{{AddonSidebar}}</div>

<div class="blockIndicator note">
<p>本文描述的是适用于 WebExtensions 的 manifest.json。若您正在寻找适用于 PWA 的 manifest.json 的有关信息,请参考这篇文章:<a href="/zh-CN/docs/Web/Manifest">Web App Manifest</a></p>
</div>

<div><code>manifest.json</code> 是每个 WebExtension 唯一必须包含的元数据文件。</div>

<p>通过使用 <code>manifest.json</code>,您可以指定扩展的基本元数据,例如名称和版本,还可以指定扩展各个方面的功能(例如后台脚本,内容脚本和某些浏览器行为)。</p>


<p>它是一个允许使用 "<code>//</code>" 撰写单行注释的、特殊的 <a href="/zh-CN/docs/Glossary/JSON">JSON</a> 文件。</p>

<h2 id="manifest.json_键列表">manifest.json 键列表</h2>

<p>支持的 <code>manifest.json</code> 键如下所示:</p>

<h3 id="简体中文内容">简体中文内容</h3>

<div class="index">{{ListSubpages("/zh-CN/Add-ons/WebExtensions/manifest.json")}}</div>

<h3 id="英文内容">英文内容</h3>

<div class="index">{{ListSubpages("/en-US/Add-ons/WebExtensions/manifest.json")}}</div>

<ul>
 <li><code>"manifest_version"</code><code>"version"</code>,和 <code>"name"</code> 是唯一的强制必填键。</li>
 <li>如果 "_locales" 目录存在的话,"<code>default_locale"</code> 也必须相应存在,否则不应存在。</li>
 <li>Google Chrome 不支持 <code>"browser_specific_settings"</code></li>
</ul>

<h3 id="在运行时访问_manifest.json_键">在运行时访问 manifest.json 键</h3>

<p>你可以在拓展的 JavaScript 代码中通过 {{WebExtAPIRef("runtime.getManifest()")}} 函数访问拓展的 manifest 数据:</p>

<pre class="brush: js; no-line-numbers">browser.runtime.getManifest().version;</pre>

<h2 id="示例">示例</h2>

<p>The block below shows the basic syntax for some common manifest keys.</p>

<div class="blockIndicator note">
<p><strong>Note:</strong> This is not intended to be used as a copy-paste-ready example. Selecting the keys you'll need depends on the extension you are developing.</p>
</div>

<p>For complete example extensions, see <a href="/en-US/docs/Mozilla/Add-ons/WebExtensions/Examples">Example extensions</a>.</p>

<pre class="brush: json;">{
  "browser_specific_settings": {
    "gecko": {
      "id": "addon@example.com",
      "strict_min_version": "42.0"
    }
  },

  "background": {
    "scripts": ["jquery.js", "my-background.js"],
  },

  "browser_action": {
    "default_icon": {
      "19": "button/geo-19.png",
      "38": "button/geo-38.png"
    },
    "default_title": "Whereami?",
    "default_popup": "popup/geo.html"
  },

  "commands": {
    "toggle-feature": {
      "suggested_key": {
        "default": "Ctrl+Shift+Y",
        "linux": "Ctrl+Shift+U"
      },
      "description": "Send a 'toggle-feature' event"
    }
  },

  "content_security_policy": "script-src 'self' https://example.com; object-src 'self'",

  "content_scripts": [
    {
      "exclude_matches": ["*://developer.mozilla.org/*"],
      "matches": ["*://*.mozilla.org/*"],
      "js": ["borderify.js"]
    }
  ],

  "default_locale": "en",

  "description": "...",

  "icons": {
    "48": "icon.png",
    "96": "icon@2x.png"
  },

  "manifest_version": 2,

  "name": "...",

  "page_action": {
    "default_icon": {
      "19": "button/geo-19.png",
      "38": "button/geo-38.png"
    },
    "default_title": "Whereami?",
    "default_popup": "popup/geo.html"
  },

  "permissions": ["webNavigation"],

  "version": "0.1",

  "user_scripts": {
    "api_script": "apiscript.js",
  },

  "web_accessible_resources": ["images/my-image.png"]
}</pre>

<h2 id="浏览器兼容性">浏览器兼容性</h2>

<p>For a full overview of all manifest keys and their sub-keys, see the<a href="/en-US/docs/Mozilla/Add-ons/WebExtensions/Browser_compatibility_for_manifest.json"> full <code>manifest.json</code> browser compatibility table</a>.</p>



<p>{{Compat("webextensions.manifest")}}</p>

<h2 id="参见">参见</h2>

<p>{{WebExtAPIRef("permissions")}} JavaScript API</p>