summaryrefslogtreecommitdiff
path: root/ext/bg/js/util.js
diff options
context:
space:
mode:
authorAlex Yatskov <alex@foosoft.net>2017-06-26 20:23:53 -0700
committerAlex Yatskov <alex@foosoft.net>2017-06-26 20:23:53 -0700
commitc23298dbb2f3f26ff96da80f69d70051cc14f733 (patch)
treeda544b095a4665655013bccd115b9efad5c8a7e1 /ext/bg/js/util.js
parent16baf9a400ac11a1c4f53ba6665db03a900511e0 (diff)
parent21b29ff135269fe33f5ae064abc74cfde6f2a5f5 (diff)
Merge branch 'master' into firefox-amo
Diffstat (limited to 'ext/bg/js/util.js')
-rw-r--r--ext/bg/js/util.js84
1 files changed, 47 insertions, 37 deletions
diff --git a/ext/bg/js/util.js b/ext/bg/js/util.js
index 75833871..cdd5ec31 100644
--- a/ext/bg/js/util.js
+++ b/ext/bg/js/util.js
@@ -451,14 +451,23 @@ function jsonLoadInt(url) {
return jsonLoad(chrome.extension.getURL(url));
}
-function jsonLoadDb(indexUrl, indexLoaded, termsLoaded, kanjiLoaded) {
- const indexDir = indexUrl.slice(0, indexUrl.lastIndexOf('/'));
- return jsonLoad(indexUrl).then(index => {
- if (!index.title || !index.version || !index.revision) {
- return Promise.reject('unrecognized dictionary format');
+/*
+ * Zip
+ */
+
+function zipLoadDb(archive, indexLoaded, termsLoaded, kanjiLoaded) {
+ return JSZip.loadAsync(archive).then(files => files.files).then(files => {
+ const indexFile = files['index.json'];
+ if (!indexFile) {
+ return Promise.reject('no dictionary index found in archive');
}
- if (indexLoaded !== null) {
+ return indexFile.async('string').then(indexJson => {
+ const index = JSON.parse(indexJson);
+ if (!index.title || !index.version || !index.revision) {
+ return Promise.reject('unrecognized dictionary format');
+ }
+
return indexLoaded(
index.title,
index.version,
@@ -467,44 +476,45 @@ function jsonLoadDb(indexUrl, indexLoaded, termsLoaded, kanjiLoaded) {
index.termBanks > 0,
index.kanjiBanks > 0
).then(() => index);
- }
+ }).then(index => {
+ const loaders = [];
+ const banksTotal = index.termBanks + index.kanjiBanks;
+ let banksLoaded = 0;
+
+ for (let i = 1; i <= index.termBanks; ++i) {
+ const bankFile = files[`term_bank_${i}.json`];
+ if (!bankFile) {
+ return Promise.reject('missing term bank file');
+ }
+
+ loaders.push(() => bankFile.async('string').then(bankJson => {
+ const bank = JSON.parse(bankJson);
+ return termsLoaded(index.title, bank, banksTotal, banksLoaded++);
+ }));
+ }
- return index;
- }).then(index => {
- const loaders = [];
- const banksTotal = index.termBanks + index.kanjiBanks;
- let banksLoaded = 0;
+ for (let i = 1; i <= index.kanjiBanks; ++i) {
+ const bankFile = files[`kanji_bank_${i}.json`];
+ if (!bankFile) {
+ return Promise.reject('missing kanji bank file');
+ }
- for (let i = 1; i <= index.termBanks; ++i) {
- const bankUrl = `${indexDir}/term_bank_${i}.json`;
- loaders.push(() => jsonLoad(bankUrl).then(entries => termsLoaded(
- index.title,
- entries,
- banksTotal,
- banksLoaded++
- )));
- }
-
- for (let i = 1; i <= index.kanjiBanks; ++i) {
- const bankUrl = `${indexDir}/kanji_bank_${i}.json`;
- loaders.push(() => jsonLoad(bankUrl).then(entries => kanjiLoaded(
- index.title,
- entries,
- banksTotal,
- banksLoaded++
- )));
- }
+ loaders.push(() => bankFile.async('string').then(bankJson => {
+ const bank = JSON.parse(bankJson);
+ return kanjiLoaded(index.title, bank, banksTotal, banksLoaded++);
+ }));
+ }
- let chain = Promise.resolve();
- for (const loader of loaders) {
- chain = chain.then(loader);
- }
+ let chain = Promise.resolve();
+ for (const loader of loaders) {
+ chain = chain.then(loader);
+ }
- return chain;
+ return chain;
+ });
});
}
-
/*
* Helpers
*/