Skip to content

Instantly share code, notes, and snippets.

@henrahmagix
Last active December 28, 2018 20:42
Show Gist options
  • Save henrahmagix/95018eeae262dfa51e896c96df6b3347 to your computer and use it in GitHub Desktop.
Save henrahmagix/95018eeae262dfa51e896c96df6b3347 to your computer and use it in GitHub Desktop.
Replace every 3rd word with "garlic"
// 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