chrome.runtime.onMessage.addListener通过iframe重复接收

时间:2018-12-07 00:09:56

标签: google-chrome google-chrome-extension

chrome.runtime.onMessage.addListener通过iframe重复接收

/// background.js
(function () {
    function toggle(tab) {
        console.log('activate:', tab.url, tab.id);
        chrome.tabs.sendMessage(tab.id, {action: 'hihi'});
     }

    chrome.browserAction.onClicked.addListener(toggle);
}());


//contents.js
(function () {
chrome.runtime.onMessage.addListener(function (request, sender, 
    sendResponse) {
        console.log('call : ', request.action);
    });
    return true;
})();

// manifest.json
"all_frames": true 

// console result
call :  hihi
call :  hihi
call :  hihi
  • “ all_frames”:是的,实例在页面的每个框架/ iframe中运行,并且每个框架都获得消息。
  • 我能知道接收到哪个iframe吗?

0 个答案:

没有答案