Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Registering a ProxyService before browser.runtime.onMessage.addListener throws an error on the Content Script. #63

Open
charlie632 opened this issue Jul 18, 2024 · 1 comment

Comments

@charlie632
Copy link
Contributor

charlie632 commented Jul 18, 2024

Just had a really weird bug.

If I have this:

export default defineBackground(() => {
    const globalStateService = registerGlobalStateService();

    const recorderService = registerRecorderService(
        globalStateService,
    );

    browser.runtime.onMessage.addListener((message, sender, sendResponse) => {
        if (message.type === "get-tab-id" && sender.tab?.id) {
            console.log("get-tab-id", sender.tab.id);

            return Promise.resolve({ tabId: sender.tab.id });
        }
        return;
    });
});

And from the content script I do this

export default defineContentScript({
    matches: ["<all_urls>"],
    allFrames: true,
    runAt: "document_start",
    async main() {
        await browser.runtime.sendMessage({type: 'get-tab-id});
    }
});

I get the following error on my content script:

Error: [messaging] Unknown message format, must include the 'type' & 'timestamp' fields, received: {"type":"get-tab-id","sender":{"id":"jeiahcjhhajlffdcbfcpigkeiahnnfbg","url":"https://es.wikipedia.org/wiki/Albatros_D.III","origin":"https://es.wikipedia.org","frameId":0,"documentId":"07EC29AE46A842CD148F5037FDF6669B","documentLifecycle":"active","tab":{"active":true,"audible":false,"autoDiscardable":true,"discarded":false,"favIconUrl":"https://es.wikipedia.org/static/favicon/wikipedia.ico","groupId":-1,"height":924,"highlighted":true,"id":527771240,"incognito":false,"index":0,"lastAccessed":1721279029617.227,"mutedInfo":{"muted":false},"pinned":false,"selected":true,"status":"loading","title":"Albatros D.III - Wikipedia, la enciclopedia libre","url":"https://es.wikipedia.org/wiki/Albatros_D.III","width":529,"windowId":527771239}}}
    at wrappedSendMessageCallback (browser-polyfill.js:1188:18)

However, if I move the background script like this:

export default defineBackground(() => {

+    browser.runtime.onMessage.addListener((message, sender, sendResponse) => {
+        if (message.type === "get-tab-id" && sender.tab?.id) {
+            console.log("get-tab-id", sender.tab.id);
+
+           return Promise.resolve({ tabId: sender.tab.id });
+        }
+        return;
+    });

    const globalStateService = registerGlobalStateService();

    const recorderService = registerRecorderService(
        globalStateService,
    );

-    browser.runtime.onMessage.addListener((message, sender, sendResponse) => {
-        if (message.type === "get-tab-id" && sender.tab?.id) {
-            console.log("get-tab-id", sender.tab.id);
-
-           return Promise.resolve({ tabId: sender.tab.id });
-        }
-        return;
-    });
});

Moving the runtime listener before the services, it will work as expected.

Not sure if it has something to do with the listener registration, but I was having a really hard time.

Dependencies

  • wxt 0.18.11
  • @webext-core/proxy-service 1.2.0
@aklinker1
Copy link
Owner

Yeah, this is definitely a big, it should work either way... I'll look into this when I can

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants