2013-07-26 24 views
0

我得到這個錯誤長期連接Uncaught Error: Attempting to use a disconnected port object「試圖使用斷開連接的端口對象」在Chrome擴展

當我運行結束我的彈出頁面後的第一時間,我有

之間的長期連接

內容腳本< - >後臺頁面< - >彈出頁面。

當我點擊瀏覽器的操作圖標,彈出頁面將會從服務器獲取一些信息,通過後臺頁面初始化。

所有的事情做工精細,在第一次點擊,但如果我關閉彈出,然後再次單擊它,它都無法擺脫的背景頁面中的信息。

這裏是我的代碼

彈出頁面

window.onload = function() { 

var port = chrome.runtime.connect({name: "stadium"}); 

chrome.tabs.query({ currentWindow: true, active: true }, function callback(tabs){ 
    console.log("send TabID to background page"); 
    port.postMessage({"method":"sendTabId","content": tabs[0].id}); 
}); 


port.postMessage({"method" : "initialPopup"});//initilaize request 

port.onMessage.addListener(function(msg) { 
    console.log("somthing"); 
    if (msg.method == "updatePage"){ 
       initialize.... 
      } 
    else if(...){...} 
}); 

和背景頁

var socket = io.connect('http://localhost:3700/'); 

    chrome.tabs.onRemoved.addListener(function(tabId,removeInfo){ 

     if(tabId==stadiumTabId){ 

     //change to the original style popup page 
     chrome.browserAction.setPopup({"popup":"../pages/popup_out_guest.html"}); 

     } 

    }); 

    chrome.runtime.onConnect.addListener(function(port) { 

     console.assert(port.name == "stadium"); 

     port.onMessage.addListener(function(msg) { 

     if (msg.method == "initialPopup"){ //get the initilaize request 

      socket.emit('updateMatchInfo',"haha"); 

      socket.on('getUpdate',function(matchInfo){ 
         console.log("background page get data from server"); 
         port.postMessage({"method":"updatePage","content": matchInfo});       
         }); 
     } 

     else if (msg.method == "something"){ 
      //insert content scripts 
      chrome.tabs.executeScript({file: 'js/content_scripts.js', allFrames: true}); 

      //change to another popup page style 
      chrome.browserAction.setPopup({"popup":"../pages/popup_in_guest.html"});    
     } 
    });//port.onMessage.addListener 

});//onConnect.addListener 

在這條線在後臺頁面發生錯誤

port.postMessage({"method":"updatePage","content": matchInfo}); 

我檢查了服務器正確地將數據發送到背景頁面,但只是無法弄清楚錯誤。

感謝您的幫助!

回答

5

您正在使用的方式Awesome Screenshot?我有這樣的錯誤消息,所以常常但一旦我禁用擴展消息就走了:)

+0

我現在非常愛你。好找! –

+0

雖然這不是真棒截圖,但有一個擴展是這樣做的。謝謝你的提示 – windmaomao

0

當你關閉彈出,它顯示也被關閉/被毀,不像始終運行在後臺的頁面。

因此,一個長期連接中斷,因爲雙方的一個不復存在。

您應該從使用長壽命連接切換到簡單消息。當它打開時,彈出窗口請求當前狀態,並且後臺頁面廣播狀態更新。如果彈出窗口沒有聽更新(因爲它沒有打開),所以不會造成傷害。