Skip to content

Instantly share code, notes, and snippets.

@tomshaw
Created September 26, 2019 11:01
Show Gist options
  • Save tomshaw/def66e5b1846309d1ed3cfd6fb5153e7 to your computer and use it in GitHub Desktop.
Save tomshaw/def66e5b1846309d1ed3cfd6fb5153e7 to your computer and use it in GitHub Desktop.
Chrome extension scan web page and highlight key words.
// 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