-2

So I've already asked this question, but the responses given didn't work (I've updated this post with a few of the suggestions from before)... Here is the link to that question in case anybody who wants to take a stab at this also wants to see what what was said before.

Chrome extension: sendMessage doesn't work

I've already read the documentation from Google on 'message passing' a few times and have probably looked at over 10 other questions with the same problem and already tried quiet a few variations of most of their "solutions" and of what I have below... This is black magic, right? Either way, here it goes.

manifest.json File:

{
    "manifest_version" : 2,
    "name" : "Message Test",
    "version" : "1.0",

    "browser_action": { 
        "default_popup": "popup.html"
    },

    "content_scripts": [
        {
        "matches" : ["<all_urls>"],
        "js": ["message-test.js"]
        }
    ]    
}

popup.html File:

<html>
<body>
<script type="text/javascript" src="popup.js"></script>
</body>
</html>

popup.js File:

chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
    var tab = tabs[0];  // do not forget to declare "tab" variable
    chrome.tabs.sendMessage(tab.id, {
        greeting: "Can you hear me?"
    }, function(response){});
});

message-Test.js File:

chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
    if (message.greeting == "Can you hear me?"){
        alert("Test");
    }
    else{
        sendResponse({});
    }
});

The alert("Test") does not go off...

Community
  • 1
  • 1
user3334776
  • 113
  • 1
  • 10
  • It works for me. Debug it with console.log() function to find out at which line it gets stuck. Also replace the alert() by this log(). Then you can check the consoles. Note: the popup window has a different console (right click>inspect element) from the content console (F12). – Skalár Wag Apr 19 '14 at 18:58

1 Answers1

0

Your code works perfectly for me. Your content script is only being injected on page load, and the message is only being passed to the first tab open-- to confirm make sure your first tab is refreshed after the extension is loaded, and then click your popup button.

Sean Adams
  • 453
  • 3
  • 7
  • Interesting, its working. Why is it only going off if I click the extension button? Isn't it suppose to go off when I load the webpage? – user3334776 Apr 19 '14 at 20:38
  • How do I make it go off when the user clicks an html button? – user3334776 Apr 19 '14 at 20:39
  • Popup.js is only triggered when the popup is displayed (i.e. when the button is clicked). – Sean Adams Apr 19 '14 at 20:40
  • Could I do something similar to a
    for an extension if I can't do inline javascript with html and I don't want the message to be passed when I click the extension button? I'm also unsure of how to replace my window.onload scripts for extensions too, that's an inline problem right? lol
    – user3334776 Apr 19 '14 at 20:52
  • Are you looking to have the user click a button within the popup.html to trigger the message? – Sean Adams Apr 19 '14 at 22:57
  • Yes I am. I want to give the user the ability to change the way the extension behaves before it executes. – user3334776 Apr 20 '14 at 00:11