diff options
Diffstat (limited to 'ext')
-rw-r--r-- | ext/bg/dictionary.js | 77 | ||||
-rw-r--r-- | ext/client.js | 4 |
2 files changed, 39 insertions, 42 deletions
diff --git a/ext/bg/dictionary.js b/ext/bg/dictionary.js index eff54890..30c34687 100644 --- a/ext/bg/dictionary.js +++ b/ext/bg/dictionary.js @@ -19,43 +19,33 @@ class Dictionary { constructor() { - this.termDicts = []; - this.kanjiDicts = []; - } - - addTermDict(termDict) { - this.termDicts.push(termDict); - } + this.terms = []; + this.termIndices = {}; - addKanjiDict(kanjiDict) { - this.kanjiDicts.push(kanjiDict); + this.kanji = []; + this.kanjiIndices = {}; } - - findTerm(term) { - let results = []; - for (let dict of this.termDicts) { - results = results.concat(this.findTermInDict(term, dict)); + addTermDict(terms) { + let index = this.terms.length; + for (const [e, r, g, t] in terms) { + this.storeIndex(this.termIndices, e, index); + this.storeIndex(this.termIndices, r, index++); + this.terms.push([e, r, g, t]); } - - return results; } - findKanji(kanji) { - const results = []; - for (let dict of this.kanjiDicts) { - const result = this.findKanjiInDict(kanji, dict); - if (result !== null) { - results.push(result); - } + addKanjiDict(kanji) { + let index = this.kanji.length; + for (const [c, k, o, g] in kanji) { + this.storeIndex(this.kanjiIndices, c, index++); + this.kanji.push([c, k, o, g]); } - - return results; } - findTermInDict(term, dict) { - return (dict.indices[term] || []).map(index => { - const [e, r, g, t] = dict.defs[index]; + findTerm(term) { + return (this.termIndices[term] || []).map(index => { + const [e, r, g, t] = this.terms[index]; return { id: index, expression: e, @@ -66,19 +56,24 @@ class Dictionary { }); } - findKanjiInDict(kanji, dict) { - const def = dict.defs[kanji]; - if (def === null) { - return null; - } + findKanji(kanji) { + return (this.kanjiIndices[kanji] || []).map(index => { + const [c, k, o, g] = def; + return { + id: kanji.charCodeAt(0), + character: c, + kunyomi: k, + onyomi: o, + glossary: g + }; + }); + } - const [c, k, o, g] = def; - return { - id: kanji.charCodeAt(0), - character: c, - kunyomi: k, - onyomi: o, - glossary: g - }; + storeIndex(indices, term, index) { + if (term.length > 0) { + const indices = this.termIndices[term] || []; + indices.push(term); + this.termIndices[term] = indices; + } } } diff --git a/ext/client.js b/ext/client.js index 2d9a470f..1c8c0a9f 100644 --- a/ext/client.js +++ b/ext/client.js @@ -27,7 +27,9 @@ class Client { this.popup.classList.add('yomichan-popup'); this.popup.addEventListener('mousedown', (e) => e.stopPropagation()); this.popup.addEventListener('scroll', (e) => e.stopPropagation()); - document.body.appendChild(this.popup); + + const base = document.body.appendChild('div'); + base.createShadowRoot().appendChild(this.popup); chrome.runtime.onMessage.addListener(this.onMessage.bind(this)); window.addEventListener('mousedown', this.onMouseDown.bind(this)); |