gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 13:33:02 25 4
gpt4 key购买 nike

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

基本上,在我的 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()... 但它也不会被调用。

介意告诉我哪里出错了吗?并为我提供使我的小示例运行的代码?

谢谢!R

编辑:我反了,现在我的代码如下:


list :

  "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_背景

<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>

流行音乐

<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>

最佳答案

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

也可能不是 if(request.function == "test3") 它应该是 if(request == "test2") (并且必须打开弹出窗口此时)

关于javascript - Chrome 消息传递,我做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6004220/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com