extension/Shared (Extension)/Resources/rr0-content.js

Summary

Maintainability
A
0 mins
Test Coverage
const containers = ["HTML", "BODY", "NAV", "UL", "OL", "SECTION", "ARTICLE", "ASIDE", "HEADER", "FOOTER"]

const textToElems = new Map()

browser.runtime.onMessage.addListener((message, sender, sendResponse) => {
  console.debug("content.js", "onMessage", message)
  switch (message.type) {
    case "getText":
      const title = message.title
      const textElems = Array.from(document.querySelectorAll("*")).filter(el => !containers.includes(el.tagName))
      const matchingElems = textElems.filter(el => el.textContent.indexOf(title) >= 0)
      let elems = textToElems.get(title)
      if (!elems) {
        elems = []
        textToElems.set(title, elems)
      }
      const count = matchingElems.length
      console.debug("content.js", "Found", count, "items containing", title, ":", matchingElems)
      sendResponse({ count })
      break
  }
})