1

I'm currently writing a Chrome extension and I'm having trouble passing messages between the service worker and popup scripts. The service worker script is supposed to send a message to the popup script whenever a sign-out event is detected, but i keep getting the following error

Unchecked runtime.lastError: Could not establish connection. Receiving end does not exist.

The service worker invokes this method whenever a sign-out event is detected:

function inputController() {
  var store, remainingSessions, request, port;
  store = getObjectStore("readonly");
  request = store.getAll();
  request.onsuccess = function () {
    remainingSessions = request.result;
    port = chrome.runtime.connect({ name: "RemainingSessions" });
    port.postMessage(remainingSessions);
    console.log("msg sent");
  };
}

This is the code for popup.js

chrome.runtime.onConnect.addListener(function (port) {
  port.onMessage.addListener(function (remainingSessions) {
    console.assert(port.name === "RemainingSessions");
    console.log("Message received " + remainingSessions);
  });
});

I've looked around for the solution, but I haven't found much on this. Any advice or pointers on what I should change?

beetsbydre
  • 11
  • 1
  • 1
    The popup runs only when shown so the usual approach is to send a message from the popup. – wOxxOm Nov 09 '21 at 18:31
  • in my case, the popup is supposed to be shown after a sign-out event (which the service worker detects). how would i go about this? – beetsbydre Nov 09 '21 at 18:36
  • If the sign-out happens due to an external event, then you can't be sure the popup is open. You can open a separate window via chrome.windows.create with a url of your extension html that shows something to the user. – wOxxOm Nov 09 '21 at 18:49
  • what script do i call windows.create from?? the popup script or the service worker script – beetsbydre Nov 16 '21 at 22:15
  • That should be from the service worker. Because the popup script only works when the popup is visible. – Md. Hasan Mahmud Mar 02 '22 at 15:10
  • I need to upload a file from a context menu, is that possible? – chovy Mar 26 '23 at 08:11

0 Answers0