javascript - Unable to retrieve message from background in popup of chrome extensions -


this question has answer here:

i trying communicate between popup.js file , background.js file chrome extension. when retrieving information using chrome storage, execution of callback no longer received client (popup.js)

within background.js file.

function gettotals(callback){   chrome.storage.sync.get(lmscats, function(data){     callback({"results": data}); // not work   }); }  chrome.runtime.onmessage.addlistener(   function(request, sender, sendresponse) {     // sendresponse({"message": "hi"}); //works     gettotals(sendresponse);   } } 

within popup.js referenced popup.html file

chrome.runtime.sendmessage({action: "total"}, function(data){     console.log(data); }); 

i not sure if i'm missing documentation appreciated. came across chrome extension onmessage wasn't sure if related or how around this.

i didn't read properly

chrome.runtime.onmessage.addlistener(   function(request, sender, sendresponse) {     // sendresponse({"message": "hi"}); //works     gettotals(sendresponse);     return true;   } } 

solved problem:

according documentation

this function becomes invalid when event listener returns, unless return true event listener indicate wish send response asynchronously (this keep message channel open other end until sendresponse called).

hope helps else in future.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -