From 0b4bdec7f2f7add052009d4aa9c113229a7304cc Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sat, 15 Oct 2016 23:23:40 -0700 Subject: WIP --- ext/bg/background.html | 1 + ext/bg/js/ankiconnect.js | 46 ++++++------------- ext/bg/js/ankinull.js | 39 +++++++++++++++++ ext/bg/js/ankiweb.js | 2 +- ext/bg/js/options-form.js | 109 ++++++++++++++++++++-------------------------- ext/bg/js/yomichan.js | 69 ++++++++++------------------- ext/bg/options.html | 14 +++--- 7 files changed, 131 insertions(+), 149 deletions(-) create mode 100644 ext/bg/js/ankinull.js (limited to 'ext/bg') 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($('