diff options
Diffstat (limited to 'ext/bg/js')
-rw-r--r-- | ext/bg/js/ankiweb.js | 2 | ||||
-rw-r--r-- | ext/bg/js/options-form.js | 18 | ||||
-rw-r--r-- | ext/bg/js/options.js | 4 | ||||
-rw-r--r-- | ext/bg/js/yomichan.js | 80 |
4 files changed, 74 insertions, 30 deletions
diff --git a/ext/bg/js/ankiweb.js b/ext/bg/js/ankiweb.js index 5b91594e..d68b81ab 100644 --- a/ext/bg/js/ankiweb.js +++ b/ext/bg/js/ankiweb.js @@ -115,7 +115,7 @@ class AnkiWeb { static login(username, password) { return new Promise((resolve, reject) => { $.post('https://ankiweb.net/account/login', {username, password, submitted: 1}, (data, status) => { - if (status !== 'success') { + if (status === 'success') { if (data.includes('class="mitem"')) { resolve(); } else { diff --git a/ext/bg/js/options-form.js b/ext/bg/js/options-form.js index 653521d6..7d280429 100644 --- a/ext/bg/js/options-form.js +++ b/ext/bg/js/options-form.js @@ -85,6 +85,9 @@ function getAnkiOptions() { return loadOptions().then(optsOld => { const optsNew = $.extend({}, optsOld); + optsNew.ankiMethod = $('#anki-method').val(); + optsNew.ankiUsername = $('#anki-username').val(); + optsNew.ankiPassword = $('#anki-password').val(); optsNew.ankiCardTags = $('#anki-card-tags').val().split(/[,; ]+/); optsNew.sentenceExtent = parseInt($('#sentence-extent').val(), 10); optsNew.ankiTermDeck = $('#anki-term-deck').val(); @@ -219,13 +222,11 @@ function onOptionsBasicChanged(e) { } function onOptionsAnkiChanged(e) { - if (!e.originalEvent && !e.isTrigger) { - return; + if (e.originalEvent || e.isTrigger) { + getAnkiOptions().then(({optsNew, optsOld}) => { + saveOptions(optsNew).then(() => yomichan().setOptions(optsNew)); + }); } - - getAnkiOptions().then(({optsNew, optsOld}) => { - saveOptions(optsNew).then(() => yomichan().setOptions(optsNew)); - }); } function onAnkiModelChanged(e) { @@ -248,9 +249,14 @@ $(document).ready(() => { $('#select-matched-text').prop('checked', opts.selectMatchedText); $('#scan-delay').val(opts.scanDelay); $('#scan-length').val(opts.scanLength); + + $('#anki-method').val(opts.ankiMethod); + $('#anki-username').val(opts.ankiUsername); + $('#anki-password').val(opts.ankiPassword); $('#anki-card-tags').val(opts.ankiCardTags.join(' ')); $('#sentence-extent').val(opts.sentenceExtent); + $('#anki-method').change(onOptionsAnkiChanged); $('.options-basic input').change(onOptionsBasicChanged); $('.options-anki input').change(onOptionsAnkiChanged); $('.anki-deck').change(onOptionsAnkiChanged); diff --git a/ext/bg/js/options.js b/ext/bg/js/options.js index e9ad74a3..0dd37c48 100644 --- a/ext/bg/js/options.js +++ b/ext/bg/js/options.js @@ -27,6 +27,10 @@ function sanitizeOptions(options) { holdShiftToScan: true, scanDelay: 15, scanLength: 20, + + ankiMethod: 'none', + ankiUsername: '', + ankiPassword: '', ankiCardTags: ['yomichan'], sentenceExtent: 200, diff --git a/ext/bg/js/yomichan.js b/ext/bg/js/yomichan.js index 0038645c..2f6d3841 100644 --- a/ext/bg/js/yomichan.js +++ b/ext/bg/js/yomichan.js @@ -23,7 +23,7 @@ class Yomichan { Handlebars.registerHelper('kanjiLinks', kanjiLinks); this.translator = new Translator(); - this.anki = new AnkiConnect(); + this.anki = null; this.options = null; this.importTabId = null; this.setState('disabled'); @@ -100,6 +100,19 @@ class Yomichan { setOptions(options) { this.options = options; + + switch (options.ankiMethod) { + case 'ankiweb': + this.anki = new AnkiWeb(options.ankiUsername, options.ankiPassword); + break; + case 'ankiconnect': + this.anki = new AnkiConnect(); + break; + default: + this.anki = null; + break; + } + this.tabInvokeAll('setOptions', this.options); } @@ -238,45 +251,66 @@ class Yomichan { } api_addDefinition({definition, mode, callback}) { - const note = this.formatNote(definition, mode); - this.anki.addNote(note).then(callback); + if (this.anki === null) { + callback(null); + } else { + const note = this.formatNote(definition, mode); + this.anki.addNote(note).then(callback); + } } api_canAddDefinitions({definitions, modes, callback}) { - const notes = []; - for (const definition of definitions) { - for (const mode of modes) { - notes.push(this.formatNote(definition, mode)); - } + if (this.anki === null) { + callback(null); } + else { + const notes = []; + for (const definition of definitions) { + for (const mode of modes) { + notes.push(this.formatNote(definition, mode)); + } + } - this.anki.canAddNotes(notes).then(results => { - const states = []; - if (results !== null) { - for (let resultBase = 0; resultBase < results.length; resultBase += modes.length) { - const state = {}; - for (let modeOffset = 0; modeOffset < modes.length; ++modeOffset) { - state[modes[modeOffset]] = results[resultBase + modeOffset]; - } + this.anki.canAddNotes(notes).then(results => { + const states = []; + if (results !== null) { + for (let resultBase = 0; resultBase < results.length; resultBase += modes.length) { + const state = {}; + for (let modeOffset = 0; modeOffset < modes.length; ++modeOffset) { + state[modes[modeOffset]] = results[resultBase + modeOffset]; + } - states.push(state); + states.push(state); + } } - } - callback(states); - }); + callback(states); + }); + } } api_getDeckNames({callback}) { - this.anki.getDeckNames().then(callback); + if (this.anki === null) { + callback(null); + } else { + this.anki.getDeckNames().then(callback); + } } api_getModelNames({callback}) { - this.anki.getModelNames().then(callback); + if (this.anki === null) { + callback(null); + } else { + this.anki.getModelNames().then(callback); + } } api_getModelFieldNames({modelName, callback}) { - this.anki.getModelFieldNames(modelName).then(callback); + if (this.anki === null) { + callback(null); + } else { + this.anki.getModelFieldNames(modelName).then(callback); + } } } |