Skip to content

Instantly share code, notes, and snippets.

@AriPerkkio
Last active November 18, 2024 07:35
Show Gist options
  • Save AriPerkkio/db83b766d13e58e4a8dd48fd02d02039 to your computer and use it in GitHub Desktop.
Save AriPerkkio/db83b766d13e58e4a8dd48fd02d02039 to your computer and use it in GitHub Desktop.
function translate() {
const walker = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT);
/** @type Node[] */
const textNodes = [];
/** @type Node */
let currentNode;
while ((currentNode = walker.nextNode())) {
textNodes.push(currentNode);
}
for (const node of textNodes) {
const text = node.textContent;
// Replace text node so that react will hold stale reference
const parent = node.parentElement;
parent.removeChild(node);
// Instead of actually translating, just uppercase the text
parent.appendChild(document.createTextNode(text.toUpperCase()));
}
return textNodes;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment