update 拦截

This commit is contained in:
unknown
2025-08-30 14:52:59 +08:00
parent 8fca906f0b
commit 0537b55c24
2 changed files with 60 additions and 14 deletions

View File

@ -187,11 +187,59 @@ const parseInterceptLanguages = (val) => {
const shouldInterceptByLanguages = (text) => {
if (!trcConfig || trcConfig.interceptChinese !== 'true') return false;
const list = parseInterceptLanguages(trcConfig.interceptLanguages);
const targets = list.length ? list : ['zh'];
// 修复:如果没有选择任何语言,则不拦截任何内容
if (list.length === 0) return false;
const targets = list;
const found = detectLanguageSet(text);
return targets.some(code=>found.has(code));
};
const sendMsg = ()=> {
// 详细语言拦截检查:返回是否拦截及原因
const checkInterceptLanguage = async (text) => {
try {
if (!trcConfig || trcConfig.interceptChinese !== 'true') return { blocked: false };
const list = parseInterceptLanguages(trcConfig.interceptLanguages);
// 修复:如果没有选择任何语言,则不拦截任何内容
if (list.length === 0) return { blocked: false };
const targets = list;
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 isErrorText = (s) => {
const patterns = [
'HTTPConnectionPool', 'timeout', '超时', '错误', 'Error',
];
return patterns.some((p) => s.includes(p));
};
const sendMsg = async ()=> {
// 最终文本验证(含错误/空文本/语言拦截),仅当开启拦截开关时检查语言
{
// 获取最终文本
const editableDiv = document.getElementById('editable-message-text');
const content = editableDiv?.textContent?.trim() || '';
// 先拦截错误与空消息(始终启用)
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; }
}
}
let sendButton = document.querySelectorAll('button.Button.send.main-button.default.secondary.round.click-allowed')[0]
if (sendButton) {
sendButton.click();
@ -385,7 +433,7 @@ const addTranslateListener = () => {
if (status === true && isProcess === false) {
const translateText = styledTextarea.getContent();
await inputMsg(translateText)
sendMsg()
await sendMsg()
styledTextarea.setTranslateStatus(false)
styledTextarea.setContent('...')
return;
@ -401,9 +449,9 @@ const addTranslateListener = () => {
const translateText = res.data;
await inputMsg(translateText)
styledTextarea.setContent(translateText);
setTimeout(()=>{
setTimeout(async ()=>{
isProcessing = false;
sendMsg()
await sendMsg()
styledTextarea.setContent('...');
},500)
}else {
@ -420,7 +468,7 @@ const addTranslateListener = () => {
return;
}
}
sendMsg();
await sendMsg();
}
},true);

View File

@ -365,7 +365,9 @@ const parseInterceptLanguages = (val) => {
const shouldInterceptByLanguages = async (text) => {
if (!trcConfig || trcConfig.interceptChinese !== 'true') return false;
const list = parseInterceptLanguages(trcConfig.interceptLanguages);
const targets = list.length ? list : ['zh'];
// 修复:如果没有选择任何语言,则不拦截任何内容
if (list.length === 0) return false;
const targets = list;
try {
const res = await ipc.detectLanguage({ text });
if (res && res.code === 2000 && res.data) {
@ -385,7 +387,9 @@ const checkInterceptLanguage = async (text) => {
try {
if (!trcConfig || trcConfig.interceptChinese !== 'true') return { blocked: false };
const list = parseInterceptLanguages(trcConfig.interceptLanguages);
const targets = list.length ? list : ['zh'];
// 修复:如果没有选择任何语言,则不拦截任何内容
if (list.length === 0) return { blocked: false };
const targets = list;
const res = await ipc.detectLanguage({ text });
const lang = String(res?.data?.lang || '').toLowerCase();
if (lang && targets.includes(lang)) {
@ -798,12 +802,6 @@ const addTranslateListener = () => {
const translateStatus = styledTextarea.getTranslateStatus();
const textContent = (await whatsappContent())?.trim();
// 拦截:在翻译/发送前就判断原始输入语言
if (trcConfig.interceptChinese === "true" && (await shouldInterceptByLanguages(textContent))) {
alert("检测到被拦截语言内容,已阻止发送");
return;
}
// 跳过空消息
if (!textContent) return;