summaryrefslogtreecommitdiff
path: root/ext
diff options
context:
space:
mode:
Diffstat (limited to 'ext')
-rw-r--r--ext/bg/js/dictionary.js117
-rw-r--r--ext/bg/js/translator.js31
2 files changed, 97 insertions, 51 deletions
diff --git a/ext/bg/js/dictionary.js b/ext/bg/js/dictionary.js
index a3793cd6..dd46064a 100644
--- a/ext/bg/js/dictionary.js
+++ b/ext/bg/js/dictionary.js
@@ -23,40 +23,26 @@ class Dictionary {
this.entities = null;
}
- loadDb() {
- this.db = null;
- this.entities = null;
-
- return new Dexie('dict').open().then((db) => {
- this.db = db;
- });
+ existsDb() {
+ return Dexie.exists('dict');
}
- resetDb() {
+ loadDb() {
this.db = null;
this.entities = null;
- return new Dexie('dict').delete().then(() => {
- return Promise.resolve(new Dexie('dict'));
- }).then((db) => {
- this.db = db;
- return this.db.version(1).stores({
- terms: '++id, e, r',
- entities: 'n',
- kanji: 'c'
- });
- });
+ return this.initDb().open();
}
- importTermDict(dict) {
- return this.db.terms.bulkAdd(dict.d).then(() => {
- this.entities = {};
- for (const name in dict.e) {
- this.entities[name] = dict.e[name];
- }
-
- return this.db.entities.bulkAdd(dict.e);
+ initDb() {
+ this.db = new Dexie('dict');
+ this.db.version(1).stores({
+ terms: '++id,expression,reading',
+ entities: '++id,name',
+ kanji: '++id,character'
});
+
+ return this.db;
}
importKanjiDict(dict) {
@@ -78,19 +64,19 @@ class Dictionary {
});
}
- findterm(term) {
+ findTerm(term) {
const results = [];
- return this.db.terms.where('e').equals(term).or('r').equals(term).each((row) => {
+ return this.db.terms.where('expression').equals(term).or('reading').equals(term).each((row) => {
results.push({
- expression: row.e,
- reading: row.r,
- tags: row.t.split(' '),
- glossary: row.g,
+ expression: row.expression,
+ reading: row.reading,
+ tags: row.tags.split(' '),
+ glossary: row.glossary,
entities: this.entities,
- id: results.id
+ id: row.id
});
}).then(() => {
- Promise.resolve(results);
+ return Promise.resolve(results);
});
}
@@ -106,4 +92,67 @@ class Dictionary {
});
});
}
+
+ // importTermDict(dict) {
+ // return this.db.terms.bulkAdd(dict.d).then(() => {
+ // this.entities = {};
+ // for (const name in dict.e) {
+ // this.entities[name] = dict.e[name];
+ // }
+
+ // return this.db.entities.bulkAdd(dict.e);
+ // });
+ // }
+
+ importTermDict(indexUrl) {
+ return Dictionary.loadJson(indexUrl).then((index) => {
+ const entities = [];
+ for (const [name, value] of index.ents) {
+ entities.push({name, value});
+ }
+
+ return this.db.entities.bulkAdd(entities).then(() => {
+ if (this.entities === null) {
+ this.entities = {};
+ }
+
+ for (const entity of entities) {
+ this.entities[entity.name] = entity.value;
+ }
+ }).then(() => {
+ const loaders = [];
+ const indexDir = indexUrl.slice(0, indexUrl.lastIndexOf('/'));
+
+ for (let i = 0; i < index.refs; ++i) {
+ const refUrl = `${indexDir}/ref_${i}.json`;
+ loaders.push(
+ Dictionary.loadJson(refUrl).then((refs) => {
+ const rows = [];
+ for (const [e, r, t, ...g] of refs) {
+ rows.push({
+ 'expression': e,
+ 'reading': r,
+ 'tags': t,
+ 'glossary': g
+ });
+ }
+
+ return this.db.terms.bulkAdd(rows);
+ })
+ );
+ }
+
+ return Promise.all(loaders);
+ });
+ });
+ }
+
+ static loadJson(url) {
+ return new Promise((resolve, reject) => {
+ const xhr = new XMLHttpRequest();
+ xhr.addEventListener('load', () => resolve(JSON.parse(xhr.responseText)));
+ xhr.open('GET', chrome.extension.getURL(url), true);
+ xhr.send();
+ });
+ }
}
diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js
index 75f91055..f9ac1d56 100644
--- a/ext/bg/js/translator.js
+++ b/ext/bg/js/translator.js
@@ -36,27 +36,24 @@ class Translator {
return Translator.loadData('bg/data/tags.json');
}).then((response) => {
this.tagMeta = JSON.parse(response);
- return this.dictionary.loadDb();
+ return this.dictionary.existsDb();
+ }).then((exists) => {
+ if (exists) {
+ return this.dictionary.loadDb();
+ } else {
+ this.dictionary.initDb();
+ return Promise.all([
+ this.dictionary.importTermDict('bg/data/edict/index.json'),
+ this.dictionary.importTermDict('bg/data/enamdict/index.json')
+ ]);
+ }
}).then(() => {
this.loaded = true;
callback();
- }).catch(() => {
- return this.dictionary.resetDb().then(() => {
- return Translator.loadData('bg/data/edict.json');
- }).then((response) => {
- return this.dictionary.importTermDict(JSON.parse(response));
- }).then(() => {
- return Translator.loadData('bg/data/enamdict.json');
- }).then((response) => {
- return this.dictionary.importTermDict(JSON.parse(response));
- }).then(() => {
- return Translator.loadData('bg/data/kanjidic.json');
- }).then((response) => {
- return this.dictionary.importKanjiDict(JSON.parse(response));
+
+ this.dictionary.findTerm('猫').then((result) => {
+ console.log(result);
});
- }).then(() => {
- this.loaded = true;
- callback();
});
}