当前位置: 首页 > 知识库问答 >
问题:

Chrome扩展:Chrome.runtime.onMessage.AddListener内的代码从不执行

瞿兴朝
2023-03-14

尝试使用sendMessage方法将变量从内容脚本传递到popup.js时遇到了很多麻烦。

这是我向后台脚本发送消息的内容脚本,该消息包含DOM节点的子节点数:

let incomingChatsNumber = incomingChatsContainer.children().length;
chrome.runtime.sendMessage({ incomingChatsNumber: incomingChatsNumber });

然后background.js监听消息并将带有相同变量的消息本身发送给popup.js:

chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
  let incomingChatsNumber = message.incomingChatsNumber;
  chrome.runtime.sendMessage({ incomingChatsNumber: incomingChatsNumber });
});

在popup.js中,我有一个按钮,如果“IncomingChatsNumber”大于0(dom容器有子级),它将触发一些代码:

  $("#js-toggleSorting").on("click", function () {
    chrome.runtime.onMessage.addListener(function (message) {
      let incomingChatsNumber = message.incomingChatsNumber;
      if (incomingChatsNumber <= 0) {
        $(".message").html("<p>No Chats To Sort, You Joker</p>");
      } else {
        $(".message").html("<p>Sorting Chats</p>");
        //This code never gets executed
        if ($("#js-toggleSorting").attr("data-click-state") == 1) {
          $("#js-toggleSorting").attr("data-click-state", 0);
          $("#js-toggleSorting").html("SORT INCOMING CHATS");
          sortFunction(false);
        } else {
          $("#js-toggleSorting").attr("data-click-state", 1);
          $("#js-toggleSorting").html("STOP SORTING INCOMING CHATS");
          sortFunction(true);
        }
        save_button_state();
      }
    });
  });

对我来说奇怪的是,popup.js获得了正确的值,我甚至可以对其进行console.log,但一旦我在if条件上添加了更多代码,即使该条件可以运行,代码也永远不会被执行。

共有1个答案

佟涵畅
2023-03-14

你不应该把一个事件侦听器放在另一个事件处理程序中,这是一个非常糟糕的做法,即使它做了你认为它应该做的事情,这将导致内存泄漏,并使代码难以阅读和语义错误。

从您的代码来看,问题似乎是消息的处理程序被多次调用,这是因为在click evet中添加了先前的事件侦听器

 let latestIncomingChatsNumber
 chrome.runtime.onMessage.addListener(function (message) {
      let incomingChatsNumber = message.incomingChatsNumber;
      // save this into the application state, i will use a parent scope variable you could use something else 
       latestIncomingChatsNumber = incomingChatsNumber
    });
    
 $("#js-toggleSorting").on("click", function () {
      if (latestIncomingChatsNumber <= 0) {
        $(".message").html("<p>No Chats To Sort, You Joker</p>");
      } else {
        $(".message").html("<p>Sorting Chats</p>");
        //This code never gets executed
        if ($("#js-toggleSorting").attr("data-click-state") == 1) {
          $("#js-toggleSorting").attr("data-click-state", 0);
          $("#js-toggleSorting").html("SORT INCOMING CHATS");
          sortFunction(false);
        } else {
          $("#js-toggleSorting").attr("data-click-state", 1);
          $("#js-toggleSorting").html("STOP SORTING INCOMING CHATS");
          sortFunction(true);
        }
        save_button_state(); // i believe this function saves the attributes to the dom elements
      }
  });

如果save_button_state做得很好,那么我共享的代码应该可以工作

 类似资料:
  • 本文向大家介绍google-chrome-extension 从扩展代码获取清单,包括了google-chrome-extension 从扩展代码获取清单的使用技巧和注意事项,需要的朋友参考一下 示例 chrome.runtime.getManifest() 以已解析对象的形式返回扩展的清单。 此方法适用于内容脚本和所有扩展页面,不需要权限, 例如,获取扩展的版本字符串:            

  • 我正在开发一个chrome扩展,遇到了一个大问题。 奇怪的是,我可以访问iframe的html。因此,这段代码在chrome扩展中可以完美地工作: 我尝试将“all_frames”:true放入清单文件中,但没有成功:(

  • 我知道如何在selenium webdriver中加载chrome扩展。但是我没有看到任何描述如何从Selenium运行chrome扩展的帖子/博客。< br> 我需要明确地让chrome扩展运行/让它从selenium执行它的功能。比如我想用Selenium Webdriver的这个扩展清除Chrome浏览器的缓存。< br> 我可以先做吗?或者Selenium WebDriver是否只帮助我将

  • 问题内容: 我正在尝试让我的Chrome扩展程序在加载新页面时运行该功能,但是在尝试了解如何执行此操作时遇到了麻烦。据我了解,我需要在background.html中执行以下操作: 使用来检查时,页面变更 使用运行的脚本。 这是我的代码: 我还想知道init()函数是否可以访问位于其他JS文件中的其他函数? 问题答案: Chrome扩展程序中的JavaScript代码可以分为以下几类: 扩展代码-

  • Tweetmeme Chrome 扩展其实就是一个精简版的 TweetMeme 按钮,其作用就是让你“随时随地”都可以向你的关注者分享你正在浏览的网页(“随时随地”加引号是因为在天朝该服务不可用),简单点说就是一个 retweet 按钮。通过 Tweetmeme Chrome 扩展分享出的网址会采用 retwt.me 短网址服务进行缩短,对于身在墙外或者随时都在翻墙状态的用户来说非常实用。 安装

  • 问题内容: 我正在做一个插件来对界面进行一些转换。我不断收到(典型的跨网站问题) 但作为扩展程序,它应该可以访问iframe的内容 … 没有人知道如何访问它的内容以便可以被捕获吗? 问题答案: 通常,没有直接访问其他来源对象的直接方法。如果要在不同框架中的内容脚本之间 安全地 通信,则必须将消息发送到后台页面,该页面又将消息发送回选项卡。 这是一个例子: 的一部分: : : 后台脚本“ bg.js

  • 问题内容: 我知道如何在Selenium Webdriver中加载Chrome扩展程序。但是我没有看到任何描述如何从Selenium 运行 chrome扩展的帖子/博客。 我需要明确地使chrome扩展程序运行/使其从selenium中执行其功能。例如,我想使用带有Selenium Webdriver的扩展程序清除Chrome浏览器的缓存。 我可以先做吗?还是Selenium WebDriver仅

  • When executing a test class that contains one or more test methods, a number of extension callbacks are called in addition to the user-supplied test and lifecycle methods. See also: Test Execution Ord