serversideup / webext-bridge

💬 Messaging in Web Extensions made easy. Batteries included.
https://serversideup.net/open-source/webext-bridge
MIT License
547 stars 50 forks source link

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

Open hoangvu12 opened 1 year ago

hoangvu12 commented 1 year ago

I'm trying to post message from web to content scripts, but for some reason it keeps showing error Unchecked runtime.lastError: Could not establish connection. Receiving end does not exist.

image

Here is my content script (index.ts):

const handleDOMLoaded = async () => {
  import("./content");
};

if (document.readyState === "loading") {
  document.addEventListener("DOMContentLoaded", handleDOMLoaded);
} else {
  handleDOMLoaded();
}

content.ts:

import { allowWindowMessaging } from "webext-bridge/content-script";

allowWindowMessaging("test");

I have no idea why it causes that, it just happens whenever I call the allowWindowMessaging function.

songpengyuan commented 1 year ago

I also have this problem.

zikaari commented 1 year ago

Can you try not importing asynchronously? It might have something to do with that

@songpengyuan Can you also share your setup?

hoangvu12 commented 1 year ago

Can you try not importing asynchronously? It might have something to do with that

@songpengyuan Can you also share your setup?

I can't import webext-bridge without import another file asynchronously.

ty888 commented 4 months ago

@hoangvu12 Hello, have you resolved this issue?

ty888 commented 4 months ago

@hoangvu12 Hello, have you resolved this issue?

hoangvu12 commented 4 months ago

@hoangvu12 Hello, have you resolved this issue?

I ended up writing my own messaging bridge 😔

ty888 commented 4 months ago
image

It looks like I found the reason. I need to use a message in the background script, even if it's just a dummy message.

NickHeiner commented 2 months ago

Hmm, I put a message in my background script as well, but it didn't solve the issue. Looks like I'm off to do https://github.com/serversideup/webext-bridge/issues/67#issuecomment-2268541565. :)