Chrome插件:瀏覽器后台與頁面間通信


  • content.js 與 background.js和popup.js 通信和 background.js與popup.js  這些通信都用 chrome.runtime.sendMessage 這個方法 

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

  • 比如

    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)) //這里獲取消息
    
    })

     


  • 免責聲明!

    本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



     
    粵ICP備18138465號   © 2018-2025 CODEPRJ.COM