Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add cambridge dictionary #1065

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions addon/locale/en-US/addon.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ service-gemini=Gemini🗝️
service-haici=Haici
service-bing=Bing
service-bingdict=Bing Dict(en↔zh)🔊
service-cambridgedict=Cambridge Dict(en↔zh)🔊
howdareyou marked this conversation as resolved.
Show resolved Hide resolved
service-haicidict=Haici Dict(en↔zh)🔊
service-collinsdict=Collins Dict(en↔zh)🔊
service-youdaodict=Youdao Dict(en↔zh)🔊
Expand Down
2 changes: 2 additions & 0 deletions addon/locale/en-US/panel.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ service-bing =
.label = Bing
service-bingdict =
.label = Bing Dict(en↔zh)🔊
service-cambridgedict =
.label = Cambridge Dict(en↔zh)🔊
service-haicidict =
.label = Haici Dict(en↔zh)🔊
service-collinsdict =
Expand Down
1 change: 1 addition & 0 deletions addon/locale/it-IT/addon.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ service-azuregpt=AzureGPT🗝️
service-haici=Haici
service-bing=Bing
service-bingdict=Bing Dict(en↔zh)🔊
service-cambridgedict=Cambridge Dict(en↔zh)🔊
service-haicidict=Haici Dict(en↔zh)🔊
service-collinsdict=Collins Dict(en↔zh)🔊
service-youdaodict=Youdao Dict(en↔zh)🔊
Expand Down
2 changes: 2 additions & 0 deletions addon/locale/it-IT/panel.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ service-bing =
.label = Bing
service-bingdict =
.label = Bing Dict(en↔zh)🔊
service-cambridgedict =
.label = Cambridge Dict(en↔zh)🔊
service-haicidict =
.label = Haici Dict(en↔zh)🔊
service-collinsdict =
Expand Down
1 change: 1 addition & 0 deletions addon/locale/zh-CN/addon.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ service-azuregpt=AzureGPT🗝️
service-haici=海词
service-bing=必应
service-bingdict=必应词典(en↔zh)🔊
service-cambridgedict=剑桥词典(en↔zh)🔊
service-haicidict=海词词典(en↔zh)🔊
service-collinsdict=科林斯词典(en↔zh)🔊
service-youdaodict=有道词典(en↔zh)🔊
Expand Down
2 changes: 2 additions & 0 deletions addon/locale/zh-CN/panel.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ service-haicidict =
.label = 海词词典(en↔zh)🔊
service-collinsdict =
.label = 科林斯词典(en↔zh)🔊
service-cambridgedict =
.label = 剑桥词典(en↔zh)🔊
service-youdaodict =
.label = 有道词典(en↔zh)🔊
service-freedictionaryapi =
Expand Down
50 changes: 50 additions & 0 deletions src/modules/services/cambridgedict.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { TranslateTaskProcessor } from "../../utils/task";

export default <TranslateTaskProcessor>async function (data) {
const base_url: string = `https://dictionary.cambridge.org/dictionary/english-chinese-simplified/${encodeURIComponent(data.raw)}`;
windingwind marked this conversation as resolved.
Show resolved Hide resolved
const xhr = await Zotero.HTTP.request("GET", base_url, {
responseType: "text",
});
if (xhr?.status !== 200) {
throw `Request error: ${xhr?.status}`;
}

let res = xhr.response;
const doc = new DOMParser().parseFromString(res, "text/html");
const audioList: Array<{ text: string; url: string }> = [];
const regions: Map<string, string> = new Map();
regions.set("uk", "英");
regions.set("us", "美");
const urls: Array<string> = [];
let result: string = "";

doc
.querySelectorAll(".entry-body__el")
.forEach((block: Element, index: number) => {
block.querySelectorAll(".dpron-i").forEach((value: Element) => {
const data = {
windingwind marked this conversation as resolved.
Show resolved Hide resolved
text: `${regions.get(value.querySelector(".region")?.textContent ?? "")} ${value.querySelector(".dpron")?.textContent}`,
url:
"https://dictionary.cambridge.org" +
(value.querySelector("source")?.getAttribute("src") ?? ""),
};
if (!urls.includes(data.url)) {
audioList.push(data);
urls.push(data.url);
}
});
result += block.querySelector(".posgram")?.textContent + "\n";
block.querySelectorAll(".dsense").forEach((value: Element, i: number) => {
let guideword =
value
.querySelector(".guideword")
?.textContent?.replace(/\s+/g, " ") ?? "";
let def_en = value.querySelector(".def")?.textContent;
let def_zh = value.querySelector(".trans")?.textContent;

result += `\t${i + 1}.${guideword} ${def_en}\n\t${def_zh}\n\n`;
});
});
data.result = result;
data.audio = audioList;
};
3 changes: 3 additions & 0 deletions src/modules/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ export class TranslationServices {
import("./caiyun").then(
(e) => (this.caiyun = new TranslateTaskRunner(e.default)),
);
import("./cambridgedict").then(
(e) => (this.cambridgedict = new TranslateTaskRunner(e.default)),
);
import("./cnki").then(
(e) => (this.cnki = new TranslateTaskRunner(e.default)),
);
Expand Down
4 changes: 4 additions & 0 deletions src/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,10 @@ export const SERVICES: Readonly<Readonly<TranslateService>[]> = <const>[
type: "word",
id: "bingdict",
},
{
type: "word",
id: "cambridgedict",
},
{
type: "word",
id: "haicidict",
Expand Down