summaryrefslogtreecommitdiff
path: root/ext/bg/js/anki.js
diff options
context:
space:
mode:
Diffstat (limited to 'ext/bg/js/anki.js')
-rw-r--r--ext/bg/js/anki.js31
1 files changed, 30 insertions, 1 deletions
diff --git a/ext/bg/js/anki.js b/ext/bg/js/anki.js
index bd4e46cd..9f851f13 100644
--- a/ext/bg/js/anki.js
+++ b/ext/bg/js/anki.js
@@ -67,14 +67,39 @@ class AnkiConnect {
if (this.remoteVersion < this.localVersion) {
this.remoteVersion = await this.ankiInvoke('version');
if (this.remoteVersion < this.localVersion) {
- throw 'Extension and plugin versions incompatible';
+ throw new Error('Extension and plugin versions incompatible');
}
}
}
+ async findNoteIds(notes) {
+ await this.checkVersion();
+ const actions = notes.map(note => ({
+ action: 'findNotes',
+ params: {
+ query: `deck:"${AnkiConnect.escapeQuery(note.deckName)}" ${AnkiConnect.fieldsToQuery(note.fields)}`
+ }
+ }));
+ return await this.ankiInvoke('multi', {actions});
+ }
+
ankiInvoke(action, params) {
return requestJson(this.server, 'POST', {action, params, version: this.localVersion});
}
+
+ static escapeQuery(text) {
+ return text.replace(/"/g, '');
+ }
+
+ static fieldsToQuery(fields) {
+ const fieldNames = Object.keys(fields);
+ if (fieldNames.length === 0) {
+ return '';
+ }
+
+ const key = fieldNames[0];
+ return `${key.toLowerCase()}:"${AnkiConnect.escapeQuery(fields[key])}"`;
+ }
}
@@ -106,4 +131,8 @@ class AnkiNull {
async guiBrowse(query) {
return [];
}
+
+ async findNoteIds(notes) {
+ return [];
+ }
}