From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../api/runtime/connectnative/index.html | 114 ++++++++ .../api/runtime/getmanifest/index.html | 79 ++++++ .../add-ons/webextensions/api/runtime/index.html | 181 ++++++++++++ .../webextensions/api/runtime/onconnect/index.html | 228 +++++++++++++++ .../webextensions/api/runtime/onmessage/index.html | 307 +++++++++++++++++++++ .../api/runtime/openoptionspage/index.html | 92 ++++++ .../api/runtime/platformarch/index.html | 70 +++++ .../api/runtime/platformos/index.html | 76 +++++ .../api/runtime/sendmessage/index.html | 157 +++++++++++ .../api/runtime/sendnativemessage/index.html | 109 ++++++++ 10 files changed, 1413 insertions(+) create mode 100644 files/zh-cn/mozilla/add-ons/webextensions/api/runtime/connectnative/index.html create mode 100644 files/zh-cn/mozilla/add-ons/webextensions/api/runtime/getmanifest/index.html create mode 100644 files/zh-cn/mozilla/add-ons/webextensions/api/runtime/index.html create mode 100644 files/zh-cn/mozilla/add-ons/webextensions/api/runtime/onconnect/index.html create mode 100644 files/zh-cn/mozilla/add-ons/webextensions/api/runtime/onmessage/index.html create mode 100644 files/zh-cn/mozilla/add-ons/webextensions/api/runtime/openoptionspage/index.html create mode 100644 files/zh-cn/mozilla/add-ons/webextensions/api/runtime/platformarch/index.html create mode 100644 files/zh-cn/mozilla/add-ons/webextensions/api/runtime/platformos/index.html create mode 100644 files/zh-cn/mozilla/add-ons/webextensions/api/runtime/sendmessage/index.html create mode 100644 files/zh-cn/mozilla/add-ons/webextensions/api/runtime/sendnativemessage/index.html (limited to 'files/zh-cn/mozilla/add-ons/webextensions/api/runtime') diff --git a/files/zh-cn/mozilla/add-ons/webextensions/api/runtime/connectnative/index.html b/files/zh-cn/mozilla/add-ons/webextensions/api/runtime/connectnative/index.html new file mode 100644 index 0000000000..d3d4d9a14d --- /dev/null +++ b/files/zh-cn/mozilla/add-ons/webextensions/api/runtime/connectnative/index.html @@ -0,0 +1,114 @@ +--- +title: 连接本地应用程序方法 - runtime.connectNative() +slug: Mozilla/Add-ons/WebExtensions/API/runtime/connectNative +tags: + - 附加组件连接本地应用程序 +translation_of: Mozilla/Add-ons/WebExtensions/API/runtime/connectNative +--- +
{{AddonSidebar()}}
+ +
该方法能够把附加组件和用户计算机上的一个本地应用程序相连接.
+ +
 
+ +
同时我们需要本地应用程序的名称作为参数. 当启动本地应用程序的时候会返回一个{{WebExtAPIRef("runtime.Port")}} 对象给调用者.
+ +
 
+ +
之后可以通过该对象的 Port.onMessage() 和 Port.postMessage()方法来和本地应用程序进行信息交互.
+ +
 
+ +
本地应用程序会一直运行直到退出, 除非调用了 Port.disconnect()方法, 亦或创建该Port对象的页面被摧毁了. 一旦Port对象断开连接, 浏览器会给该进程几秒的时间以便安全优雅的退出和释放, 之后如果发现该进程没退出的话就直接暴力干掉.
+ +
 
+ +

更多信息, 请查看 Native messaging.

+ +

语法

+ +
var port = browser.runtime.connectNative(
+  application // 这是一个字符串
+)
+
+ +

参数

+ +
+
application
+
值类型为string. 该参数的值为要连接的本地应用程序的名称. 必须要跟 native application's manifest file 中的"name"特性的值一致.
+
+ +

返回值

+ +

是一个 {{WebExtAPIRef('runtime.Port')}} 对象. 该对象是用来跟本地应用程序进行消息交互的.

+ +

浏览器的兼容性

+ + + +

{{Compat("webextensions.api.runtime.connectNative")}}

+ +

示例

+ +

本示例中连接了本地应用程序"ping_pong"并且启动了监听以便接收消息. 当用户单击浏览器上的操作按钮时它会发送一个本地应用程序的消息:

