I am making an extension and want to pass the message from the popup.html to content.js but the following code alert undefined. Please give me a simple script that send message from popup.html to content.js and vice versa, further i will handle it. I want to access the DOM through this extension for modifying and designing the website layouts.
Manifest
{
"manifest_version": 2,
"name": "Extension",
"description": "Description",
"version": "1.0",
"background": {
"scripts": ["background.js"],
"persistent": true
},
"content_scripts": [{
"matches": ["*"],
"js": ["content.js"]
}],
"browser_action": {
"default_icon": "icons/icon.png",
"default_popup": "popup.html"
},
"permissions":["tabs"]
}
popup.js
document.addEventListener('DOMContentLoaded',function(){
document.getElementById('button').onclick=function(){
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {greeting: "hello"}, function(response) {
alert(response);
});
});
}
});
Content.js
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
console.log(sender.tab ?
"from a content script:" + sender.tab.url :
"from the extension");
if (request.greeting == "hello")
sendResponse({farewell: "goodbye"});
});