Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

85
Views
Trying to update badge based on number of elements inside the page of example.com

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?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!