From dab46eda3e42e5f0a624bb285a555b078b999a99 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sun, 13 Nov 2016 11:58:23 -0800 Subject: WIP --- ext/bg/js/import.js | 36 ---------------------------------- ext/bg/js/translator.js | 52 +++++++++---------------------------------------- ext/bg/js/yomichan.js | 17 +--------------- 3 files changed, 10 insertions(+), 95 deletions(-) delete mode 100644 ext/bg/js/import.js (limited to 'ext/bg/js') diff --git a/ext/bg/js/import.js b/ext/bg/js/import.js deleted file mode 100644 index 0601cb9f..00000000 --- a/ext/bg/js/import.js +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright (C) 2016 Alex Yatskov - * Author: Alex Yatskov - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - - -function api_setProgress(progress) { - $('.progress-bar').css('width', `${progress}%`); - - if (progress === 100.0) { - $('.progress').hide(); - $('.alert').show(); - } -} - -chrome.runtime.onMessage.addListener(({action, params}, sender, callback) => { - const method = this['api_' + action]; - if (typeof(method) === 'function') { - method.call(this, params); - } - - callback(); -}); diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js index ed8c720b..01213bed 100644 --- a/ext/bg/js/translator.js +++ b/ext/bg/js/translator.js @@ -25,54 +25,20 @@ class Translator { this.deinflector = new Deinflector(); } - loadData(callback) { + prepare() { if (this.loaded) { return Promise.resolve(); } - return loadJsonInt('bg/data/rules.json').then(rules => { - this.deinflector.setRules(rules); - return loadJsonInt('bg/data/tags.json'); - }).then(tagMeta => { - this.tagMeta = tagMeta; - return this.database.prepare(); - }).then(exists => { - if (exists) { - return; - } - - if (callback) { - callback({state: 'begin', progress: 0}); - } - - const banks = {}; - const bankCallback = (total, loaded, indexUrl) => { - banks[indexUrl] = {loaded, total}; - - let percent = 0.0; - for (const url in banks) { - percent += banks[url].loaded / banks[url].total; - } + const promises = [ + loadJsonInt('bg/data/rules.json'), + loadJsonInt('bg/data/tags.json'), + this.database.prepare() + ]; - percent /= 3.0; - - if (callback) { - callback({state: 'update', progress: Math.ceil(100.0 * percent)}); - } - }; - - return Promise.all([ - this.database.importDictionary(chrome.extension.getURL('bg/data/edict/index.json'), bankCallback), - this.database.importDictionary(chrome.extension.getURL('bg/data/enamdict/index.json'), bankCallback), - this.database.importDictionary(chrome.extension.getURL('bg/data/kanjidic/index.json'), bankCallback), - ]).then(() => { - return this.database.seal(); - }).then(() => { - if (callback) { - callback({state: 'end', progress: 100.0}); - } - }); - }).then(() => { + return Promise.all(promises).then(([rules, tags]) => { + this.deinflector.setRules(rules); + this.tagMeta = tags; this.loaded = true; }); } diff --git a/ext/bg/js/yomichan.js b/ext/bg/js/yomichan.js index d471add4..62b6c7b1 100644 --- a/ext/bg/js/yomichan.js +++ b/ext/bg/js/yomichan.js @@ -25,7 +25,6 @@ class Yomichan { this.translator = new Translator(); this.anki = new AnkiNull(); this.options = null; - this.importTabId = null; this.setState('disabled'); chrome.runtime.onMessage.addListener(this.onMessage.bind(this)); @@ -39,20 +38,6 @@ class Yomichan { }); } - 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; - } - } - onMessage(request, sender, callback) { const {action, params} = request, method = this['api_' + action]; @@ -91,7 +76,7 @@ class Yomichan { break; case 'loading': chrome.browserAction.setBadgeText({text: '...'}); - this.translator.loadData(this.onImport.bind(this)).then(() => this.setState('enabled')); + this.translator.prepare().then(this.setState('enabled')); break; } -- cgit v1.2.3