diff --git a/src/modules/services/haicidict.ts b/src/modules/services/haicidict.ts index 2cdb27a..3a17efb 100644 --- a/src/modules/services/haicidict.ts +++ b/src/modules/services/haicidict.ts @@ -1,39 +1,38 @@ 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) { throw `Request error: ${xhr?.status}`; } - let res = xhr.response as string; - let tgt = ""; + const res = xhr.response as string; 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) => !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 7034702..0c2598c 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,