+ +
/*
+启动时, 连接"ping_pong"本地应用程序.
+*/
+var port = browser.runtime.connectNative("ping_pong");
+
+/*
+监听(接收)来自"ping_pong"本地应用程序的消息.
+*/
+port.onMessage.addListener((response) => {
+  console.log("Received: " + response);
+});
+
+/*
+当浏览器上的单击操作被触发时, 发送一个消息给本地应用程序.
+*/
+browser.browserAction.onClicked.addListener(() => {
+  console.log("Sending:  ping");
+  port.postMessage("ping");
+});
+ +

{{WebExtExamples}}

+ +
万分感谢 + +

该 API 是基于 Chromium 的 chrome.runtime API. 本文档采自 Chromium 代码中的 runtime.json.

+ +

Microsoft Edge 的兼容性数据由微软公司提供, 并被列入以下许可证 Creative Commons Attribution 3.0 United States License.

+
+ + diff --git a/files/zh-cn/mozilla/add-ons/webextensions/api/runtime/getmanifest/index.html b/files/zh-cn/mozilla/add-ons/webextensions/api/runtime/getmanifest/index.html new file mode 100644 index 0000000000..77870d7010 --- /dev/null +++ b/files/zh-cn/mozilla/add-ons/webextensions/api/runtime/getmanifest/index.html @@ -0,0 +1,79 @@ +--- +title: 读取主文件信息方法 - runtime.getManifest() +slug: Mozilla/Add-ons/WebExtensions/API/runtime/getManifest +tags: + - 读取主文件信息方法 + - 附加组件 +translation_of: Mozilla/Add-ons/WebExtensions/API/runtime/getManifest +--- +
{{AddonSidebar()}}
+ +
该方法会获取一个完整的主文件 manifest.json , 并返回一个序列化后的 JSON 对象.
+ +
 
+ +

语法

+ +
browser.runtime.getManifest()
+
+ +

参数

+ +

无.

+ +

返回值

+ +

是一个能表示主文件所有信息的 JSON 对象.

+ +

浏览器兼容性

+ + + +

{{Compat("webextensions.api.runtime.getManifest")}}

+ +

示例

+ +

取得主文件中的 name 特性的值, 并输出到控制台:

+ +
var manifest = browser.runtime.getManifest();
+console.log(manifest.name);
+ +

{{WebExtExamples}}

+ +
Acknowledgements + +

This API is based on Chromium's chrome.runtime API. This documentation is derived from runtime.json in the Chromium code.

+ +

Microsoft Edge compatibility data is supplied by Microsoft Corporation and is included here under the Creative Commons Attribution 3.0 United States License.

+
+ + diff --git a/files/zh-cn/mozilla/add-ons/webextensions/api/runtime/index.html b/files/zh-cn/mozilla/add-ons/webextensions/api/runtime/index.html new file mode 100644 index 0000000000..5a1618e9e7 --- /dev/null +++ b/files/zh-cn/mozilla/add-ons/webextensions/api/runtime/index.html @@ -0,0 +1,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 +--- +
{{AddonSidebar}}
+ +

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

+ +

它提供一组消息通信API,允许你:

+ + + +

Types

+ +
+
{{WebExtAPIRef("runtime.Port")}}
+
表示两个特定上下文之间的连接的一端,可用于交换消息。
+
{{WebExtAPIRef("runtime.MessageSender")}}
+
+
+
+
+
+

包含有关消息或连接请求的发件人的信息。

+
+
+
+
+
+
{{WebExtAPIRef("runtime.PlatformOs")}}
+
标识浏览器的操作系统。
+
{{WebExtAPIRef("runtime.PlatformArch")}}
+
标识浏览器的处理器架构。
+
{{WebExtAPIRef("runtime.PlatformInfo")}}
+
包含有关浏览器正在运行的平台的信息。
+
{{WebExtAPIRef("runtime.RequestUpdateCheckStatus")}}
+
{{WebExtAPIRef("runtime.requestUpdateCheck()")}} 的返回结果。
+
{{WebExtAPIRef("runtime.OnInstalledReason")}}
+
{{WebExtAPIRef("runtime.onInstalled")}} 事件被触发的原因。
+
{{WebExtAPIRef("runtime.OnRestartRequiredReason")}}
+
{{WebExtAPIRef("runtime.onRestartRequired")}} 事件被触发的原因。
+
+ +

Properties

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

Functions

+ +
+
{{WebExtAPIRef("runtime.getBackgroundPage()")}}
+
取得当前扩展的后台页的 Window 对象。
+
{{WebExtAPIRef("runtime.openOptionsPage()")}}
+
+

打开你的扩展的 选项页面

