fix catch en

This commit is contained in:
unknown
2025-08-26 12:17:46 +08:00
parent 9e94987255
commit 6e49dcf58f

View File

@ -380,6 +380,35 @@ const shouldInterceptByLanguages = async (text) => {
return targets.some((code) => found.has(code));
};
// 详细语言拦截检查:返回是否拦截及原因
const checkInterceptLanguage = async (text) => {
try {
if (!trcConfig || trcConfig.interceptChinese !== 'true') return { blocked: false };
const list = parseInterceptLanguages(trcConfig.interceptLanguages);
const targets = list.length ? list : ['zh'];
const res = await ipc.detectLanguage({ text });
const lang = String(res?.data?.lang || '').toLowerCase();
if (lang && targets.includes(lang)) {
return { blocked: true, reason: `语言 ${lang} 在拦截列表(${targets.join(',')})` };
}
const found = detectLanguageSet(text);
for (const code of targets) {
if (found.has(code)) return { blocked: true, reason: `检测到可能的语言 ${code} 在拦截列表(${targets.join(',')})` };
}
} catch (e) {}
return { blocked: false };
};
// 校验最终发送文本:为空/错误内容/语言被拦截,返回原因字符串;通过则返回空字符串
const validateFinalText = async (text) => {
const s = (text ?? '').toString();
if (!s.trim()) return '消息为空或仅包含空白';
if (isErrorText(s)) return '检测到错误内容(翻译失败或错误信息)';
const chk = await checkInterceptLanguage(s);
if (chk.blocked) return chk.reason;
return '';
};
// 错误消息检测:拦截把错误当消息发送
const isErrorText = (text) => {
if (!text) return false;
@ -393,35 +422,33 @@ const isErrorText = (text) => {
const sendMsg = async () => {
let sendButton = getSendBtn();
// 新增:拦截中文逻辑
if (trcConfig.interceptChinese === "true" || true) {
// 最终文本验证(含错误/空文本/语言拦截),仅当开启拦截开关时检查语言
{
// 从 footer 开始查找输入框
const footer = document.querySelector("footer._ak1i");
if (!footer) {
console.error("未找到输入框容器");
return;
}
// 查找富文本输入框
const richTextInput = footer.querySelector(
'.lexical-rich-text-input div[contenteditable="true"]'
);
let content = richTextInput?.textContent?.trim() || '';
// 获取最终文本,先尝试通用方法,失败再回退到选择器
let content = '';
try { if (typeof whatsappContent === 'function') { content = (await whatsappContent())?.trim(); } } catch (e) {}
if (!content) {
const plainInput = document.querySelector('footer div[aria-owns="emoji-suggestion"][contenteditable="true"]');
content = plainInput?.textContent?.trim() || '';
}
// 1) 多语言拦截(兼容旧:仅中文)
try {
if (trcConfig.interceptChinese === "true" && (await shouldInterceptByLanguages(content))) {
alert("检测到被拦截语言内容,已阻止发送");
return;
const richTextInput = footer.querySelector('.lexical-rich-text-input div[contenteditable="true"]');
content = richTextInput?.textContent?.trim() || '';
if (!content) {
const plainInput = document.querySelector('footer div[aria-owns="emoji-suggestion"][contenteditable="true"]');
content = plainInput?.textContent?.trim() || '';
}
} catch (e) {}
// 2) 错误内容拦截(翻译失败/接口错误等)
if (isErrorText(content)) {
alert("翻译失败,未发送。请稍后重试或更换翻译通道。");
return;
}
// 先拦截错误与空消息(始终启用)
const s = content;
if (!s || !s.trim()) { alert('已拦截:消息为空或仅包含空白'); return; }
if (isErrorText(s)) { alert('已拦截:检测到错误内容(翻译失败或错误信息)'); return; }
// 再按配置拦截语言
if (trcConfig.interceptChinese === 'true') {
const chk = await checkInterceptLanguage(s);
if (chk.blocked) { alert(`已拦截:${chk.reason}`); return; }
}
}