Chrome 消息传递,我做错了什么?

发布于 2024-11-07 14:09:17 字数 1965 浏览 0 评论 0原文

这不是一个真正的扩展,我只是想看看它是如何工作的。

基本上,在我的 background.html 页面中,我有这个:

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

在我的 popup.html 页面中,我有这个:

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

但警报“Works”似乎从未被调用过...... 我什至尝试

 alert("Works!");

用 call_test_function();

它又具有一个alert()...但也不会被调用。

介意告诉我我哪里错了吗?并给我代码让我的小例子工作?

谢谢! 编辑

: 我颠倒了它,现在我的代码如下:


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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温暖的光 2024-11-14 14:09:17

警报在弹出窗口中不起作用。请改用console.log。

另外,也许应该是 if(request == "test2") 而不是 if(request.function == "test3") (此时必须打开弹出窗口)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文