Made with ChatGPT, paste into your browser console and then run replaceMentionsOfElonWithToothFairy()
. Pretty sure theres a simpler way to replace on the whole page, but not a web dev. try it on https://en.wikipedia.org/wiki/Elon_Musk for example. Also it knows regex.
Last active
December 10, 2022 01:39
-
-
Save atomicwrites/bfc10de15e66550cc3c7de0ef3f13eca to your computer and use it in GitHub Desktop.
Script written by ChatGPT to replace all mentions of Elon Musk with the Tooth Fairy
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 replaceMentionsOfElonWithToothFairy() { | |
// Get all the text nodes on the page | |
var textNodes = getTextNodes(); | |
// Iterate through all the text nodes | |
for (var i = 0; i < textNodes.length; i++) { | |
var textNode = textNodes[i]; | |
// Replace all mentions of Elon Reeve Musk, Elon, Elon Musk, or Musk with the Tooth Fairy | |
textNode.nodeValue = textNode.nodeValue.replace(/(Elon Reeve Musk|Elon( Musk)?|Musk)/g, "The Tooth Fairy"); | |
} | |
} | |
function getTextNodes() { | |
var textNodes = []; | |
// This function gets called for each node in the document | |
function getTextNodesFromElement(element) { | |
if (element.nodeType === Node.TEXT_NODE) { | |
// If the node is a text node, add it to the list | |
textNodes.push(element); | |
} else { | |
// If it's not a text node, iterate through its children and call this function recursively | |
for (var i = 0; i < element.childNodes.length; i++) { | |
getTextNodesFromElement(element.childNodes[i]); | |
} | |
} | |
} | |
// Start the recursive search for text nodes from the root of the document | |
getTextNodesFromElement(document.body); | |
return textNodes; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment