Problems to send message from content script to background script


(Computermaus) #1

I hope someone can help me.

I have a content script which shall start when the user enters my website. Then i want to send a message to the background script.
content script:

 var sending = browser.runtime.sendMessage({
     Call: "StartRead"
 });
 sending.then(handleResponse, handleError);  


function handleResponse(message) {
    console.log(`Message from the background script:  ${message.response}`);
}

function handleError(error) {
    console.log(`Error: ${error}`);
}

My background script:

 function handleMessage(request, sender, sendResponse) {    
   console.log("+++++ Message from the content script: " + request.Call);

   sendResponse({ response: "Response from background script" });

    if (request.Call == "StartRead") {
    
        Start();
     }
 }

 browser.runtime.onMessage.addListener(handleMessage);

When I start It I get the message in handleError:
Error: Error: Could not establish connection. Receiving end does not exist.

What does this mean? What is wrong in my scripts?


(Martin Giger) #2

That means either there is no background script loaded, or the content script isn’t loaded into the current page anymore.