summaryrefslogtreecommitdiff
path: root/ext/bg/js
diff options
context:
space:
mode:
authorAlex Yatskov <alex@foosoft.net>2016-11-13 11:58:23 -0800
committerAlex Yatskov <alex@foosoft.net>2016-11-13 11:58:23 -0800
commitdab46eda3e42e5f0a624bb285a555b078b999a99 (patch)
treede2729f5370cdc5ed0af544fe743fb9ea6b2e3c4 /ext/bg/js
parent7598de2234393433e98c9af3c312a1bcfd82c32f (diff)
WIP
Diffstat (limited to 'ext/bg/js')
-rw-r--r--ext/bg/js/import.js36
-rw-r--r--ext/bg/js/translator.js52
-rw-r--r--ext/bg/js/yomichan.js17
3 files changed, 10 insertions, 95 deletions
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 <alex@foosoft.net>
- * Author: Alex Yatskov <alex@foosoft.net>
- *
- * 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 <http://www.gnu.org/licenses/>.
- */
-
-
-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;
}