aboutsummaryrefslogtreecommitdiff
path: root/ext/bg
diff options
context:
space:
mode:
Diffstat (limited to 'ext/bg')
-rw-r--r--ext/bg/js/anki-note-builder.js7
-rw-r--r--ext/bg/js/anki.js141
-rw-r--r--ext/bg/js/backend.js10
3 files changed, 78 insertions, 80 deletions
diff --git a/ext/bg/js/anki-note-builder.js b/ext/bg/js/anki-note-builder.js
index 8a707006..700d8237 100644
--- a/ext/bg/js/anki-note-builder.js
+++ b/ext/bg/js/anki-note-builder.js
@@ -16,7 +16,8 @@
*/
class AnkiNoteBuilder {
- constructor({audioSystem, renderTemplate}) {
+ constructor({anki, audioSystem, renderTemplate}) {
+ this._anki = anki;
this._audioSystem = audioSystem;
this._renderTemplate = renderTemplate;
}
@@ -101,7 +102,7 @@ class AnkiNoteBuilder {
}
}
- async injectScreenshot(definition, fields, screenshot, anki) {
+ async injectScreenshot(definition, fields, screenshot) {
if (!this._containsMarker(fields, 'screenshot')) { return; }
const now = new Date(Date.now());
@@ -109,7 +110,7 @@ class AnkiNoteBuilder {
const data = screenshot.dataUrl.replace(/^data:[\w\W]*?,/, '');
try {
- await anki.storeMediaFile(filename, data);
+ await this._anki.storeMediaFile(filename, data);
} catch (e) {
return;
}
diff --git a/ext/bg/js/anki.js b/ext/bg/js/anki.js
index c7f7c0cc..38823431 100644
--- a/ext/bg/js/anki.js
+++ b/ext/bg/js/anki.js
@@ -19,122 +19,119 @@
* requestJson
*/
-/*
- * AnkiConnect
- */
-
class AnkiConnect {
constructor(server) {
- this.server = server;
- this.localVersion = 2;
- this.remoteVersion = 0;
+ this._enabled = false;
+ this._server = server;
+ this._localVersion = 2;
+ this._remoteVersion = 0;
+ }
+
+ setServer(server) {
+ this._server = server;
+ }
+
+ getServer() {
+ return this._server;
+ }
+
+ setEnabled(enabled) {
+ this._enabled = enabled;
+ }
+
+ isEnabled() {
+ return this._enabled;
}
async addNote(note) {
- await this.checkVersion();
- return await this.ankiInvoke('addNote', {note});
+ if (!this._enabled) { return null; }
+ await this._checkVersion();
+ return await this._invoke('addNote', {note});
}
async canAddNotes(notes) {
- await this.checkVersion();
- return await this.ankiInvoke('canAddNotes', {notes});
+ if (!this._enabled) { return []; }
+ await this._checkVersion();
+ return await this._invoke('canAddNotes', {notes});
}
async getDeckNames() {
- await this.checkVersion();
- return await this.ankiInvoke('deckNames');
+ if (!this._enabled) { return []; }
+ await this._checkVersion();
+ return await this._invoke('deckNames');
}
async getModelNames() {
- await this.checkVersion();
- return await this.ankiInvoke('modelNames');
+ if (!this._enabled) { return []; }
+ await this._checkVersion();
+ return await this._invoke('modelNames');
}
async getModelFieldNames(modelName) {
- await this.checkVersion();
- return await this.ankiInvoke('modelFieldNames', {modelName});
+ if (!this._enabled) { return []; }
+ await this._checkVersion();
+ return await this._invoke('modelFieldNames', {modelName});
}
async guiBrowse(query) {
- await this.checkVersion();
- return await this.ankiInvoke('guiBrowse', {query});
+ if (!this._enabled) { return []; }
+ await this._checkVersion();
+ return await this._invoke('guiBrowse', {query});
}
async storeMediaFile(filename, dataBase64) {
- await this.checkVersion();
- return await this.ankiInvoke('storeMediaFile', {filename, data: dataBase64});
- }
-
- async checkVersion() {
- if (this.remoteVersion < this.localVersion) {
- this.remoteVersion = await this.ankiInvoke('version');
- if (this.remoteVersion < this.localVersion) {
- throw new Error('Extension and plugin versions incompatible');
- }
+ if (!this._enabled) {
+ throw new Error('AnkiConnect not enabled');
}
+ await this._checkVersion();
+ return await this._invoke('storeMediaFile', {filename, data: dataBase64});
}
async findNoteIds(notes) {
- await this.checkVersion();
+ if (!this._enabled) { return []; }
+ await this._checkVersion();
const actions = notes.map((note) => ({
action: 'findNotes',
params: {
- query: `deck:"${AnkiConnect.escapeQuery(note.deckName)}" ${AnkiConnect.fieldsToQuery(note.fields)}`
+ query: `deck:"${this._escapeQuery(note.deckName)}" ${this._fieldsToQuery(note.fields)}`
}
}));
- return await this.ankiInvoke('multi', {actions});
+ return await this._invoke('multi', {actions});
}
- ankiInvoke(action, params) {
- return requestJson(this.server, 'POST', {action, params, version: this.localVersion});
+ // Private
+
+ async _checkVersion() {
+ if (this._remoteVersion < this._localVersion) {
+ this._remoteVersion = await this._invoke('version');
+ if (this._remoteVersion < this._localVersion) {
+ throw new Error('Extension and plugin versions incompatible');
+ }
+ }
+ }
+
+ async _invoke(action, params) {
+ const result = await requestJson(this._server, 'POST', {action, params, version: this._localVersion});
+ if (isObject(result)) {
+ const error = result.error;
+ if (typeof error !== 'undefined') {
+ throw new Error(`AnkiConnect error: ${error}`);
+ }
+ }
+ return result;
}
- static escapeQuery(text) {
+ _escapeQuery(text) {
return text.replace(/"/g, '');
}
- static fieldsToQuery(fields) {
+ _fieldsToQuery(fields) {
const fieldNames = Object.keys(fields);
if (fieldNames.length === 0) {
return '';
}
const key = fieldNames[0];
- return `${key.toLowerCase()}:"${AnkiConnect.escapeQuery(fields[key])}"`;
- }
-}
-
-
-/*
- * AnkiNull
- */
-
-class AnkiNull {
- async addNote() {
- return null;
- }
-
- async canAddNotes() {
- return [];
- }
-
- async getDeckNames() {
- return [];
- }
-
- async getModelNames() {
- return [];
- }
-
- async getModelFieldNames() {
- return [];
- }
-
- async guiBrowse() {
- return [];
- }
-
- async findNoteIds() {
- return [];
+ return `${key.toLowerCase()}:"${this._escapeQuery(fields[key])}"`;
}
}
diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js
index 04a3b932..a1b788df 100644
--- a/ext/bg/js/backend.js
+++ b/ext/bg/js/backend.js
@@ -18,7 +18,6 @@
/* global
* AnkiConnect
* AnkiNoteBuilder
- * AnkiNull
* AudioSystem
* AudioUriBuilder
* BackendApiForwarder
@@ -46,7 +45,7 @@ class Backend {
this.database = new Database();
this.dictionaryImporter = new DictionaryImporter();
this.translator = new Translator(this.database);
- this.anki = new AnkiNull();
+ this.anki = new AnkiConnect();
this.mecab = new Mecab();
this.clipboardMonitor = new ClipboardMonitor({getClipboard: this._onApiClipboardGet.bind(this)});
this.options = null;
@@ -55,6 +54,7 @@ class Backend {
this.audioSystem = new AudioSystem({getAudioUri: this._getAudioUri.bind(this)});
this.audioUriBuilder = new AudioUriBuilder();
this.ankiNoteBuilder = new AnkiNoteBuilder({
+ anki: this.anki,
audioSystem: this.audioSystem,
renderTemplate: this._renderTemplate.bind(this)
});
@@ -214,7 +214,8 @@ class Backend {
this.setExtensionBadgeText('');
}
- this.anki = options.anki.enable ? new AnkiConnect(options.anki.server) : new AnkiNull();
+ this.anki.setServer(options.anki.server);
+ this.anki.setEnabled(options.anki.enable);
if (options.parsing.enableMecabParser) {
this.mecab.startListener();
@@ -505,8 +506,7 @@ class Backend {
await this.ankiNoteBuilder.injectScreenshot(
definition,
options.anki.terms.fields,
- details.screenshot,
- this.anki
+ details.screenshot
);
}