I'm having trouble with the Chrome extension - it keeps throwing a Disconnected Port

In the process of creating a Chrome extension, I have encountered an issue regarding communication between two content scripts. One script is loaded on the main page, while the other is on an external page within an iframe that I manage. To enable bidirectional communication between these scripts, I am working on implementing the Messaging API.

From what I gather, it is recommended to have each page communicate through background.js. This is the approach I am currently trying to implement. My initial focus is on verifying if messages are being successfully transmitted, but I keep running into the following error:

Error: Attempting to use a disconnected port object

The error seems to be linked to the line "page_post.postMessage(msg)" in background.js. Could the issue be related to using multiple ports?

menu.js

var port = chrome.runtime.connect({name: "menuport"});
port.postMessage({source: "menu", status: "ready", id: menu_id});
port.onMessage.addListener(function(msg) {
    console.log('MenuJS')
    console.log(msg)
});

page.js

var page_port = chrome.runtime.connect({name: "pageport"});
page_port.postMessage({source: "page", status: "ready"});
page_port.onMessage.addListener(function(msg) {
  console.log('PageJS')
  console.log(msg)
});

background.js

var menu_port = chrome.runtime.connect({name: "menuport"});
var page_port = chrome.runtime.connect({name: "pageport"});

chrome.runtime.onConnect.addListener(function(menu_port) {
  menu_port.onMessage.addListener(function(msg) {
      console.log('BGJS')
      console.log(msg)
      page_port.postMessage(msg);
  });
});

chrome.runtime.onConnect.addListener(function(page_port) {
  page_port.onMessage.addListener(function(msg) {
      console.log(msg)
  });
});

Answer №1

Make sure your background page only registers the runtime.onConnect listener once and differentiates between ports using the port.name property.

background.js

chrome.runtime.onConnect.addListener(function(port) {
  if(port.name === "menuport") {
    // logic for menu port
  } else if (port.name === "pageport") {
    // logic for page port
  }
});

Answer №2

I rephrased the question and received a prompt response. The key is to save the port once connected. Check out this helpful post on Chrome extension messaging using multiple ports

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Updating Property of a div in Vue.js

I have a Vue file and I am trying to specify an 'action' attribute for my form. <template> <div> <form> </form> </div> </template> export default { created() { var test = document.getElement ...

What is the best way to send data from a child functional component to a parent class component?

I am facing a situation where I have a parent class component and a child functional component. While there are examples available on passing values from a child class component to a parent class component, I am wondering how to pass a value from this type ...

What is the best way to tailor my functions to apply only to specific objects within an array, instead of affecting them all?

I am facing an issue where my functions showMore and showLess are triggering for all objects in the array when onClick is fired. I want these functions to be called individually for each object. Both of these functions are responsible for toggling betwee ...

Issue encountered: An unexpected token = error appeared after updating from RN version 0.64.2 to 0.65.1

After upgrading from version 64.1 to 65.1 using the upgrade helper, I encountered an error that has been difficult to resolve. Even after updating node/npm to version 14.17 and trying various other solutions, I have not been able to fix this issue during ...

Having difficulties in properly connecting faces while UV mapping a cube in Three.js

After successfully applying an image texture to a cube through UV mapping for a photo-sphere viewer, I noticed that thin straight lines are visible where the faces of the cube join. Interestingly, this issue does not occur when splitting texture tiles via ...

My web browser doesn't recognize my JavaScript as actual JavaScript?

HTML: <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script> <script src="/js/grabber.js" type="text/javascript"></script> <script type="text/javascript"> $(docu ...

Cloudflare does not restrict access to my Nuxt application

Seeking a compact project involving Nuxt JS with the intention of utilizing cloudflare for my domain. However, upon enabling cloudflare, I am unable to access my development platform as cloudflare does not permit port number 8000. Currently, in order to v ...

Is it possible for jQuery AJAX to function across various servers?

While working on my local server (MAMP) everything was running smoothly. But once I transferred my web application to a different server, I encountered an issue where the request wasn't working and returned the error: function() { console.log("oh no") ...

Assign an onClick event to a Button that was dynamically generated within a loop in JavaScript

Is there a way to set the onClick property for buttons created within a loop? I've tried using JQuery with no success. for(started somewhere... var button = document.createElement('button') var div = document.createEl ...

Secure your JavaScript file with encryption

I am curious if anyone has ever used a tool to protect and fully encrypt JavaScript files. I recently came across a tool that claims to be able to completely encrypt JS files. Have you heard of this before? What are your thoughts on it? ? ...

Guide to preventing the display of the URL address bar with JavaScript or jQuery

I am working with a Spring MVC framework and looking for a way to hide the URL address bar when my page loads. The application is not public, so I don't need it to be visible. Is there a way to accomplish this using JavaScript or jQuery? Update: If ...

jQuery failing to recognize clicks on dynamically added element

Here's the scenario: <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header" ...

What is the process for uploading an image using Vue.js?

Looking for guidance on how to upload an image using Vue.js. This is my HTML code to insert a picture: <input type="file" id="wizard-picture"> And here is my Vue.js code: <script> submitBox = new Vue({ el: "#submitBox", data: { usernam ...

Guide to updating the class attribute of an HTML article element using JavaScript

I am working with 6 different styles in CSS and I am trying to apply them randomly using the following code: <script> function getRandom(max) { return "style" + Math.floor(Math.random() * max); } document.getElementById('YYY ...

Altering value with Angular in a script element

Is there a way to dynamically change the radius of a circle based on user input without using angular curly braces in the script tag? I'm looking for an alternative method that mimics how Angular binds HTML properties. <script src="https://aj ...

Intersection observer automatically removes images from carousel (Siema) after they have been viewed

Check out this example to see the issue I'm facing. I've implemented an intersection observer for lazy loading images, here's the code: const pictures = document.querySelectorAll("[data-src]"); function loadPicture(pic){ const src = p ...

What is the best way to implement a series of delayed animations in jQuery that are connected

Imagine you have the following items: <div id="d1"><span>This is div1</span></div> <div id="d2"><span>This is div2</span></div> <div id="d3"><span>This is div3</sp ...

Difficulty in getting callbacks triggered for basic conversation with Botkit 4 and SlackAdapter

Struggling to get conversation callbacks firing correctly. Has anyone successfully implemented botkit 4 with Slack and can share a working sample? I've set up the necessary adapters and middleware, but my callbacks just won't trigger. I followed ...

What is the best way to modify a current DOM element in React?

I have been utilizing the cropper.js tool to extract screenshots from a PDF file. My goal now is to personalize the crop box tool by adding some custom buttons at the bottom of the crop box. Upon loading the PDF file, cropper.js generates certain DOM eleme ...

Incorporating React Native elements into a native Android interface (UI element)

I am facing an issue in my react-native application where I have a native Android view (created in java) rendering correctly. However, when I attempt to include one of my react-native javascript components within it, I encounter this error: java.lang.Cla ...