diff --git a/ext/bg/background.html b/ext/bg/background.html index a849fadb..863d1ab4 100644 --- a/ext/bg/background.html +++ b/ext/bg/background.html @@ -5,6 +5,7 @@ + diff --git a/ext/bg/js/ankiconnect.js b/ext/bg/js/ankiconnect.js index 66cea094..de199b2c 100644 --- a/ext/bg/js/ankiconnect.js +++ b/ext/bg/js/ankiconnect.js @@ -19,58 +19,40 @@ class AnkiConnect { constructor() { this.asyncPools = {}; - this.pluginVersion = null; - this.apiVersion = 1; + this.localVersion = 1; + this.remoteVersion = null; } addNote(note) { - return this.ankiInvokeSafe('addNote', {note}, null); + return this.checkVersion().then(() => this.ankiInvoke('addNote', {note}, null)); } canAddNotes(notes) { - return this.ankiInvokeSafe('canAddNotes', {notes}, 'notes'); + return this.checkVersion().then(() => this.ankiInvoke('canAddNotes', {notes}, 'notes')); } getDeckNames() { - return this.ankiInvokeSafe('deckNames', {}, null); + return this.checkVersion().then(() => this.ankiInvoke('deckNames', {}, null)); } getModelNames() { - return this.ankiInvokeSafe('modelNames', {}, null); + return this.checkVersion().then(() => this.ankiInvoke('modelNames', {}, null)); } getModelFieldNames(modelName) { - return this.ankiInvokeSafe('modelFieldNames', {modelName}, null); + return this.checkVersion().then(() => this.ankiInvoke('modelFieldNames', {modelName}, null)); } - getStatus() { - return this.getVersion().then(version => { - if (version === null) { - return 'disconnected'; - } else if (version === this.apiVersion) { - return 'ready'; - } else { - return 'mismatch'; - } - }); - } - - getVersion() { - return this.ankiInvoke('version', {}, null); - } - - ankiInvokeSafe(action, params, pool) { - if (this.pluginVersion === this.apiVersion) { - return this.ankiInvoke(action, params, pool); + checkVersion() { + if (this.localVersion === this.remoteVersion) { + return Promise.resolve(true); } - return this.getVersion().then(version => { - if (version === this.apiVersion) { - this.pluginVersion = version; - return this.ankiInvoke(action, params, pool); + return this.ankiInvoke('version', {}, null).then(version => { + this.remoteVersion = version; + if (this.remoteVersion !== this.localVersion) { + return Promise.reject('browser extension and anki plugin version mismatch'); } - - return null; }); } diff --git a/ext/bg/js/ankinull.js b/ext/bg/js/ankinull.js new file mode 100644 index 00000000..0d0ed903 --- /dev/null +++ b/ext/bg/js/ankinull.js @@ -0,0 +1,39 @@ +/* + * Copyright (C) 2016 Alex Yatskov + * Author: Alex Yatskov + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +class AnkiNull { + addNote(note) { + return Promise.reject('unsupported action'); + } + + canAddNotes(notes) { + return Promise.resolve([]); + } + + getDeckNames() { + return Promise.resolve([]); + } + + getModelNames() { + return Promise.resolve([]); + } + + getModelFieldNames(modelName) { + return Promise.resolve([]); + } +} diff --git a/ext/bg/js/ankiweb.js b/ext/bg/js/ankiweb.js index 9fc1fb57..ce909fcf 100644 --- a/ext/bg/js/ankiweb.js +++ b/ext/bg/js/ankiweb.js @@ -21,7 +21,7 @@ class AnkiWeb { this.username = username; this.password = password; this.noteInfo = null; - this.logged = false; + this.logged = true; } addNote(note) { diff --git a/ext/bg/js/options-form.js b/ext/bg/js/options-form.js index 674ff07d..7e2e9a7d 100644 --- a/ext/bg/js/options-form.js +++ b/ext/bg/js/options-form.js @@ -104,47 +104,51 @@ function getAnkiOptions() { } function populateAnkiDeckAndModel(opts) { - const yomi = yomichan(); + const anki = yomichan().anki; - const ankiDeck = $('.anki-deck'); - ankiDeck.find('option').remove(); - yomi.api_getDeckNames({callback: names => { - if (names !== null) { + const populateDecks = () => { + const ankiDeck = $('.anki-deck'); + ankiDeck.find('option').remove(); + return anki.getDeckNames().then(names => { names.forEach(name => ankiDeck.append($('