Issue with message passing in manifest V3 extension

385 views
Skip to first unread message

Sagar Mittal

unread,
Aug 2, 2023, 2:27:06 AM8/2/23
to Chromium Extensions

Hi all,

 

I need to migrate a browser extension to manifest V3 and I am facing an issue with message passing. I am new to this, so please forgive if my question seems novice.

 

We are using chrome.runtime.sendMessage() API to send a message via Popup:

 

 

class ChromeBroadcastService {

    sendMessage(message: any, callback?: Function): void {

        chrome.runtime.sendMessage(message, callback);

    }

}

 

And we have defined a listener inside another class called ChromeSubscriptionService like this. It is supposed to listen to the sent message: 

 

class ChromeSubscriptionService {

    constructor() {

        this.listenToMessages((notificationType: NotificationType, data: any, sendResponse: Function, sender: chrome.runtime.MessageSender) => {

            // some method call

        });

    }

    private listenToMessages(callback: Function): void {

        chrome.runtime.onMessage.addListener((message: any, sender: chrome.runtime.MessageSender, sendResponse: Function) => {

            if (message) {

                callback(message.notificationType, message.data, sendResponse, sender);

                if (sendResponse) {

                    return true;

                } else {

                    return false;

                }

            }

        });

    }

}

 

 

This used to work fine for manifest V2, but the listener is not able to receive the message for V3.  Is there anything that I am missing here?

Thanks

wOxxOm

unread,
Aug 2, 2023, 4:20:42 AM8/2/23
to Chromium Extensions, Sagar Mittal
This code is fine so the problem is elsewhere (in other code). Do you see an error? Did you debug the code in devtools?

Sagar Mittal

unread,
Aug 8, 2023, 6:58:20 AM8/8/23
to Chromium Extensions, wOxxOm, Sagar Mittal
Hi, thanks for your reply. I debugged and found out that it is due to some errors related to the service worker itself.
Reply all
Reply to author
Forward
0 new messages