0

I want to update the badge label on my chrome extension's action button with the number of child elements inside a specific page.

For example let's say example.com's body div includes a changing ammount of elements... I want to update my badge number everytime example.com loads...

Here's my code

background.js

chrome.alarms.create({ delayInMinutes: 0.1, periodInMinutes: 0.2 });

chrome.alarms.onAlarm.addListener((tabId, changeInfo, tab) => {
  if (changeInfo.status === "complete") {
    if (tab.url.includes("example.com")) {
      var count = 0;
      try {
        count = document.querySelector("body div").childElementCount;
      } catch {
        count = 0;
      }
      chrome.action.setBadgeText({ tabId: tabId, text: count });
      chrome.action.setIcon({
        path: getRandomIconPath(),
      });
    }
  }
});

manifest.json

{
  "name": "my extension",
  "description": "my extension",
  "manifest_version": 3,
  "version": "1.2",
  "permissions": ["alarms", "activeTab", "scripting", "tabs", "contextMenus"],
  "background": {
    "service_worker": "background.js"
  },
  "host_permissions": [
    "*://example.com/*"
  ]
}

But this code's not working... what am I doing wrong?

TD540
  • 1,617
  • 3
  • 14
  • 26

0 Answers0