From 50e0fbbb662230a3a9f6e7354c229200bd1a03a2 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Thu, 28 Nov 2019 15:18:27 -0500 Subject: Use schema to validate options --- ext/bg/js/backend.js | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'ext/bg/js') diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index 1a874dc8..55841cd6 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -23,6 +23,7 @@ class Backend { this.anki = new AnkiNull(); this.mecab = new Mecab(); this.options = null; + this.optionsSchema = null; this.optionsContext = { depth: 0, url: window.location.href @@ -38,7 +39,16 @@ class Backend { async prepare() { await this.translator.prepare(); + + this.optionsSchema = await requestJson(chrome.runtime.getURL('/bg/data/options-schema.json'), 'GET'); this.options = await optionsLoad(); + try { + this.options = JsonSchema.getValidValueOrDefault(this.optionsSchema, this.options); + } catch (e) { + // This shouldn't happen, but catch errors just in case of bugs + logError(e); + } + this.onOptionsUpdated('background'); if (chrome.commands !== null && typeof chrome.commands === 'object') { -- cgit v1.2.3