/* * @Author: Hgh * @Date: 2025-07-21 11:38:15 * @LastEditTime: 2025-07-21 11:38:35 * @LastEditors: Hgh * @Description: */ import { toTrad, toSimp, LangMode } from './transform' let currentMode: LangMode = 'zh-s' export function setCurrentMode(mode: LangMode) { currentMode = mode } export function runTransform(text: string): string { return currentMode === 'zh-s' ? toTrad(text) : toSimp(text) } export function shouldTransform(text: string, options?: { include?: string[]; exclude?: string[] }): boolean { if (!text.trim()) return false const { include, exclude } = options || {} if (exclude?.some(e => text.includes(e))) return false if (include?.length) return include.some(i => text.includes(i)) return true } export function handleTranslate(root: HTMLElement | ChildNode, options?: { include?: string[]; exclude?: string[] }) { const children = (root as HTMLElement).childNodes ?? [] children.forEach(node => { if (['SCRIPT', 'STYLE', 'TEXTAREA'].includes((node as HTMLElement).tagName)) return if (node.nodeType === Node.TEXT_NODE) { if (shouldTransform(node.textContent || '', options)) { node.textContent = runTransform(node.textContent || '') } } else { const el = node as HTMLElement ['title', 'placeholder', 'alt', 'value'].forEach(attr => { const val = (el as any)[attr] if (val && typeof val === 'string' && shouldTransform(val, options)) { (el as any)[attr] = runTransform(val) } }) handleTranslate(node, options) } }) }