+
+
{{WebExtAPIRef("runtime.getManifest()")}}
+
获得完整的 manifest.json 声明文件的序列化对象。
+
{{WebExtAPIRef("runtime.getURL()")}}
+
给定某个打包在扩展中的资源的基于 manifest.json 相对路径,返回一个完整有效的 URL。
+
{{WebExtAPIRef("runtime.setUninstallURL()")}}
+
指定一个此扩展被卸载后打开的 URL。
+
{{WebExtAPIRef("runtime.reload()")}}
+
重新加载此扩展。
+
{{WebExtAPIRef("runtime.requestUpdateCheck()")}}
+
检查此扩展的更新。
+
{{WebExtAPIRef("runtime.connect()")}}
+
建立一个页面脚本到扩展主进程,或扩展主进程到页面脚本之间的通信连接。
+
{{WebExtAPIRef("runtime.connectNative()")}}
+
+
建立一个浏览器扩展与用户电脑上的原生应用的通信连接。
+
+
{{WebExtAPIRef("runtime.sendMessage()")}}
+
发送一条消息到此扩展或其他扩展的事件监听器,类似于  {{WebExtAPIRef('runtime.connect')}} 但只能发送一条消息,以及可选的响应处理函数。
+
{{WebExtAPIRef("runtime.sendNativeMessage()")}}
+
从扩展发送一条消息到原生应用。
+
{{WebExtAPIRef("runtime.getPlatformInfo()")}}
+
返回当前所在平台的信息。
+
{{WebExtAPIRef("runtime.getBrowserInfo()")}}
+
返回此扩展所在的浏览器的信息。
+
{{WebExtAPIRef("runtime.getPackageDirectoryEntry()")}}
+
返回此扩展所在目录的 DirectoryEntry。
+
+ +

Events

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

Browser compatibility

+ +

{{Compat("webextensions.api.runtime")}}

+ + + +

{{Compat("webextensions.api.runtime")}} {{WebExtExamples("h2")}}

+ +
Acknowledgements + +

This API is based on Chromium's chrome.runtime API. This documentation is derived from runtime.json in the Chromium code.

+ +

Microsoft Edge compatibility data is supplied by Microsoft Corporation and is included here under the Creative Commons Attribution 3.0 United States License.

+
+ + diff --git a/files/zh-cn/mozilla/add-ons/webextensions/api/runtime/onconnect/index.html b/files/zh-cn/mozilla/add-ons/webextensions/api/runtime/onconnect/index.html new file mode 100644 index 0000000000..d51cb8abbc --- /dev/null +++ b/files/zh-cn/mozilla/add-ons/webextensions/api/runtime/onconnect/index.html @@ -0,0 +1,228 @@ +--- +title: runtime.onConnect +slug: Mozilla/Add-ons/WebExtensions/API/runtime/onConnect +translation_of: Mozilla/Add-ons/WebExtensions/API/runtime/onConnect +--- +
{{AddonSidebar()}}
+ +

当使用扩展处理或content script建立连接时触发.

+ +

Syntax

+ +
browser.runtime.onConnect.addListener(listener)
+browser.runtime.onConnect.removeListener(listener)
+browser.runtime.onConnect.hasListener(listener)
+
+ +

事件有三个方法:

+ +
+
addListener(callback)
+
为 这个事件添加一个监听器.
+
removeListener(listener)
+
停止监听这个事件. listener 参数就是要移除的监听器.
+
hasListener(listener)
+
检查监听器是否已经注册到这个事件上. 如果已经监听,则返回 true 否则返回 false.
+
+ +

addListener 语法

+ +

参数

+ +
+
function
+
+

A callback function that will be called when this event occurs. The function will be passed the following arguments:

+ +
+
port
+
A {{WebExtAPIRef('runtime.Port')}} object connecting the current script to the other context it is connecting to.
+
+
+
+ +

Browser compatibility

+ + + +

{{Compat("webextensions.api.runtime.onConnect")}}

+ +

Examples

+ +

This content script:

+ + + +
// content-script.js
+
+var myPort = browser.runtime.connect({name:"port-from-cs"});
+myPort.postMessage({greeting: "hello from content script"});
+
+myPort.onMessage.addListener(function(m) {
+  console.log("In content script, received message from background script: ");
+  console.log(m.greeting);
+});
+
+document.body.addEventListener("click", function() {
+  myPort.postMessage({greeting: "they clicked the page!"});
+});
+ +

The corresponding background script:

