Chrome 消息传递,我做错了什么?
这不是一个真正的扩展,我只是想看看它是如何工作的。
基本上,在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
警报在弹出窗口中不起作用。请改用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 beif(request == "test2")
perhaps (and popup must be opened at this moment)