diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2019-09-07 14:21:26 -0400 |
---|---|---|
committer | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2019-09-10 20:09:33 -0400 |
commit | 1b2a1e50ebcd62cf54b397516e991333afa5158c (patch) | |
tree | 2dd31c4e907eb18125b4178c0e8b74c8be00de2f /ext/bg | |
parent | bc8793eb56b2ce985f2e5dc0a9fd270f98fbf17a (diff) |
Add getOptions function to backend
Diffstat (limited to 'ext/bg')
-rw-r--r-- | ext/bg/js/api.js | 2 | ||||
-rw-r--r-- | ext/bg/js/backend.js | 41 |
2 files changed, 27 insertions, 16 deletions
diff --git a/ext/bg/js/api.js b/ext/bg/js/api.js index 0b80f099..45dc36e7 100644 --- a/ext/bg/js/api.js +++ b/ext/bg/js/api.js @@ -18,7 +18,7 @@ function apiOptionsGetSync(optionsContext) { - return utilBackend().options; + return utilBackend().getOptions(optionsContext); } async function apiOptionsGet(optionsContext) { diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index 6afa9617..59de5a50 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -22,6 +22,9 @@ class Backend { this.translator = new Translator(); this.anki = new AnkiNull(); this.options = null; + this.optionsContext = { + depth: 0 + }; this.apiForwarder = new BackendApiForwarder(); } @@ -35,26 +38,15 @@ class Backend { } chrome.runtime.onMessage.addListener(this.onMessage.bind(this)); - if (this.options.general.showGuide) { + const options = this.getOptions(this.optionsContext); + if (options.general.showGuide) { chrome.tabs.create({url: chrome.extension.getURL('/bg/guide.html')}); } } onOptionsUpdated(options) { - options = utilIsolate(options); - this.options = options; - - if (!options.general.enable) { - this.setExtensionBadgeBackgroundColor('#555555'); - this.setExtensionBadgeText('off'); - } else if (!dictConfigured(options)) { - this.setExtensionBadgeBackgroundColor('#f0ad4e'); - this.setExtensionBadgeText('!'); - } else { - this.setExtensionBadgeText(''); - } - - this.anki = options.anki.enable ? new AnkiConnect(options.anki.server) : new AnkiNull(); + this.options = utilIsolate(options); + this.applyOptions(); const callback = () => this.checkLastError(chrome.runtime.lastError); chrome.tabs.query({}, tabs => { @@ -136,6 +128,25 @@ class Backend { return true; } + applyOptions() { + const options = this.getOptions(this.optionsContext); + if (!options.general.enable) { + this.setExtensionBadgeBackgroundColor('#555555'); + this.setExtensionBadgeText('off'); + } else if (!dictConfigured(options)) { + this.setExtensionBadgeBackgroundColor('#f0ad4e'); + this.setExtensionBadgeText('!'); + } else { + this.setExtensionBadgeText(''); + } + + this.anki = options.anki.enable ? new AnkiConnect(options.anki.server) : new AnkiNull(); + } + + getOptions(optionsContext) { + return this.options; + } + setExtensionBadgeBackgroundColor(color) { if (typeof chrome.browserAction.setBadgeBackgroundColor === 'function') { chrome.browserAction.setBadgeBackgroundColor({color}); |