beetsbydre
beetsbydre

Reputation: 11

Passing messages between service worker and popup.js

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?

Upvotes: 1

Views: 1424

Answers (0)

Related Questions