0

I try to create an chrome extension who works with an another API. So my content script get the data in DOM and ask the API the result (in background.js)

So, I send a message from contentscript.js to background.js and I wait the response of my API since sendResponse. But, I have a problem the response is always undefined because contentscript.js receive the response except I don't yet send the response.

contentscript.js :

chrome.runtime.sendMessage({type: "new", data: {/* ... */}}, function(response) {
    console.log(response);
});

background.js :

chrome.runtime.onMessage.addListener(function(msg, _, sendResponse) {
    if (msg.type == "new") {

        execute(msg.data).then(function (response) {
            sendResponse(response);
        });
    }
});

function execute(data){
    return new Promise( function (resolve, reject){

        /* ... */

        $.ajax(options).done(function(data) {
            return resolve(data);
        });
    });

}

What's the solution ?

I need to add onMessageListener in my contentscript and background, so, I don't use sendResponse ?

Thanks

MatinGarrix
  • 77
  • 1
  • 9

0 Answers0