Skip to content

Instantly share code, notes, and snippets.

@atomicwrites
Last active December 10, 2022 01:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atomicwrites/bfc10de15e66550cc3c7de0ef3f13eca to your computer and use it in GitHub Desktop.
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

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.

Example: image

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