I make an extension for chrome. I need to send a message to background from my content file and get an answer.
I send from content so:
chrome.runtime.sendMessage({type:'test', method: 'getHtml', data: 'test'}, {}, function (response) { console.log(response); }); background message handler:
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) { console.log('chrome.runtime.onMessage', request, sender); if(request&&request.type&&request.method) { sendResponse(window[request.type][request.method](request.data)); } }); getHtml in background :
function getHtml (name) { // fetch не работает. let html = fetch('/html/'+name+'.html').then(function(response) { return response.text(); }); return html; // Promise тоже не работает. return $.get('/html/'+name+'.html').then(result => { console.log(result); return result; }); } The received data is output to the console, but in response an empty object comes to content .
What am I doing wrong?
Examples are taken from here - do not work
Tritely, this example does not work, but although it is written in the answer to another question:
function getResult() { var q1 = $.get('/html/test.html'); var q2 = q1.then(function (data) { return data; }); console.log('ffffffff'); return q2; } In theory, the request should first work out and then exit the console. But the opposite is happening.