runtime.MessageSender

一个包含有关消息发送者或连接请求信息的对象,该对象会传递给 runtime.onMessage() 侦听器。

它也是 runtime.Port 的一个属性,但仅在传递给 runtime.onConnect()runtime.onConnectExternal() 侦听器的 Port 实例中。

类型

此类型的值是对象。它们包含以下属性:

documentId 可选

string。打开连接的文档的 UUID。

documentLifecycle 可选

string。打开连接的文档在创建 port 时所处的生命周期状态。请注意,文档的生命周期状态可能自 port 创建以来已发生变化。

frameId 可选

integer。打开连接的 frame。顶层 frame 为零,子 frame 为正。仅在设置了 tab 时设置。

id 可选

string。发送消息的扩展程序的 ID,如果消息是由扩展程序发送的。如果发送者的 ID 使用 manifest.json 中的 browser_specific_settings 键显式设置,则 id 具有此值。否则,它具有为发送者生成的 ID。

origin 可选

string。打开连接的页面或 frame 的源。它可能与 url 属性不同(例如,about:blank)或不透明(例如,沙盒 iframe)。如果源无法从 URL 中显现,这对于确定源是否可信很有用。

tab 可选

tabs.Tab。打开连接的 tabs.Tab。仅当连接是从 tab(包括内容脚本)打开时,此属性才存在。

tlsChannelId 可选

string。打开连接的页面或 frame 的 TLS 通道 ID,如果扩展程序请求且可用。

url 可选

string。托管发送消息的脚本的页面或 frame 的 URL。

如果发送者是在扩展程序页面(例如 background pageoptions pagebrowser actionpage action popup)中运行的脚本,则 URL 的格式为 "moz-extension://<extension-internal-id>/path/to/page.html"。如果发送者是 background script 并且您未包含 background page,则为 "moz-extension://<extension-internal-id>/_generated_background_page.html"

如果发送者是在网页(包括内容脚本和普通页面脚本)中运行的脚本,则 url 是网页 URL。如果脚本在 iframe 中运行,则 url 是 iframe 的 URL。

userScriptWorldId 可选

string。发送消息的 USER_SCRIPT world 的 worldId。仅在 runtime.onUserScriptMessage 中以及 port.sender 中对于 runtime.onUserScriptConnect 存在。

浏览器兼容性

注意:此 API 基于 Chromium 的 chrome.runtime API。本文档来源于 Chromium 代码中的 runtime.json