22

I want to update the html in popup.html when I open it through the browser action button. The popup.js should send a message to the content script running on the current tab, and should receive a response and update the html. However the content script does not receive any message, therefore not sending a proper response.

Content.js

var text = "hello";
chrome.runtime.onMessage.addListener(
    function(message, sender, sendResponse) {
        switch(message.type) {
            case "getText":
                sendResponse(text);
            break;
        }
    }
);

Popup.js

chrome.tabs.getCurrent(function(tab){
    chrome.tabs.sendMessage(tab.id, {type:"getText"}, function(response){
        alert(response)
        $("#text").text(response);
    });
});

Manifest.json

{
  "manifest_version": 2,
  "name": "It's Just A Name",
  "description": "This extension is able to",
  "version": "1.0",
  "permissions" : ["tabs"],
  "browser_action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html",
    "default_title": "Click here!"
  },
  "content_scripts": [
  {
    "matches": ["https://*/*"],
    "js": ["jquery.min.js","content.js"]
  }]
}

Popup.html

<!doctype html>
<html>
    <head>
        <title>Title</title>
        <style>
            body {
                font-family: "Segoe UI", "Lucida Grande", Tahoma, sans-serif;
                font-size: 100%;
            }
            #status {
                white-space: pre;
                text-overflow: ellipsis;
                overflow: hidden;
                max-width: 400px;
            }
        </style>
        <script src="popup.js"></script>
    </head>
    <body>
        <p id="text"></p>
    </body>
</html>
Denis L
  • 2,996
  • 1
  • 20
  • 35
user3501676
  • 387
  • 1
  • 2
  • 8
  • 1
    tabs.getCurrent is not what you think it is. Use tabs.query. Always check API documentation before using a method for the first time. Also note, content scripts aren't automatically injected when you reload your extension. – wOxxOm Jul 19 '17 at 05:02

2 Answers2

24

chrome.tabs.getCurrent uses for:

Gets the tab that this script call is being made from

Your popup.js should be:

chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
    chrome.tabs.sendMessage(tabs[0].id, {type:"getText"}, function(response){
        alert(response)
        $("#text").text(response);
    });
});
Denis L
  • 2,996
  • 1
  • 20
  • 35
  • 3
    You need it to be `chrome.tabs.query({active:true,currentWindow:true}...`. You need to specify `currentWindow:true` in order to limit results only to the current window. If you don't, then you will get intermittent issues when the user has more than one window open (i.e. sometimes `tabs[0]` will be the active tab in the current window, sometimes it will be the active tab in some other window). – Makyen Jul 19 '17 at 18:46
  • @Deliaz how would one send a message/data from the content.js to the popup.js ? plz help – Dean Van Greunen Jan 11 '18 at 16:51
  • 1
    @DeanVanGreunen, check this question [How to send data from content script to popup.html](https://stackoverflow.com/questions/20019958/chrome-extension-how-to-send-data-from-content-script-to-popup-html) – Denis L Jan 11 '18 at 17:47
10

To add to above answer, You often want to send a msg from a popup to all tabs, so

popup:

chrome.tabs.query({}, tabs => {
    tabs.forEach(tab => {
    chrome.tabs.sendMessage(tab.id, msgObj);
  });
});

content script:

chrome.runtime.onMessage.addListener(msgObj => {
    // do something with msgObj
});
user3335966
  • 2,575
  • 4
  • 28
  • 33
kofifus
  • 14,411
  • 12
  • 82
  • 140