I'm facing a challenge in creating an Extension that transfers data from popup.html to the tab dome. Unfortunately, I am having trouble with getting the sendMessage function to work and I can't figure out why. I'm using the following guideline as my reference: https://developer.chrome.com/extensions/messaging.html
Below are the sources I am working with:
manifest:
{
"manifest_version": 2,
"name": "OMG Campaign Preview Maker",
"description": "Easy OMG Campaign Preview Maker",
"background": { "scripts": ["background.js"]},
"version": "0.1",
"content_scripts": [
{
"matches": ["http://*/*", "https://*/*"],
"js": ["jquery-1.7.min.js", "content.js"]
}
],
"browser_action": {
"default_icon": "logo_128.jpg",
"popup": "popup.html",
"default_popup": "popup.html"
},
"icons": {
"128": "logo_128.jpg"
}
}
popup.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="jquery-1.7.min.js"></script>
<script type="text/javascript" src="popup.js"></script>
</head>
<body>
<h1>Clicks</h1>
<p>extra1:
<input type="text" name="extraclicks[]" id="extra1">
<br>
extra2:
<input type="text" name="extraclicks[]" id="extra2">
<br>
extra3:
<input type="text" name="extraclicks[]" id="extra3">
<br>
<br>
<input type="submit" name="sendclicks" id="sendclicks" value="Invia Clicks">
</p>
</body>
</html>
popup.js
function sendClicks(){
console.log("popup.js > sendClicks()");
var clicksArr = new Array();
$("input[name='extraclicks\\[\\]']").each(function(i, value){
clicksArr.push($(this).val());
});
chrome.tabs.getSelected(null, function(tab) {
console.log("popup.js > tab id: "+tab.id)
chrome.tabs.sendMessage(tab.id, {type:"clicks" },
function(response) {
console.log(response.msg);
}
);
});
console.log("Has it been sent?");
}
$(function(){
console.log("popup.js > OMD Extension ready");
$('#sendclicks').click(function(){sendClicks();});
});
content.js
chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
console.log("content.js > onMessage");
if (request.type == 'clicks') {
console.log("content.js > clicks");
sendResponse({msg: "success"});
}
});
Even though I can see the logs generated by popup.js in the console perfectly, I am unable to see any of the logs triggered by content.js. What could be the mistake I am making?