1

I have added a notification to my chrome extension and I get the following error:

Unchecked runtime.lastError: The message port closed before a response was received.

chrome.runtime.onMessage.addListener(
    function (request, sender, sendResponse) {
        if (request.type === 'RONotification') {
            createNotification(request).then(() =>{
                sendResponse({status: true});
            });
        } else {
            if (functions[request.func]) {
                if (!request.args) {
                    request.args = {};
                }
                request.args.sender = sender;
                functions[request.func].call(this, request.args).then((response) => {
                    sendResponse(response);
                });
            }
        }

        return true;
    });


function createNotification(request) {
    return new Promise((resolve, reject) => {
        try {
            let notificationId = `notification-${Date.now()}`;
            let notificationsOptions = createNotificationOptions(request);
            chrome.notifications.create(notificationId, notificationsOptions, function (res) {
                if (res) {
                    chrome.tabs.query({active: true, currentWindow: true}, function (tabs) {
                        chrome.tabs.sendMessage(tabs[0].id, {
                            notificationWasDisplayed: true,
                            notificationId: notificationId
                        }, function (response) {
                        });
                    });
                } else {
                    console.error("error create notification on background.js :", chrome.runtime.lastError);
                }
            });
        }
       catch (err) {
           console.error("exception create notification on background.js :", chrome.runtime.lastError);
       }
    });
}

I see that the code lines which cause the errors are :

   chrome.tabs.query({active: true, currentWindow: true}, function (tabs) {
                        chrome.tabs.sendMessage(tabs[0].id, {
                            RONotificationWasDisplayed: true,
                            notificationId: notificationId
                        }, function (response) {
                        });

index.js:

function chromeOnChangeListener(request, sender, response) {
    if (request.NotificationWasDisplayed === true) {
            notifications.setBadgeCounter(notificationConstants.liNotificationKey).then(function (res) {
            });
    }
    return true;
}

which is called on the cb from chrome.notification.create... How can I solve it?

Ortal Blumenfeld Lagziel
  • 2,415
  • 3
  • 23
  • 33
  • i see the lines which is the error origin are: chrome.tabs.query({active: true, currentWindow: true}, function (tabs) { chrome.tabs.sendMessage(tabs[0].id, { RONotificationWasDisplayed: true, notificationId: notificationId }, function (response) { }); – Ortal Blumenfeld Lagziel May 02 '21 at 18:10
  • its not related because i cooment out these line of code (the listner on index.js) and still get this error. the error is disappear only when I comment out the chrome.tabs,query.....(these lines of code which is written on the background.js file – Ortal Blumenfeld Lagziel May 02 '21 at 18:19
  • index.js: function chromeOnChangeListener(request, sender, response) { if (request.NotificationWasDisplayed === true) { notifications.setBadgeCounter(notificationConstants.liNotificationKey).then(function (res) { }); } return true; } – Ortal Blumenfeld Lagziel May 02 '21 at 18:25
  • If this is the entire code then the problem occurs because there's no sendResponse inside onMessage. – wOxxOm May 02 '21 at 18:27
  • More info: [Chrome extensions: 'Unchecked runtime.lastError: The message port closed before a response was received.' Is this the best we can do?](https://stackoverflow.com/a/59915897) – wOxxOm May 02 '21 at 18:29

0 Answers0