Last active
October 26, 2019 01:25
-
-
Save KW-M/2e3c1728e9ef9d96db7ef8284fcd18f2 to your computer and use it in GitHub Desktop.
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
(function highlight() { | |
console.log("working") | |
var wikiContentTextElement = document.getElementById("mw-content-text"); | |
window.wordIndex = 0; | |
function textNodesUnder(node) { | |
var all = []; | |
for (node = node.firstChild; node; node = node.nextSibling) { | |
if (node.nodeType == 3) all.push(node); | |
else all = all.concat(textNodesUnder(node)); | |
} | |
return all; | |
} | |
var textNodes = textNodesUnder(wikiContentTextElement); | |
for (var i = 0; i < textNodes.length; i++) { | |
var node = textNodes[i]; | |
words = node.nodeValue.trim().split(/\s+/); | |
var spanNode = document.createElement("span"); | |
for (var wordIndex = 0; wordIndex < words.length; wordIndex++) { | |
var wordNode = document.createElement("span"); | |
if (wordIndex % 2 == 0) { | |
wordNode.style.backgroundColor = | |
"#FFFF" + Math.round(Math.random() * 99); | |
} | |
wordNode.innerText = words[wordIndex] + " "; | |
spanNode.appendChild(wordNode); | |
} | |
node.parentElement.replaceChild(spanNode, node); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment