From be87e916df3a2f26e32e3861ce3430aaaa2ea67e Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sun, 10 Sep 2017 21:49:37 -0700 Subject: wip on new database --- ext/bg/js/database.js | 174 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 103 insertions(+), 71 deletions(-) (limited to 'ext/bg/js/database.js') diff --git a/ext/bg/js/database.js b/ext/bg/js/database.js index e00cb7a3..c346da4e 100644 --- a/ext/bg/js/database.js +++ b/ext/bg/js/database.js @@ -24,33 +24,23 @@ class Database { this.tagCache = {}; } - async sanitize() { - try { - const db = new Dexie('dict'); - await db.open(); - db.close(); - if (db.verno !== this.version) { - await db.delete(); - } - } catch(e) { - // NOP - } - } - async prepare() { if (this.db) { throw 'database already initialized'; } - await this.sanitize(); - this.db = new Dexie('dict'); - this.db.version(this.version).stores({ - terms: '++id,dictionary,expression,reading', - kanji: '++,dictionary,character', - tagMeta: '++,dictionary', + this.db.version(2).stores({ + terms: '++id,dictionary,expression,reading', + kanji: '++,dictionary,character', + tagMeta: '++,dictionary', dictionaries: '++,title,version' }); + this.db.version(3).stores({ + termFreq: '++,dictionary,expression', + kanjiFreq: '++,dictionary,character', + tagMeta: '++,dictionary' + }); await this.db.open(); } @@ -154,35 +144,32 @@ class Database { return Promise.reject('database not initialized'); } - let summary = null; - const indexLoaded = async (title, version, revision, tagMeta, hasTerms, hasKanji) => { - summary = {title, version, revision, hasTerms, hasKanji}; - - const count = await this.db.dictionaries.where('title').equals(title).count(); - if (count > 0) { - throw `dictionary "${title}" is already imported`; - } + // const rows = []; + // for (const tag in tagMeta || {}) { + // const meta = tagMeta[tag]; + // const row = dictTagSanitize({ + // name: tag, + // category: meta.category, + // notes: meta.notes, + // order: meta.order, + // dictionary: title + // }); - await this.db.dictionaries.add({title, version, revision, hasTerms, hasKanji}); + // rows.push(row); + // } - const rows = []; - for (const tag in tagMeta || {}) { - const meta = tagMeta[tag]; - const row = dictTagSanitize({ - name: tag, - category: meta.category, - notes: meta.notes, - order: meta.order, - dictionary: title - }); + // await this.db.tagMeta.bulkAdd(rows); - rows.push(row); + const indexDataLoaded = async summary => { + const count = await this.db.dictionaries.where('title').equals(summary.title).count(); + if (count > 0) { + throw `dictionary "${title}" is already imported`; } - await this.db.tagMeta.bulkAdd(rows); + await this.db.dictionaries.add(summary); }; - const termsLoaded = async (title, entries, total, current) => { + const termDataLoaded = async (title, entries, total, current) => { if (callback) { callback(total, current); } @@ -203,7 +190,7 @@ class Database { await this.db.terms.bulkAdd(rows); }; - const kanjiLoaded = async (title, entries, total, current) => { + const kanjiDataLoaded = async (title, entries, total, current) => { if (callback) { callback(total, current); } @@ -223,11 +210,26 @@ class Database { await this.db.kanji.bulkAdd(rows); }; - await Database.importDictionaryZip(archive, indexLoaded, termsLoaded, kanjiLoaded); - return summary; + return await Database.importDictionaryZip( + archive, + indexDataLoaded, + termDataLoaded, + null, + kanjiDataLoaded, + null, + null + ); } - static async importDictionaryZip(archive, indexLoaded, termsLoaded, kanjiLoaded) { + static async importDictionaryZip( + archive, + indexDataLoaded, + termDataLoaded, + termFreqDataLoaded, + kanjiDataLoaded, + kanjiFreqDataLoaded, + tagDataLoaded + ) { const files = (await JSZip.loadAsync(archive)).files; const indexFile = files['index.json']; @@ -240,36 +242,66 @@ class Database { throw 'unrecognized dictionary format'; } - await indexLoaded( - index.title, - index.version, - index.revision, - index.tagMeta || {}, - index.termBanks > 0, - index.kanjiBanks > 0 - ); - - const banksTotal = index.termBanks + index.kanjiBanks; - let banksLoaded = 0; + const summary = {title: index.title, version: index.version, revision: index.revision}; + if (indexDataLoaded) { + await indexDataLoaded(summary); + } - for (let i = 1; i <= index.termBanks; ++i) { - const bankFile = files[`term_bank_${i}.json`]; - if (bankFile) { - const bank = JSON.parse(await bankFile.async('string')); - await termsLoaded(index.title, bank, banksTotal, banksLoaded++); - } else { - throw 'missing term bank file'; + if (tagDataLoaded && index.tagMeta) { + const tags = []; + for (const name of index.tagMeta) { + const tag = index.tagMeta; + tags.push([name, tag.category, tag.order, tag.notes]); } + + tagDataLoaded(tags); } - for (let i = 1; i <= index.kanjiBanks; ++i) { - const bankFile = files[`kanji_bank_${i}.json`]; - if (bankFile) { - const bank = JSON.parse(await bankFile.async('string')); - await kanjiLoaded(index.title, bank, banksTotal, banksLoaded++); - } else { - throw 'missing kanji bank file'; + const buildTermBankName = index => `term_bank_${index + 1}.json`; + const buildTermFreqBankName = index => `termfreq_bank_${index + 1}.json`; + const buildKanjiBankName = index => `kanji_bank_${index + 1}.json`; + const buildKanjiFreqBankName = index => `kanjifreq_bank_${index + 1}.json`; + const buildTagBankName = index => `tag_bank_${index + 1}.json`; + + const countBanks = namer => { + let count = 0; + while (files[namer(count)]) { + ++count; } - } + + return count; + }; + + const termBankCount = countBanks(buildTermBankName); + const kanjiBankCount = countBanks(buildTermBankName); + const termFreqBankCount = countBanks(buildTermBankName); + const kanjiFreqBankCount = countBanks(buildTermBankName); + const tagBankCount = countBanks(buildTermBankName); + + let bankLoadedCount = 0; + const bankTotalCount = + termBankCount + + hanjiBankCount + + termFreqBankCount + + kanjiFreqBankCount + + tagBankCount; + + const loadBank = async (namer, count, callback) => { + if (callback) { + for (let i = 0; i < count; ++i) { + const bankFile = namer(i); + const bank = JSON.parse(await bankFile.async('string')); + await callback(index.title, bank, bankTotalCount, bankLoadedCount++); + } + } + }; + + await loadBank(buildTermBankName, termBankCount, termDataLoaded); + await loadBank(buildTermFreqBankName, termFreqBankCount, termFreqDataLoaded); + await loadBank(buildKanjiBankName, kanjiBankCount, kanjiDataLoaded); + await loadBank(buildKanjiFreqBankName, kanjiFreqBankCount, kanjiFreqDataLoaded); + await loadBank(buildTagBankName, tagBankCount, tagDataLoaded); + + return summary; } } -- cgit v1.2.3 From af93d446de6548eb3753a5ca36c42d9c8c8a3730 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Tue, 12 Sep 2017 13:29:13 -0700 Subject: fix dictionary importing --- ext/bg/js/database.js | 73 +++++++++++++++++++++++++++------------------------ 1 file changed, 39 insertions(+), 34 deletions(-) (limited to 'ext/bg/js/database.js') diff --git a/ext/bg/js/database.js b/ext/bg/js/database.js index c346da4e..71179ed6 100644 --- a/ext/bg/js/database.js +++ b/ext/bg/js/database.js @@ -144,22 +144,6 @@ class Database { return Promise.reject('database not initialized'); } - // const rows = []; - // for (const tag in tagMeta || {}) { - // const meta = tagMeta[tag]; - // const row = dictTagSanitize({ - // name: tag, - // category: meta.category, - // notes: meta.notes, - // order: meta.order, - // dictionary: title - // }); - - // rows.push(row); - // } - - // await this.db.tagMeta.bulkAdd(rows); - const indexDataLoaded = async summary => { const count = await this.db.dictionaries.where('title').equals(summary.title).count(); if (count > 0) { @@ -210,6 +194,27 @@ class Database { await this.db.kanji.bulkAdd(rows); }; + const tagDataLoaded = async (title, entries, total, current) => { + if (callback) { + callback(total, current); + } + + const rows = []; + for (const [name, category, order, notes] of entries) { + const row = dictTagSanitize({ + name, + category, + order, + notes, + dictionary: title + }); + + rows.push(row); + } + + await this.db.tagMeta.bulkAdd(rows); + }; + return await Database.importDictionaryZip( archive, indexDataLoaded, @@ -217,7 +222,7 @@ class Database { null, kanjiDataLoaded, null, - null + tagDataLoaded ); } @@ -247,16 +252,6 @@ class Database { await indexDataLoaded(summary); } - if (tagDataLoaded && index.tagMeta) { - const tags = []; - for (const name of index.tagMeta) { - const tag = index.tagMeta; - tags.push([name, tag.category, tag.order, tag.notes]); - } - - tagDataLoaded(tags); - } - const buildTermBankName = index => `term_bank_${index + 1}.json`; const buildTermFreqBankName = index => `termfreq_bank_${index + 1}.json`; const buildKanjiBankName = index => `kanji_bank_${index + 1}.json`; @@ -273,23 +268,33 @@ class Database { }; const termBankCount = countBanks(buildTermBankName); - const kanjiBankCount = countBanks(buildTermBankName); - const termFreqBankCount = countBanks(buildTermBankName); - const kanjiFreqBankCount = countBanks(buildTermBankName); - const tagBankCount = countBanks(buildTermBankName); + const termFreqBankCount = countBanks(buildTermFreqBankName); + const kanjiBankCount = countBanks(buildKanjiBankName); + const kanjiFreqBankCount = countBanks(buildKanjiFreqBankName); + const tagBankCount = countBanks(buildTagBankName); let bankLoadedCount = 0; - const bankTotalCount = + let bankTotalCount = termBankCount + - hanjiBankCount + termFreqBankCount + + kanjiBankCount + kanjiFreqBankCount + tagBankCount; + if (tagDataLoaded && index.tagMeta) { + const bank = []; + for (const name in index.tagMeta) { + const tag = index.tagMeta[name]; + bank.push([name, tag.category, tag.order, tag.notes]); + } + + tagDataLoaded(index.title, bank, ++bankTotalCount, bankLoadedCount++); + } + const loadBank = async (namer, count, callback) => { if (callback) { for (let i = 0; i < count; ++i) { - const bankFile = namer(i); + const bankFile = files[namer(i)]; const bank = JSON.parse(await bankFile.async('string')); await callback(index.title, bank, bankTotalCount, bankLoadedCount++); } -- cgit v1.2.3 From 28364b97b0dcc72ecde43aad32f8c58561895382 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Tue, 12 Sep 2017 14:12:40 -0700 Subject: hopeful workaround to firefox crash --- ext/bg/js/database.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'ext/bg/js/database.js') diff --git a/ext/bg/js/database.js b/ext/bg/js/database.js index 71179ed6..4a4f5e82 100644 --- a/ext/bg/js/database.js +++ b/ext/bg/js/database.js @@ -171,7 +171,7 @@ class Database { }); } - await this.db.terms.bulkAdd(rows); + await this.db.terms.bulkAdd(utilIsolate(rows)); }; const kanjiDataLoaded = async (title, entries, total, current) => { @@ -191,7 +191,7 @@ class Database { }); } - await this.db.kanji.bulkAdd(rows); + await this.db.kanji.bulkAdd(utilIsolate(rows)); }; const tagDataLoaded = async (title, entries, total, current) => { @@ -212,7 +212,7 @@ class Database { rows.push(row); } - await this.db.tagMeta.bulkAdd(rows); + await this.db.tagMeta.bulkAdd(utilIsolate(rows)); }; return await Database.importDictionaryZip( -- cgit v1.2.3 From 3b29893072b89b75c7fd82b9138b09572ec6248c Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Tue, 12 Sep 2017 18:29:16 -0700 Subject: add frequency table support for terms --- ext/bg/js/database.js | 34 +++++++++++++++++++++++++++++++++- ext/bg/js/translator.js | 7 +++++++ 2 files changed, 40 insertions(+), 1 deletion(-) (limited to 'ext/bg/js/database.js') diff --git a/ext/bg/js/database.js b/ext/bg/js/database.js index 4a4f5e82..ea55416c 100644 --- a/ext/bg/js/database.js +++ b/ext/bg/js/database.js @@ -87,6 +87,21 @@ class Database { return results; } + async findTermFreq(term, titles) { + if (!this.db) { + throw 'database not initialized'; + } + + const results = []; + await this.db.termFreq.where('expression').equals(term).each(row => { + if (titles.includes(row.dictionary)) { + results.push({frequency: row.frequency, dictionary: row.dictionary}); + } + }); + + return results; + } + async findKanji(kanji, titles) { if (!this.db) { return Promise.reject('database not initialized'); @@ -174,6 +189,23 @@ class Database { await this.db.terms.bulkAdd(utilIsolate(rows)); }; + const termFreqDataLoaded = async (title, entries, total, current) => { + if (callback) { + callback(total, current); + } + + const rows = []; + for (const [expression, frequency] of entries) { + rows.push({ + expression, + frequency, + dictionary: title + }); + } + + await this.db.termFreq.bulkAdd(utilIsolate(rows)); + }; + const kanjiDataLoaded = async (title, entries, total, current) => { if (callback) { callback(total, current); @@ -219,7 +251,7 @@ class Database { archive, indexDataLoaded, termDataLoaded, - null, + termFreqDataLoaded, kanjiDataLoaded, null, tagDataLoaded diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js index 1be485c7..3b9a1128 100644 --- a/ext/bg/js/translator.js +++ b/ext/bg/js/translator.js @@ -62,7 +62,14 @@ class Translator { for (const definition of deinflection.definitions) { const tags = definition.tags.map(tag => dictTagBuild(tag, definition.tagMeta)); tags.push(dictTagBuildSource(definition.dictionary)); + + let frequencies = await this.database.findTermFreq(definition.expression, titles); + if (frequencies.length === 0) { + frequencies = await this.database.findTermFreq(definition.reading, titles); + } + definitions.push({ + frequencies, source: deinflection.source, reasons: deinflection.reasons, score: definition.score, -- cgit v1.2.3 From 79b99131f69ab778e4c8203caa0894e8accde436 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Tue, 12 Sep 2017 20:20:03 -0700 Subject: add frequency table support for kanji --- ext/bg/js/database.js | 42 +++++++++++++++++++++++++++++++++++++----- ext/bg/js/settings.js | 4 +++- ext/bg/js/translator.js | 1 + 3 files changed, 41 insertions(+), 6 deletions(-) (limited to 'ext/bg/js/database.js') diff --git a/ext/bg/js/database.js b/ext/bg/js/database.js index ea55416c..1760a70a 100644 --- a/ext/bg/js/database.js +++ b/ext/bg/js/database.js @@ -129,6 +129,21 @@ class Database { return results; } + async findKanjiFreq(kanji, titles) { + if (!this.db) { + throw 'database not initialized'; + } + + const results = []; + await this.db.kanjiFreq.where('character').equals(kanji).each(row => { + if (titles.includes(row.dictionary)) { + results.push({frequency: row.frequency, dictionary: row.dictionary}); + } + }); + + return results; + } + async cacheTagMeta(titles) { if (!this.db) { throw 'database not initialized'; @@ -186,7 +201,7 @@ class Database { }); } - await this.db.terms.bulkAdd(utilIsolate(rows)); + await this.db.terms.bulkAdd(rows); }; const termFreqDataLoaded = async (title, entries, total, current) => { @@ -203,7 +218,7 @@ class Database { }); } - await this.db.termFreq.bulkAdd(utilIsolate(rows)); + await this.db.termFreq.bulkAdd(rows); }; const kanjiDataLoaded = async (title, entries, total, current) => { @@ -223,7 +238,24 @@ class Database { }); } - await this.db.kanji.bulkAdd(utilIsolate(rows)); + await this.db.kanji.bulkAdd(rows); + }; + + const kanjiFreqDataLoaded = async (title, entries, total, current) => { + if (callback) { + callback(total, current); + } + + const rows = []; + for (const [character, frequency] of entries) { + rows.push({ + character, + frequency, + dictionary: title + }); + } + + await this.db.kanjiFreq.bulkAdd(rows); }; const tagDataLoaded = async (title, entries, total, current) => { @@ -244,7 +276,7 @@ class Database { rows.push(row); } - await this.db.tagMeta.bulkAdd(utilIsolate(rows)); + await this.db.tagMeta.bulkAdd(rows); }; return await Database.importDictionaryZip( @@ -253,7 +285,7 @@ class Database { termDataLoaded, termFreqDataLoaded, kanjiDataLoaded, - null, + kanjiFreqDataLoaded, tagDataLoaded ); } diff --git a/ext/bg/js/settings.js b/ext/bg/js/settings.js index 55b469d0..b5ac8c8b 100644 --- a/ext/bg/js/settings.js +++ b/ext/bg/js/settings.js @@ -82,7 +82,9 @@ function formUpdateVisibility(options) { const debug = $('#debug'); if (options.general.debugInfo) { - const text = JSON.stringify(options, null, 4); + const temp = utilIsolate(options); + temp.anki.fieldTemplates = '...'; + const text = JSON.stringify(temp, null, 4); debug.html(handlebarsEscape(text)); debug.show(); } else { diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js index 3b9a1128..1e79b6fc 100644 --- a/ext/bg/js/translator.js +++ b/ext/bg/js/translator.js @@ -127,6 +127,7 @@ class Translator { const tags = definition.tags.map(tag => dictTagBuild(tag, definition.tagMeta)); tags.push(dictTagBuildSource(definition.dictionary)); definition.tags = dictTagsSort(tags); + definition.frequencies = await this.database.findKanjiFreq(definition.character, titles); } return definitions; -- cgit v1.2.3 From 219eeb6e81aba136af109770974f42b703bdae60 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Wed, 13 Sep 2017 15:41:06 -0700 Subject: cleanup --- ext/bg/js/database.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) (limited to 'ext/bg/js/database.js') diff --git a/ext/bg/js/database.js b/ext/bg/js/database.js index 1760a70a..cd53f681 100644 --- a/ext/bg/js/database.js +++ b/ext/bg/js/database.js @@ -20,7 +20,6 @@ class Database { constructor() { this.db = null; - this.version = 2; this.tagCache = {}; } @@ -177,7 +176,7 @@ class Database { const indexDataLoaded = async summary => { const count = await this.db.dictionaries.where('title').equals(summary.title).count(); if (count > 0) { - throw `dictionary "${title}" is already imported`; + throw `dictionary "${summary.title}" is already imported`; } await this.db.dictionaries.add(summary); @@ -299,9 +298,9 @@ class Database { kanjiFreqDataLoaded, tagDataLoaded ) { - const files = (await JSZip.loadAsync(archive)).files; + const zip = await JSZip.loadAsync(archive); - const indexFile = files['index.json']; + const indexFile = zip.files['index.json']; if (!indexFile) { throw 'no dictionary index found in archive'; } @@ -324,7 +323,7 @@ class Database { const countBanks = namer => { let count = 0; - while (files[namer(count)]) { + while (zip.files[namer(count)]) { ++count; } @@ -358,7 +357,7 @@ class Database { const loadBank = async (namer, count, callback) => { if (callback) { for (let i = 0; i < count; ++i) { - const bankFile = files[namer(i)]; + const bankFile = zip.files[namer(i)]; const bank = JSON.parse(await bankFile.async('string')); await callback(index.title, bank, bankTotalCount, bankLoadedCount++); } -- cgit v1.2.3 From 13961e6a10554fdc43c5b1b66f28ea72d2fc21b6 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Wed, 13 Sep 2017 16:42:04 -0700 Subject: better tag handling --- ext/bg/js/database.js | 32 +++++++++----------------------- ext/bg/js/translator.js | 21 +++++++++++++++++++-- 2 files changed, 28 insertions(+), 25 deletions(-) (limited to 'ext/bg/js/database.js') diff --git a/ext/bg/js/database.js b/ext/bg/js/database.js index cd53f681..6ae36db2 100644 --- a/ext/bg/js/database.js +++ b/ext/bg/js/database.js @@ -20,7 +20,6 @@ class Database { constructor() { this.db = null; - this.tagCache = {}; } async prepare() { @@ -38,7 +37,7 @@ class Database { this.db.version(3).stores({ termFreq: '++,dictionary,expression', kanjiFreq: '++,dictionary,character', - tagMeta: '++,dictionary' + tagMeta: '++,dictionary,name' }); await this.db.open(); @@ -52,7 +51,6 @@ class Database { this.db.close(); await this.db.delete(); this.db = null; - this.tagCache = {}; await this.prepare(); } @@ -78,11 +76,6 @@ class Database { } }); - await this.cacheTagMeta(titles); - for (const result of results) { - result.tagMeta = this.tagCache[result.dictionary] || {}; - } - return results; } @@ -120,11 +113,6 @@ class Database { } }); - await this.cacheTagMeta(titles); - for (const result of results) { - result.tagMeta = this.tagCache[result.dictionary] || {}; - } - return results; } @@ -143,21 +131,19 @@ class Database { return results; } - async cacheTagMeta(titles) { + async findTag(name, titles) { if (!this.db) { throw 'database not initialized'; } - for (const title of titles) { - if (!this.tagCache[title]) { - const tagMeta = {}; - await this.db.tagMeta.where('dictionary').equals(title).each(row => { - tagMeta[row.name] = {category: row.category, notes: row.notes, order: row.order}; - }); - - this.tagCache[title] = tagMeta; + let result = null; + await this.db.tagMeta.where('name').equals(name).each(row => { + if (titles.includes(row.dictionary)) { + result = row; } - } + }); + + return result; } async getDictionaries() { diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js index a2322e36..99147618 100644 --- a/ext/bg/js/translator.js +++ b/ext/bg/js/translator.js @@ -60,7 +60,7 @@ class Translator { let definitions = []; for (const deinflection of deinflections) { for (const definition of deinflection.definitions) { - const tags = definition.tags.map(tag => dictTagBuild(tag, definition.tagMeta)); + const tags = await this.buildTags(definition.tags, titles); tags.push(dictTagBuildSource(definition.dictionary)); let frequencies = await this.database.findTermFreq(definition.expression, titles); @@ -124,12 +124,29 @@ class Translator { } for (const definition of definitions) { - const tags = definition.tags.map(tag => dictTagBuild(tag, definition.tagMeta)); + const tags = await this.buildTags(definition.tags, titles); tags.push(dictTagBuildSource(definition.dictionary)); + definition.tags = dictTagsSort(tags); definition.frequencies = await this.database.findKanjiFreq(definition.character, titles); } return definitions; } + + async buildTags(names, titles) { + const results = []; + for (const name of names) { + const meta = await this.database.findTag(name.split(':')[0], titles); + + const result = {name}; + for (const prop in meta || {}) { + result[prop] = meta[prop]; + } + + results.push(dictTagSanitize(result)); + } + + return results; + } } -- cgit v1.2.3 From 4d4b819d6c197df6e0f91ba170d0ee909dfcb5c9 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Wed, 13 Sep 2017 17:26:02 -0700 Subject: tag caching --- ext/bg/js/database.js | 22 +++++++++++++++------- ext/bg/js/dictionary.js | 10 ---------- ext/bg/js/translator.js | 8 ++++---- 3 files changed, 19 insertions(+), 21 deletions(-) (limited to 'ext/bg/js/database.js') diff --git a/ext/bg/js/database.js b/ext/bg/js/database.js index 6ae36db2..1b171b03 100644 --- a/ext/bg/js/database.js +++ b/ext/bg/js/database.js @@ -20,6 +20,7 @@ class Database { constructor() { this.db = null; + this.tagCache = {}; } async prepare() { @@ -51,6 +52,7 @@ class Database { this.db.close(); await this.db.delete(); this.db = null; + this.tagCache = {}; await this.prepare(); } @@ -131,17 +133,23 @@ class Database { return results; } - async findTag(name, titles) { + async findTag(name, title) { if (!this.db) { throw 'database not initialized'; } - let result = null; - await this.db.tagMeta.where('name').equals(name).each(row => { - if (titles.includes(row.dictionary)) { - result = row; - } - }); + this.tagCache[title] = this.tagCache[title] || {}; + + let result = this.tagCache[title][name]; + if (!result) { + await this.db.tagMeta.where('name').equals(name).each(row => { + if (title === row.dictionary) { + result = row; + } + }); + + this.tagCache[title][name] = result; + } return result; } diff --git a/ext/bg/js/dictionary.js b/ext/bg/js/dictionary.js index e749390f..1cd1a846 100644 --- a/ext/bg/js/dictionary.js +++ b/ext/bg/js/dictionary.js @@ -148,16 +148,6 @@ function dictTagBuildSource(name) { return dictTagSanitize({name, category: 'dictionary', order: 100}); } -function dictTagBuild(name, meta) { - const tag = {name}; - const symbol = name.split(':')[0]; - for (const prop in meta[symbol] || {}) { - tag[prop] = meta[symbol][prop]; - } - - return dictTagSanitize(tag); -} - function dictTagSanitize(tag) { tag.name = tag.name || 'untitled'; tag.category = tag.category || 'default'; diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js index 99147618..5de99e8e 100644 --- a/ext/bg/js/translator.js +++ b/ext/bg/js/translator.js @@ -60,7 +60,7 @@ class Translator { let definitions = []; for (const deinflection of deinflections) { for (const definition of deinflection.definitions) { - const tags = await this.buildTags(definition.tags, titles); + const tags = await this.buildTags(definition.tags, definition.dictionary); tags.push(dictTagBuildSource(definition.dictionary)); let frequencies = await this.database.findTermFreq(definition.expression, titles); @@ -124,7 +124,7 @@ class Translator { } for (const definition of definitions) { - const tags = await this.buildTags(definition.tags, titles); + const tags = await this.buildTags(definition.tags, definition.dictionary); tags.push(dictTagBuildSource(definition.dictionary)); definition.tags = dictTagsSort(tags); @@ -134,10 +134,10 @@ class Translator { return definitions; } - async buildTags(names, titles) { + async buildTags(names, title) { const results = []; for (const name of names) { - const meta = await this.database.findTag(name.split(':')[0], titles); + const meta = await this.database.findTag(name.split(':')[0], title); const result = {name}; for (const prop in meta || {}) { -- cgit v1.2.3 From 04f9a0f54316d123628168aa5a15e8e48907d783 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Wed, 13 Sep 2017 17:30:45 -0700 Subject: cleanup --- ext/bg/js/database.js | 2 +- ext/bg/js/settings.js | 2 +- ext/bg/js/util.js | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) (limited to 'ext/bg/js/database.js') diff --git a/ext/bg/js/database.js b/ext/bg/js/database.js index 1b171b03..630243b2 100644 --- a/ext/bg/js/database.js +++ b/ext/bg/js/database.js @@ -154,7 +154,7 @@ class Database { return result; } - async getDictionaries() { + async getTitles() { if (this.db) { return this.db.dictionaries.toArray(); } else { diff --git a/ext/bg/js/settings.js b/ext/bg/js/settings.js index b5ac8c8b..a2580c35 100644 --- a/ext/bg/js/settings.js +++ b/ext/bg/js/settings.js @@ -213,7 +213,7 @@ async function dictionaryGroupsPopulate(options) { const dictGroups = $('#dict-groups').empty(); const dictWarning = $('#dict-warning').hide(); - const dictRows = await utilDatabaseGetDictionaries(); + const dictRows = await utilDatabaseGetTitles(); if (dictRows.length === 0) { dictWarning.show(); } diff --git a/ext/bg/js/util.js b/ext/bg/js/util.js index a92fd0bc..f44582eb 100644 --- a/ext/bg/js/util.js +++ b/ext/bg/js/util.js @@ -42,8 +42,8 @@ function utilAnkiGetModelFieldNames(modelName) { return utilBackend().anki.getModelFieldNames(modelName); } -function utilDatabaseGetDictionaries() { - return utilBackend().translator.database.getDictionaries(); +function utilDatabaseGetTitles() { + return utilBackend().translator.database.getTitles(); } function utilDatabasePurge() { -- cgit v1.2.3 From 33d9d6ff573d72a8b1600b23b20faf2b0ca4419c Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Wed, 13 Sep 2017 18:03:55 -0700 Subject: cleanup --- ext/bg/js/database.js | 2 +- ext/bg/js/translator.js | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) (limited to 'ext/bg/js/database.js') diff --git a/ext/bg/js/database.js b/ext/bg/js/database.js index 630243b2..b56cf7e2 100644 --- a/ext/bg/js/database.js +++ b/ext/bg/js/database.js @@ -133,7 +133,7 @@ class Database { return results; } - async findTag(name, title) { + async findTagForTitle(name, title) { if (!this.db) { throw 'database not initialized'; } diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js index 5de99e8e..0ecae16b 100644 --- a/ext/bg/js/translator.js +++ b/ext/bg/js/translator.js @@ -137,11 +137,13 @@ class Translator { async buildTags(names, title) { const results = []; for (const name of names) { - const meta = await this.database.findTag(name.split(':')[0], title); + const meta = await this.database.findTagForTitle(name.split(':')[0], title); const result = {name}; for (const prop in meta || {}) { - result[prop] = meta[prop]; + if (prop !== 'name') { + result[prop] = meta[prop]; + } } results.push(dictTagSanitize(result)); -- cgit v1.2.3 From ba25fbfd1f6187e75bbf3aa2e36717c081f09e7c Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sun, 17 Sep 2017 12:56:34 -0700 Subject: backwards compat --- ext/bg/js/database.js | 114 +++++++++++++++++++++++++++++++++---------------- ext/bg/js/templates.js | 48 +++++++++++++++------ tmpl/kanji.html | 30 ++++++++++++- 3 files changed, 141 insertions(+), 51 deletions(-) (limited to 'ext/bg/js/database.js') diff --git a/ext/bg/js/database.js b/ext/bg/js/database.js index b56cf7e2..0acf974f 100644 --- a/ext/bg/js/database.js +++ b/ext/bg/js/database.js @@ -110,6 +110,8 @@ class Database { kunyomi: dictFieldSplit(row.kunyomi), tags: dictFieldSplit(row.tags), glossary: row.meanings, + indices: row.indices, + stats: row.stats, dictionary: row.dictionary }); } @@ -168,6 +170,10 @@ class Database { } const indexDataLoaded = async summary => { + if (summary.version > 2) { + throw 'unsupported dictionary version'; + } + const count = await this.db.dictionaries.where('title').equals(summary.title).count(); if (count > 0) { throw `dictionary "${summary.title}" is already imported`; @@ -176,28 +182,42 @@ class Database { await this.db.dictionaries.add(summary); }; - const termDataLoaded = async (title, entries, total, current) => { + const termDataLoaded = async (summary, entries, total, current) => { if (callback) { callback(total, current); } const rows = []; - for (const [expression, reading, tags, rules, score, ...glossary] of entries) { - rows.push({ - expression, - reading, - tags, - rules, - score, - glossary, - dictionary: title - }); + if (summary.version === 1) { + for (const [expression, reading, tags, rules, score, ...glossary] of entries) { + rows.push({ + expression, + reading, + tags, + rules, + score, + glossary, + dictionary: summary.title + }); + } + } else { + for (const [expression, reading, tags, rules, score, glossary] of entries) { + rows.push({ + expression, + reading, + tags, + rules, + score, + glossary, + dictionary: summary.title + }); + } } await this.db.terms.bulkAdd(rows); }; - const termFreqDataLoaded = async (title, entries, total, current) => { + const termFreqDataLoaded = async (summary, entries, total, current) => { if (callback) { callback(total, current); } @@ -207,34 +227,49 @@ class Database { rows.push({ expression, frequency, - dictionary: title + dictionary: summary.title }); } await this.db.termFreq.bulkAdd(rows); }; - const kanjiDataLoaded = async (title, entries, total, current) => { + const kanjiDataLoaded = async (summary, entries, total, current) => { if (callback) { callback(total, current); } const rows = []; - for (const [character, onyomi, kunyomi, tags, ...meanings] of entries) { - rows.push({ - character, - onyomi, - kunyomi, - tags, - meanings, - dictionary: title - }); + if (summary.version === 1) { + for (const [character, onyomi, kunyomi, tags, ...meanings] of entries) { + rows.push({ + character, + onyomi, + kunyomi, + tags, + meanings, + dictionary: summary.title + }); + } + } else { + for (const [character, onyomi, kunyomi, tags, meanings, indices, stats] of entries) { + rows.push({ + character, + onyomi, + kunyomi, + tags, + meanings, + indices, + stats, + dictionary: summary.title + }); + } } await this.db.kanji.bulkAdd(rows); }; - const kanjiFreqDataLoaded = async (title, entries, total, current) => { + const kanjiFreqDataLoaded = async (summary, entries, total, current) => { if (callback) { callback(total, current); } @@ -244,14 +279,14 @@ class Database { rows.push({ character, frequency, - dictionary: title + dictionary: summary.title }); } await this.db.kanjiFreq.bulkAdd(rows); }; - const tagDataLoaded = async (title, entries, total, current) => { + const tagDataLoaded = async (summary, entries, total, current) => { if (callback) { callback(total, current); } @@ -263,7 +298,7 @@ class Database { category, order, notes, - dictionary: title + dictionary: summary.title }); rows.push(row); @@ -300,11 +335,16 @@ class Database { } const index = JSON.parse(await indexFile.async('string')); - if (!index.title || !index.version || !index.revision) { + if (!index.title || !index.revision) { throw 'unrecognized dictionary format'; } - const summary = {title: index.title, version: index.version, revision: index.revision}; + const summary = { + title: index.title, + revision: index.revision, + version: index.format || index.version + }; + if (indexDataLoaded) { await indexDataLoaded(summary); } @@ -345,24 +385,24 @@ class Database { bank.push([name, tag.category, tag.order, tag.notes]); } - tagDataLoaded(index.title, bank, ++bankTotalCount, bankLoadedCount++); + tagDataLoaded(summary, bank, ++bankTotalCount, bankLoadedCount++); } - const loadBank = async (namer, count, callback) => { + const loadBank = async (summary, namer, count, callback) => { if (callback) { for (let i = 0; i < count; ++i) { const bankFile = zip.files[namer(i)]; const bank = JSON.parse(await bankFile.async('string')); - await callback(index.title, bank, bankTotalCount, bankLoadedCount++); + await callback(summary, bank, bankTotalCount, bankLoadedCount++); } } }; - await loadBank(buildTermBankName, termBankCount, termDataLoaded); - await loadBank(buildTermFreqBankName, termFreqBankCount, termFreqDataLoaded); - await loadBank(buildKanjiBankName, kanjiBankCount, kanjiDataLoaded); - await loadBank(buildKanjiFreqBankName, kanjiFreqBankCount, kanjiFreqDataLoaded); - await loadBank(buildTagBankName, tagBankCount, tagDataLoaded); + await loadBank(summary, buildTermBankName, termBankCount, termDataLoaded); + await loadBank(summary, buildTermFreqBankName, termFreqBankCount, termFreqDataLoaded); + await loadBank(summary, buildKanjiBankName, kanjiBankCount, kanjiDataLoaded); + await loadBank(summary, buildKanjiFreqBankName, kanjiFreqBankCount, kanjiFreqDataLoaded); + await loadBank(summary, buildTagBankName, tagBankCount, tagDataLoaded); return summary; } diff --git a/ext/bg/js/templates.js b/ext/bg/js/templates.js index a8fc87b6..0d546909 100644 --- a/ext/bg/js/templates.js +++ b/ext/bg/js/templates.js @@ -36,8 +36,12 @@ templates['kanji.html'] = template({"1":function(container,depth0,helpers,partia + ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.onyomi : depth0),{"name":"if","hash":{},"fn":container.program(11, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + " \n \n \n" + ((stack1 = helpers["if"].call(alias1,((stack1 = (depth0 != null ? depth0.glossary : depth0)) != null ? stack1["1"] : stack1),{"name":"if","hash":{},"fn":container.program(13, data, 0),"inverse":container.program(16, data, 0),"data":data})) != null ? stack1 : "") - + " \n \n \n \n\n" - + ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.debug : depth0),{"name":"if","hash":{},"fn":container.program(18, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + + " \n \n
\n" + + ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.stats : depth0),{"name":"if","hash":{},"fn":container.program(18, data, 0),"inverse":container.program(21, data, 0),"data":data})) != null ? stack1 : "") + + "
\n \n \n \n Dictionary Indices\n \n \n \n
\n" + + ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.indices : depth0),{"name":"if","hash":{},"fn":container.program(23, data, 0),"inverse":container.program(25, data, 0),"data":data})) != null ? stack1 : "") + + "
\n \n \n \n\n" + + ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.debug : depth0),{"name":"if","hash":{},"fn":container.program(27, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + "\n"; },"2":function(container,depth0,helpers,partials,data) { return " \n \n"; @@ -84,35 +88,55 @@ templates['kanji.html'] = template({"1":function(container,depth0,helpers,partia + container.escapeExpression(container.lambda(((stack1 = (depth0 != null ? depth0.glossary : depth0)) != null ? stack1["0"] : stack1), depth0)) + "\n"; },"18":function(container,depth0,helpers,partials,data) { + var stack1; + + return ((stack1 = helpers.each.call(depth0 != null ? depth0 : (container.nullContext || {}),(depth0 != null ? depth0.stats : depth0),{"name":"each","hash":{},"fn":container.program(19, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : ""); +},"19":function(container,depth0,helpers,partials,data) { + var helper, alias1=container.escapeExpression; + + return "
" + + alias1(((helper = (helper = helpers.key || (data && data.key)) != null ? helper : helpers.helperMissing),(typeof helper === "function" ? helper.call(depth0 != null ? depth0 : (container.nullContext || {}),{"name":"key","hash":{},"data":data}) : helper))) + + ": " + + alias1(container.lambda(depth0, depth0)) + + "
\n"; +},"21":function(container,depth0,helpers,partials,data) { + return " No statistical data found\n"; +},"23":function(container,depth0,helpers,partials,data) { + var stack1; + + return ((stack1 = helpers.each.call(depth0 != null ? depth0 : (container.nullContext || {}),(depth0 != null ? depth0.indices : depth0),{"name":"each","hash":{},"fn":container.program(19, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : ""); +},"25":function(container,depth0,helpers,partials,data) { + return " No index data found\n"; +},"27":function(container,depth0,helpers,partials,data) { var stack1, helper, options, buffer = "
";
-  stack1 = ((helper = (helper = helpers.dumpObject || (depth0 != null ? depth0.dumpObject : depth0)) != null ? helper : helpers.helperMissing),(options={"name":"dumpObject","hash":{},"fn":container.program(19, data, 0),"inverse":container.noop,"data":data}),(typeof helper === "function" ? helper.call(depth0 != null ? depth0 : (container.nullContext || {}),options) : helper));
+  stack1 = ((helper = (helper = helpers.dumpObject || (depth0 != null ? depth0.dumpObject : depth0)) != null ? helper : helpers.helperMissing),(options={"name":"dumpObject","hash":{},"fn":container.program(28, data, 0),"inverse":container.noop,"data":data}),(typeof helper === "function" ? helper.call(depth0 != null ? depth0 : (container.nullContext || {}),options) : helper));
   if (!helpers.dumpObject) { stack1 = helpers.blockHelperMissing.call(depth0,stack1,options)}
   if (stack1 != null) { buffer += stack1; }
   return buffer + "
\n"; -},"19":function(container,depth0,helpers,partials,data) { +},"28":function(container,depth0,helpers,partials,data) { var stack1; return ((stack1 = container.lambda(depth0, depth0)) != null ? stack1 : ""); -},"21":function(container,depth0,helpers,partials,data,blockParams,depths) { +},"30":function(container,depth0,helpers,partials,data,blockParams,depths) { var stack1; - return ((stack1 = helpers.each.call(depth0 != null ? depth0 : (container.nullContext || {}),(depth0 != null ? depth0.definitions : depth0),{"name":"each","hash":{},"fn":container.program(22, data, 0, blockParams, depths),"inverse":container.noop,"data":data})) != null ? stack1 : ""); -},"22":function(container,depth0,helpers,partials,data,blockParams,depths) { + return ((stack1 = helpers.each.call(depth0 != null ? depth0 : (container.nullContext || {}),(depth0 != null ? depth0.definitions : depth0),{"name":"each","hash":{},"fn":container.program(31, data, 0, blockParams, depths),"inverse":container.noop,"data":data})) != null ? stack1 : ""); +},"31":function(container,depth0,helpers,partials,data,blockParams,depths) { var stack1; - return ((stack1 = helpers.unless.call(depth0 != null ? depth0 : (container.nullContext || {}),(data && data.first),{"name":"unless","hash":{},"fn":container.program(23, data, 0, blockParams, depths),"inverse":container.noop,"data":data})) != null ? stack1 : "") + return ((stack1 = helpers.unless.call(depth0 != null ? depth0 : (container.nullContext || {}),(data && data.first),{"name":"unless","hash":{},"fn":container.program(32, data, 0, blockParams, depths),"inverse":container.noop,"data":data})) != null ? stack1 : "") + "\n" + ((stack1 = container.invokePartial(partials.kanji,depth0,{"name":"kanji","hash":{"root":(depths[1] != null ? depths[1].root : depths[1]),"source":(depths[1] != null ? depths[1].source : depths[1]),"addable":(depths[1] != null ? depths[1].addable : depths[1]),"debug":(depths[1] != null ? depths[1].debug : depths[1])},"data":data,"helpers":helpers,"partials":partials,"decorators":container.decorators})) != null ? stack1 : ""); -},"23":function(container,depth0,helpers,partials,data) { +},"32":function(container,depth0,helpers,partials,data) { return "
"; -},"25":function(container,depth0,helpers,partials,data) { - return "

No results found.

\n"; +},"34":function(container,depth0,helpers,partials,data) { + return "

No results found

\n"; },"compiler":[7,">= 4.0.0"],"main":function(container,depth0,helpers,partials,data,blockParams,depths) { var stack1; return "\n" - + ((stack1 = helpers["if"].call(depth0 != null ? depth0 : (container.nullContext || {}),(depth0 != null ? depth0.definitions : depth0),{"name":"if","hash":{},"fn":container.program(21, data, 0, blockParams, depths),"inverse":container.program(25, data, 0, blockParams, depths),"data":data})) != null ? stack1 : ""); + + ((stack1 = helpers["if"].call(depth0 != null ? depth0 : (container.nullContext || {}),(depth0 != null ? depth0.definitions : depth0),{"name":"if","hash":{},"fn":container.program(30, data, 0, blockParams, depths),"inverse":container.program(34, data, 0, blockParams, depths),"data":data})) != null ? stack1 : ""); },"main_d": function(fn, props, container, depth0, data, blockParams, depths) { var decorators = container.decorators; diff --git a/tmpl/kanji.html b/tmpl/kanji.html index 9ab5b2a8..6cea57f9 100644 --- a/tmpl/kanji.html +++ b/tmpl/kanji.html @@ -53,7 +53,33 @@ {{glossary.[0]}} {{/if}} - + +
+ {{#if stats}} + {{#each stats}} +
{{@key}}: {{.}}
+ {{/each}} + {{else}} + No statistical data found + {{/if}} +
+ + + + Dictionary Indices + + + +
+ {{#if indices}} + {{#each indices}} +
{{@key}}: {{.}}
+ {{/each}} + {{else}} + No index data found + {{/if}} +
+ @@ -69,5 +95,5 @@ {{> kanji debug=../debug addable=../addable source=../source root=../root}} {{/each}} {{else}} -

No results found.

+

No results found

{{/if}} -- cgit v1.2.3 From e90274519a57d43e77e864ed3259d1b6c73654be Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sun, 17 Sep 2017 19:57:39 -0700 Subject: combine indices and stats --- ext/bg/js/database.js | 4 +--- ext/bg/js/translator.js | 15 +++++++-------- 2 files changed, 8 insertions(+), 11 deletions(-) (limited to 'ext/bg/js/database.js') diff --git a/ext/bg/js/database.js b/ext/bg/js/database.js index 0acf974f..f94c572e 100644 --- a/ext/bg/js/database.js +++ b/ext/bg/js/database.js @@ -110,7 +110,6 @@ class Database { kunyomi: dictFieldSplit(row.kunyomi), tags: dictFieldSplit(row.tags), glossary: row.meanings, - indices: row.indices, stats: row.stats, dictionary: row.dictionary }); @@ -252,14 +251,13 @@ class Database { }); } } else { - for (const [character, onyomi, kunyomi, tags, meanings, indices, stats] of entries) { + for (const [character, onyomi, kunyomi, tags, meanings, stats] of entries) { rows.push({ character, onyomi, kunyomi, tags, meanings, - indices, stats, dictionary: summary.title }); diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js index ef2227ea..7583d39a 100644 --- a/ext/bg/js/translator.js +++ b/ext/bg/js/translator.js @@ -140,8 +140,7 @@ class Translator { tags.push(dictTagBuildSource(definition.dictionary)); definition.tags = dictTagsSort(tags); - definition.stats = await this.expandTaggedValues(definition.stats, definition.dictionary); - definition.indices = await this.expandTaggedValues(definition.indices, definition.dictionary); + definition.stats = await this.expandStats(definition.stats, definition.dictionary); definition.frequencies = await this.database.findKanjiFreq(definition.character, titles); } @@ -174,22 +173,22 @@ class Translator { return tags; } - async expandTaggedValues(items, title) { - const tags = []; + async expandStats(items, title) { + const stats = []; for (const name in items) { const base = name.split(':')[0]; const meta = await this.database.findTagForTitle(base, title); - const tag = {name, value: items[name]}; + const stat = {name, value: items[name]}; for (const prop in meta || {}) { if (prop !== 'name') { - tag[prop] = meta[prop]; + stat[prop] = meta[prop]; } } - tags.push(dictTagSanitize(tag)); + stats.push(dictTagSanitize(stat)); } - return tags; + return stats; } } -- cgit v1.2.3 From 8ba8397170c99e1d4416277e26d91ebb4b8cfed1 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Fri, 22 Sep 2017 19:39:05 -0700 Subject: update error handling --- ext/bg/js/anki.js | 2 +- ext/bg/js/audio.js | 4 ++-- ext/bg/js/database.js | 26 +++++++++++++------------- ext/bg/js/request.js | 4 ++-- ext/bg/js/settings.js | 8 ++++---- ext/bg/settings.html | 23 ++++++----------------- ext/mixed/js/display.js | 6 +++--- 7 files changed, 31 insertions(+), 42 deletions(-) (limited to 'ext/bg/js/database.js') diff --git a/ext/bg/js/anki.js b/ext/bg/js/anki.js index c327969f..183f37bc 100644 --- a/ext/bg/js/anki.js +++ b/ext/bg/js/anki.js @@ -62,7 +62,7 @@ class AnkiConnect { if (this.remoteVersion < this.localVersion) { this.remoteVersion = await this.ankiInvoke('version'); if (this.remoteVersion < this.localVersion) { - throw 'extension and plugin versions incompatible'; + throw 'Extension and plugin versions incompatible'; } } } diff --git a/ext/bg/js/audio.js b/ext/bg/js/audio.js index 0952887e..ce47490c 100644 --- a/ext/bg/js/audio.js +++ b/ext/bg/js/audio.js @@ -57,7 +57,7 @@ async function audioBuildUrl(definition, mode, cache={}) { const xhr = new XMLHttpRequest(); xhr.open('POST', 'https://www.japanesepod101.com/learningcenter/reference/dictionary_post'); xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); - xhr.addEventListener('error', () => reject('failed to scrape audio data')); + xhr.addEventListener('error', () => reject('Failed to scrape audio data')); xhr.addEventListener('load', () => { cache[definition.expression] = xhr.responseText; resolve(xhr.responseText); @@ -87,7 +87,7 @@ async function audioBuildUrl(definition, mode, cache={}) { } else { const xhr = new XMLHttpRequest(); xhr.open('GET', `http://jisho.org/search/${definition.expression}`); - xhr.addEventListener('error', () => reject('failed to scrape audio data')); + xhr.addEventListener('error', () => reject('Failed to scrape audio data')); xhr.addEventListener('load', () => { cache[definition.expression] = xhr.responseText; resolve(xhr.responseText); diff --git a/ext/bg/js/database.js b/ext/bg/js/database.js index f94c572e..e7316b3a 100644 --- a/ext/bg/js/database.js +++ b/ext/bg/js/database.js @@ -25,7 +25,7 @@ class Database { async prepare() { if (this.db) { - throw 'database already initialized'; + throw 'Database already initialized'; } this.db = new Dexie('dict'); @@ -46,7 +46,7 @@ class Database { async purge() { if (!this.db) { - throw 'database not initialized'; + throw 'Database not initialized'; } this.db.close(); @@ -59,7 +59,7 @@ class Database { async findTerms(term, titles) { if (!this.db) { - throw 'database not initialized'; + throw 'Database not initialized'; } const results = []; @@ -83,7 +83,7 @@ class Database { async findTermFreq(term, titles) { if (!this.db) { - throw 'database not initialized'; + throw 'Database not initialized'; } const results = []; @@ -98,7 +98,7 @@ class Database { async findKanji(kanji, titles) { if (!this.db) { - return Promise.reject('database not initialized'); + throw 'Database not initialized'; } const results = []; @@ -121,7 +121,7 @@ class Database { async findKanjiFreq(kanji, titles) { if (!this.db) { - throw 'database not initialized'; + throw 'Database not initialized'; } const results = []; @@ -136,7 +136,7 @@ class Database { async findTagForTitle(name, title) { if (!this.db) { - throw 'database not initialized'; + throw 'Database not initialized'; } this.tagCache[title] = this.tagCache[title] || {}; @@ -159,23 +159,23 @@ class Database { if (this.db) { return this.db.dictionaries.toArray(); } else { - throw 'database not initialized'; + throw 'Database not initialized'; } } async importDictionary(archive, callback) { if (!this.db) { - return Promise.reject('database not initialized'); + throw 'Database not initialized'; } const indexDataLoaded = async summary => { if (summary.version > 2) { - throw 'unsupported dictionary version'; + throw 'Unsupported dictionary version'; } const count = await this.db.dictionaries.where('title').equals(summary.title).count(); if (count > 0) { - throw `dictionary "${summary.title}" is already imported`; + throw 'Dictionary is already imported'; } await this.db.dictionaries.add(summary); @@ -329,12 +329,12 @@ class Database { const indexFile = zip.files['index.json']; if (!indexFile) { - throw 'no dictionary index found in archive'; + throw 'No dictionary index found in archive'; } const index = JSON.parse(await indexFile.async('string')); if (!index.title || !index.revision) { - throw 'unrecognized dictionary format'; + throw 'Unrecognized dictionary format'; } const summary = { diff --git a/ext/bg/js/request.js b/ext/bg/js/request.js index 94fd135a..e4359863 100644 --- a/ext/bg/js/request.js +++ b/ext/bg/js/request.js @@ -22,7 +22,7 @@ function requestJson(url, action, params) { const xhr = new XMLHttpRequest(); xhr.overrideMimeType('application/json'); xhr.addEventListener('load', () => resolve(xhr.responseText)); - xhr.addEventListener('error', () => reject('failed to execute network request')); + xhr.addEventListener('error', () => reject('Failed to connect')); xhr.open(action, url); if (params) { xhr.send(JSON.stringify(params)); @@ -34,7 +34,7 @@ function requestJson(url, action, params) { return JSON.parse(responseText); } catch (e) { - return Promise.reject('invalid JSON response'); + return Promise.reject('Invalid response'); } }); } diff --git a/ext/bg/js/settings.js b/ext/bg/js/settings.js index d73d7509..161e9abe 100644 --- a/ext/bg/js/settings.js +++ b/ext/bg/js/settings.js @@ -142,7 +142,7 @@ async function onReady() { $('#scan-length').val(options.scanning.length); $('#scan-modifier-key').val(options.scanning.modifier); - $('#dict-purge').click(utilAsync(onDictionaryPurge)); + $('#dict-purge-link').click(utilAsync(onDictionaryPurge)); $('#dict-file').change(utilAsync(onDictionaryImport)); $('#anki-enable').prop('checked', options.anki.enable); @@ -179,7 +179,7 @@ $(document).ready(utilAsync(onReady)); function dictionaryErrorShow(error) { const dialog = $('#dict-error'); if (error) { - dialog.show().find('span').text(error); + dialog.show().text(error); } else { dialog.hide(); } @@ -245,7 +245,7 @@ async function onDictionaryPurge(e) { e.preventDefault(); const dictControls = $('#dict-importer, #dict-groups').hide(); - const dictProgress = $('#dict-purge-progress').show(); + const dictProgress = $('#dict-purge').show(); try { dictionaryErrorShow(); @@ -314,7 +314,7 @@ function ankiSpinnerShow(show) { function ankiErrorShow(error) { const dialog = $('#anki-error'); if (error) { - dialog.show().find('span').text(error); + dialog.show().text(error); } else { dialog.hide(); diff --git a/ext/bg/settings.html b/ext/bg/settings.html index c2612967..4315d74b 100644 --- a/ext/bg/settings.html +++ b/ext/bg/settings.html @@ -7,7 +7,7 @@