diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2019-10-09 20:27:05 -0400 |
---|---|---|
committer | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2019-10-09 20:27:05 -0400 |
commit | 97f5b7139fcb69c68560d025f99418b0f697940c (patch) | |
tree | b0b7e3ceeedc4a4b6ab8001f7ffbc132965fb1a6 /ext/bg/js | |
parent | c5d6b9452d1bfa51a5fef9a19082d115ef180a9b (diff) |
Add findNoteIds to AnkiConnect
Diffstat (limited to 'ext/bg/js')
-rw-r--r-- | ext/bg/js/anki.js | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/ext/bg/js/anki.js b/ext/bg/js/anki.js index 5c89aad8..9f851f13 100644 --- a/ext/bg/js/anki.js +++ b/ext/bg/js/anki.js @@ -72,9 +72,34 @@ class AnkiConnect { } } + 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 []; + } } |