Skip to content

Instantly share code, notes, and snippets.

@mindfullsilence
Last active April 16, 2026 19:28
Show Gist options
  • Select an option

  • Save mindfullsilence/74deed11a1195363da0a4e3bbacffb5c to your computer and use it in GitHub Desktop.

Select an option

Save mindfullsilence/74deed11a1195363da0a4e3bbacffb5c to your computer and use it in GitHub Desktop.
replace-text.js
;(() => {
const find = 'Available Now' // the text you want to find (not case sensitive)
const replace = 'COMING SOON' // the text you want to replace it with
// IGNORE THE REST BELOW
const findReplace = (node, find, replace) => {
const next = () => findReplace(node, find, replace)
if ( ! node ) {
return next
}
if (node.nodeType === Node.TEXT_NODE && node.nodeValue.trim() !== '') {
if ( node.nodeValue.trim().toLowerCase() === find.toLowerCase() ) {
node.nodeValue = replace
}
}
for (let i = 0; i < node.childNodes.length; i++) {
findReplace(node.childNodes[i], find, replace);
}
return next
}
document.addEventListener('DOMContentLoaded', findReplace(document.body, find, replace))
})()
@mindfullsilence

mindfullsilence commented Apr 16, 2026

Copy link
Copy Markdown
Author

Note this script invokes immediately, as well as once the dom is loaded. To scope it to specific elements, simply call once for each element:

document.addEventListener('DOMContentLoaded', findReplace(document.querySelector('header'), find, replace))
document.addEventListener('DOMContentLoaded', findReplace(document.querySelector('footer'), find, replace))

This script is a bit computationally heavy considering its a recursive dom manipulator, but running it on very large pages hasn't seemed to have any meaningful impact thus far.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment