ai-dictionary
Version:
Ask AI to explain the word in sentence and save definition to Anki.
42 lines (41 loc) • 1.17 kB
JavaScript
const NAME = "AI Dictionary";
export async function addNote(Sentence, Word, PartOfSpeech, Definition) {
try {
const decks = await invoke("deckNames");
if (!decks.includes(NAME)) {
console.log("Can't find deck, skip.");
return;
}
const params = {
note: {
deckName: NAME,
modelName: NAME,
fields: {
ID: new Date().toISOString(),
Sentence,
Word,
PartOfSpeech,
Definition,
},
},
};
await invoke("addNote", params);
}
catch (error) {
console.error(error);
}
}
async function invoke(action, params = {}) {
const response = await fetch("http://127.0.0.1:8765", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ action, version: 6, params }),
});
const body = (await response.json());
if (typeof body.error === "string") {
throw new Error(body.error);
}
return body.result;
}