+ + + +
// background-script.js
+
+var portFromCS;
+
+function connected(p) {
+  portFromCS = p;
+  portFromCS.postMessage({greeting: "hi there content script!"});
+  portFromCS.onMessage.addListener(function(m) {
+    console.log("In background script, received message from content script")
+    console.log(m.greeting);
+  });
+}
+
+browser.runtime.onConnect.addListener(connected);
+
+browser.browserAction.onClicked.addListener(function() {
+  portFromCS.postMessage({greeting: "they clicked the button!"});
+});
+ +

{{WebExtExamples}}

+ +
Acknowledgements + +

This API is based on Chromium's chrome.runtime API. This documentation is derived from runtime.json in the Chromium code.

+ +

Microsoft Edge compatibility data is supplied by Microsoft Corporation and is included here under the Creative Commons Attribution 3.0 United States License.

+
+ + + +
+ + + + + +
diff --git a/files/zh-cn/mozilla/add-ons/webextensions/api/runtime/onmessage/index.html b/files/zh-cn/mozilla/add-ons/webextensions/api/runtime/onmessage/index.html new file mode 100644 index 0000000000..afa54c1aaf --- /dev/null +++ b/files/zh-cn/mozilla/add-ons/webextensions/api/runtime/onmessage/index.html @@ -0,0 +1,307 @@ +--- +title: runtime.onMessage +slug: Mozilla/Add-ons/WebExtensions/API/runtime/onMessage +translation_of: Mozilla/Add-ons/WebExtensions/API/runtime/onMessage +--- +
{{AddonSidebar()}}
+ +
利用此事件来监听来自你的扩展其他部分的消息。例如,使用:
+ +
+ + + +

To send a message that is received by the onMessage listener, use {{WebExtAPIRef("runtime.sendMessage()")}} or (to send a message to a content script) {{WebExtAPIRef("tabs.sendMessage()")}}.

+ +
+

Avoid creating multiple onMessage listeners for the same type of message, as the order in which multiple listeners will fire is not guaranteed. Where you want to guarantee the delivery of a message to a specific end point, use the connection-based approach to exchange messages.

+
+ +

Along with the message itself, the listener is passed:

+ + + +

You can send a synchronous response to the message by calling the sendResponse function inside your listener. See an example.

+ +

To send an asynchronous response, there are two options:

+ + + +
+

Returning a Promise is now preferred as sendResponse will be removed from the W3C spec. The popular webextension-polyfill library has already removed the sendResponse function from its implementation.

+
+ +
+

You can also use a connection-based approach to exchange messages.

+
+ +

Syntax

+ +
browser.runtime.onMessage.addListener(listener)
+browser.runtime.onMessage.removeListener(listener)
+browser.runtime.onMessage.hasListener(listener)
+
+ +

Events have three functions:

+ +
+
addListener(callback)
+
Adds a listener to this event.
+
removeListener(listener)
+
Stop listening to this event. The listener argument is the listener to remove.
+
hasListener(listener)
+
Checks whether a listener is registered for this event. Returns true if it is listening, false otherwise.
+
+ +

addListener syntax

+ +

Parameters

+ +
+
function
+
+

A listener function that will be called when this event occurs. The function will be passed the following arguments:

+ +
+
message
+
object. The message itself. This is a JSON-ifiable object.
+
+ +
+
sender
+
A {{WebExtAPIRef('runtime.MessageSender')}} object representing the sender of the message.
+
+ +
+
sendResponse
+
+

A function to call, at most once, to send a response to the message. The function takes a single argument, which may be any JSON-ifiable object. This argument is passed back to the message sender.

+ +

If you have more than one onMessage listener in the same document, then only one may send a response.

+ +

To send a response synchronously, call sendResponse before the listener function returns. To send a response asynchronously:

+ +
    +
  • either keep a reference to the sendResponse argument and return true from the listener function. You will then be able to call sendResponse after the listener function has returned.
  • +
  • or return a Promise from the listener function and resolve the promise when the response is ready. This is a preferred way.
  • +
+
+
+ +

The listener function can return either a Boolean or a Promise.

+ +
+

Do not call addListener using the async function, as in:

+ +
browser.runtime.onMessage.addListener(async (data, sender) => {
+  if (data.type === 'handle_me') return 'done';
+});
+
+ +

as the listener will consume every message it receives, effectively blocking all other listeners from receiving and processing messages.

+ +

If you want to take an asynchronous approach, use a promise instead, as in:

+ +
browser.runtime.onMessage.addListener(data, sender) => {
+  if (data.type === 'handle_me') return Promise.resolve('done');
+});
+
+
+
+
+ +

