From 05ac93128539cf4be5417a50728329c3ae070580 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sat, 10 Sep 2016 18:57:00 -0700 Subject: Work on deinflector --- ext/bg/js/deinflector.js | 52 ++++++++++++++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 22 deletions(-) (limited to 'ext/bg/js/deinflector.js') diff --git a/ext/bg/js/deinflector.js b/ext/bg/js/deinflector.js index 0eabd0f3..2d567e6b 100644 --- a/ext/bg/js/deinflector.js +++ b/ext/bg/js/deinflector.js @@ -26,26 +26,30 @@ class Deinflection { } validate(validator) { - for (const tags of validator(this.term)) { - if (this.tags.length === 0) { - return true; - } - - for (const tag of this.tags) { - if (tags.indexOf(tag) !== -1) { + return validator(this.term).then(tagSets => { + for (const tags of tagSets) { + if (this.tags.length === 0) { return true; } + + for (const tag of this.tags) { + if (tags.indexOf(tag) !== -1) { + return true; + } + } } - } - return false; + return false; + }); } deinflect(validator, rules) { - if (this.validate(validator)) { - const child = new Deinflection(this.term, this.tags); - this.children.push(child); - } + const promises = [ + this.validate(validator).then(valid => { + const child = new Deinflection(this.term, this.tags); + this.children.push(child); + }) + ]; for (const rule in rules) { for (const variant of rules[rule]) { @@ -63,13 +67,19 @@ class Deinflection { const term = this.term.slice(0, -variant.ki.length) + variant.ko; const child = new Deinflection(term, variant.to, rule); - if (child.deinflect(validator, rules)) { - this.children.push(child); - } + promises.push( + child.deinflect(validator, rules).then(valid => { + if (valid) { + this.children.push(child); + } + } + )); } } - return this.children.length > 0; + return Promise.all(promises).then(() => { + return this.children.length > 0; + }); } gather() { @@ -105,10 +115,8 @@ class Deinflector { deinflect(term, validator) { const node = new Deinflection(term); - if (node.deinflect(validator, this.rules)) { - return node.gather(); - } - - return null; + return node.deinflect(validator, this.rules).then(success => { + return success ? node.gather() : null; + }); } } -- cgit v1.2.3 From 058739988302513c8496e569cf3a4fd1eb5920b4 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sat, 10 Sep 2016 19:44:54 -0700 Subject: WIP --- ext/bg/js/deinflector.js | 2 +- ext/bg/js/translator.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'ext/bg/js/deinflector.js') diff --git a/ext/bg/js/deinflector.js b/ext/bg/js/deinflector.js index 2d567e6b..4cdc9a3d 100644 --- a/ext/bg/js/deinflector.js +++ b/ext/bg/js/deinflector.js @@ -116,7 +116,7 @@ class Deinflector { deinflect(term, validator) { const node = new Deinflection(term); return node.deinflect(validator, this.rules).then(success => { - return success ? node.gather() : null; + return success ? node.gather() : []; }); } } diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js index dcc2e5ac..ccf1876f 100644 --- a/ext/bg/js/translator.js +++ b/ext/bg/js/translator.js @@ -63,7 +63,7 @@ class Translator { this.deinflector.deinflect(text.slice(0, i), term => { return this.dictionary.findTerm(term).then(definitions => definitions.map(def => def.tags)); }).then(inflects => { - for (const inflect of inflects || []) { + for (const inflect of inflects) { this.processTerm(groups, df.source, df.tags, df.rules, df.root); } }) -- cgit v1.2.3 From d5ea03171ea997d6734e6d31197c7f233fff7084 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sun, 11 Sep 2016 12:29:18 -0700 Subject: Working with IndexDb --- ext/bg/js/deinflector.js | 12 +++++------- ext/bg/js/dictionary.js | 14 +++++++------- ext/bg/js/translator.js | 35 ++++++++++++++++++++++------------- ext/bg/js/yomichan.js | 18 +++++++++--------- 4 files changed, 43 insertions(+), 36 deletions(-) (limited to 'ext/bg/js/deinflector.js') diff --git a/ext/bg/js/deinflector.js b/ext/bg/js/deinflector.js index 4cdc9a3d..e5b1efe5 100644 --- a/ext/bg/js/deinflector.js +++ b/ext/bg/js/deinflector.js @@ -26,14 +26,14 @@ class Deinflection { } validate(validator) { - return validator(this.term).then(tagSets => { - for (const tags of tagSets) { + return validator(this.term).then(sets => { + for (const tags of sets) { if (this.tags.length === 0) { return true; } for (const tag of this.tags) { - if (tags.indexOf(tag) !== -1) { + if (tags.includes(tag)) { return true; } } @@ -55,7 +55,7 @@ class Deinflection { for (const variant of rules[rule]) { let allowed = this.tags.length === 0; for (const tag of this.tags) { - if (variant.ti.indexOf(tag) !== -1) { + if (variant.ti.includes(tag)) { allowed = true; break; } @@ -115,8 +115,6 @@ class Deinflector { deinflect(term, validator) { const node = new Deinflection(term); - return node.deinflect(validator, this.rules).then(success => { - return success ? node.gather() : []; - }); + return node.deinflect(validator, this.rules).then(success => success ? node.gather() : []); } } diff --git a/ext/bg/js/dictionary.js b/ext/bg/js/dictionary.js index 624fe15e..0c5e4c4a 100644 --- a/ext/bg/js/dictionary.js +++ b/ext/bg/js/dictionary.js @@ -59,15 +59,15 @@ class Dictionary { findKanji(kanji) { const results = []; - return this.db.kanji.where('c').equals(kanji).each(row => { + return this.db.kanji.where('character').equals(kanji).each(row => { results.push({ - character: row.c, - onyomi: row.o.split(' '), - kunyomi: row.k.split(' '), - tags: row.t.split(' '), - glossary: row.m + character: row.character, + onyomi: row.onyomi.split(' '), + kunyomi: row.kunyomi.split(' '), + tags: row.tags.split(' '), + glossary: row.meanings }); - }); + }).then(() => results); } getEntities(tags) { diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js index 9b7b4bf5..6b08f485 100644 --- a/ext/bg/js/translator.js +++ b/ext/bg/js/translator.js @@ -55,17 +55,23 @@ class Translator { } findTermGroups(text) { - const groups = {}; - + const deinflectGroups = {}; const deinflectPromises = []; + for (let i = text.length; i > 0; --i) { deinflectPromises.push( this.deinflector.deinflect(text.slice(0, i), term => { return this.dictionary.findTerm(term).then(definitions => definitions.map(definition => definition.tags)); - }).then(inflects => { + }).then(deinflects => { const processPromises = []; - for (const inflect of inflects) { - processPromises.push(this.processTerm(groups, inflect.source, inflect.tags, inflect.rules, inflect.root)); + for (const deinflect of deinflects) { + processPromises.push(this.processTerm( + deinflectGroups, + deinflect.source, + deinflect.tags, + deinflect.rules, + deinflect.root + )); } return Promise.all(processPromises); @@ -73,14 +79,14 @@ class Translator { ); } - return Promise.all(deinflectPromises).then(() => groups); + return Promise.all(deinflectPromises).then(() => deinflectGroups); } findTerm(text) { - return this.findTermGroups(text).then(groups => { + return this.findTermGroups(text).then(deinflectGroups => { let definitions = []; - for (const key in groups) { - definitions.push(groups[key]); + for (const key in deinflectGroups) { + definitions.push(deinflectGroups[key]); } definitions = definitions.sort((v1, v2) => { @@ -121,17 +127,20 @@ class Translator { } findKanji(text) { - let definitions = []; const processed = {}; + const promises = []; for (const c of text) { if (!processed[c]) { - definitions = definitions.concat(this.dictionary.findKanji(c)); + promises.push(this.dictionary.findKanji(c).then((definitions) => definitions)); processed[c] = true; } } - return this.processKanji(definitions); + return Promise.all(promises).then((sets) => { + const definitions = sets.reduce((a, b) => a.concat(b)); + return this.processKanji(definitions); + }); } processTerm(groups, source, tags, rules, root) { @@ -143,7 +152,7 @@ class Translator { let matched = tags.length === 0; for (const tag of tags) { - if (definition.tags.indexOf(tag) !== -1) { + if (definition.tags.includes(tag)) { matched = true; break; } diff --git a/ext/bg/js/yomichan.js b/ext/bg/js/yomichan.js index 557f8780..11f348bf 100644 --- a/ext/bg/js/yomichan.js +++ b/ext/bg/js/yomichan.js @@ -41,10 +41,10 @@ class Yomichan { chrome.runtime.onInstalled.addListener(this.onInstalled.bind(this)); chrome.runtime.onMessage.addListener(this.onMessage.bind(this)); chrome.browserAction.onClicked.addListener(this.onBrowserAction.bind(this)); - chrome.tabs.onCreated.addListener((tab) => this.onTabReady(tab.id)); + chrome.tabs.onCreated.addListener(tab => this.onTabReady(tab.id)); chrome.tabs.onUpdated.addListener(this.onTabReady.bind(this)); - loadOptions((opts) => { + loadOptions(opts => { this.setOptions(opts); if (this.options.activateOnStartup) { this.setState('loading'); @@ -118,7 +118,7 @@ class Yomichan { } tabInvokeAll(action, params) { - chrome.tabs.query({}, (tabs) => { + chrome.tabs.query({}, tabs => { for (const tab of tabs) { this.tabInvoke(tab.id, action, params); } @@ -133,7 +133,7 @@ class Yomichan { if (this.ankiConnectVer === this.getApiVersion()) { this.ankiInvoke(action, params, pool, callback); } else { - this.api_getVersion({callback: (version) => { + this.api_getVersion({callback: version => { if (version === this.getApiVersion()) { this.ankiConnectVer = version; this.ankiInvoke(action, params, pool, callback); @@ -209,7 +209,7 @@ class Yomichan { break; case 'tags': if (definition.tags) { - value = definition.tags.map((t) => t.name); + value = definition.tags.map(t => t.name); } break; } @@ -244,7 +244,7 @@ class Yomichan { }; for (const name in fields) { - if (fields[name].indexOf('{audio}') !== -1) { + if (fields[name].includes('{audio}')) { audio.fields.push(name); } } @@ -274,7 +274,7 @@ class Yomichan { } } - this.ankiInvokeSafe('canAddNotes', {notes}, 'notes', (results) => { + this.ankiInvokeSafe('canAddNotes', {notes}, 'notes', results => { const states = []; if (results !== null) { @@ -293,11 +293,11 @@ class Yomichan { } api_findKanji({text, callback}) { - callback(this.translator.findKanji(text)); + this.translator.findKanji(text).then(result => callback(result)); } api_findTerm({text, callback}) { - this.translator.findTerm(text).then((result) => callback(result)); + this.translator.findTerm(text).then(result => callback(result)); } api_getDeckNames({callback}) { -- cgit v1.2.3 From c05f7a7c1cb9f25e3658ced51aefd61233f65bd3 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Fri, 16 Sep 2016 20:28:59 -0700 Subject: Work on progress bar --- ext/bg/guide.html | 29 ----------------------------- ext/bg/import.html | 45 +++++++++++++++++++++++++++++++++++++++++---- ext/bg/js/deinflector.js | 4 ++++ ext/bg/js/import.js | 10 +++++++--- ext/bg/js/yomichan.js | 9 +-------- 5 files changed, 53 insertions(+), 44 deletions(-) delete mode 100644 ext/bg/guide.html (limited to 'ext/bg/js/deinflector.js') diff --git a/ext/bg/guide.html b/ext/bg/guide.html deleted file mode 100644 index a3fa8221..00000000 --- a/ext/bg/guide.html +++ /dev/null @@ -1,29 +0,0 @@ - - - - - Yomichan Guide - - - - -
- - - -

This is a minimal guide to get you started with Yomichan. For complete documentation, visit the official homepage.

- -
    -
  1. Left-click on the icon to enable or disable Yomichan for the current browser instance.
  2. -
  3. Right-click on the icon and select Options to open the Yomichan options page.
  4. -
  5. Hold down Shift or the middle mouse button as you move your cursor over text to see definitions.
  6. -
  7. Resize the definition window by dragging the bottom-left corner inwards or outwards.
  8. -
  9. Click on Kanji in the definition window to view additional information about that character.
  10. -
- -

Enjoy!

-
- - diff --git a/ext/bg/import.html b/ext/bg/import.html index 85841eee..3dcdfdbd 100644 --- a/ext/bg/import.html +++ b/ext/bg/import.html @@ -5,18 +5,55 @@ Yomichan Dictionary Import +
-

Importing dictionary data, this can take a while...

+

Thank you for downloading this extension! I sincerely hope that it will assist you on your language learning journey.

-
-
0%
+
+

Dictionary Import

+ +

+ Before it can be used for the first time, Yomichan must import the Japanese dictionary data included with this extension. This process can take a + couple of minutes to finish so please be patient! Please do not completely exit out of your browser until this process completes. +

+ +
+
+
+ +
Dictionary import complete!
+ +
+

Quick Guide

+ +

+ Please read the steps outlined below to get quickly get up and running with Yomichan. For complete documentation, + visit the official homepage. +

+ +
    +
  1. Left-click on the icon to enable or disable Yomichan for the current browser instance.
  2. +
  3. Right-click on the icon and select Options to open the Yomichan options page.
  4. +
  5. Hold down Shift or the middle mouse button as you move your cursor over text to see definitions.
  6. +
  7. Resize the definitions window by dragging the bottom-left corner inwards or outwards.
  8. +
  9. Click on Kanji in the definition window to view additional information about that character.
  10. +
+
+ +
+ +

よろしくね!

diff --git a/ext/bg/js/deinflector.js b/ext/bg/js/deinflector.js index e5b1efe5..8b9f88e2 100644 --- a/ext/bg/js/deinflector.js +++ b/ext/bg/js/deinflector.js @@ -66,6 +66,10 @@ class Deinflection { } const term = this.term.slice(0, -variant.ki.length) + variant.ko; + if (term.length === 0) { + continue; + } + const child = new Deinflection(term, variant.to, rule); promises.push( child.deinflect(validator, rules).then(valid => { diff --git a/ext/bg/js/import.js b/ext/bg/js/import.js index ebc7c7be..0601cb9f 100644 --- a/ext/bg/js/import.js +++ b/ext/bg/js/import.js @@ -17,9 +17,13 @@ */ -function api_setProgress({state, progress}) { - const str = `${progress}%`; - $('.progress-bar').css('width', str).text(str); +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) => { diff --git a/ext/bg/js/yomichan.js b/ext/bg/js/yomichan.js index 24ddf92d..f1b3ffc4 100644 --- a/ext/bg/js/yomichan.js +++ b/ext/bg/js/yomichan.js @@ -28,7 +28,6 @@ class Yomichan { this.ankiConnectVer = 0; this.setState('disabled'); - chrome.runtime.onInstalled.addListener(this.onInstalled.bind(this)); chrome.runtime.onMessage.addListener(this.onMessage.bind(this)); chrome.browserAction.onClicked.addListener(this.onBrowserAction.bind(this)); chrome.tabs.onCreated.addListener(tab => this.onTabReady(tab.id)); @@ -42,19 +41,13 @@ class Yomichan { }); } - onInstalled(details) { - if (details.reason === 'install') { - chrome.tabs.create({url: chrome.extension.getURL('bg/guide.html')}); - } - } - 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', {state, progress}); + this.tabInvoke(this.importTabId, 'setProgress', progress); } if (state === 'end') { -- cgit v1.2.3