diff options
Diffstat (limited to 'ext/bg/js')
-rw-r--r-- | ext/bg/js/display-window.js | 4 | ||||
-rw-r--r-- | ext/bg/js/options.js | 10 | ||||
-rw-r--r-- | ext/bg/js/util.js | 35 | ||||
-rw-r--r-- | ext/bg/js/yomichan.js | 28 |
4 files changed, 45 insertions, 32 deletions
diff --git a/ext/bg/js/display-window.js b/ext/bg/js/display-window.js index 44d1f7a9..568c5526 100644 --- a/ext/bg/js/display-window.js +++ b/ext/bg/js/display-window.js @@ -31,8 +31,8 @@ window.displayWindow = new class extends Display { return instYomi().definitionsAddable(definitions, modes); } - textRender(template, data) { - return instYomi().textRender(template, data); + templateRender(template, data) { + return instYomi().templateRender(template, data); } kanjiFind(character) { diff --git a/ext/bg/js/options.js b/ext/bg/js/options.js index f06cc056..84ecc6e3 100644 --- a/ext/bg/js/options.js +++ b/ext/bg/js/options.js @@ -187,10 +187,7 @@ function onDictionaryPurge(e) { return optionsLoad(); }).then(options => { options.dictionaries = {}; - return optionsSave(options).then(() => { - populateDictionaries(options); - instYomi().optionsSet(options); - }); + return optionsSave(options).then(populateDictionaries); }); } @@ -208,7 +205,7 @@ function onDictionaryImport() { optionsLoad().then(options => { instDb().importDictionary(dictUrl.val(), (total, current) => setProgress(current / total * 100.0)).then(summary => { options.dictionaries[summary.title] = {enabled: true, priority: 0}; - return optionsSave(options).then(() => instYomi().optionsSet(options)); + return optionsSave(options); }).then(() => populateDictionaries(options)).catch(showDictionaryError).then(() => { showDictionarySpinner(false); dictProgress.hide(); @@ -339,7 +336,7 @@ function onAnkiModelChanged(e) { optionsNew.anki[tabId].fields = {}; populateAnkiFields(element, optionsNew).then(() => { - optionsSave(optionsNew).then(() => instYomi().optionsSet(optionsNew)); + optionsSave(optionsNew); }).catch(showAnkiError).then(() => showAnkiSpinner(false)); }); } @@ -351,7 +348,6 @@ function onOptionsChanged(e) { getFormData().then(({optionsNew, optionsOld}) => { return optionsSave(optionsNew).then(() => { - instYomi().optionsSet(optionsNew); updateVisibility(optionsNew); const ankiUpdated = diff --git a/ext/bg/js/util.js b/ext/bg/js/util.js index c57f6ae1..d91415f7 100644 --- a/ext/bg/js/util.js +++ b/ext/bg/js/util.js @@ -23,7 +23,7 @@ function promiseCallback(promise, callback) { return promise.then(result => { - callback({result}); + callback({result}); }).catch(error => { /* eslint-disable */ console.log(error); @@ -50,6 +50,22 @@ function instAnki() { } +/* + * Foreground + */ + +function fgBroadcast(action, params) { + chrome.tabs.query({}, tabs => { + for (const tab of tabs) { + chrome.tabs.sendMessage(tab.id, {action, params}, () => null); + } + }); +} + +function fgOptionsSet(options) { + fgBroadcast('optionsSet', options); +} + /* * Options @@ -188,6 +204,9 @@ function optionsSave(options) { return new Promise((resolve, reject) => { chrome.storage.sync.set(options, resolve); + }).then(() => { + instYomi().optionsSet(options); + fgOptionsSet(options); }); } @@ -468,7 +487,7 @@ function jsonLoadDb(indexUrl, indexLoaded, termsLoaded, kanjiLoaded) { * Helpers */ -function helperKanjiLinks(options) { +function handlebarsKanjiLinks(options) { const isKanji = c => { const code = c.charCodeAt(0); return code >= 0x4e00 && code < 0x9fb0 || code >= 0x3400 && code < 0x4dc0; @@ -486,6 +505,16 @@ function helperKanjiLinks(options) { return result; } -function helperMultiLine(options) { +function handlebarsMultiLine(options) { return options.fn(this).split('\n').join('<br>'); } + +function handlebarsRegister() { + Handlebars.partials = Handlebars.templates; + Handlebars.registerHelper('kanjiLinks', handlebarsKanjiLinks); + Handlebars.registerHelper('multiLine', handlebarsMultiLine); +} + +function handlebarsRender(template, data) { + return Handlebars.templates[template](data); +} diff --git a/ext/bg/js/yomichan.js b/ext/bg/js/yomichan.js index 37fe74ee..82a06146 100644 --- a/ext/bg/js/yomichan.js +++ b/ext/bg/js/yomichan.js @@ -19,9 +19,7 @@ window.yomichan = new class { constructor() { - Handlebars.partials = Handlebars.templates; - Handlebars.registerHelper('kanjiLinks', helperKanjiLinks); - Handlebars.registerHelper('multiLine', helperMultiLine); + handlebarsRegister(); this.translator = new Translator(); this.anki = new AnkiNull(); @@ -38,32 +36,22 @@ window.yomichan = new class { optionsSet(options) { this.options = options; - let usable = false; + let configured = false; for (const title in options.dictionaries) { if (options.dictionaries[title].enabled) { - usable = true; + configured = true; break; } } chrome.browserAction.setBadgeBackgroundColor({color: '#f0ad4e'}); - chrome.browserAction.setBadgeText({text: usable ? '' : '!'}); + chrome.browserAction.setBadgeText({text: configured ? '' : '!'}); if (options.anki.enable) { this.anki = new AnkiConnect(this.options.anki.server); } else { this.anki = new AnkiNull(); } - - this.tabInvokeAll('optionsSet', this.options); - } - - tabInvokeAll(action, params) { - chrome.tabs.query({}, tabs => { - for (const tab of tabs) { - chrome.tabs.sendMessage(tab.id, {action, params}, () => null); - } - }); } noteFormat(definition, mode) { @@ -155,8 +143,8 @@ window.yomichan = new class { }); } - textRender(template, data) { - return Promise.resolve(Handlebars.templates[template](data)); + templateRender(template, data) { + return Promise.resolve(handlebarsRender(template, data)); } onInstalled(details) { @@ -179,8 +167,8 @@ window.yomichan = new class { promiseCallback(this.termsFind(text), callback); } - api_textRender({template, data, callback}) { - promiseCallback(this.textRender(template, data), callback); + api_templateRender({template, data, callback}) { + promiseCallback(this.templateRender(template, data), callback); } api_definitionAdd({definition, mode, callback}) { |