Simplified to Traditional Chinese Converter
Converts all text on a webpage from Simplified Chinese to Traditional Chinese (Taiwan standard, with phrases). Uses the OpenCC-JS library.
Convert SC->TCView Source Code
// This is the readable version of the code
(async function() {
// 1. Check if OpenCC is already on the page. If not, create a script tag to load it.
if (typeof OpenCC === 'undefined') {
const script = document.createElement('script');
script.src = 'https://cdn.jsdelivr.net/npm/opencc-js@1.0/dist/umd/full.js';
document.head.appendChild(script);
// Wait for the script to load before continuing
await new Promise(resolve => {
script.onload = resolve;
});
}
// 2. Now that the script is loaded, run the conversion logic.
console.log("OpenCC loaded. Starting conversion...");
try {
const converter = await OpenCC.Converter({ from: 'cn', to: 'twp' });
const walk = (node) => {
// Process text nodes
if (node.nodeType === 3) {
const text = node.nodeValue;
const convertedText = converter(text);
if (convertedText !== text) {
node.nodeValue = convertedText;
}
return;
}
// Process element nodes (but not scripts or styles)
if (node.nodeType === 1 && node.nodeName !== 'SCRIPT' && node.nodeName !== 'STYLE') {
for (let i = 0; i < node.childNodes.length; i++) {
walk(node.childNodes[i]);
}
}
};
walk(document.body);
console.log("Page converted to Traditional Chinese.");
} catch (error) {
console.error("Failed to convert page:", error);
alert("Failed to convert page to Traditional Chinese. See console for details.");
}
})();