Last active
August 29, 2015 14:19
-
-
Save ZER0/d49c9cd35ad4dd08389c to your computer and use it in GitHub Desktop.
highlight a word in a page
This file contains hidden or 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
let word = "Mozilla"; | |
// set the walker for non-empty text node | |
let walker = document.createTreeWalker( | |
document.body, | |
NodeFilter.SHOW_TEXT, | |
{ acceptNode: (node) => node.nodeValue.trim().length && NodeFilter.FILTER_ACCEPT } | |
); | |
// collecting the nodes | |
let nodes = []; | |
while(walker.nextNode()) nodes.push(walker.currentNode); | |
// collecting the ranges from the previous node, | |
// where the content matches the string given (`word`) | |
let ranges = []; | |
for (let node of nodes) { | |
let text = node.nodeValue; | |
let index = text.indexOf(word); | |
while(index > -1) { | |
let range = document.createRange(); | |
range.setStart(node, index); | |
range.setEnd(node, index + word.length); | |
ranges.push(range); | |
index = text.indexOf(word, index + 1); | |
} | |
} | |
// clearing the nodes - we don't need them anymore | |
nodes.length = 0; | |
// adding marks to the matching text | |
let marks = []; | |
for (let range of ranges) { | |
let mark = document.createElement("mark"); | |
mark.textContent = word; | |
range.deleteContents(); | |
range.insertNode(mark); | |
marks.push(mark); | |
} | |
// clear the ranges - we don't need them anymore | |
ranges.length = 0; | |
// clear - using a timeout, in case of code execution | |
// from web console / scratchpad | |
setTimeout(() =>{ | |
for (let mark of marks) { | |
let node = document.createTextNode(mark.textContent); | |
let range = document.createRange(); | |
range.selectNode(mark); | |
range.deleteContents(); | |
range.insertNode(node); | |
// normalize the parent, to unify the text nodes again | |
node.parentNode.normalize(); | |
} | |
// clear the marks - we don't need them anymore | |
marks.length = 0; | |
}, 4000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment