I have a chrome extension I'm working on. My background.js script does something and returns a string (backgroundResponse) back to my content_script.js. This works fine as shown here:
webpage (index.html):
jQuery("#clickmenow").on("click", function (event)
{
document.dispatchEvent(new CustomEvent('funcDoRequest', {
'detail': { task: 'dir' }
}));
// I want backgroundResponse here instead somehow..?
});
<a href="#" id="clickmenow">Click me now</a>
background.js
document.addEventListener("funcDoRequest", function (stuff)
{
chrome.runtime.sendMessage(stuff.detail, function (backgroundResponse)
{ alert('Returning data from background.js: ' + backgroundResponse); });
});
My problem is I would like the response backgroundResponse to be returned synchronously to the calling jQuery on click somehow. So when someone clicks #clickmenow it ends up running my background.js stuff, and returns the value all the way back to the onclick (or dispatchEvent), not just to content_script.js which is what it is doing now. How can I do this? I can't figure it out.