Add getOptions function to backend

This commit is contained in:
toasted-nutbread 2019-09-07 14:21:26 -04:00
parent bc8793eb56
commit 1b2a1e50eb
2 changed files with 27 additions and 16 deletions

View File

@ -18,7 +18,7 @@
function apiOptionsGetSync(optionsContext) {
return utilBackend().options;
return utilBackend().getOptions(optionsContext);
}
async function apiOptionsGet(optionsContext) {

View File

@ -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});