aboutsummaryrefslogtreecommitdiff
path: root/ext/bg/js/backend.js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2020-04-11 15:21:43 -0400
committertoasted-nutbread <toasted-nutbread@users.noreply.github.com>2020-04-11 15:25:19 -0400
commit51e17b35e3a855c0db6c4be94a8cb416b14c8ad7 (patch)
treee59ccc1e8bf78c9db172af9cf1c3eac25f48b6bb /ext/bg/js/backend.js
parent3c48290cd83744983df2e708b892a8415bcf750f (diff)
Convert some util* functions into api* functions
Diffstat (limited to 'ext/bg/js/backend.js')
-rw-r--r--ext/bg/js/backend.js45
1 files changed, 44 insertions, 1 deletions
diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js
index be8ea322..bc687a24 100644
--- a/ext/bg/js/backend.js
+++ b/ext/bg/js/backend.js
@@ -102,7 +102,13 @@ class Backend {
['getQueryParserTemplatesHtml', {handler: this._onApiGetQueryParserTemplatesHtml.bind(this), async: true}],
['getZoom', {handler: this._onApiGetZoom.bind(this), async: true}],
['getMessageToken', {handler: this._onApiGetMessageToken.bind(this), async: false}],
- ['getDefaultAnkiFieldTemplates', {handler: this._onApiGetDefaultAnkiFieldTemplates.bind(this), async: false}]
+ ['getDefaultAnkiFieldTemplates', {handler: this._onApiGetDefaultAnkiFieldTemplates.bind(this), async: false}],
+ ['getAnkiDeckNames', {handler: this._onApiGetAnkiDeckNames.bind(this), async: true}],
+ ['getAnkiModelNames', {handler: this._onApiGetAnkiModelNames.bind(this), async: true}],
+ ['getAnkiModelFieldNames', {handler: this._onApiGetAnkiModelFieldNames.bind(this), async: true}],
+ ['getDictionaryInfo', {handler: this._onApiGetDictionaryInfo.bind(this), async: true}],
+ ['getDictionaryCounts', {handler: this._onApiGetDictionaryCounts.bind(this), async: true}],
+ ['purgeDatabase', {handler: this._onApiPurgeDatabase.bind(this), async: true}]
]);
this._commandHandlers = new Map([
@@ -704,6 +710,36 @@ class Backend {
return this.defaultAnkiFieldTemplates;
}
+ async _onApiGetAnkiDeckNames(params, sender) {
+ this._validatePrivilegedMessageSender(sender);
+ return await this.anki.getDeckNames();
+ }
+
+ async _onApiGetAnkiModelNames(params, sender) {
+ this._validatePrivilegedMessageSender(sender);
+ return await this.anki.getModelNames();
+ }
+
+ async _onApiGetAnkiModelFieldNames({modelName}, sender) {
+ this._validatePrivilegedMessageSender(sender);
+ return await this.anki.getModelFieldNames(modelName);
+ }
+
+ async _onApiGetDictionaryInfo(params, sender) {
+ this._validatePrivilegedMessageSender(sender);
+ return await this.translator.database.getDictionaryInfo();
+ }
+
+ async _onApiGetDictionaryCounts({dictionaryNames, getTotal}, sender) {
+ this._validatePrivilegedMessageSender(sender);
+ return await this.translator.database.getDictionaryCounts(dictionaryNames, getTotal);
+ }
+
+ async _onApiPurgeDatabase(params, sender) {
+ this._validatePrivilegedMessageSender(sender);
+ return await this.translator.purgeDatabase();
+ }
+
// Command handlers
async _onCommandSearch(params) {
@@ -800,6 +836,13 @@ class Backend {
// Utilities
+ _validatePrivilegedMessageSender(sender) {
+ const url = sender.url;
+ if (!(typeof url === 'string' && yomichan.isExtensionUrl(url))) {
+ throw new Error('Invalid message sender');
+ }
+ }
+
async _getAudioUri(definition, source, details) {
let optionsContext = (typeof details === 'object' && details !== null ? details.optionsContext : null);
if (!(typeof optionsContext === 'object' && optionsContext !== null)) {