From d8a630fa2881a0cf012c6548706dde7c4740fe14 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sun, 20 Mar 2016 17:15:40 -0700 Subject: Simple lookup now works. --- ext/content.js | 2 +- ext/jp/dictionary.js | 50 +++++++++++++++++++++++++++++++++++++++++++++++--- ext/jp/translator.js | 15 ++++++--------- ext/manifest.json | 11 ++++------- 4 files changed, 58 insertions(+), 20 deletions(-) (limited to 'ext') diff --git a/ext/content.js b/ext/content.js index dc7c7cd7..aa51d758 100644 --- a/ext/content.js +++ b/ext/content.js @@ -52,4 +52,4 @@ function onMouseDown(e) { -window.addEventListener('mousedown', onMouseDown, false); +// window.addEventListener('mousedown', onMouseDown, false); diff --git a/ext/jp/dictionary.js b/ext/jp/dictionary.js index 2b11d252..871a9550 100644 --- a/ext/jp/dictionary.js +++ b/ext/jp/dictionary.js @@ -18,11 +18,55 @@ class Dictionary { - constructor(rules) { - this.rules = rules; + constructor() { + this.termDicts = []; + this.kanjiDicts = []; } - deinflect(term, validator) { + addTermDict(termDict) { + this.termDicts.push(termDict); + } + + addKanjiDict(kanjiDict) { + this.kanjiDicts.push(kanjiDict); + } + + + findTerm(term) { + let results = []; + for (let dict of this.termDicts) { + results = results.concat(this.findTermInDict(term, dict)); + } + + return results; + } + + findKanji(kanji) { + const results = []; + for (let dict of this.kanjiDicts) { + const result = this.findKanjiInDict(kanji, dict); + if (result !== null) { + results.push(result); + } + } + + return results; + } + + findTermInDict(term, dict) { + return (dict.indices[term] || []).map(index => { + const [e, r, g, t] = dict.defs[index]; + return {expression: e, reading: r, glossary: g, tags: t}; + }); + } + + findKanjiInDict(kanji, dict) { + const def = dict.defs[kanji]; + if (def === null) { + return null; + } + const [c, k, o, g] = def; + return {character: c, kunyomi: k, onyomi: o, glossary: g}; } } diff --git a/ext/jp/translator.js b/ext/jp/translator.js index be15c397..79f2b369 100644 --- a/ext/jp/translator.js +++ b/ext/jp/translator.js @@ -18,10 +18,7 @@ class Translator { constructor() { - this.rules = {}; - this.edict = {}; - this.enamdict = {}; - this.kanjidic = {}; + this.dictionary = new Dictionary(); this.initialized = false; } @@ -38,10 +35,9 @@ class Translator { } $.when.apply($, loaders).done((rules, edict, enamdict, kanjidic) => { - this.rules = rules; - this.edict = edict; - this.enamdict = enamdict; - this.kanjidic = kanjidic; + this.dictionary.addTermDict(edict[0]); + this.dictionary.addTermDict(enamdict[0]); + this.dictionary.addKanjiDict(kanjidic[0]); this.initialized = true; @@ -60,5 +56,6 @@ trans.initialize({ enamdict: 'jp/data/enamdict.json', kanjidic: 'jp/data/kanjidic.json', }, function() { - alert('Loaded'); + // alert('Loaded'); + // alert(trans.dictionary.findTerm('猫')); }); diff --git a/ext/manifest.json b/ext/manifest.json index 1cd15a9d..3685d949 100644 --- a/ext/manifest.json +++ b/ext/manifest.json @@ -4,15 +4,12 @@ "description": "Yomichan Extension", "version": "0.0.1", - "browser_action": { - "default_icon": "icon.png" - }, - - "background": { "page": "background.html" }, + "browser_action": {"default_icon": "icon.png"}, + "background": {"page": "background.html"}, "content_scripts": [{ - "matches": [ "http://*/*", "https://*/*" ], - "js": [ "content.js" ], + "matches": ["http://*/*", "https://*/*"], + "js": ["content.js"], "run_at": "document_end" }] } -- cgit v1.2.3