1

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

jenush
  • 173
  • 1
  • 1
  • 6
  • Your code seems fine to me. Did you reload the page in question after (re)loading the extension? [Chrome does not automatically inject scripts into open tabs](http://stackoverflow.com/a/23895822/934239). Content script logs go to the page's log, popup's logs can be examined with a right-click on the button, "Inspect popup". – Xan Nov 07 '14 at 12:16
  • 3
    Try moving `window.close();` inside the callback function for `chrome.tabs.query`. – rsanchez Nov 07 '14 at 17:30
  • 3
    @rsanchez Or maybe even have the response function close the window. – Teepeemm Nov 07 '14 at 19:21
  • @rsanchez Care to make that an answer? – Xan Nov 08 '14 at 16:18

0 Answers0