Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sendMessage from popup to content.js not working in chrome extension

I'm trying to make a popup interface for a chrome extension. I can't seem to send a message from the popup.html/popup.js to the content.js script. Here's what I have so far. When I click on the extension icon I get a button that says clickme. I click it and nothing happens, no errors in the chrome javascript console, and no message to content.js.

Manifest

{
"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'",
  "manifest_version": 2,
  "name": "extensiontest",
  "version": "0.2",
  "content_scripts": [
  {
    "matches": [
      "<all_urls>"
    ],
    "js": ["content.js"]
  }
],
"browser_action": {
  "default_icon": "Beaker.png",
    "default_popup":"popup.html"
},
"background": {
  "scripts": ["background.js"]
},
"permissions": [
    "tabs"
  ]
}

popup.html

<html>
<head></head>
<script src="popup.js"></script>
<body>
<input id="button1" type=button value=clickme>
</body></html>

popup.js

function popup(){
    alert(1);
      chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
    var activeTab = tabs[0];
    chrome.tabs.sendMessage(activeTab.id, {"message": "start"});
  });

button1=document.getElementById("button1");
button1.addEventListener('click', popup)
}

content.js

   chrome.runtime.onMessage.addListener(
      function(request, sender, sendResponse) {
        if( request.message === "start" ) {
         start();
             }
      }
    );

    function start(){
        alert("started");
    }
like image 378
techdog Avatar asked Apr 28 '15 17:04

techdog


2 Answers

I modified your popup.js and used DOMContentLoaded as Chrome extension suggested like:

popup.js:

function popup() {
    chrome.tabs.query({currentWindow: true, active: true}, function (tabs){
        var activeTab = tabs[0];
        chrome.tabs.sendMessage(activeTab.id, {"message": "start"});
    });
}

document.addEventListener("DOMContentLoaded", function() {
    document.getElementById("button1").addEventListener("click", popup);
});

content.js:

chrome.runtime.onMessage.addListener(
    function(request, sender, sendResponse) {
        if( request.message === "start" ) {
            start();
        }
    }
);

function start(){
    alert("started");
}

popup.html:

<!DOCTYPE html>
<html>
    <head>
        <script src="popup.js"></script>
    </head>
    <body>
        <input id="button1" type=button value=clickme>
    </body>
</html>

I've tested it on my end it works.

like image 127
Dayton Wang Avatar answered Nov 18 '22 23:11

Dayton Wang


The answer from 2015 still works on chrome version 88.0.4324.104. I just tested this. One thing I would update is instead of:

alert("started"); inside content.js use console.log("started");

You have your proof (Hit F12, console tab) that it worked which persists for you and you can verify the x number of clicks produces x number of "started" log entries.

Make sure you reload the target page when you "Load Unpacked" each time, that had me stuck for a moment.

my manifest.json looks like this:

{
    "name":"Chr Ext",
    "description":"blah blah",
    "version":"2",
    "minimum_chrome_version": "46",
    "manifest_version":2,
    "content_scripts": [
    {
        "matches": [
            "<all_urls>"
        ],
        "js": ["content.js"]
    }],
    "permissions": [
        "tabs"
    ],
    "browser_action": {
        "default_popup": "popup.html"
    }
}
like image 2
SchonSauce Avatar answered Nov 18 '22 23:11

SchonSauce