Chrome 扩展:消息传递混乱

发布于 2024-12-06 04:02:04 字数 2604 浏览 0 评论 0 原文

我正在尝试将页面上已检查项目的数量发送到弹出窗口。弹出警报上的添加输入按钮会向控制台发送网页上选中的复选框数量。

我的脚本没有这样做,我认为消息传递的工作原理可能会出现混乱。这是我所拥有的:

清单:

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

ma​​in_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');  
        });

});

I'm trying to send the number of checked items on a page over to a popup. The add input button on the popup alerts or sends to the console the amount of checkboxes checked on the webpage.

My script is not doing this and I thought there may be a confusion on how message passing works. Here is what I have:

Manifest:

{
  "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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谁对谁错谁最难过 2024-12-13 04:02:04

chrome.extension.onRequest 不能在 popup 中使用。它将在后台页面中使用。

背景页

var count=0;  //"count" is outside of any function
chrome.extension.onRequest(function(request, sender, sendResponse){
          count=request.count;
          console.log(request.count);
});

弹出 - 使用 chrome.extension.getBackgroundPage() 获取变量。

var count = chrome.extension.getBackgroundPage().count;  //get it back
alert("There are "+count+" checkboxes checked.");  //Voilà!

chrome.extension.onRequest is not to be used in popup. It is to be used in the background page.

Background page

var count=0;  //"count" is outside of any function
chrome.extension.onRequest(function(request, sender, sendResponse){
          count=request.count;
          console.log(request.count);
});

Popup - use chrome.extension.getBackgroundPage() to get back the variable.

var count = chrome.extension.getBackgroundPage().count;  //get it back
alert("There are "+count+" checkboxes checked.");  //Voilà!
泪是无色的血 2024-12-13 04:02:04

这里的问题是您的内容脚本正在发送消息,但没有活动的侦听器。 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文