Browser compatibility

+ + + +

{{Compat("webextensions.api.runtime.onMessage")}}

+ +

Examples

+ +

Simple example

+ +

This content script listens for click events on the web page. If the click was on a link, it messages the background page with the target URL:

+ +
// content-script.js
+
+window.addEventListener("click", notifyExtension);
+
+function notifyExtension(e) {
+  if (e.target.tagName != "A") {
+    return;
+  }
+  browser.runtime.sendMessage({"url": e.target.href});
+}
+
+ +

The background script listens for these messages and displays a notification using the notifications API:

+ +
// background-script.js
+
+browser.runtime.onMessage.addListener(notify);
+
+function notify(message) {
+  browser.notifications.create({
+    "type": "basic",
+    "iconUrl": browser.extension.getURL("link.png"),
+    "title": "You clicked a link!",
+    "message": message.url
+  });
+}
+ +

Sending a synchronous response

+ +

This content script sends a message to the background script when the user clicks on the page. It also logs any response sent by the background script:

+ +
// content-script.js
+
+function handleResponse(message) {
+  console.log(`background script sent a response: ${message.response}`);
+}
+
+function handleError(error) {
+  console.log(`Error: ${error}`);
+}
+
+function sendMessage(e) {
+  var sending = browser.runtime.sendMessage({content: "message from the content script"});
+  sending.then(handleResponse, handleError);
+}
+
+window.addEventListener("click", sendMessage);
+ +

Here is a version of the corresponding background script, that sends a response synchronously, from inside in the listener:

+ +
// background-script.js
+
+function handleMessage(request, sender, sendResponse) {
+  console.log(`content script sent a message: ${request.content}`);
+  sendResponse({response: "response from background script"});
+}
+
+browser.runtime.onMessage.addListener(handleMessage);
+ +

And here is another version, that uses Promise.resolve():

+ +
// background-script.js
+
+function handleMessage(request, sender, sendResponse) {
+  console.log(`content script sent a message: ${request.content}`);
+  return Promise.resolve({response: "response from background script"});
+}
+
+browser.runtime.onMessage.addListener(handleMessage);
+ +

Sending an asynchronous response using sendResponse

+ +

Here is an alternative version of the background script from the previous example. It sends a response asynchronously after the listener has returned. Note return true; in the listener: this tells the browser that you intend to use the sendResponse argument after the listener has returned.

+ +
// background-script.js
+
+function handleMessage(request, sender, sendResponse) {
+  console.log(`content script sent a message: ${request.content}`);
+  setTimeout(() => {
+    sendResponse({response: "async response from background script"});
+  }, 1000);
+  return true;
+}
+
+browser.runtime.onMessage.addListener(handleMessage);
+
+ +

Sending an asynchronous response using a Promise

+ +

This content script gets the first <a> link on the page and sends a message asking if the link's location is bookmarked. It expects to get a Boolean response: true if the location is bookmarked, false otherwise:

+ +
// content-script.js
+
+const firstLink = document.querySelector("a");
+
+function handleResponse(isBookmarked) {
+  if (isBookmarked) {
+    firstLink.classList.add("bookmarked");
+  }
+}
+
+browser.runtime.sendMessage({
+  url: firstLink.href
+}).then(handleResponse);
+ +

Here is the background script. It uses {{WebExtAPIRef("bookmarks.search()")}} to see if the link is bookmarked, which returns a Promise:

+ +
// background-script.js
+
+function isBookmarked(message, sender, response) {
+  return browser.bookmarks.search({
+    url: message.url
+  }).then(function(results) {
+    return results.length > 0;
+  });
+}
+
+browser.runtime.onMessage.addListener(isBookmarked);
+ +

If the asynchronous handler doesn't return a promise, you can explicitly construct a promise. This rather contrived example sends a response after a 1-second delay, using Window.setTimeout():

+ +
// background-script.js
+
+function handleMessage(request, sender, sendResponse) {
+  return new Promise(resolve => {
+    setTimeout(() => {
+      resolve({response: "async response from background script"});
+    }, 1000);
+  });
+}
+
+browser.runtime.onMessage.addListener(handleMessage);
+ +

{{WebExtExamples}}

+ +
Acknowledgements + +

This API is based on Chromium's chrome.runtime API. This documentation is derived from runtime.json in the Chromium code.

+ +

Microsoft Edge compatibility data is supplied by Microsoft Corporation and is included here under the Creative Commons Attribution 3.0 United States License.

