MCPcopy
hub / github.com/Bistutu/FluentRead / handleInputBoxTranslation

Function handleInputBoxTranslation

entrypoints/content.ts:947–1016  ·  view source on GitHub ↗

* 处理输入框翻译

(element: HTMLElement)

Source from the content-addressed store, hash-verified

945 * 处理输入框翻译
946 */
947async 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

Callers 1

setupInputBoxTranslationFunction · 0.85

Calls 7

getInputBoxTextFunction · 0.85
removeTriggerSymbolsFunction · 0.85
addInputBoxAnimationFunction · 0.85
createTranslationTooltipFunction · 0.85
translateWithMicrosoftFunction · 0.85
setInputBoxTextFunction · 0.85
removeExistingTooltipFunction · 0.85

Tested by

no test coverage detected