Created
September 26, 2019 11:01
-
-
Save tomshaw/def66e5b1846309d1ed3cfd6fb5153e7 to your computer and use it in GitHub Desktop.
Chrome extension scan web page and highlight key words.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Thoroughly untested. | |
let searchTerms = ['law', 'software', 'news', 'health']; | |
let elems = document.querySelectorAll("h1, h2, h3, h4, h5, h6, li, p, a") | |
for (let i = 0, total = elems.length; i < total; i++) { | |
let element = elems[i]; | |
if (element && element.innerText) { | |
let innerText = element.innerText; | |
for (let j = 0; j < searchTerms.length; j++) { | |
const reg = new RegExp(searchTerms[j], 'gi') | |
const matches = innerText.toLowerCase().match(reg) || [] | |
if (matches.length) { | |
for (let n = 0; n < matches.length; n++) { | |
element.innerHTML = innerText.replace(reg, '<span style="color:red">' + searchTerms[j] + '</span>'); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment