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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
|
/*
* Copyright (C) 2023-2024 Yomitan Authors
* Copyright (C) 2016-2022 Yomichan Authors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {ExtensionError} from '../core/extension-error.js';
import {parseJson} from '../core/json.js';
import {isObjectNotArray} from '../core/object-utilities.js';
import {getRootDeckName} from '../data/anki-util.js';
/**
* This class controls communication with Anki via the AnkiConnect plugin.
*/
export class AnkiConnect {
/**
* Creates a new instance.
*/
constructor() {
/** @type {boolean} */
this._enabled = false;
/** @type {?string} */
this._server = null;
/** @type {number} */
this._localVersion = 2;
/** @type {number} */
this._remoteVersion = 0;
/** @type {?Promise<number>} */
this._versionCheckPromise = null;
/** @type {?string} */
this._apiKey = null;
}
/**
* Gets the URL of the AnkiConnect server.
* @type {?string}
*/
get server() {
return this._server;
}
/**
* Assigns the URL of the AnkiConnect server.
* @param {string} value The new server URL to assign.
*/
set server(value) {
this._server = value;
}
/**
* Gets whether or not server communication is enabled.
* @type {boolean}
*/
get enabled() {
return this._enabled;
}
/**
* Sets whether or not server communication is enabled.
* @param {boolean} value The enabled state.
*/
set enabled(value) {
this._enabled = value;
}
/**
* Gets the API key used when connecting to AnkiConnect.
* The value will be `null` if no API key is used.
* @type {?string}
*/
get apiKey() {
return this._apiKey;
}
/**
* Sets the API key used when connecting to AnkiConnect.
* @param {?string} value The API key to use, or `null` if no API key should be used.
*/
set apiKey(value) {
this._apiKey = value;
}
/**
* Checks whether a connection to AnkiConnect can be established.
* @returns {Promise<boolean>} `true` if the connection was made, `false` otherwise.
*/
async isConnected() {
try {
await this._getVersion();
return true;
} catch (e) {
return false;
}
}
/**
* Gets the AnkiConnect API version number.
* @returns {Promise<?number>} The version number
*/
async getVersion() {
if (!this._enabled) { return null; }
await this._checkVersion();
return await this._getVersion();
}
/**
* @param {import('anki').Note} note
* @returns {Promise<?import('anki').NoteId>}
*/
async addNote(note) {
if (!this._enabled) { return null; }
await this._checkVersion();
const result = await this._invoke('addNote', {note});
if (result !== null && typeof result !== 'number') {
throw this._createUnexpectedResultError('number|null', result);
}
return result;
}
/**
* @param {import('anki').Note[]} notes
* @returns {Promise<boolean[]>}
*/
async canAddNotes(notes) {
if (!this._enabled) { return []; }
await this._checkVersion();
const result = await this._invoke('canAddNotes', {notes});
return this._normalizeArray(result, notes.length, 'boolean');
}
/**
* @param {import('anki').NoteId[]} noteIds
* @returns {Promise<(?import('anki').NoteInfo)[]>}
*/
async notesInfo(noteIds) {
if (!this._enabled) { return []; }
await this._checkVersion();
const result = await this._invoke('notesInfo', {notes: noteIds});
return this._normalizeNoteInfoArray(result);
}
/**
* @returns {Promise<string[]>}
*/
async getDeckNames() {
if (!this._enabled) { return []; }
await this._checkVersion();
const result = await this._invoke('deckNames', {});
return this._normalizeArray(result, -1, 'string');
}
/**
* @returns {Promise<string[]>}
*/
async getModelNames() {
if (!this._enabled) { return []; }
await this._checkVersion();
const result = await this._invoke('modelNames', {});
return this._normalizeArray(result, -1, 'string');
}
/**
* @param {string} modelName
* @returns {Promise<string[]>}
*/
async getModelFieldNames(modelName) {
if (!this._enabled) { return []; }
await this._checkVersion();
const result = await this._invoke('modelFieldNames', {modelName});
return this._normalizeArray(result, -1, 'string');
}
/**
* @param {string} query
* @returns {Promise<import('anki').CardId[]>}
*/
async guiBrowse(query) {
if (!this._enabled) { return []; }
await this._checkVersion();
const result = await this._invoke('guiBrowse', {query});
return this._normalizeArray(result, -1, 'number');
}
/**
* @param {import('anki').NoteId} noteId
* @returns {Promise<import('anki').CardId[]>}
*/
async guiBrowseNote(noteId) {
return await this.guiBrowse(`nid:${noteId}`);
}
/**
* @param {import('anki').NoteId[]} noteIds
* @returns {Promise<import('anki').CardId[]>}
*/
async guiBrowseNotes(noteIds) {
return await this.guiBrowse(`nid:${noteIds.join(',')}`);
}
/**
* Opens the note editor GUI.
* @param {import('anki').NoteId} noteId The ID of the note.
* @returns {Promise<void>} Nothing is returned.
*/
async guiEditNote(noteId) {
await this._invoke('guiEditNote', {note: noteId});
}
/**
* Stores a file with the specified base64-encoded content inside Anki's media folder.
* @param {string} fileName The name of the file.
* @param {string} content The base64-encoded content of the file.
* @returns {Promise<?string>} The actual file name used to store the file, which may be different; or `null` if the file was not stored.
* @throws {Error} An error is thrown is this object is not enabled.
*/
async storeMediaFile(fileName, content) {
if (!this._enabled) {
throw new Error('AnkiConnect not enabled');
}
await this._checkVersion();
const result = await this._invoke('storeMediaFile', {filename: fileName, data: content});
if (result !== null && typeof result !== 'string') {
throw this._createUnexpectedResultError('string|null', result);
}
return result;
}
/**
* Finds notes matching a query.
* @param {string} query Searches for notes matching a query.
* @returns {Promise<import('anki').NoteId[]>} An array of note IDs.
* @see https://docs.ankiweb.net/searching.html
*/
async findNotes(query) {
if (!this._enabled) { return []; }
await this._checkVersion();
const result = await this._invoke('findNotes', {query});
return this._normalizeArray(result, -1, 'number');
}
/**
* @param {import('anki').Note[]} notes
* @returns {Promise<import('anki').NoteId[][]>}
*/
async findNoteIds(notes) {
if (!this._enabled) { return []; }
await this._checkVersion();
const actions = [];
const actionsTargetsList = [];
/** @type {Map<string, import('anki').NoteId[][]>} */
const actionsTargetsMap = new Map();
/** @type {import('anki').NoteId[][]} */
const allNoteIds = [];
for (const note of notes) {
const query = this._getNoteQuery(note);
let actionsTargets = actionsTargetsMap.get(query);
if (typeof actionsTargets === 'undefined') {
actionsTargets = [];
actionsTargetsList.push(actionsTargets);
actionsTargetsMap.set(query, actionsTargets);
actions.push({action: 'findNotes', params: {query}});
}
/** @type {import('anki').NoteId[]} */
const noteIds = [];
allNoteIds.push(noteIds);
actionsTargets.push(noteIds);
}
const result = await this._invokeMulti(actions);
for (let i = 0, ii = Math.min(result.length, actionsTargetsList.length); i < ii; ++i) {
const noteIds = /** @type {number[]} */ (this._normalizeArray(result[i], -1, 'number'));
for (const actionsTargets of actionsTargetsList[i]) {
for (const noteId of noteIds) {
actionsTargets.push(noteId);
}
}
}
return allNoteIds;
}
/**
* @param {import('anki').CardId[]} cardIds
* @returns {Promise<boolean>}
*/
async suspendCards(cardIds) {
if (!this._enabled) { return false; }
await this._checkVersion();
const result = await this._invoke('suspend', {cards: cardIds});
return typeof result === 'boolean' && result;
}
/**
* @param {string} query
* @returns {Promise<import('anki').CardId[]>}
*/
async findCards(query) {
if (!this._enabled) { return []; }
await this._checkVersion();
const result = await this._invoke('findCards', {query});
return this._normalizeArray(result, -1, 'number');
}
/**
* @param {import('anki').NoteId} noteId
* @returns {Promise<import('anki').CardId[]>}
*/
async findCardsForNote(noteId) {
return await this.findCards(`nid:${noteId}`);
}
/**
* Gets information about the AnkiConnect APIs available.
* @param {string[]} scopes A list of scopes to get information about.
* @param {?string[]} actions A list of actions to check for
* @returns {Promise<import('anki').ApiReflectResult>} Information about the APIs.
*/
async apiReflect(scopes, actions = null) {
const result = await this._invoke('apiReflect', {scopes, actions});
if (!(typeof result === 'object' && result !== null)) {
throw this._createUnexpectedResultError('object', result);
}
const {scopes: resultScopes, actions: resultActions} = /** @type {import('core').SerializableObject} */ (result);
const resultScopes2 = /** @type {string[]} */ (this._normalizeArray(resultScopes, -1, 'string', ', field scopes'));
const resultActions2 = /** @type {string[]} */ (this._normalizeArray(resultActions, -1, 'string', ', field scopes'));
return {
scopes: resultScopes2,
actions: resultActions2
};
}
/**
* Checks whether a specific API action exists.
* @param {string} action The action to check for.
* @returns {Promise<boolean>} Whether or not the action exists.
*/
async apiExists(action) {
const {actions} = await this.apiReflect(['actions'], [action]);
return actions.includes(action);
}
/**
* Checks if a specific error object corresponds to an unsupported action.
* @param {Error} error An error object generated by an API call.
* @returns {boolean} Whether or not the error indicates the action is not supported.
*/
isErrorUnsupportedAction(error) {
if (error instanceof ExtensionError) {
const {data} = error;
if (typeof data === 'object' && data !== null && /** @type {import('core').SerializableObject} */ (data).apiError === 'unsupported action') {
return true;
}
}
return false;
}
// Private
/**
* @returns {Promise<void>}
*/
async _checkVersion() {
if (this._remoteVersion < this._localVersion) {
if (this._versionCheckPromise === null) {
const promise = this._getVersion();
promise
.catch(() => {})
.finally(() => { this._versionCheckPromise = null; });
this._versionCheckPromise = promise;
}
this._remoteVersion = await this._versionCheckPromise;
if (this._remoteVersion < this._localVersion) {
throw new Error('Extension and plugin versions incompatible');
}
}
}
/**
* @param {string} action
* @param {import('core').SerializableObject} params
* @returns {Promise<unknown>}
*/
async _invoke(action, params) {
/** @type {import('anki').MessageBody} */
const body = {action, params, version: this._localVersion};
if (this._apiKey !== null) { body.key = this._apiKey; }
let response;
try {
if (this._server === null) { throw new Error('Server URL is null'); }
response = await fetch(this._server, {
method: 'POST',
mode: 'cors',
cache: 'default',
credentials: 'omit',
headers: {
'Content-Type': 'application/json'
},
redirect: 'follow',
referrerPolicy: 'no-referrer',
body: JSON.stringify(body)
});
} catch (e) {
const error = new ExtensionError('Anki connection failure');
error.data = {action, params, originalError: e};
throw error;
}
if (!response.ok) {
const error = new ExtensionError(`Anki connection error: ${response.status}`);
error.data = {action, params, status: response.status};
throw error;
}
let responseText = null;
/** @type {unknown} */
let result;
try {
responseText = await response.text();
result = parseJson(responseText);
} catch (e) {
const error = new ExtensionError('Invalid Anki response');
error.data = {action, params, status: response.status, responseText, originalError: e};
throw error;
}
if (typeof result === 'object' && result !== null && !Array.isArray(result)) {
const apiError = /** @type {import('core').SerializableObject} */ (result).error;
if (typeof apiError !== 'undefined') {
const error = new ExtensionError(`Anki error: ${apiError}`);
error.data = {action, params, status: response.status, apiError: typeof apiError === 'string' ? apiError : `${apiError}`};
throw error;
}
}
return result;
}
/**
* @param {{action: string, params: import('core').SerializableObject}[]} actions
* @returns {Promise<unknown[]>}
*/
async _invokeMulti(actions) {
const result = await this._invoke('multi', {actions});
if (!Array.isArray(result)) {
throw this._createUnexpectedResultError('array', result);
}
return result;
}
/**
* @param {string} text
* @returns {string}
*/
_escapeQuery(text) {
return text.replace(/"/g, '');
}
/**
* @param {import('anki').NoteFields} fields
* @returns {string}
*/
_fieldsToQuery(fields) {
const fieldNames = Object.keys(fields);
if (fieldNames.length === 0) {
return '';
}
const key = fieldNames[0];
return `"${key.toLowerCase()}:${this._escapeQuery(fields[key])}"`;
}
/**
* @param {import('anki').Note} note
* @returns {?('collection'|'deck'|'deck-root')}
*/
_getDuplicateScopeFromNote(note) {
const {options} = note;
if (typeof options === 'object' && options !== null) {
const {duplicateScope} = options;
if (typeof duplicateScope !== 'undefined') {
return duplicateScope;
}
}
return null;
}
/**
* @param {import('anki').Note} note
* @returns {string}
*/
_getNoteQuery(note) {
let query = '';
switch (this._getDuplicateScopeFromNote(note)) {
case 'deck':
query = `"deck:${this._escapeQuery(note.deckName)}" `;
break;
case 'deck-root':
query = `"deck:${this._escapeQuery(getRootDeckName(note.deckName))}" `;
break;
}
query += this._fieldsToQuery(note.fields);
return query;
}
/**
* @returns {Promise<number>}
*/
async _getVersion() {
const version = await this._invoke('version', {});
return typeof version === 'number' ? version : 0;
}
/**
* @param {string} message
* @param {unknown} data
* @returns {ExtensionError}
*/
_createError(message, data) {
const error = new ExtensionError(message);
error.data = data;
return error;
}
/**
* @param {string} expectedType
* @param {unknown} result
* @param {string} [context]
* @returns {ExtensionError}
*/
_createUnexpectedResultError(expectedType, result, context) {
return this._createError(`Unexpected type${typeof context === 'string' ? context : ''}: expected ${expectedType}, received ${this._getTypeName(result)}`, result);
}
/**
* @param {unknown} value
* @returns {string}
*/
_getTypeName(value) {
if (value === null) { return 'null'; }
return Array.isArray(value) ? 'array' : typeof value;
}
/**
* @template [T=unknown]
* @param {unknown} result
* @param {number} expectedCount
* @param {'boolean'|'string'|'number'} type
* @param {string} [context]
* @returns {T[]}
* @throws {Error}
*/
_normalizeArray(result, expectedCount, type, context) {
if (!Array.isArray(result)) {
throw this._createUnexpectedResultError(`${type}[]`, result, context);
}
if (expectedCount < 0) {
expectedCount = result.length;
} else if (expectedCount !== result.length) {
throw this._createError(`Unexpected result array size${context}: expected ${expectedCount}, received ${result.length}`, result);
}
for (let i = 0; i < expectedCount; ++i) {
const item = /** @type {unknown} */ (result[i]);
if (typeof item !== type) {
throw this._createError(`Unexpected result type at index ${i}${context}: expected ${type}, received ${this._getTypeName(item)}`, result);
}
}
return /** @type {T[]} */ (result);
}
/**
* @param {unknown} result
* @returns {(?import('anki').NoteInfo)[]}
* @throws {Error}
*/
_normalizeNoteInfoArray(result) {
if (!Array.isArray(result)) {
throw this._createUnexpectedResultError('array', result, '');
}
/** @type {(?import('anki').NoteInfo)[]} */
const result2 = [];
for (let i = 0, ii = result.length; i < ii; ++i) {
const item = /** @type {unknown} */ (result[i]);
if (item === null || typeof item !== 'object') {
throw this._createError(`Unexpected result type at index ${i}: expected Notes.NoteInfo, received ${this._getTypeName(item)}`, result);
}
const {noteId} = /** @type {{[key: string]: unknown}} */ (item);
if (typeof noteId !== 'number') {
result2.push(null);
continue;
}
const {tags, fields, modelName, cards} = /** @type {{[key: string]: unknown}} */ (item);
if (typeof modelName !== 'string') {
throw this._createError(`Unexpected result type at index ${i}, field modelName: expected string, received ${this._getTypeName(modelName)}`, result);
}
if (!isObjectNotArray(fields)) {
throw this._createError(`Unexpected result type at index ${i}, field fields: expected object, received ${this._getTypeName(fields)}`, result);
}
const tags2 = /** @type {string[]} */ (this._normalizeArray(tags, -1, 'string', ', field tags'));
const cards2 = /** @type {number[]} */ (this._normalizeArray(cards, -1, 'number', ', field cards'));
/** @type {{[key: string]: import('anki').NoteFieldInfo}} */
const fields2 = {};
for (const [key, fieldInfo] of Object.entries(fields)) {
if (!isObjectNotArray(fieldInfo)) { continue; }
const {value, order} = fieldInfo;
if (typeof value !== 'string' || typeof order !== 'number') { continue; }
fields2[key] = {value, order};
}
/** @type {import('anki').NoteInfo} */
const item2 = {
noteId,
tags: tags2,
fields: fields2,
modelName,
cards: cards2
};
result2.push(item2);
}
return result2;
}
}
|