Chrome扩展程序:sendMessage不起作用

编程入门 行业动态 更新时间:2024-10-28 08:21:43
本文介绍了Chrome扩展程序:sendMessage不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经多次阅读了谷歌关于消息传递的文档,并且可能已经查看了10多个同样问题的其他问题,并且已经尝试了大部分解决方案的几个变体以及我有下面...这是黑魔法,对吧?

清单文件:

{ manifest_version:2,name:Message Test,version:1.0, browser_action:{default_popup:popup.html}, background:{scripts:[background.js] } , content_scripts:[ {matches:[< all_urls>],js:[message-test。 js] } ] }

我知道扩展不假设使用内联JS,但我离开这个,所以原来的问题可以留下来,因为我仍然无法从背景页面发送消息,当我从弹出到后台,我从manifest.json中删除了相应的行

popup.html文件:

< HTML> < head> < script> chrome.tabs.query({active:true,currentWindow:true},function(tabs){ chrome.tabs.sendMessage(tabs [0] .id,{greeting:hello,theMessage :为什么不能这样工作?},function(response){ console.log(response.farewell); }); }); < / script> < / head> < body> < / body> < / html>

OR

background.js文件:

chrome.tabs.query({active:true,currentWindow:true},function(tabs){ chrome .tabs.sendMessage(tabs [0] .id,{greeting:hello,theMessage:为什么没有这个工作?),function(response){ console.log(response.farewell ); }); });

message-test.js file:

var Mymessage; chrome.runtime.onMessage.addListener(function(message,sender,sendResponse){ if(message.greeting ==hello){ Mymessage = message.theMessage; alert(Mymessage); } else { sendResponse({}); } });

我得到一个未定义的警报...

我也试图在按下弹出式窗口中的按钮并在指定的URL上有一个窗口后执行此操作,但这是后面的问题。除了在addEventListener(click....: pastebin/KhqxLx5y AND http:// pastebin/JaGcp6tj

解决方案

您的代码有几个问题。

Chrome不允许在扩展程序中使用内嵌脚本,您必须将popup.html划分为脚本+ HTML: $ b

// popup.html < html> < body> < script type =text / javascriptsrc =popup。 js>< / script> < / body> < / html> // popup.js chrome.tabs.query {active:true,currentWindow:true},function(tabs){ var tab = tabs [0]; //不要忘记声明tab变量 chrome.tabs.sendMessage(tab。 ID,{问候ing:你能听到我吗?},function(response){}); });

I've already read the documentation from Google on 'message passing' a few times and have probably looked at over 10 other questions with the same problem and already tried quiet a few variations of most of their "solutions" and of what I have below... This is black magic, right? Either way, here it goes.

Manifest File:

{ "manifest_version" : 2, "name" : "Message Test", "version" : "1.0", "browser_action": { "default_popup": "popup.html" }, "background": { "scripts": ["background.js"] }, "content_scripts": [ { "matches" : ["<all_urls>"], "js": ["message-test.js"] } ] }

I'm aware extensions aren't suppose to use inline JS, but I'm leaving this in so the original question can be left as it was since I still can't get the message to send from the background page, When I switch from the popup to the background, I removed the appropriate lines from the manifest.json

popup.html file:

<html> <head> <script> chrome.tabs.query({active: true, currentWindow: true}, function(tabs) { chrome.tabs.sendMessage(tabs[0].id, {greeting: "hello", theMessage: "Why isn\'t this working?"}, function(response) { console.log(response.farewell); }); }); </script> </head> <body> </body> </html>

OR

background.js file:

chrome.tabs.query({active: true, currentWindow: true}, function(tabs) { chrome.tabs.sendMessage(tabs[0].id, {greeting: "hello", theMessage: "Why isn\'t this working?"}, function(response) { console.log(response.farewell); }); });

message-test.js file:

var Mymessage; chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) { if (message.greeting == "hello"){ Mymessage = message.theMessage; alert(Mymessage); } else{ sendResponse({}); } });

I get an undefined alert...

I'm also trying to execute this after pressing a button from a popup and having a window at a specified url, but that's a later issue. The other files for the button and window can be found here except with the background.js content wrapped in an addEventListener("click"....: pastebin/KhqxLx5y AND pastebin/JaGcp6tj

解决方案

There are several issues in your code.

Chrome doesn't allow inline scripts in extensions. You must divide your popup.html to script + HTML:

// popup.html <html> <body> <script type="text/javascript" src="popup.js"></script> </body> </html> // popup.js chrome.tabs.query({active: true, currentWindow: true}, function(tabs) { var tab = tabs[0]; // do not forget to declare "tab" variable chrome.tabs.sendMessage(tab.id, { greeting: "Can you hear me?" }, function(response){}); });

更多推荐

Chrome扩展程序:sendMessage不起作用

本文发布于:2023-08-06 04:55:04,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不起作用   程序   Chrome   sendMessage

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!