Add api.isAnkiConnected (#1011)

This commit is contained in:
toasted-nutbread 2020-11-08 15:53:06 -05:00 committed by GitHub
parent 0cbc6523e6
commit 4e30409410
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 0 deletions

View File

@ -40,6 +40,15 @@ class AnkiConnect {
this._enabled = value; this._enabled = value;
} }
async isConnected() {
try {
await this._invoke('version');
return true;
} catch (e) {
return false;
}
}
async addNote(note) { async addNote(note) {
if (!this._enabled) { return null; } if (!this._enabled) { return null; }
await this._checkVersion(); await this._checkVersion();

View File

@ -83,6 +83,7 @@ class Backend {
['kanjiFind', {async: true, contentScript: true, handler: this._onApiKanjiFind.bind(this)}], ['kanjiFind', {async: true, contentScript: true, handler: this._onApiKanjiFind.bind(this)}],
['termsFind', {async: true, contentScript: true, handler: this._onApiTermsFind.bind(this)}], ['termsFind', {async: true, contentScript: true, handler: this._onApiTermsFind.bind(this)}],
['textParse', {async: true, contentScript: true, handler: this._onApiTextParse.bind(this)}], ['textParse', {async: true, contentScript: true, handler: this._onApiTextParse.bind(this)}],
['isAnkiConnected', {async: true, contentScript: true, handler: this._onApiIsAnkiConnected.bind(this)}],
['addAnkiNote', {async: true, contentScript: true, handler: this._onApiAddAnkiNote.bind(this)}], ['addAnkiNote', {async: true, contentScript: true, handler: this._onApiAddAnkiNote.bind(this)}],
['getAnkiNoteInfo', {async: true, contentScript: true, handler: this._onApiGetAnkiNoteInfo.bind(this)}], ['getAnkiNoteInfo', {async: true, contentScript: true, handler: this._onApiGetAnkiNoteInfo.bind(this)}],
['injectAnkiNoteMedia', {async: true, contentScript: true, handler: this._onApiInjectAnkiNoteMedia.bind(this)}], ['injectAnkiNoteMedia', {async: true, contentScript: true, handler: this._onApiInjectAnkiNoteMedia.bind(this)}],
@ -420,6 +421,10 @@ class Backend {
return results; return results;
} }
async _onApiIsAnkiConnected() {
return await this._anki.isConnected();
}
async _onApiAddAnkiNote({note}) { async _onApiAddAnkiNote({note}) {
return await this._anki.addNote(note); return await this._anki.addNote(note);
} }

View File

@ -69,6 +69,10 @@ const api = (() => {
return this._invoke('kanjiFind', {text, optionsContext}); return this._invoke('kanjiFind', {text, optionsContext});
} }
isAnkiConnected() {
return this._invoke('isAnkiConnected');
}
addAnkiNote(note) { addAnkiNote(note) {
return this._invoke('addAnkiNote', {note}); return this._invoke('addAnkiNote', {note});
} }