0

When I am send a message from google chrome extension v3 popup to the background. shows error:

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

this is my popup.js send code:

chrome.runtime.sendMessage({greeting: "Hello"},function(response){
        alert(response);
      });

this is the background.js receive code:

chrome.runtime.onMessage.addListener(function (request,sender,sendResponse){
    if( request.greeting === "Hello" )
    {
        var tabURL = "Not set yet";
        debugger;
        chrome.tabs.query({active:true},function(tabs){
            if(tabs.length === 0) {
                sendResponse({});
                return;
            }
            // tabURL = tabs[0].url;
            sendResponse( {navURL:tabURL} );
        });        
    }
});

I have debugging the code and found the background will send a response to the popup, why still get this error? something I am missing?

liangqicai
  • 925
  • 5
  • 18
  • 1
    Add `return true` at the end of onMessage listener's `if` block. Also, you don't need messaging for this because your popup can access chrome.tabs directly. – wOxxOm Jan 30 '22 at 10:50

0 Answers0