fox*_*hen 0 javascript google-chrome google-chrome-extension
我收到这个错误 Uncaught Error: Attempting to use a disconnected port object
当我第一次打开弹出页面后,我之间的联系很长
内容脚本< - >后台页面< - >弹出页面.
当我点击浏览器操作图标时,弹出页面将从服务器通过后台页面获取一些信息进行初始化.
所有的东西在第一次点击时工作正常,但如果我关闭弹出窗口并再次点击它,它只是无法从后台页面获取信息.
这是我的代码
弹出页面
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(...){...}
});
Run Code Online (Sandbox Code Playgroud)
和背景页面
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
Run Code Online (Sandbox Code Playgroud)
错误发生在后台页面的这一行
port.postMessage({"method":"updatePage","content": matchInfo});
Run Code Online (Sandbox Code Playgroud)
我检查过服务器正确地将数据发送到后台页面,但是无法弄清楚错误.
感谢帮助 !!
| 归档时间: |
|
| 查看次数: |
3976 次 |
| 最近记录: |