简体   繁体   English

chrome扩展中具有长期连接的“尝试使用断开连接的端口对象”

[英]“Attempting to use a disconnected port object” with Long-lived connections in chrome extension

i get this error Uncaught Error: Attempting to use a disconnected port object 我收到此错误Uncaught Error: Attempting to use a disconnected port object

when i opend my popup page after the first time, i have a long-lived connections between 第一次打开弹出页面时,我之间存在着长期的联系

content script<->background page<->popup page. 内容脚本<->背景页面<->弹出页面。

When i click on browser action icon , popup page will get some information from server through background page to initialize. 当我单击浏览器操作图标时,弹出页面将通过后台页面从服务器获取一些信息以进行初始化。

All things work fine at the first click, but if i close the popup and click it again, it just cant get the information from background page. 第一次单击时,一切正常,但如果我关闭弹出窗口并再次单击,则无法从后台页面获取信息。

here is my code 这是我的代码

popup page 弹出页面

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(...){...}
 });

and background page 和背景页面

    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

the error occurs at this line in background page 错误发生在后台页面的这一行

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

i've checked that server send the data to background page correctly, but just can't figure out the error. 我已经检查过服务器将数据正确发送到后台页面,但是无法找出错误。

thanks for help !! 感谢帮助 !!

Are you using by the way Awesome Screenshot ? 您是否正在使用Awesome屏幕截图 I had that error message so often but once I disable that extension the message went away :) 我经常收到该错误消息,但是一旦禁用该扩展名,该消息就会消失:)

Whenever you close the popup, the page it displays is also closed / destroyed, unlike the background page that's always running. 每当关闭弹出窗口时,它显示的页面也会被关闭/破坏,这与始终运行的后台页面不同。

So a long-lived connection breaks, as one of the sides ceases to exist. 因此,长久的连接中断了,因为其中一方不再存在。

You should switch from using long-lived connection to simple messages. 您应该从使用长期连接切换为简单消息。 As it opens, the popup requests the current state, and the background page broadcasts state updates. 打开时,弹出窗口请求当前状态,而后台页面广播状态更新。 If the popup is not listening to updates (because it's not open), no harm is done. 如果弹出窗口没有监听更新(因为它没有打开),则不会造成任何危害。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Chrome扩展程序的长期消息连接-如何使用回调函数? - Chrome extension long-lived message connection - how to use callback functions? 在Chrome浏览器扩展程序中将消息从内容发送到弹出窗口时,“尝试使用断开的端口对象” - “Attempting to use a disconnected port object” while sending message from content to popup in Chrome extension 在 React Base chrome 扩展中使用长寿命连接时无法获取状态值 - Unable to get state value in when using long-lived connection in react base chrome extension 使用Apache / PHP / Javascript的长期连接(异步服务器推送)? - Long-lived connections (asynchronous server push) with Apache/PHP/Javascript? Recoil:将长期存在的客户端 object 传递给选择器 - Recoil: Passing a long-lived client object to a selector Background.js 到 content.js 使用端口和长寿命消息传递 - Background.js to content.js using Port & Long-lived messaging Facebook 图 API 长寿命令牌 - Facebook Graph API Long-Lived Token 将短期访问令牌交换为长期有效 - Exchange short-lived access token for long-lived, not working 又一次有关:错误:尝试使用断开连接的端口对象,该怎么办? - one more time about: Error: Attempting to use a disconnected port object, how to? Chrome扩展程序的长期连接:从后台检索消息到内容 - Chrome extension long lived connection: retrieve message from background to content
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM