Chrome messenger, send info from background page to popup

2,092 views
Skip to first unread message

Jim Heald

unread,
Jul 4, 2016, 12:04:59 PM7/4/16
to Chromium-Extensions-Announce
So I have been having trouble getting the message system to work. I made a simple example to try to get the text of the first header on my site, send it to the popup window, and have the window display it. Here are my three files in the extension:

popup.html
<html>
    <head>
        <script src=populatePopup.js></script>
    </head>

    <body>
        <p id="fillMe"></p>
    </body>

</html>

getInfo.js (content script)
var title = document.querySelector("h3");

chrome.runtime.sendMessage(title);
console.log("Sent message!");

populatePopup.js
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse){
    if(message) {
        console.log("Got message!");
        var fill = document.getElementById("fillMe");
        fill.innerHTML = "The first h3 found was " + message.innerHTML;
        return true;
    }
});

However, my popup never displays anything. How can I get this to work? Thanks!

Jim Heald

unread,
Jul 4, 2016, 12:07:10 PM7/4/16
to Chromium-Extensions-Announce
Oh and in the console I get my sent confirmation but "Got message!" never appears, leading me to believe it doesn't receive the message 

PhistucK

unread,
Jul 4, 2016, 12:55:19 PM7/4/16
to Jim Heald, Chromium-Extensions-Announce
The popup (well, either a tab that shows popup.html, or the browser action popup that shows popup.html) must be open when you send the message (and not only after you send). Is it open?


PhistucK

--
You received this message because you are subscribed to the Google Groups "Chromium-Extensions-Announce" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To post to this group, send email to chromium-...@chromium.org.
Visit this group at https://groups.google.com/a/chromium.org/group/chromium-extensions/.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/76e3ecdf-4d04-4f4b-a391-71dc6eedf6ea%40chromium.org.

Jim Heald

unread,
Jul 4, 2016, 1:22:29 PM7/4/16
to PhistucK, Chromium-Extensions-Announce
No, and having it open doesn't work for my purposes. popup.html is what you see when you click the extension and this is getting the communication elements to work before I make my main extensions I've been working on.

My main extension is a survey auto-clicker. It will get the elements it should click from the storage. However, each time the survey page is opened it will search for multiple choice questions, find the responses, and send it to my populatePopup.js which will create a dropdown in the extension window that allows you to select which answers you want automatically filled. Therefore, the popup will only be brought up every now and then and will be dynamically generated. Unless... should my populatePopup.js call the getInfo script?

PhistucK

unread,
Jul 4, 2016, 3:23:35 PM7/4/16
to Jim Heald, Chromium-Extensions-Announce
I believe it is more efficient for your extension to use chrome.tabs.executeScript within the popup instead of using a content script. Beside not loading useless code even when you do not need it, this also makes (kind of) sure that the popup is open when the messages are sent.


PhistucK
Reply all
Reply to author
Forward
0 new messages