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

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

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

  2. 比如

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

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

     

    P->C  B->C

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

    3.接收消息都是 

    chrome.runtime.onMessage.addListener(function(message, sender, sendResponse){
    
      alert(JSON.stringify(message)) //这里获取消息
    
    })

     

posted @ 2019-05-18 15:16  方方和圆圆  阅读(4543)  评论(0编辑  收藏  举报

再过一百年, 我会在哪里?