javascript - 浏览器扩展 : Send messages (with response) between browser-action-popup and background-script

标签 javascript google-chrome-extension firefox-addon-webextensions

首先,我不是在寻找长期连接。我正在专门寻找一种发送消息并发送对这些消息的直接响应的方法。

当您在内容脚本和背景脚本之间发送消息时,它非常简单,因为您使用 chrome.tabs API 向内容脚本发送消息/从内容脚本接收消息。并使用 chrome.runtime API 从后台脚本发送消息/从后台脚本接收消息。

但是浏览器 Action 弹出窗口有点不同,因为两者都在后台上下文中运行。所以我想他们都必须使用 chrome.runtime API。

但这意味着我必须在浏览器操作弹出窗口和后台脚本中收听 chrome.runtime.onMessage。所以基本上我会在后台脚本中收到从弹出窗口发送的消息,也会在弹出窗口本身中收到消息。反之亦然。

所以是的,这实际上行不通:

/////////////background-script//////////////
//Send message from background-script to browser-action-popup
chrome.runtime.sendMessage({msg:"This is a message sent from the background-script to the browser-action-popup"})
  .then(response => { //Receive response from the browser-action-popup
      console.log(response.msg)
  })

//Receive messages from browser-action-popup
chrome.runtime.onMessage.addListener(function(message,sender,sendResponse) {
    sendResponse({msg:"This is a response message sent from the background-script"})
    return true
})

...

///////////browser-action-popup/////////////
//Send message from browser-action-popup to background-script
chrome.runtime.sendMessage({msg:"This is a message sent from the browser-action-popup to the background-script"})
  .then(response => { //Receive response from the background-script
      console.log(response.msg)
  })

//Receive message from background-script
chrome.runtime.onMessage.addListener(function(message,sender,sendResponse) {
    sendResponse({msg:"This is a response message sent from the browser-action-popup"})
    return true
})

但由于它们都在后台上下文中运行,我也想知道是否有比发送消息更简单的方法:是否可以在两者之间共享变量或者它们是否完全隔离运行?

最佳答案

来自 the docs :

If sending to your extension, the runtime.onMessage event will be fired in every frame of your extension (except for the sender's frame)

因此您不必担心来自弹出窗口的消息会触发弹出窗口的 onMessage 事件。

您还要求通过共享变量来实现另一种通信方式。从弹出窗口中,您可以调用 chrome.runtime.getBackgroundPage ,即获取背景页面的 JavaScript 窗口对象。从后台页面你可以调用chrome.extension.getViews使用 {type: 'popup'} 访问弹出窗口的窗口对象(如果它是打开的)。

Firefox documentation对于 getBackgroundPage 状态:

This provides a convenient way for other privileged add-on scripts to get direct access to the background script's scope. This enables them to access variables or call functions defined in that scope. "Privileged script" here includes scripts running in options pages, or scripts running in browser action or page action popups, but does not include content scripts.

关于javascript - 浏览器扩展 : Send messages (with response) between browser-action-popup and background-script,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41895619/

相关文章:

javascript - Chrome - 扩展选项屏幕不允许我关注下一个字段

javascript - 使用 firefox webextension api 清除所有 cookie

google-chrome-extension - Chrome 标签查询返回空标签数组

php - 使用 AJAX 将变量发送到 php

javascript - 以编程方式单击标记和 map

javascript - chrome....onBeforeRequest...() URL 列表未按预期运行

javascript - JS - 当新文本元素添加到页面时如何再次运行脚本

javascript - 为什么 'selector' 返回值不同于 'this'

javascript - 如何在数组react-final-form中设置错误

css - Firefox:Windows Firefox Web 扩展图标背后的黑色背景