我得到這個錯誤長期連接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});
我檢查了服務器正確地將數據發送到背景頁面,但只是無法弄清楚錯誤。
感謝您的幫助!
我現在非常愛你。好找! –
雖然這不是真棒截圖,但有一個擴展是這樣做的。謝謝你的提示 – windmaomao