Chrome扩展程序sendMessage从popup.html到content.js不起作用

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

我正尝试构建一个将popup.html的数据发送到Tab圆顶的扩展,但是我无法使sendMessage正常工作,我不明白为什么。 我遵循以下指导原则: developer.chrome/extensions/ messaging.html

I'm trying to build an Extension which sends data from the popup.html to the tab dome, but I can't get the sendMessage to work and I don't understand why. I'm following this guidline: developer.chrome/extensions/messaging.html

这里是我的来源:

here are my sources:

manifest: { "manifest_version": 2, "name": "OMG Campaign Preview Maker", "description": "Easy OMG Campaign Preview Maker", "background": { "scripts": ["background.js"]}, "version": "0.1", "content_scripts": [ { "matches": ["*/*", "*/*"], "js": ["jquery-1.7.min.js", "content.js"] } ], "browser_action": { "default_icon": "logo_128.jpg", "popup": "popup.html", "default_popup": "popup.html" }, "icons": { "128": "logo_128.jpg" } }

popup.html

popup.html

<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script type="text/javascript" src="jquery-1.7.min.js"></script> <script type="text/javascript" src="popup.js"></script> </head> <body> <h1>Clicks</h1> <p>extra1: <input type="text" name="extraclicks[]" id="extra1"> <br> extra2: <input type="text" name="extraclicks[]" id="extra2"> <br> extra3: <input type="text" name="extraclicks[]" id="extra3"> <br> <br> <input type="submit" name="sendclicks" id="sendclicks" value="Invia Clicks"> </p> </body> </html>

popup.js

popup.js

function sendClicks(){ console.log("popup.js > sendClicks()"); var clicksArr = new Array(); $("input[name='extraclicks\\[\\]']").each(function(i, value){ clicksArr.push($(this).val()); }); chrome.tabs.getSelected(null, function(tab) { console.log("popup.js > tab id: "+tab.id) chrome.tabs.sendMessage(tab.id, {type:"clicks" }, function(response) { console.log(response.msg); } ); }); console.log("avra' inviato?"); } $(function(){ console.log("popup.js > OMD Extension ready"); $('#sendclicks').click(function(){sendClicks();}); });

content.js

content.js

chrome.extension.onMessage.addListener(function(request, sender, sendResponse) { console.log("content.js > onMessage"); if (request.type == 'clicks') { console.log("content.js > clicks"); sendResponse({msg: "success"}); } });

虽然我完全可以在控制台中看到弹出式日志弹出的日志,但是我没有看到由content.js发起的任何日志。 我在做什么错了?

While I can perfectly see the logs gnerated from popup.js in the console, I don't see any of the logs launched by content.js. What am I doing wrong?

推荐答案

好的,我很笨... 错了事情是...我正在看的检查员页面。 console.log内的content.js打印在 TAB INSPECTOR CONSOLE 上,不在扩展名之一!

Ok, I'm stupid... the wrong thing was... the inspector page I was watching. "console.log" inside of the content.js prints on the TAB INSPECTOR CONSOLE not on the extension one!

I希望它能帮助别人。

I hope it will help others.

更多推荐

Chrome扩展程序sendMessage从popup.html到content.js不起作用

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

发布评论

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

>www.elefans.com

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