+
+ + diff --git a/files/zh-cn/mozilla/add-ons/webextensions/api/runtime/openoptionspage/index.html b/files/zh-cn/mozilla/add-ons/webextensions/api/runtime/openoptionspage/index.html new file mode 100644 index 0000000000..701af9c2ea --- /dev/null +++ b/files/zh-cn/mozilla/add-ons/webextensions/api/runtime/openoptionspage/index.html @@ -0,0 +1,92 @@ +--- +title: runtime.openOptionsPage() +slug: Mozilla/Add-ons/WebExtensions/API/runtime/openOptionsPage +tags: + - API + - OpenPractices + - WebExtensions +translation_of: Mozilla/Add-ons/WebExtensions/API/runtime/openOptionsPage +--- +
 
+ +
假如你的页面有options page(设置页面)的定义,使用此方法打开它。
+ +
 
+ +
这是一个异步方法,返回一个 Promise对象
+ +
 
+ +

语法

+ +
var openingPage = browser.runtime.openOptionsPage()
+
+ +

参数

+ +

+ +

返回值

+ +

当设置页面成功创建,执行Promise的无参成功回调方法,否则执行Promise的失败回调方法,参数为错误信息。

+ +

浏览器兼容性

+ + + +

{{Compat("webextensions.api.runtime.openOptionsPage")}}

+ +

例子

+ +

当用户点击浏览器行为图标时,打开一个设置页面。

+ +
function onOpened() {
+  console.log(`Options page opened`);
+}
+
+function onError(error) {
+  console.log(`Error: ${error}`);
+}
+
+var opening = browser.runtime.openOptionsPage();
+opening.then(onOpened, onError);
+ +

{{WebExtExamples}}

+ +
Acknowledgements + +

This API is based on Chromium's chrome.runtime API. This documentation is derived from runtime.json in the Chromium code.

+ +

Microsoft Edge compatibility data is supplied by Microsoft Corporation and is included here under the Creative Commons Attribution 3.0 United States License.

+
+ + diff --git a/files/zh-cn/mozilla/add-ons/webextensions/api/runtime/platformarch/index.html b/files/zh-cn/mozilla/add-ons/webextensions/api/runtime/platformarch/index.html new file mode 100644 index 0000000000..0adfaf39f1 --- /dev/null +++ b/files/zh-cn/mozilla/add-ons/webextensions/api/runtime/platformarch/index.html @@ -0,0 +1,70 @@ +--- +title: 获取处理器架构 - runtime.PlatformArch +slug: Mozilla/Add-ons/WebExtensions/API/runtime/PlatformArch +tags: + - 获取处理器架构 + - 附加组件 +translation_of: Mozilla/Add-ons/WebExtensions/API/runtime/PlatformArch +--- +
{{AddonSidebar()}}
+ +

当前浏览器所在的计算机的处理器架构.

+ +

值类型

+ +

该值的类型是字符串. 可能的值如下:

+ +
+
"arm"
+
标识平台基于 arm 架构.
+
"x86-32"
+
表示平台基于 x86 32-bit 架构.
+
"x86-64"
+
表示平台基于 x86 64-bit 架构.
+
+ +

浏览器兼容性

+ + + +

{{Compat("webextensions.api.runtime.PlatformArch")}}

+ +

{{WebExtExamples}}

+ +
Acknowledgements + +

This API is based on Chromium's chrome.runtime API. This documentation is derived from runtime.json in the Chromium code.

+ +

Microsoft Edge compatibility data is supplied by Microsoft Corporation and is included here under the Creative Commons Attribution 3.0 United States License.

+
+ + diff --git a/files/zh-cn/mozilla/add-ons/webextensions/api/runtime/platformos/index.html b/files/zh-cn/mozilla/add-ons/webextensions/api/runtime/platformos/index.html new file mode 100644 index 0000000000..6c32d46b6f --- /dev/null +++ b/files/zh-cn/mozilla/add-ons/webextensions/api/runtime/platformos/index.html @@ -0,0 +1,76 @@ +--- +title: 获取当前操作系统 - runtime.PlatformOs +slug: Mozilla/Add-ons/WebExtensions/API/runtime/PlatformOs +tags: + - 获取当前操作系统 + - 附加组件 +translation_of: Mozilla/Add-ons/WebExtensions/API/runtime/PlatformOs +--- +
{{AddonSidebar()}}
+ +

获取当前浏览器运行所在的操作系统.

+ +

值类型

+ +

该值的类型是字符串. 可能的值如下:

