javascript - Chrome 扩展消息传递架构

标签 javascript google-chrome google-chrome-extension sendmessage

我最近开始开发我的第一个 Chrome 扩展程序,主要是想看看我是否能做到,我开始想知道消息传递的确切机制。这可能是关于 windows.postMessage API 的更普遍的问题。 ,但我想知道是否有人可以解释控制消息接收和传播的幕后过程。

这是我目前对消息传递 w.r.t Chrome 扩展的理解:

  • 有四种 Javascript 类:(1) JS 绑定(bind)到弹出窗口,(2) JS 在后台运行或作为事件运行,(3)内容脚本中的 JS,以及 (4) JS注入(inject)页面。
  • (2) -> (1)(3) -> (1)runtime.sendMessage() 发送.只有在弹出窗口打开时才会收到它们。
  • (1) -> (2)(3) -> (2)runtime.sendMessage() 发送或 tabs.sendMessage()来自 (3)(1)分别。它们会尽快收到,因为背景/事件 JS 是持久的(我不太确定这个词是否正确,因为事件 JS 必须绑定(bind)到 eventListeners)。
  • (1) -> (3)(2) -> (3)tabs.sendMessage() 发送.它们会尽快收到,因为只要网页处于事件状态,内容脚本就会处于事件状态(因为它存在于并行沙箱中)。
  • (*) -> (4)(4) -> (*) chrome.* 无法处理出于安全问题,但是 (3) -> (4)(4) -> (3)可以通过window.postMessage()处理,因为它们都存在于网页的上下文中。

有几件事我有具体的疑问 - 第一个当然是,如果我已经正确描述了事情。其他的是这些:

  • runtime.sendMessage() 之间到底有什么区别?和 tabs.sendMessage()这决定了哪些 JS 可以实际使用它们?
  • 消息如何传递? *.sendMessage() 时后台会发生什么或 window.postMessage()叫什么?

最佳答案

  • There are four classes of Javascript: (1) JS tied to the popup, (2) JS running in the background or as an event, (3) JS in content scripts, and (4) JS injected into the page.

(1)(2)实际上是相同的(“弹出窗口”也属于“扩展代码”- 参见 Contexts and methods for communication between the browser action, background scripts, and content scripts of chrome extensions?)。

  • (2) -> (1) and (3) -> (1) are sent by runtime.sendMessage(). They're only received when the popup is open.
  • (1) -> (2) and (3) -> (2) are sent by runtime.sendMessage() or tabs.sendMessage() from (3) and (1) respectively. They're received ASAP, because background/event JS is persistent (I'm not quite sure if that's the right word, since event JS has to be tied into eventListeners).

runtime.sendMessage确实可以通过(1)发送, (2)(3) .在接收者的 Angular 色中,(1)之间没有特别的区别。和 (2) .这里是my classification of scripts输入:chrome.runtime.sendMessage 发送的消息由扩展代码接收,发送方的帧除外。例如,如果您输入 <iframe>在后台页面中调用chrome.runtime.sendMessage来自后台页面,chrome.runtime.onMessage将在该帧中触发。

event pages正在使用中,仅在等待事件页面加载后才调度消息事件。

  • (1) -> (3) and (2) -> (3) are sent by tabs.sendMessage(). They're received ASAP, since the content script is active as long as the webpage is (since it exists in a parallel sandbox).

chrome.tabs.sendMessage只要您拥有有效的选项卡 ID,就有意义。由于后台页面没有选项卡 ID,因此无法使用 tabs.sendMessage 向后台页面发送消息。 .内容脚本、选项卡中的扩展页面、选项卡中的扩展框架都可以接收chrome.tabs.sendMessage发送的消息。 ,因为它们是选项卡的一部分。

  • (*) -> (4) and (4) -> (*) can't be handled by chrome.* for security issues, but (3) -> (4) and (4) -> (3) can be handled by window.postMessage(), since they both exist within the context of the webpage.

(4) -> (1,2) (网页到扩展代码)可以通过 externally_connectable 实现.

(4) -> (4)也可以使用 window.postMessage , 但不是直接用 *.sendMessage (因为这不包括发件人)。

  • What exactly is the distinction between runtime.sendMessage() and tabs.sendMessage() that dictates which JS can actually use them?

tabs.sendMessage只能在选项卡 API 可用时用于向选项卡发送消息,而 runtime.sendMessage也可以被内容脚本使用。

简而言之,使用 tabs.sendMessage要将消息发送到选项卡,请使用 runtime.sendMessage将消息发送到您的扩展程序的另一部分。

  • How do messages get passed around? What happens in the background when *.sendMessage() or window.postMessage() is called?

关于javascript - Chrome 扩展消息传递架构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36371072/

相关文章:

javascript - 如何使用 Javascript 中声明的图像编写用于翻转图像的 CSS?

google-chrome - 最近 Chrome 版本中的 WebGL 性能下降

javascript - 内容脚本未收到我发送的对象

javascript - 如何检测 HTML5 data-* 属性是否具有空字符串作为值?

javascript - 使用标签名称插入的组件不起作用

android - 1px 边框在放大移动 chrome 之前不会显示

google-chrome - 为什么 Chrome 不在其网络工具中显示传出流量?

javascript - 在 Chrome 的 F12 控制台中创建上下文菜单?

google-chrome - 关闭 chrome 时清除本地存储(不是关闭特定选项卡时)?

javascript - 解释在 javascript 中将全局变量重新绑定(bind)到本地变量的模式