Ryan
Ryan

Reputation: 10049

Chrome message passing, what am I doing wrong?

This is not a real extension, I'm just screwing around to see how this works.

Basically, in my background.html page I have this:

function test3()
{
alert("blah3");
chrome.extension.sendRequest('test2');
}

and in my popup.html page, I have this:

chrome.extension.onRequest.addListener(
    function(request, sender, sendResponse) {
        if(request.function == "test3") {
           alert("Works!");
           }
    }
);

But the alert "Works" never seems to be called... I even tried to replace the

 alert("Works!");

with call_test_function();

which in turn has an alert()... but that does not get called either.

Mind telling me where I went wrong? And giving me the code to make my little example work?

Thanks! R

EDIT: I reversed it, now my code is as below:


Manifest:

  "name": "RyanTest for Chrome",
  "version": "0.1",
  "description": "Ryan testing messaging!",
     "background_page": "mf_background.html",
  "browser_action": {
    "default_icon": "icon.png",
    "popup": "pop_mf.html"

mf_background

<head>
<style type="text/css">
.style1 {
    font-family: Arial, Helvetica, sans-serif;
}
.style2 {
    font-size: x-small;
    font-family: Arial, Helvetica, sans-serif;
}
.style3 {
    margin-top: 1px;
}
.style4 {
    font-size: x-small;
}
.style5 {
    font-size: x-small;
    font-family: Arial, Helvetica, sans-serif;
    text-align: center;
}
</style><script>
var b="100";
chrome.extension.onRequest.addListener(
    function(request, sender, sendResponse) {
        if(request.function == "test2") {
           alert("Works!");
           console.log("This is in popup!");
           }
    }
);


</script>


</head>

pop_mf

<html><head><style>
body {
  min-width:357px;
  overflow-x:hidden;
}


</style>

<script>
// <!--

function test3()
{
//alert(b);
chrome.extension.sendRequest('test2');
}


test3();
// -->
</script></head><body>RyanTEST
</body></html>

Upvotes: 1

Views: 223

Answers (1)

serg
serg

Reputation: 111335

Alerts don't work in popups. Use console.log instead.

Also instead of if(request.function == "test3") it should be if(request == "test2") perhaps (and popup must be opened at this moment)

Upvotes: 3

Related Questions