Reputation: 56
when I write below code and run it I got error:
Uncaught (in promise) Error: Could not establish connection. Receiving end does not exist.
worker.js:
async function createOffscreen() {
if (await chrome.offscreen.hasDocument()) return;
await chrome.offscreen.createDocument({
url: "offscreen.html",
reasons: ["AUDIO_PLAYBACK"],
justification: "testing",
});
}
chrome.runtime.onMessage.addListener(async (msg) => {
switch (msg.type) {
case "play":
await createOffscreen();
await chrome.runtime.sendMessage({
type: "play",
play: msg.play,
offscreen: true,
});
break;
case "pause":
await createOffscreen();
await chrome.runtime.sendMessage({ type: "pause", offscreen: true });
break;
}
});
chrome.tabs.onUpdated.addListener(
function (tabId, changeInfo, tab) {
chrome.runtime.sendMessage({ type: "pause", offscreen: true });
}
)
I want to send message when the url changed, does there have any other methods to do it?
Upvotes: 1
Views: 814
Reputation: 524
I think this might be an issue where your onUpdated listener is trying to send a message, but it has nowhere to send that message to. The solution would depend on what you're trying to do:
Upvotes: 2