ALR*_*ALR 6 google-chrome-extension
我试图从消息发送background.js给content.js使用下面的代码:
背景
chrome.runtime.sendMessage({'method': 'test'});
Run Code Online (Sandbox Code Playgroud)
内容
chrome.runtime.onMessage.addListener(function(message,sender,sendResponse){
if(message.method == 'test')
console.log('Got message');
});
Run Code Online (Sandbox Code Playgroud)
当background.js接收popup.js到单击事件发生的特定消息时,将发送后台消息.因此,用户单击弹出窗口中的按钮,然后将消息发送到后台,然后发送到内容.
我有一种感觉,我的问题与在弹出窗口中单击按钮(这是一个单独的选项卡)的事实有关,内容脚本不会接收它,因为它不是当前活动的选项卡.
请帮帮我.
Xan*_*Xan 14
sendMessageChrome API中有两个功能.
chrome.runtime.sendMessage向所有打开的扩展页面发送消息(即背景,弹出窗口等)chrome.tabs.sendMessage从给定选项卡中的扩展名向所有内容脚本发送消息(可能按帧ID过滤)因此,要向内容脚本发送消息,您需要使用chrome.tabs.要从内容脚本(或扩展页面)发送消息,您需要使用chrome.runtime.
本次活动是chrome.runtime.onMessage在两种情况下.
有关详细信息,请参阅消息传递文档.
| 归档时间: |
|
| 查看次数: |
5599 次 |
| 最近记录: |