Javascript &引用;试图使用断开连接的端口对象“;在chrome扩展中具有长寿命连接
我收到此错误Javascript &引用;试图使用断开连接的端口对象“;在chrome扩展中具有长寿命连接,javascript,google-chrome,google-chrome-extension,Javascript,Google Chrome,Google Chrome Extension,我收到此错误未捕获错误:试图使用断开连接的端口对象 当我第一次打开我的弹出页面后,我有一个长期的连接 内容脚本背景页面弹出页面 当我点击浏览器操作图标时,弹出页面将通过后台页面从服务器获取一些信息进行初始化 在第一次点击时,所有的东西都可以正常工作,但是如果我关闭弹出窗口并再次点击它,它就无法从后台页面获取信息 这是我的密码 弹出页面 window.onload = function() { var port = chrome.runtime.connect({name: "stadium"}
未捕获错误:试图使用断开连接的端口对象
当我第一次打开我的弹出页面后,我有一个长期的连接
内容脚本背景页面弹出页面
当我点击浏览器操作图标时,弹出页面将通过后台页面从服务器获取一些信息进行初始化
在第一次点击时,所有的东西都可以正常工作,但是如果我关闭弹出窗口并再次点击它,它就无法从后台页面获取信息
这是我的密码
弹出页面
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});
错误发生在后台页的这一行
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});
我已经检查了服务器是否正确地将数据发送到后台页面,但就是无法找出错误
谢谢你的帮助 顺便问一下,你在用什么?我经常收到这样的错误消息,但一旦我禁用了该扩展,消息就消失了:)顺便问一下,您正在使用吗?我经常收到这样的错误消息,但一旦我禁用了扩展,消息就消失了:)每当你关闭弹出窗口时,它显示的页面也会关闭/销毁,这与总是运行的后台页面不同 因此,当其中一方不复存在时,一个长期存在的联系就会破裂
您应该从使用长期连接切换到使用简单消息。打开时,弹出窗口请求当前状态,后台页面广播状态更新。如果弹出窗口未侦听更新(因为它未打开),则不会造成任何伤害。每当关闭弹出窗口时,它显示的页面也会关闭/销毁,这与始终运行的后台页面不同 因此,当其中一方不复存在时,一个长期存在的联系就会破裂
您应该从使用长期连接切换到使用简单消息。打开时,弹出窗口请求当前状态,后台页面广播状态更新。如果弹出窗口未收听更新(因为它未打开),则不会造成任何伤害。虽然它不是一个很棒的屏幕截图,但有一个扩展正在这样做。感谢Tipal,我认为这不是一个很棒的截图,但是有一个扩展正在这样做。感谢您的提示,简单消息中的问题也一样简单消息中的问题也一样