I have this strange problem:
on content_script
:
function getText(){ var = text; chrome.extension.sendMessage({}, function(response){ text = response.data; }); return text; } if (getText()) { console.log('OK') } else{ console.log('Not OK') }
on background.js
:
var text = 'tests'; chrome.extension.onMessage.addListener(function(request, sender, sendResponse){ sendResponse( { 'data': text } ); })
Place a breakpoint on line return text
and you get OK
on console.
Disable breakpoints and you get only Not OK
.;
Seems to be some timing problem, like text
not being defined at the time of return, unless you give Chrome some time by using a breakpoint.
manifest.json:
{ ... "permissions": ["tabs"], "background": { "scripts": ["js/background.js"] }, "content_scripts": [ { ... "js": [ "js/content_script.js"], "run_at": "document_end" } ], "manifest_version": 2 }
Can someone reproduce this?