Skip to content
This repository has been archived by the owner on Feb 25, 2023. It is now read-only.

Commit

Permalink
Add api.isAnkiConnected (#1011)
Browse files Browse the repository at this point in the history
  • Loading branch information
toasted-nutbread authored Nov 8, 2020
1 parent 0cbc652 commit 4e30409
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
9 changes: 9 additions & 0 deletions ext/bg/js/anki.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ class AnkiConnect {
this._enabled = value;
}

async isConnected() {
try {
await this._invoke('version');
return true;
} catch (e) {
return false;
}
}

async addNote(note) {
if (!this._enabled) { return null; }
await this._checkVersion();
Expand Down
5 changes: 5 additions & 0 deletions ext/bg/js/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ class Backend {
['kanjiFind', {async: true, contentScript: true, handler: this._onApiKanjiFind.bind(this)}],
['termsFind', {async: true, contentScript: true, handler: this._onApiTermsFind.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)}],
['getAnkiNoteInfo', {async: true, contentScript: true, handler: this._onApiGetAnkiNoteInfo.bind(this)}],
['injectAnkiNoteMedia', {async: true, contentScript: true, handler: this._onApiInjectAnkiNoteMedia.bind(this)}],
Expand Down Expand Up @@ -420,6 +421,10 @@ class Backend {
return results;
}

async _onApiIsAnkiConnected() {
return await this._anki.isConnected();
}

async _onApiAddAnkiNote({note}) {
return await this._anki.addNote(note);
}
Expand Down
4 changes: 4 additions & 0 deletions ext/mixed/js/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ const api = (() => {
return this._invoke('kanjiFind', {text, optionsContext});
}

isAnkiConnected() {
return this._invoke('isAnkiConnected');
}

addAnkiNote(note) {
return this._invoke('addAnkiNote', {note});
}
Expand Down

0 comments on commit 4e30409

Please sign in to comment.