forked from ankidroid/Anki-Android
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjs-api.js
More file actions
178 lines (171 loc) · 5.99 KB
/
js-api.js
File metadata and controls
178 lines (171 loc) · 5.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
* AnkiDroid JavaScript API
* Version: 0.0.3
*/
/**
* jsApiList
*
* name: method name
* value: endpoint
*/
const jsApiList = {
ankiGetNewCardCount: "newCardCount",
ankiGetLrnCardCount: "lrnCardCount",
ankiGetRevCardCount: "revCardCount",
ankiGetETA: "eta",
ankiGetCardMark: "cardMark",
ankiGetCardFlag: "cardFlag",
ankiGetNextTime1: "nextTime1",
ankiGetNextTime2: "nextTime2",
ankiGetNextTime3: "nextTime3",
ankiGetNextTime4: "nextTime4",
ankiGetCardReps: "cardReps",
ankiGetCardInterval: "cardInterval",
ankiGetCardFactor: "cardFactor",
ankiGetCardMod: "cardMod",
ankiGetCardId: "cardId",
ankiGetCardNid: "cardNid",
ankiGetCardType: "cardType",
ankiGetCardDid: "cardDid",
ankiGetCardLeft: "cardLeft",
ankiGetCardODid: "cardODid",
ankiGetCardODue: "cardODue",
ankiGetCardQueue: "cardQueue",
ankiGetCardLapses: "cardLapses",
ankiGetCardDue: "cardDue",
ankiIsInFullscreen: "isInFullscreen",
ankiIsTopbarShown: "isTopbarShown",
ankiIsInNightMode: "isInNightMode",
ankiIsDisplayingAnswer: "isDisplayingAnswer",
ankiGetDeckName: "deckName",
ankiIsActiveNetworkMetered: "isActiveNetworkMetered",
ankiTtsFieldModifierIsAvailable: "ttsFieldModifierIsAvailable",
ankiTtsIsSpeaking: "ttsIsSpeaking",
ankiTtsStop: "ttsStop",
ankiBuryCard: "buryCard",
ankiBuryNote: "buryNote",
ankiSuspendCard: "suspendCard",
ankiSuspendNote: "suspendNote",
ankiAddTagToCard: "addTagToCard",
ankiResetProgress: "resetProgress",
ankiMarkCard: "markCard",
ankiToggleFlag: "toggleFlag",
ankiSearchCard: "searchCard",
ankiSearchCardWithCallback: "searchCardWithCallback",
ankiTtsSpeak: "ttsSpeak",
ankiTtsSetLanguage: "ttsSetLanguage",
ankiTtsSetPitch: "ttsSetPitch",
ankiTtsSetSpeechRate: "ttsSetSpeechRate",
ankiEnableHorizontalScrollbar: "enableHorizontalScrollbar",
ankiEnableVerticalScrollbar: "enableVerticalScrollbar",
ankiSetCardDue: "setCardDue",
ankiShowNavigationDrawer: "showNavigationDrawer",
ankiShowOptionsMenu: "showOptionsMenu",
ankiShowToast: "showToast",
ankiShowAnswer: "showAnswer",
ankiAnswerEase1: "answerEase1",
ankiAnswerEase2: "answerEase2",
ankiAnswerEase3: "answerEase3",
ankiAnswerEase4: "answerEase4",
ankiSttSetLanguage: "sttSetLanguage",
ankiSttStart: "sttStart",
ankiSttStop: "sttStop",
ankiAddTagToNote: "addTagToNote",
ankiSetNoteTags: "setNoteTags",
ankiGetNoteTags: "getNoteTags",
};
class AnkiDroidJS {
constructor({ developer, version }) {
this.developer = developer;
this.version = version;
this.handleRequest(`init`);
}
static init({ developer, version }) {
return new AnkiDroidJS({ developer, version });
}
handleRequest = async (endpoint, data) => {
const url = `/jsapi/${endpoint}`;
try {
const response = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
developer: this.developer,
version: this.version,
data,
}),
});
if (!response.ok) {
throw new Error("Failed to make the request");
}
const responseData = await response.text();
if (endpoint.includes("nextTime") || endpoint.includes("deckName")) {
return responseData;
}
return JSON.parse(responseData);
} catch (error) {
console.error("Request error:", error);
throw error;
}
};
}
Object.keys(jsApiList).forEach(method => {
if (method === "ankiAddTagToNote") {
AnkiDroidJS.prototype[method] = async function (noteId, tag) {
console.warn("ankiAddTagToNote is deprecated. Use ankiSetNoteTags instead");
const endpoint = jsApiList[method];
const data = JSON.stringify({ noteId, tag });
return await this.handleRequest(endpoint, data);
};
return;
}
if (method === "ankiSetNoteTags") {
AnkiDroidJS.prototype[method] = async function (noteId, tags) {
let hasSpaces = false;
for (let i = 0; i < tags.length; i++) {
tags[i] = tags[i].trim();
if (tags[i].includes(" ") || tags[i].includes("\u3000")) {
tags[i] = tags[i].replace(" ", "_").replace("\u3000", "_");
hasSpaces = true;
}
}
if (hasSpaces) {
console.warn("Spaces in tags have been converted to underscores");
}
const endpoint = jsApiList[method];
const data = JSON.stringify({ noteId, tags });
return await this.handleRequest(endpoint, data);
};
return;
}
if (method === "ankiGetNoteTags") {
AnkiDroidJS.prototype[method] = async function (noteId) {
const endpoint = jsApiList[method];
const data = JSON.stringify({ noteId });
return await this.handleRequest(endpoint, data);
};
return;
}
if (method === "ankiTtsSpeak") {
AnkiDroidJS.prototype[method] = async function (text, queueMode = 0) {
const endpoint = jsApiList[method];
const data = JSON.stringify({ text, queueMode });
return await this.handleRequest(endpoint, data);
};
return;
}
if (method === "ankiShowToast") {
AnkiDroidJS.prototype[method] = async function (text, shortLength = true) {
const endpoint = jsApiList[method];
const data = JSON.stringify({ text, shortLength });
return await this.handleRequest(endpoint, data);
};
return;
}
AnkiDroidJS.prototype[method] = async function (data) {
const endpoint = jsApiList[method];
return await this.handleRequest(endpoint, data);
};
});