Chrome 扩展:消息传递混乱
我正在尝试将页面上已检查项目的数量发送到弹出窗口。弹出警报上的添加
输入按钮会向控制台发送网页上选中的复选框数量。
我的脚本没有这样做,我认为消息传递的工作原理可能会出现混乱。这是我所拥有的:
清单:
{
"name": "A plugin for...",
"version": "1.0",
"background_page": "background.html",
"permissions": [
"tabs", "http://*/*", "https://", "*"
],
"content_scripts": [
{
"matches": ["http://*/*","https://*/*"],
"js": ["main_content_script", jquery.min.js", "json.js"]
}
],
"browser_action": {
"name": "Plugin",
"default_icon": "icon.png",
"popup": "popup.html"
}
}
Popup.html
<html>
<script src="jquery.min.js"></script>
<script>
$(document).ready(function() {
$('#add').click(function(){
add();
alert("add clicked");
});
$('#save').click(function(){
alert("save clicked");
});
$('#delete').click(function(){
alert("delete clicked");
});
});
</script>
<script>
//chrome.tabs.sendRequest(integer tabId, any request, function responseCallback)
//chrome.extension.onRequest.addListener(function(any request, MessageSender sender, function sendResponse) {...}));
function add(){
chrome.extension.onRequest.addListener(function(request, sender, sendResponse)
console.log(request.count); //? nothing in console
});
</script>
<form action="http://www.xxxxx.com/xxxxx/xxxx/session.php" method="post" enctype="text/plain">
<input type="submit" value="Add" name="add" id="add"/>
<input type="submit" value="Save" name="save" id="save"/>
<input type="submit" value="Delete" name="delete" id="delete"/>
</form>
</html>
main_content_script.js
$(document).ready(function() {
//var location = window.location.href;
var checked = $('input:checkbox:checked').length;
if(checked > 0){
var values = $('input:checkbox:checked').map(function () {
//return $(this).parent().next().html(); --> returns the name
var strong_tag = $(this).parent().next().html();
var link_tag = $(strong_tag + 'a').html();
return $(link_tag).attr('href');
}).get();
} else {
return false;
}
if(values){
var count = values.length;
alert(count + " values selected: " + values);
chrome.extension.sendRequest({count:count}, function(response) {
console.log('count sent');
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
chrome.extension.onRequest
不能在popup
中使用。它将在后台页面
中使用。背景页
弹出 - 使用
chrome.extension.getBackgroundPage()
获取变量。chrome.extension.onRequest
is not to be used inpopup
. It is to be used in thebackground page
.Background page
Popup - use
chrome.extension.getBackgroundPage()
to get back the variable.这里的问题是您的内容脚本正在发送消息,但没有活动的侦听器。
popup.html
页面的生命周期是从单击浏览器操作时开始直到它消失为止。除非您发送请求时该页面恰好存在,否则不会有任何内容监听您的请求。要解决此问题,您应该在 后台页面(在您的扩展程序的生命周期内持续存在)。
The problem here is that your content script is sending messages, but there is no active listener. The lifetime of the
popup.html
page is from when the browser action is clicked until it disappears. Unless the page happens to exist when you send your request, there's nothing listening to your requests.To fix the problem, you should setup your listener (
chrome.extension.onRequest.addListener
) in the background page (which persists for the lifetime of your extension).