Last active
December 28, 2018 20:42
-
-
Save henrahmagix/95018eeae262dfa51e896c96df6b3347 to your computer and use it in GitHub Desktop.
Replace every 3rd word with "garlic"
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
// Select an element in the inspector, then paste this into the console. | |
function textNodesUnder(el){ | |
var n, a=[], walk=document.createTreeWalker(el,NodeFilter.SHOW_TEXT,null,false); | |
while(n=walk.nextNode()) a.push(n); | |
return a; | |
} | |
function notAllWhitespace(value) { | |
return !value.match(/^\s*$/g); | |
} | |
var count = Math.floor(Math.random() * 3) | |
var textNodesToChange = textNodesUnder($0).filter(t => notAllWhitespace(t.nodeValue)); | |
textNodesToChange.forEach(t => { | |
let words = t.nodeValue.split(/ /); | |
let replaced = words.map(s => { | |
if (notAllWhitespace(s) && count++ %3 === 0) { | |
if (s.match(/^[A-Z0-9]/)) { | |
return 'Garlic'; | |
} | |
return 'garlic'; | |
} | |
return s | |
}); | |
t.nodeValue = replaced.join(' '); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment