aboutsummaryrefslogtreecommitdiff
path: root/ext/bg/js/anki.js
diff options
context:
space:
mode:
authorAlex Yatskov <alex@foosoft.net>2019-10-10 19:50:30 -0700
committerAlex Yatskov <alex@foosoft.net>2019-10-10 19:50:30 -0700
commit06d7713189be9eb51669d3842b78278371e6cfa4 (patch)
treee98640dc323c486fb1190280502acbf2fe9d8187 /ext/bg/js/anki.js
parent499239ce94e0480783af93f813c2b4096b495808 (diff)
parent55b2c1d8f51c658b0457ae8329fb1b0e52f5f799 (diff)
Merge branch 'master' into testing
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 [];
+ }
}