Last active
November 18, 2024 07:35
-
-
Save AriPerkkio/db83b766d13e58e4a8dd48fd02d02039 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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