Reputation: 51
I'm trying to build a chrome extension but I can not send message from popup to content script. Please tell me where's the problem.
The popup script code :
$(document).ready(function(){
$('#button').click(function(){
chrome.tabs.query({active: true, currentWindow: true},function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {message: "hello"});
});
});
});
The content script code :
chrome.extension.onMessage.addListener(
function(request, sender) {
alert("Contentscript has received a message from from background script: '" + request.message + "'");
});
this is manifest.json :
{
"manifest_version" : 2,
"name" : "First My Chrome Extension",
"version" : "0.0.1",
"description" : "The first web-app using chrome extension technology",
"icons" : {
"16" : "images/icon16px.png",
"48" : "images/icon48px.png",
"128" : "images/icon128px.png"
},
"background": {
"scripts": ["scripts/background.js"]
},
"browser_action" :{
"default_icon":{
"48" : "images/icon48px.png"
},
"default_title" : "Nghoangvutn",
"default_popup" : "popup.html"
},
"content_scripts":[
{
"matches":[
"<all_urls>"
],
"js" : [
"scripts/jquery.js",
"scripts/content.js"
]
}
],
"permissions" : [
"tabs",
"storage"
]
}
Upvotes: 3
Views: 5972
Reputation: 1832
The content script code should be updated to use the following method chrome.runtime.onMessage.addListener
. Note the change from chrome.extension
to chrome.runtime
.
chrome.runtime.onMessage.addListener(
function(request, sender) {
alert("Contentscript has received a message from from background script: '" + request.message + "'");
});
Upvotes: 2