javascript - Chrome Extension Message passing: response not sent -
javascript - Chrome Extension Message passing: response not sent -
i trying pass messages between content script , extension
here have in content-script
chrome.runtime.sendmessage({type: "geturls"}, function(response) { console.log(response) });
and in background script have
chrome.runtime.onmessage.addlistener( function(request, sender, sendresponse) { if (request.type == "geturls"){ geturls(request, sender, sendresponse) } }); function geturls(request, sender, sendresponse){ var resp = sendresponse; $.ajax({ url: "http://localhost:3000/urls", method: 'get', success: function(d){ resp({urls: d}) } }); }
now if send response before ajax phone call in geturls
function, response sent successfully, in success method of ajax phone call when send response doesn't send it, when go debugging can see port null within code sendresponse
function.
from the documentation chrome.runtime.onmessage.addlistener
:
this function becomes invalid when event listener returns, unless homecoming true event listener indicate wish send response asynchronously (this maintain message channel open other end until sendresponse called).
so need add together return true;
after phone call geturls
indicate you'll phone call response function asynchronously.
javascript google-chrome google-chrome-extension google-chrome-app
Comments
Post a Comment