+ +
+
"mac"
+
表示底层操作系统是 Mac OS X.
+
"win"
+
表示底层操作系统是 Windows.
+
"android"
+
表示底层操作系统是 Android.
+
"cros"
+
表示底层操作系统是 Chrome OS.
+
"linux"
+
表示底层操作系统是 Linux.
+
"openbsd"
+
表示底层操作系统是 Open/FreeBSD.
+
+ +

浏览器兼容性

+ + + +

{{Compat("webextensions.api.runtime.PlatformOs")}}

+ +

{{WebExtExamples}}

+ +
Acknowledgements + +

This API is based on Chromium's chrome.runtime API. This documentation is derived from runtime.json in the Chromium code.

+ +

Microsoft Edge compatibility data is supplied by Microsoft Corporation and is included here under the Creative Commons Attribution 3.0 United States License.

+
+ + diff --git a/files/zh-cn/mozilla/add-ons/webextensions/api/runtime/sendmessage/index.html b/files/zh-cn/mozilla/add-ons/webextensions/api/runtime/sendmessage/index.html new file mode 100644 index 0000000000..86ec753075 --- /dev/null +++ b/files/zh-cn/mozilla/add-ons/webextensions/api/runtime/sendmessage/index.html @@ -0,0 +1,157 @@ +--- +title: runtime.sendMessage() +slug: Mozilla/Add-ons/WebExtensions/API/runtime/sendMessage +translation_of: Mozilla/Add-ons/WebExtensions/API/runtime/sendMessage +--- +
{{AddonSidebar()}}
+ +
向你的扩展或其他扩展发送单条消息。
+ +
 
+ +
如果想发给你自己的扩展,则省略 extensionId 参数。扩展中所有页面的{{WebExtAPIRef('runtime.onMessage')}}将会被触发,除了调用runtime.sendMessage的页面。
+ +

 

+ +

如果发送给其他扩展,则将参数 extensionId 设置为其他扩展的ID。其他扩展的 {{WebExtAPIRef('runtime.onMessageExternal')}} 将会被触发。

+ +

此接口不能给 content script 发消息,如果要给 content script 发消息,请使用 {{WebExtAPIRef('tabs.sendMessage')}}。

+ +

这是个异步方法,将返回一个 Promise

+ +

Syntax

+ +
var sending = browser.runtime.sendMessage(
+  extensionId,             // optional string
+  message,                 // any
+  options                  // optional object
+)
+
+ +

参数

+ +
+
extensionId{{optional_inline}}
+
string. 若你想要发给不同的扩展,这里传入接收方的扩展ID。The ID of the extension to send the message to. Include this to send the message to a different extension. If the intended recipient has set an ID explicitly using the applications key in manifest.json, then extensionId should have this value. Otherwise it should be have the ID that was generated for the intended recipient.
+
若此省略此参数,则发送给自己的扩展。
+
message
+
any. 任何可以序列化成JSON的东西。
+
options{{optional_inline}}
+
object. +
+
includeTlsChannelId{{optional_inline}}
+
boolean. Whether the TLS channel ID will be passed into {{WebExtAPIRef('runtime.onMessageExternal')}} for processes that are listening for the connection event.
+
toProxyScript{{optional_inline}}
+
boolean. Must be True if the message is intended for a PAC file loaded using the {{WebExtAPIRef("proxy")}} API.
+
+
+
+ +

根据给出的参数不同,API遵循如下规则:

+ + + +
+

在Firefox 55之前,只给出2个参数时,规则会有所不同:
+ Under the old rules, if the first argument was a string, it was treated as the extensionId, with the message as the second argument. This meant that if you called sendMessage() with arguments like ("my-message", {}), then it would send an empty message to the extension identified by "my-message". Under the new rules, with these arguments you would send the message "my-message" internally, with an empty options object.

+
+ +

Return value

+ +

返回一个 Promise。若接收方响应,Promise将会变为 fulfilled 并且返回接收方响应的JSON对象(数字、字符串、数组、true/false都是合法的JSON对象)。否则,Promise会变为 fulfilled 但是不返回任何参数。如果发生了连接错误,Promise将会变为 rejected 并返回一个错误消息。

+ +

Browser compatibility

+ + + +

{{Compat("webextensions.api.runtime.sendMessage")}}

+ +

Examples

+ +

Here's a content script that sends a message to the background script when the user clicks the content window. The message payload is {greeting: "Greeting from the content script"}, and the sender also expects to get a response, which is handled in the handleResponse function:

+ +
// content-script.js
+
+function handleResponse(message) {
+  console.log(`Message from the background script:  ${message.response}`);
+}
+
+function handleError(error) {
+  console.log(`Error: ${error}`);
+}
+
+function notifyBackgroundPage(e) {
+  var sending = browser.runtime.sendMessage({
+    greeting: "Greeting from the content script"
+  });
+  sending.then(handleResponse, handleError);
+}
+
+window.addEventListener("click", notifyBackgroundPage);
+ +

The corresponding background script looks like this:

+ +
// background-script.js
+
+function handleMessage(request, sender, sendResponse) {
+  console.log("Message from the content script: " +
+    request.greeting);
+  sendResponse({response: "Response from background script"});
+}
+
+browser.runtime.onMessage.addListener(handleMessage);
+ +

{{WebExtExamples}}

+ +
Acknowledgements + +

This API is based on Chromium's chrome.runtime API. This documentation is derived from runtime.json in the Chromium code.

+ +

Microsoft Edge compatibility data is supplied by Microsoft Corporation and is included here under the Creative Commons Attribution 3.0 United States License.

+
+ + diff --git a/files/zh-cn/mozilla/add-ons/webextensions/api/runtime/sendnativemessage/index.html b/files/zh-cn/mozilla/add-ons/webextensions/api/runtime/sendnativemessage/index.html new file mode 100644 index 0000000000..349b7a06d0 --- /dev/null +++ b/files/zh-cn/mozilla/add-ons/webextensions/api/runtime/sendnativemessage/index.html @@ -0,0 +1,109 @@ +--- +title: runtime.sendNativeMessage() +slug: Mozilla/Add-ons/WebExtensions/API/runtime/sendNativeMessage +tags: + - sendNativeMessage + - 扩展 + - 附加组件 + - 非标准 +translation_of: Mozilla/Add-ons/WebExtensions/API/runtime/sendNativeMessage +--- +
{{AddonSidebar()}}
+ +

从 WebExtension 发送单条消息到 native application。

+ +

它需要两个强制的参数:native application 的名字和要发送给它的JSON对象。浏览器将会加载 native application 然后发送这个消息。

+ +

这是一个异步函数,返回一个 Promise对象。native application 发送的第一条消息将被当作sendNativeMessage() 的回复,并且 promise 将这个消息作为参数.。注意你不能使用 {{WebExtAPIRef("runtime.onMessage")}} 从应用获取回复:你必须使用回调函数来替代。

+ +

每次调用 runtime.sendNativeMessage()都会生成一个新的实例。浏览器将会在收到回复后结束这个 native application。为了结束这个 native application,浏览器将会关闭 pipe,并给进程几秒的时间优雅的退出,如果它没有关闭就杀死它。

+ +

更对信息,参考 Native messaging

+ +

语法

+ +
var sending = browser.runtime.sendNativeMessage(
+  application,             // string
+  message                  // object
+)
+
+ +

参数

+ +
+
application
+
字符串类型。native application的名字。它必须和 native application's manifest file中的‘name’字段一致。
+
message
+
对象类型。一个将要发送给 native application的JSON对象。
+
+ +

返回值

+ +

一个 Promise对象。如果native application发送了一个回复,它将会填充回复的JSON对象作为参数。否则它不会填充参数。如果在native application连接期间发生了错误,promise将会被一个错误的消息拒绝。

+ +

浏览器兼容性

+ + + +

{{Compat("webextensions.api.runtime.sendNativeMessage")}}

+ +

示例

+ +

这是一个 background script ,当使用者点击浏览器的browser action时,它会发送 "ping" 消息到 "ping_pong" 应用并且把回复记录下来:

+ +
function onResponse(response) {
+  console.log(`Received ${response}`);
+}
+
+function onError(error) {
+  console.log(`Error: ${error}`);
+}
+
+/*
+On a click on the browser action, send the app a message.
+*/
+browser.browserAction.onClicked.addListener(() => {
+  console.log("Sending:  ping");
+  var sending = browser.runtime.sendNativeMessage("ping_pong", "ping");
+  sending.then(onResponse, onError);
+});
+ +

{{WebExtExamples}}

+ +
Acknowledgements + +

这个API 基于 Chromium 的 chrome.runtime API。 本文来自 Chromium 代码中的 runtime.json

+ +

微软 Edge 的兼容性数据由 Microsoft Corporation 提供,并且包含在这里基于 Creative Commons Attribution 3.0 United States License。

+
+ + -- cgit v1.2.3-54-g00ecf