From 53a7eb15c0b40d66653dfff10c7da28a618f30dc Mon Sep 17 00:00:00 2001 From: hymbz Date: Sat, 17 Aug 2024 17:59:01 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20:bug:=20=E4=BF=AE=E5=A4=8D=E9=83=A8?= =?UTF-8?q?=E5=88=86=E7=BD=91=E7=AB=99=E4=B8=8A=E6=97=A0=E6=B3=95=E7=BF=BB?= =?UTF-8?q?=E8=AF=91=E7=9A=84=20bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://sleazyfork.org/zh-CN/scripts/374903/discussions/255895 --- .../Manga/actions/translation/cotrans.ts | 65 ++++++++++++------- src/helper/other.ts | 2 +- 2 files changed, 42 insertions(+), 25 deletions(-) diff --git a/src/components/Manga/actions/translation/cotrans.ts b/src/components/Manga/actions/translation/cotrans.ts index 03a8e95e..20babdbf 100644 --- a/src/components/Manga/actions/translation/cotrans.ts +++ b/src/components/Manga/actions/translation/cotrans.ts @@ -1,4 +1,4 @@ -import { t, log, canvasToBlob, waitImgLoad } from 'helper'; +import { t, log, canvasToBlob, waitImgLoad, sleep } from 'helper'; import { store } from '../../store'; @@ -27,33 +27,51 @@ type QueryV1Message = type: 'not_found'; }; +const handleMessage = (msg: QueryV1Message, i: number) => { + switch (msg.type) { + case 'result': + return msg.result.translation_mask; + case 'pending': + setMessage(i, t('translation.tip.pending', { pos: msg.pos })); + break; + case 'status': + setMessage(i, t(`translation.status.${msg.status}`) || msg.status); + break; + + case 'error': + throw new Error(`${t('translation.tip.error')}:id ${msg.error_id}`); + case 'not_found': + throw new Error(`${t('translation.tip.error')}:Not Found`); + } +}; + +const waitTranslationPolling = async (id: string, i: number) => { + let result: string | undefined; + while (result === undefined) { + const res = await request( + `https://api.cotrans.touhou.ai/task/${id}/status/v1`, + { responseType: 'json' }, + ); + result = handleMessage(res.response, i); + await sleep(1000); + } + return result; +}; + /** 等待翻译完成 */ const waitTranslation = (id: string, i: number) => { const ws = new WebSocket(`wss://api.cotrans.touhou.ai/task/${id}/event/v1`); + // 如果网站设置了 CSP connect-src 就只能轮询了 + if (ws.readyState > 1) return waitTranslationPolling(id, i); + return new Promise((resolve, reject) => { ws.onmessage = (e) => { - const msg = JSON.parse(e.data) as QueryV1Message; - - switch (msg.type) { - case 'result': - resolve(msg.result.translation_mask); - break; - case 'pending': - setMessage(i, t('translation.tip.pending', { pos: msg.pos })); - break; - case 'status': - setMessage(i, t(`translation.status.${msg.status}`) || msg.status); - break; - - case 'error': - reject( - new Error(`${t('translation.tip.error')}:id ${msg.error_id}`), - ); - break; - case 'not_found': - reject(new Error(`${t('translation.tip.error')}:Not Found`)); - break; + try { + const result = handleMessage(JSON.parse(e.data), i); + if (result) resolve(result); + } catch (error) { + reject(error as Error); } }; }); @@ -67,8 +85,7 @@ const mergeImage = async (rawImage: Blob, maskUri: string) => { canvasCtx.drawImage(img, 0, 0); const img2 = new Image(); - img2.src = maskUri; - img2.crossOrigin = 'anonymous'; + img2.src = URL.createObjectURL(await download(maskUri)); await waitImgLoad(img2); canvasCtx.drawImage(img2, 0, 0); diff --git a/src/helper/other.ts b/src/helper/other.ts index 5c5187be..f5a054c4 100644 --- a/src/helper/other.ts +++ b/src/helper/other.ts @@ -67,7 +67,7 @@ export const querySelectorClick = ( /** 找出数组中出现最多次的元素 */ export const getMostItem = (list: T[]) => { const counts = new Map(); - for (const val of list) counts.set(val, counts.get(val) ?? 0 + 1); + for (const val of list) counts.set(val, (counts.get(val) ?? 0) + 1); // eslint-disable-next-line unicorn/no-array-reduce return [...counts.entries()].reduce((maxItem, item) =>