From 13956ec93570f60aab82f3eefe01d64eb401c3a8 Mon Sep 17 00:00:00 2001 From: Allo Date: Thu, 28 Nov 2024 16:28:27 +0800 Subject: [PATCH 1/2] refactor(service/haicidict): using DOMParser to get the information --- src/modules/services/haicidict.ts | 47 +++++++++++++++---------------- tsconfig.json | 2 +- 2 files changed, 24 insertions(+), 25 deletions(-) diff --git a/src/modules/services/haicidict.ts b/src/modules/services/haicidict.ts index 2cdb27a7..1e0dfa62 100644 --- a/src/modules/services/haicidict.ts +++ b/src/modules/services/haicidict.ts @@ -1,7 +1,7 @@ import { TranslateTaskProcessor } from "../../utils/task"; export default async function (data) { - const xhr = await Zotero.HTTP.request("GET", `http://dict.cn/${data.raw}`, { + const xhr = await Zotero.HTTP.request("GET", `https://dict.cn/${data.raw}`, { responseType: "text", }); if (xhr?.status !== 200) { @@ -9,31 +9,30 @@ export default async function (data) { } let res = xhr.response as string; - let tgt = ""; try { - const audioRegex = /naudio="(\w+.mp3\?t=\w+?)"/; - data.audio = - res.match(new RegExp(audioRegex, "gi"))?.map((s: string) => ({ - text: "", - url: "http://audio.dict.cn/" + s.match(new RegExp(audioRegex, "i"))![1], - })) || []; - const symbolsRegex = /(.)[\n\t\s]*?(.+?)<\/bdo>/; - const symbols: string[] = []; - res.match(new RegExp(symbolsRegex, "g"))!.forEach((line) => { - const [_, country, sym] = line.match(symbolsRegex)!; - symbols.push(`${country} ${sym}`); - }); - tgt += symbols.join("\n") + "\n"; - res = res.match(/
    [\s\S]+?<\/ul>/)![0]; + const doc = new DOMParser().parseFromString(res, "text/html"); + + const audioList: Array<{ text: string; url: string }> = []; + for (const span of doc.querySelectorAll( + "div.phonetic > span", + )) { + const text = span.innerText.replace(/\s+/g, " ").trim(); + for (const item of span.querySelectorAll("i")) { + audioList.push({ + text: `${text} ${item.title}`, + url: `https://audio.dict.cn/${item.getAttribute("naudio")}`, + }); + } + } + data.audio = audioList; + const items = Array.from( + doc.querySelectorAll("ul.dict-basic-ul > li"), + ) + .filter((item) => !Boolean(item.querySelector("script"))) + .map((item) => item.innerText.replace(/\s+/g, " ").trim()) + .filter((item) => Boolean(item)); + data.result = `${items.join("\n")}\n`; } catch (e) { throw "Parse error"; } - for (const line of res.match(/
  • [\s\S]+?<\/li>/g) || []) { - tgt += - line - .replace(/<\/?.+?>/g, "") - .replace(/[\n\t]+/g, " ") - .trim() + "\n"; - } - data.result = tgt; }; diff --git a/tsconfig.json b/tsconfig.json index 7034702c..0c2598c0 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -2,7 +2,7 @@ "compilerOptions": { "experimentalDecorators": true, "module": "commonjs", - "lib": ["ESNext", "DOM"], + "lib": ["ESNext", "DOM", "DOM.Iterable"], "target": "ES2016", "resolveJsonModule": true, "skipLibCheck": true, From 9ba5fbcc11133b48cf08a8b779b71cc9b65e1b07 Mon Sep 17 00:00:00 2001 From: A1lo Date: Thu, 28 Nov 2024 17:55:37 +0800 Subject: [PATCH 2/2] chore: run eslint fix --- src/modules/services/haicidict.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/modules/services/haicidict.ts b/src/modules/services/haicidict.ts index 1e0dfa62..3a17efb9 100644 --- a/src/modules/services/haicidict.ts +++ b/src/modules/services/haicidict.ts @@ -8,7 +8,7 @@ export default async function (data) { throw `Request error: ${xhr?.status}`; } - let res = xhr.response as string; + const res = xhr.response as string; try { const doc = new DOMParser().parseFromString(res, "text/html"); @@ -28,7 +28,7 @@ export default async function (data) { const items = Array.from( doc.querySelectorAll("ul.dict-basic-ul > li"), ) - .filter((item) => !Boolean(item.querySelector("script"))) + .filter((item) => !item.querySelector("script")) .map((item) => item.innerText.replace(/\s+/g, " ").trim()) .filter((item) => Boolean(item)); data.result = `${items.join("\n")}\n`;