Chrome扩展:浏览器操作弹出窗口如何与活动选项卡通信

Chrome Extensions: How does A Browser Action popup communicate with the active tab?

本文关键字:活动 选项 通信 窗口 扩展 浏览器 操作 Chrome      更新时间:2023-09-26

我希望在浏览器操作弹出窗口上运行一个脚本,以便在调用时从当前活动选项卡获取信息。我不清楚如何在两者之间沟通。我需要一个在活动选项卡上运行的内容脚本和一个从中请求信息的chrome.tabs.sendMessage()吗?我应该请求什么权限?

是的,内容脚本和其他脚本(后台、浏览器操作、页面操作)之间的通信通过消息进行。所以,在每一边都有这样的代码:

chrome.runtime.onConnect.addListener(function(port) {
  port.onMessage.addListener(function(request) {
    // process the request
    // reply as port.postMessage(data) if needed
  };
};