diff options
Diffstat (limited to 'ext/bg/js/yomichan.js')
-rw-r--r-- | ext/bg/js/yomichan.js | 49 |
1 files changed, 23 insertions, 26 deletions
diff --git a/ext/bg/js/yomichan.js b/ext/bg/js/yomichan.js index fd9b84d3..f1b3ffc4 100644 --- a/ext/bg/js/yomichan.js +++ b/ext/bg/js/yomichan.js @@ -20,31 +20,20 @@ class Yomichan { constructor() { Handlebars.partials = Handlebars.templates; - Handlebars.registerHelper('kanjiLinks', function(options) { - let result = ''; - for (const c of options.fn(this)) { - if (Translator.isKanji(c)) { - result += Handlebars.templates['kanji-link.html']({kanji: c}).trim(); - } else { - result += c; - } - } - - return result; - }); + Handlebars.registerHelper('kanjiLinks', kanjiLinks); this.translator = new Translator(); + this.importTabId = null; this.asyncPools = {}; this.ankiConnectVer = 0; this.setState('disabled'); - chrome.runtime.onInstalled.addListener(this.onInstalled.bind(this)); chrome.runtime.onMessage.addListener(this.onMessage.bind(this)); chrome.browserAction.onClicked.addListener(this.onBrowserAction.bind(this)); - chrome.tabs.onCreated.addListener((tab) => this.onTabReady(tab.id)); + chrome.tabs.onCreated.addListener(tab => this.onTabReady(tab.id)); chrome.tabs.onUpdated.addListener(this.onTabReady.bind(this)); - loadOptions((opts) => { + loadOptions().then(opts => { this.setOptions(opts); if (this.options.activateOnStartup) { this.setState('loading'); @@ -52,9 +41,17 @@ class Yomichan { }); } - onInstalled(details) { - if (details.reason === 'install') { - chrome.tabs.create({url: chrome.extension.getURL('bg/guide.html')}); + onImport({state, progress}) { + if (state === 'begin') { + chrome.tabs.create({url: chrome.extension.getURL('bg/import.html')}, tab => this.importTabId = tab.id); + } + + if (this.importTabId !== null) { + this.tabInvoke(this.importTabId, 'setProgress', progress); + } + + if (state === 'end') { + this.importTabId = null; } } @@ -101,7 +98,7 @@ class Yomichan { break; case 'loading': chrome.browserAction.setBadgeText({text: '...'}); - this.translator.loadData({loadEnamDict: this.options.loadEnamDict}, () => this.setState('enabled')); + this.translator.loadData(this.onImport.bind(this)).then(() => this.setState('enabled')); break; } @@ -118,7 +115,7 @@ class Yomichan { } tabInvokeAll(action, params) { - chrome.tabs.query({}, (tabs) => { + chrome.tabs.query({}, tabs => { for (const tab of tabs) { this.tabInvoke(tab.id, action, params); } @@ -133,7 +130,7 @@ class Yomichan { if (this.ankiConnectVer === this.getApiVersion()) { this.ankiInvoke(action, params, pool, callback); } else { - this.api_getVersion({callback: (version) => { + this.api_getVersion({callback: version => { if (version === this.getApiVersion()) { this.ankiConnectVer = version; this.ankiInvoke(action, params, pool, callback); @@ -209,7 +206,7 @@ class Yomichan { break; case 'tags': if (definition.tags) { - value = definition.tags.map((t) => t.name); + value = definition.tags.map(t => t.name); } break; } @@ -244,7 +241,7 @@ class Yomichan { }; for (const name in fields) { - if (fields[name].indexOf('{audio}') !== -1) { + if (fields[name].includes('{audio}')) { audio.fields.push(name); } } @@ -274,7 +271,7 @@ class Yomichan { } } - this.ankiInvokeSafe('canAddNotes', {notes}, 'notes', (results) => { + this.ankiInvokeSafe('canAddNotes', {notes}, 'notes', results => { const states = []; if (results !== null) { @@ -293,11 +290,11 @@ class Yomichan { } api_findKanji({text, callback}) { - callback(this.translator.findKanji(text)); + this.translator.findKanji(text).then(result => callback(result)); } api_findTerm({text, callback}) { - callback(this.translator.findTerm(text)); + this.translator.findTerm(text).then(result => callback(result)); } api_getDeckNames({callback}) { |