aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/mozilla/add-ons/webextensions/api/runtime/index.html
blob: 5a1618e9e71292ba291e14a56464f72b742a3b8e (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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
---
title: runtime
slug: Mozilla/Add-ons/WebExtensions/API/runtime
tags:
  - API
  - Add-ons
  - Extensions
  - Interface
  - NeedsTranslation
  - Non-standard
  - Reference
  - TopicStub
  - WebExtensions
  - runtime
translation_of: Mozilla/Add-ons/WebExtensions/API/runtime
---
<div>{{AddonSidebar}}</div>

<p>该模块提供关于附加组件以及运行环境的信息。</p>

<p>它提供一组消息通信API,允许你:</p>

<ul>
 <li>在附加组件的不同模块间通信。</li>
 <li>和其它的附加组件通信。</li>
 <li>和native应用通信。</li>
</ul>

<h2 id="Types">Types</h2>

<dl>
 <dt>{{WebExtAPIRef("runtime.Port")}}</dt>
 <dd>表示两个特定上下文之间的连接的一端,可用于交换消息。</dd>
 <dt>{{WebExtAPIRef("runtime.MessageSender")}}</dt>
 <dd>
 <div class="trans-right">
 <div class="output-wrap small-font">
 <div class="output-mod ordinary-wrap">
 <div class="output-bd" dir="ltr" style="padding-bottom: 0px;">
 <p class="ordinary-output target-output clearfix"><span>包含有关消息或连接请求的发件人的信息。</span></p>
 </div>
 </div>
 </div>
 </div>
 </dd>
 <dt>{{WebExtAPIRef("runtime.PlatformOs")}}</dt>
 <dd>标识浏览器的操作系统。</dd>
 <dt>{{WebExtAPIRef("runtime.PlatformArch")}}</dt>
 <dd>标识浏览器的处理器架构。</dd>
 <dt>{{WebExtAPIRef("runtime.PlatformInfo")}}</dt>
 <dd>包含有关浏览器正在运行的平台的信息。</dd>
 <dt>{{WebExtAPIRef("runtime.RequestUpdateCheckStatus")}}</dt>
 <dd>{{WebExtAPIRef("runtime.requestUpdateCheck()")}} 的返回结果。</dd>
 <dt>{{WebExtAPIRef("runtime.OnInstalledReason")}}</dt>
 <dd>{{WebExtAPIRef("runtime.onInstalled")}} 事件被触发的原因。</dd>
 <dt>{{WebExtAPIRef("runtime.OnRestartRequiredReason")}}</dt>
 <dd>{{WebExtAPIRef("runtime.onRestartRequired")}} 事件被触发的原因。</dd>
</dl>

<h2 id="Properties">Properties</h2>

<dl>
 <dt>{{WebExtAPIRef("runtime.lastError")}}</dt>
 <dd>当异步方法执行时发生了错误,它需要向其调用方报告时,该值会被设置。</dd>
 <dt>{{WebExtAPIRef("runtime.id")}}</dt>
 <dd>当前扩展的ID。</dd>
</dl>

<h2 id="Functions">Functions</h2>

<dl>
 <dt>{{WebExtAPIRef("runtime.getBackgroundPage()")}}</dt>
 <dd>取得当前扩展的后台页的 <a href="https://wiki.developer.mozilla.org/zh-CN/docs/Web/API/Window">Window</a> 对象。</dd>
 <dt>{{WebExtAPIRef("runtime.openOptionsPage()")}}</dt>
 <dd>
 <p>打开你的扩展的 <a href="/zh-CN/Add-ons/WebExtensions/Anatomy_of_a_WebExtension#Options_pages">选项页面</a></p>
 </dd>
 <dt>{{WebExtAPIRef("runtime.getManifest()")}}</dt>
 <dd>获得完整的 <a href="/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json">manifest.json</a> 声明文件的序列化对象。</dd>
 <dt>{{WebExtAPIRef("runtime.getURL()")}}</dt>
 <dd>给定某个打包在扩展中的资源的基于 <a href="/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json">manifest.json</a> 相对路径,返回一个完整有效的 URL。</dd>
 <dt>{{WebExtAPIRef("runtime.setUninstallURL()")}}</dt>
 <dd>指定一个此扩展被卸载后打开的 URL。</dd>
 <dt>{{WebExtAPIRef("runtime.reload()")}}</dt>
 <dd>重新加载此扩展。</dd>
 <dt>{{WebExtAPIRef("runtime.requestUpdateCheck()")}}</dt>
 <dd>检查此扩展的更新。</dd>
 <dt>{{WebExtAPIRef("runtime.connect()")}}</dt>
 <dd>建立一个页面脚本到扩展主进程,或扩展主进程到页面脚本之间的通信连接。</dd>
 <dt>{{WebExtAPIRef("runtime.connectNative()")}}</dt>
 <dd>
 <div>建立一个浏览器扩展与用户电脑上的原生应用的通信连接。</div>
 </dd>
 <dt>{{WebExtAPIRef("runtime.sendMessage()")}}</dt>
 <dd>发送一条消息到此扩展或其他扩展的事件监听器,类似于  {{WebExtAPIRef('runtime.connect')}} 但只能发送一条消息,以及可选的响应处理函数。</dd>
 <dt>{{WebExtAPIRef("runtime.sendNativeMessage()")}}</dt>
 <dd>从扩展发送一条消息到原生应用。</dd>
 <dt>{{WebExtAPIRef("runtime.getPlatformInfo()")}}</dt>
 <dd>返回当前所在平台的信息。</dd>
 <dt>{{WebExtAPIRef("runtime.getBrowserInfo()")}}</dt>
 <dd>返回此扩展所在的浏览器的信息。</dd>
 <dt>{{WebExtAPIRef("runtime.getPackageDirectoryEntry()")}}</dt>
 <dd>返回此扩展所在目录的 DirectoryEntry。</dd>
</dl>

<h2 id="Events">Events</h2>

<dl>
 <dt>{{WebExtAPIRef("runtime.onStartup")}}</dt>
 <dd>当一个拥有此扩展的账户第一次启动时触发,注意若处于隐私模式中则不会触发。</dd>
 <dt>{{WebExtAPIRef("runtime.onInstalled")}}</dt>
 <dd>当扩展第一次安装,扩展更新,浏览器更新后触发。</dd>
 <dt>{{WebExtAPIRef("runtime.onSuspend")}}</dt>
 <dd>当扩展将被停止前触发,使得扩展可以执行一些清理工作。</dd>
 <dt>{{WebExtAPIRef("runtime.onSuspendCanceled")}}</dt>
 <dd>在此事件 {{WebExtAPIRef("runtime.onSuspend")}} 后触发,表明扩展最终没有被停止。</dd>
 <dt>{{WebExtAPIRef("runtime.onUpdateAvailable")}}</dt>
 <dd>当扩展更新可用时触发,注意若扩展运行中,更新不会马上被安装。</dd>
 <dt>{{WebExtAPIRef("runtime.onBrowserUpdateAvailable")}}</dt>
 <dd>当浏览器更新可用时触发,注意浏览器需要重启才能安装更新。</dd>
 <dt>{{WebExtAPIRef("runtime.onConnect")}}</dt>
 <dd>与扩展进程或页面脚本(content script)建立通信连接时触发。</dd>
 <dt>{{WebExtAPIRef("runtime.onConnectExternal")}}</dt>
 <dd>与其他扩展建立通信连接时触发。</dd>
 <dt>{{WebExtAPIRef("runtime.onMessage")}}</dt>
 <dd>当收到扩展进程或页面脚本(content script)的消息时触发。</dd>
 <dt>{{WebExtAPIRef("runtime.onMessageExternal")}}</dt>
 <dd>当收到其他扩展的消息时触发,不能在页面脚本(content script)中使用。</dd>
 <dt>{{WebExtAPIRef("runtime.onRestartRequired")}}</dt>
 <dd>当设备要重启时触发。</dd>
</dl>

<h2 id="Browser_compatibility">Browser compatibility</h2>

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

<div class="hidden note">
<p>The "Chrome incompatibilities" section is included from <a href="https://developer.mozilla.org/en-US/Add-ons/WebExtensions/Chrome_incompatibilities"> https://developer.mozilla.org/en-US/Add-ons/WebExtensions/Chrome_incompatibilities</a> using the <a href="/en-US/docs/Template:WebExtChromeCompat">WebExtChromeCompat</a> macro.</p>

<p>If you need to update this content, edit <a href="https://developer.mozilla.org/en-US/Add-ons/WebExtensions/Chrome_incompatibilities">https://developer.mozilla.org/en-US/Add-ons/WebExtensions/Chrome_incompatibilities</a>, then shift-refresh this page to see your changes.</p>
</div>

<p>{{Compat("webextensions.api.runtime")}} {{WebExtExamples("h2")}}</p>

<div class="note"><strong>Acknowledgements</strong>

<p>This API is based on Chromium's <a href="https://developer.chrome.com/extensions/runtime"><code>chrome.runtime</code></a> API. This documentation is derived from <a href="https://chromium.googlesource.com/chromium/src/+/master/extensions/common/api/runtime.json"><code>runtime.json</code></a> in the Chromium code.</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 class="hidden">
<pre class="notranslate">// Copyright 2015 The Chromium Authors. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//    * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//    * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//    * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
</pre>
</div>