* 处理输入框翻译
(element: HTMLElement)
| 945 | * 处理输入框翻译 |
| 946 | */ |
| 947 | async function handleInputBoxTranslation(element: HTMLElement): Promise<void> { |
| 948 | let tooltip: HTMLElement | null = null; |
| 949 | |
| 950 | try { |
| 951 | const originalText = getInputBoxText(element); |
| 952 | |
| 953 | if (!originalText) { |
| 954 | return; |
| 955 | } |
| 956 | |
| 957 | // 根据触发方式去除末尾的触发符号 |
| 958 | const cleanedText = removeTriggerSymbols(originalText, config.inputBoxTranslationTrigger); |
| 959 | |
| 960 | if (!cleanedText) { |
| 961 | return; |
| 962 | } |
| 963 | |
| 964 | // 显示翻译中的动画和提示 |
| 965 | addInputBoxAnimation(element, 'translating'); |
| 966 | tooltip = createTranslationTooltip(element, '微软翻译中', 'translating'); |
| 967 | |
| 968 | try { |
| 969 | // 直接调用微软翻译API,不使用缓存 |
| 970 | const translatedText = await translateWithMicrosoft(cleanedText, config.inputBoxTranslationTarget); |
| 971 | |
| 972 | if (translatedText && translatedText !== cleanedText) { |
| 973 | // 移除翻译中的动画 |
| 974 | element.classList.remove('fluent-input-translating'); |
| 975 | |
| 976 | // 设置翻译结果 |
| 977 | setInputBoxText(element, translatedText); |
| 978 | |
| 979 | // 显示成功动画和提示 |
| 980 | addInputBoxAnimation(element, 'success'); |
| 981 | removeExistingTooltip(); |
| 982 | tooltip = createTranslationTooltip(element, '翻译成功', 'success'); |
| 983 | } else { |
| 984 | // 翻译结果与原文相同或为空 |
| 985 | element.classList.remove('fluent-input-translating'); |
| 986 | addInputBoxAnimation(element, 'error'); |
| 987 | removeExistingTooltip(); |
| 988 | tooltip = createTranslationTooltip(element, '内容无需翻译', 'error'); |
| 989 | } |
| 990 | } catch (translationError) { |
| 991 | // 翻译失败 |
| 992 | element.classList.remove('fluent-input-translating'); |
| 993 | addInputBoxAnimation(element, 'error'); |
| 994 | removeExistingTooltip(); |
| 995 | tooltip = createTranslationTooltip(element, '微软翻译失败', 'error'); |
| 996 | console.error('微软翻译失败:', translationError); |
| 997 | } |
| 998 | |
| 999 | // 自动隐藏提示 |
| 1000 | setTimeout(() => removeExistingTooltip(), 2500); |
| 1001 | |
| 1002 | } catch (error) { |
| 1003 | console.error('输入框翻译失败:', error); |
| 1004 |
no test coverage detected