aboutsummaryrefslogtreecommitdiff
path: root/ext/bg/js/backend.js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2020-04-18 14:12:41 -0400
committerGitHub <noreply@github.com>2020-04-18 14:12:41 -0400
commit106172e2c1261f2d685802280af580c70ac4580b (patch)
tree1e96325ee884db8c946e7401882fd7b46f3552e8 /ext/bg/js/backend.js
parent9e7750125c40d1c4121e8d015957606721bede7c (diff)
parentcdd817a0e1c573b24114836a389236f49dd9b279 (diff)
Merge pull request #447 from toasted-nutbread/util-conversions
Util conversions
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 2265c1a9..04a3b932 100644
--- a/ext/bg/js/backend.js
+++ b/ext/bg/js/backend.js
@@ -100,7 +100,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([
@@ -722,6 +728,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) {
@@ -818,6 +854,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)) {