summaryrefslogtreecommitdiff
path: root/ext/bg
diff options
context:
space:
mode:
Diffstat (limited to 'ext/bg')
-rw-r--r--ext/bg/background.html1
-rw-r--r--ext/bg/js/ankiweb.js2
-rw-r--r--ext/bg/js/options-form.js18
-rw-r--r--ext/bg/js/options.js4
-rw-r--r--ext/bg/js/yomichan.js80
-rw-r--r--ext/bg/options.html19
6 files changed, 94 insertions, 30 deletions
diff --git a/ext/bg/background.html b/ext/bg/background.html
index a849fadb..65707416 100644
--- a/ext/bg/background.html
+++ b/ext/bg/background.html
@@ -2,6 +2,7 @@
<html lang="en">
<body>
<script src="../lib/handlebars.min.js"></script>
+ <script src="../lib/jquery-2.2.2.min.js"></script>
<script src="../lib/dexie.min.js"></script>
<script src="js/ankiweb.js"></script>
<script src="js/ankiconnect.js"></script>
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);
+ }
}
}
diff --git a/ext/bg/options.html b/ext/bg/options.html
index f5bad2bd..efb3a82f 100644
--- a/ext/bg/options.html
+++ b/ext/bg/options.html
@@ -97,6 +97,15 @@
<div class="options-anki">
<h3>Anki Options</h3>
+ <div class="form-group">
+ <label class="control-label" for="anki-method">Integration method</label>
+ <select class="form-control" id="anki-method">
+ <option value="none">None</option>
+ <option value="ankiweb">AnkiWeb</option>
+ <option value="ankiconnect">AnkiConnect</option>
+ </select>
+ </div>
+
<div class="alert alert-danger error-dlg error-dlg-connection">
<strong>Unable to Connect</strong><br>
Is the <a href="https://foosoft.net/projects/anki-connect">AnkiConnect</a> extension for <a href="http://ankisrs.net/">Anki</a> installed and running? This software is required for Anki-related features.
@@ -108,6 +117,16 @@
<form class="form-horizontal options-anki-controls">
<div class="form-group">
+ <label for="anki-username" class="control-label col-sm-2">Username</label>
+ <div class="col-sm-10"><input type="text" id="anki-username" class="form-control"></div>
+ </div>
+
+ <div class="form-group">
+ <label for="anki-password" class="control-label col-sm-2">Password</label>
+ <div class="col-sm-10"><input type="password" id="anki-password" class="form-control"></div>
+ </div>
+
+ <div class="form-group">
<label for="anki-card-tags" class="control-label col-sm-2">Card tags</label>
<div class="col-sm-10"><input type="text" id="anki-card-tags" class="form-control"></div>
</div>