I'm trying to send messages from the background page to a content script, then send a message from that content script to an injected script. I've tried this, but it isn't working.
Here's what my code looks like.
manifest.json
{
manifest_version: 2,
name: NAME,
description: :D,
version: 0.0,
permissions: [
tabs,<all_urls>
],
content_scripts: [
{
matches: [<all_urls>],
js: [content_script.js]
}
],
web_accessible_resources: [
injected.js
],
background:{
scripts:[background.js]
}
}
background.js
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {greeting: hello}, function(response){});
});
content_script.js
var s = document.createElement('script');
s.src = chrome.extension.getURL('injected.js');
s.onload = function(){
this.parentNode.removeChild(this);
};
(document.head||document.documentElement).appendChild(s);
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
document.dispatchEvent(new CustomEvent('Buffer2Remote', {todo: LOL}));
});
injected.js
document.addEventListener('Buffer2Remote', function(e){
alert(e.todo);
});
The message sending doesn't work from the first part, background -> content_script.
Is there anything wrong with my code?