0

我目前正在编写一个 Chrome 扩展程序,并且在服务工作者和弹出脚本之间传递消息时遇到问题。每当检测到注销事件时,服务工作者脚本应该向弹出脚本发送一条消息,但我不断收到以下错误

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

每当检测到注销事件时,服务工作者都会调用此方法:

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");
  };
}

这是 popup.js 的代码

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

我四处寻找解决方案,但在这方面我没有找到太多。关于我应该改变什么的任何建议或指示?

4

0 回答 0