Trouble with sendMessage from popup.js to content.js

2,771 views
Skip to first unread message

Dan Savage

unread,
Apr 28, 2015, 10:10:14 PM4/28/15
to chromium-...@chromium.org

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. I've been reading up on this for 2 days and there are examples of this working, but I can't get my example to work.


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");
    }

Onno van der Zee

unread,
Apr 29, 2015, 5:55:04 PM4/29/15
to chromium-...@chromium.org
You did not set up a connection and a listener.

in popup.js you need to connect:
var port = chrome.tabs.connect(...
then post
port.postMessage(...

in content.js you need to set up a onconnect listener:
chrome.runtime.onConnect.addListener(function (...
port.onMessage.addListener(...

succes.

Op woensdag 29 april 2015 04:10:14 UTC+2 schreef Dan Savage:

prabakaran....@borngroup.com

unread,
Oct 29, 2018, 3:17:52 PM10/29/18
to Chromium Extensions, hastal...@dds.nl
Could you give me an example program?

Darren Govoni

unread,
Oct 29, 2018, 3:55:09 PM10/29/18
to prabakaran....@borngroup.com, chromium-...@chromium.org, hastal...@dds.nl
You can just use window.postMessage({field:value},"*");

::DISCLAIMER::
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
The contents of this e-mail and any attachment(s) are confidential and intended for the named recipient(s) only. E-mail transmission is not guaranteed to be secure or error-free as information could be intercepted, corrupted, lost, destroyed, arrive late or incomplete, or may contain viruses in transmission. The email and its contents (with or without referred errors) shall therefore not attach any liability on the originator or BORN or its affiliates. Views or opinions, if any, presented in this email are solely those of the author and may not necessarily reflect the views or opinions of BORN or its affiliates. Any form of reproduction, dissemination, copying, disclosure, modification, distribution and/or publication of this message without the prior written consent of authorized representative of BORN is strictly prohibited. If you have received this email in error please delete it and notify the sender immediately. Before opening any email and/or attachments, please check them for viruses and other defects.

--
You received this message because you are subscribed to the Google Groups "Chromium Extensions" 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/8e89b340-50c3-4a72-afed-0ad1aa4c6741%40chromium.org.
For more options, visit https://groups.google.com/a/chromium.org/d/optout.

Prabakaran Palaniyandi

unread,
Oct 29, 2018, 5:31:55 PM10/29/18
to darre...@gmail.com, chromium-...@chromium.org, hastal...@dds.nl
Thank you! 

Prabakaran P

Front End Developer, BORN

India Direct Dial: +91 9942352417

www.borngroup.com

2nd Floor, Hardy Tower, Ramanujan IT SEZ,

TRIL Infopark Limited, Taramani, Chennai – 600 113

www.borngroup.com


puja...@gmail.com

unread,
Oct 29, 2018, 6:20:57 PM10/29/18
to chromium-...@chromium.org
BitBounce
     
     

Hello! I use a new email filtering service called BitBounce to filter my email. To deliver your email to my inbox, please click the button below and pay the small cryptocurrency fee. Thank you!

$0.05 to deliver your email.

We’ve never met — I’ll pay your fee.

I know you — Add me to your whitelist.

     
     

BitBounce is powered by
 the Credo cryptocurrency

I’m from a business — 
what are my delivery options

BitBounce and Credo are
transacted through CredoEx

Made by Turing Technology Inc. in San Mateo, California Sign Up for BitBounce

puja...@gmail.com

unread,
Oct 29, 2018, 6:21:02 PM10/29/18
to chromium-...@chromium.org
Reply all
Reply to author
Forward
0 new messages