Chrome插件:浏览器后台与页面间通讯

  1. content.js 与 background.js和popup.js 通讯和 background.js与popup.js  这些通讯都用 chrome.runtime.sendMessage 这个方法 chrome

     background.js和popup.js 与 content.js通讯 都用 chrome.tabs.sendMessage 方法spa

  2. 好比

    C->P 或者 C->B 或者 b->pcode

    chrome.runtime.sendMessage({name:value},function(){
    
    })

     

    P->C  B->Cblog

    复制代码
    chrome.tabs.query({active: true, currentWindow: true}, function(tabs){
    
               chrome.tabs.sendMessage(tabs[0].id, {name:value}, function(response) {
                    
    
               });  
     })
    复制代码

    3.接收消息都是 string

    chrome.runtime.onMessage.addListener(function(message, sender, sendResponse){
    
      alert(JSON.stringify(message)) //这里获取消息
    
    })
相关文章
相关标签/搜索