I know this is very common question and I've already read lots of docs and answers to similar situations, but I still can't get it work. So I'm sorry, but my question is how to make content script get the message from popupjs.
Workflow is: when user clicks extension icon, it should show popup with some options. User clicks on options, and extension makes some stuff depending on which options was clicked.
I tried following:
manifest.js
{
"name": "MyName",
"version": "1",
"description": "My chrome extension",
"browser_action": {
"default_popup": "popup.html"
},
"content_scripts": [{
"matches": ["http://*/*", "https://*/*"],
"css": [
"style.css"
],
"js": [
"bower_components/jquery/dist/jquery.js",
"bower_components/bootstrap/dist/js/bootstrap.js",
"content.js"
]
}],
"permissions": [
"activeTab",
"http://127.0.0.1:8000/"
],
"manifest_version": 2
}
popup.html
<!doctype html>
<html>
<head>
<title>My Extension</title>
<script src="popup.js"></script>
</head>
<body>
<ul>
<li id="push" class="option">Push</li>
<li>Actions
<ul>
<li id="dashboard" class="option">Dashboard</li>
<li id="list" class="option">Your Lists</li>
<li id="settings" class="option">Settings</li>
</ul>
</li>
</ul>
</body>
</html>
popup.js
function click(e) {
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
console.log('message sent');
chrome.tabs.sendMessage(tabs[0].id, {text:"getStuff", action: e.target.id});
});
window.close();
}
document.addEventListener('DOMContentLoaded', function () {
var options = document.querySelectorAll('.option');
for (var i = 0; i < options.length; i++) {
options[i].addEventListener('click', click);
}
});
content.js
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if (request.text == "getStuff") {
console.log('test sent');
}
});
So I cant's see any console logs (should it be shown in usual console of the page?), and any code which I put inside chrome.runtime.onMessage.addListener doesn't work.
I do not quite understand why it is not working. I tried also using background script to listen for message from popup.js, and then pass message from background script to content.js, and it doesn't work also