From d57c5530b7ad56a7cc89782b4d186d8fddb55d86 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sat, 1 Jul 2017 18:27:49 -0700 Subject: view added notes --- ext/bg/js/anki-connect.js | 16 ++++++++++------ ext/bg/js/anki-null.js | 4 ++++ ext/bg/js/display-window.js | 4 ++++ ext/bg/js/templates.js | 12 ++++++------ ext/bg/js/yomichan.js | 8 ++++++++ ext/fg/js/display-frame.js | 4 ++++ ext/fg/js/util.js | 3 +++ ext/mixed/img/view-note.png | Bin 0 -> 622 bytes ext/mixed/js/display.js | 36 +++++++++++++++++++++++++++++++++--- tmpl/kanji.html | 3 ++- tmpl/terms.html | 3 ++- 11 files changed, 76 insertions(+), 17 deletions(-) create mode 100644 ext/mixed/img/view-note.png diff --git a/ext/bg/js/anki-connect.js b/ext/bg/js/anki-connect.js index 9759c8f5..173feefd 100644 --- a/ext/bg/js/anki-connect.js +++ b/ext/bg/js/anki-connect.js @@ -26,7 +26,7 @@ class AnkiConnect { } addNote(note) { - return this.checkVersion().then(() => this.ankiInvoke('addNote', {note}, null)); + return this.checkVersion().then(() => this.ankiInvoke('addNote', {note})); } canAddNotes(notes) { @@ -34,15 +34,19 @@ class AnkiConnect { } getDeckNames() { - return this.checkVersion().then(() => this.ankiInvoke('deckNames', {}, null)); + return this.checkVersion().then(() => this.ankiInvoke('deckNames', {})); } getModelNames() { - return this.checkVersion().then(() => this.ankiInvoke('modelNames', {}, null)); + return this.checkVersion().then(() => this.ankiInvoke('modelNames', {})); } getModelFieldNames(modelName) { - return this.checkVersion().then(() => this.ankiInvoke('modelFieldNames', {modelName}, null)); + return this.checkVersion().then(() => this.ankiInvoke('modelFieldNames', {modelName})); + } + + guiBrowse(query) { + return this.checkVersion().then(() => this.ankiInvoke('guiBrowse', {query})); } checkVersion() { @@ -60,13 +64,13 @@ class AnkiConnect { ankiInvoke(action, params, pool) { return new Promise((resolve, reject) => { - if (pool !== null && this.asyncPools.hasOwnProperty(pool)) { + if (pool && this.asyncPools.hasOwnProperty(pool)) { this.asyncPools[pool].abort(); } const xhr = new XMLHttpRequest(); xhr.addEventListener('loadend', () => { - if (pool !== null) { + if (pool) { delete this.asyncPools[pool]; } diff --git a/ext/bg/js/anki-null.js b/ext/bg/js/anki-null.js index 99dc2f30..8dad6915 100644 --- a/ext/bg/js/anki-null.js +++ b/ext/bg/js/anki-null.js @@ -37,4 +37,8 @@ class AnkiNull { getModelFieldNames(modelName) { return Promise.resolve([]); } + + guiBrowse(query) { + return Promise.resolve([]); + } } diff --git a/ext/bg/js/display-window.js b/ext/bg/js/display-window.js index ae97cd36..64e56f72 100644 --- a/ext/bg/js/display-window.js +++ b/ext/bg/js/display-window.js @@ -37,6 +37,10 @@ window.displayWindow = new class extends Display { return instYomi().definitionsAddable(definitions, modes).catch(() => []); } + noteView(noteId) { + return instYomi().noteView(noteId); + } + templateRender(template, data) { return instYomi().templateRender(template, data); } diff --git a/ext/bg/js/templates.js b/ext/bg/js/templates.js index f10134ee..50686ed4 100644 --- a/ext/bg/js/templates.js +++ b/ext/bg/js/templates.js @@ -315,10 +315,10 @@ templates['fields.html'] = template({"1":function(container,depth0,helpers,parti templates['kanji.html'] = template({"1":function(container,depth0,helpers,partials,data) { var stack1, helper, alias1=depth0 != null ? depth0 : {}; - return "
\n
\n \n" + return "
\n
\n" + ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.addable : depth0),{"name":"if","hash":{},"fn":container.program(2, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.source : depth0),{"name":"if","hash":{},"fn":container.program(4, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") - + "
\n\n
" + + " \n
\n\n
" + container.escapeExpression(((helper = (helper = helpers.character || (depth0 != null ? depth0.character : depth0)) != null ? helper : helpers.helperMissing),(typeof helper === "function" ? helper.call(alias1,{"name":"character","hash":{},"data":data}) : helper))) + "
\n\n
\n \n \n \n \n \n\n"; },"useData":true}); templates['terms.html'] = template({"1":function(container,depth0,helpers,partials,data) { - var stack1, alias1=depth0 != null ? depth0 : {}; + var stack1, alias1=depth0 != null ? depth0 : (container.nullContext || {}); return ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.tags : depth0),{"name":"if","hash":{},"fn":container.program(2, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + ((stack1 = helpers["if"].call(alias1,((stack1 = (depth0 != null ? depth0.glossary : depth0)) != null ? stack1["1"] : stack1),{"name":"if","hash":{},"fn":container.program(5, data, 0),"inverse":container.program(9, data, 0),"data":data})) != null ? stack1 : ""); @@ -458,10 +458,10 @@ templates['terms.html'] = template({"1":function(container,depth0,helpers,partia var stack1; return "
\n" - + ((stack1 = helpers.each.call(depth0 != null ? depth0 : {},(depth0 != null ? depth0.tags : depth0),{"name":"each","hash":{},"fn":container.program(3, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + + ((stack1 = helpers.each.call(depth0 != null ? depth0 : (container.nullContext || {}),(depth0 != null ? depth0.tags : depth0),{"name":"each","hash":{},"fn":container.program(3, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + "
\n"; },"3":function(container,depth0,helpers,partials,data) { - var helper, alias1=depth0 != null ? depth0 : {}, alias2=helpers.helperMissing, alias3="function", alias4=container.escapeExpression; + var helper, alias1=depth0 != null ? depth0 : (container.nullContext || {}), alias2=helpers.helperMissing, alias3="function", alias4=container.escapeExpression; return " \n" - + ((stack1 = helpers.each.call(depth0 != null ? depth0 : {},(depth0 != null ? depth0.glossary : depth0),{"name":"each","hash":{},"fn":container.program(6, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + + ((stack1 = helpers.each.call(depth0 != null ? depth0 : (container.nullContext || {}),(depth0 != null ? depth0.glossary : depth0),{"name":"each","hash":{},"fn":container.program(6, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + "\n"; },"6":function(container,depth0,helpers,partials,data) { var stack1, helper, options, buffer = "
  • "; - stack1 = ((helper = (helper = helpers.multiLine || (depth0 != null ? depth0.multiLine : depth0)) != null ? helper : helpers.helperMissing),(options={"name":"multiLine","hash":{},"fn":container.program(7, data, 0),"inverse":container.noop,"data":data}),(typeof helper === "function" ? helper.call(depth0 != null ? depth0 : {},options) : helper)); + stack1 = ((helper = (helper = helpers.multiLine || (depth0 != null ? depth0.multiLine : depth0)) != null ? helper : helpers.helperMissing),(options={"name":"multiLine","hash":{},"fn":container.program(7, data, 0),"inverse":container.noop,"data":data}),(typeof helper === "function" ? helper.call(depth0 != null ? depth0 : (container.nullContext || {}),options) : helper)); if (!helpers.multiLine) { stack1 = helpers.blockHelperMissing.call(depth0,stack1,options)} if (stack1 != null) { buffer += stack1; } return buffer + "
  • \n"; @@ -488,7 +488,7 @@ templates['terms.html'] = template({"1":function(container,depth0,helpers,partia },"9":function(container,depth0,helpers,partials,data) { var stack1, helper, options, buffer = "
    "; - stack1 = ((helper = (helper = helpers.multiLine || (depth0 != null ? depth0.multiLine : depth0)) != null ? helper : helpers.helperMissing),(options={"name":"multiLine","hash":{},"fn":container.program(10, data, 0),"inverse":container.noop,"data":data}),(typeof helper === "function" ? helper.call(depth0 != null ? depth0 : {},options) : helper)); + stack1 = ((helper = (helper = helpers.multiLine || (depth0 != null ? depth0.multiLine : depth0)) != null ? helper : helpers.helperMissing),(options={"name":"multiLine","hash":{},"fn":container.program(10, data, 0),"inverse":container.noop,"data":data}),(typeof helper === "function" ? helper.call(depth0 != null ? depth0 : (container.nullContext || {}),options) : helper)); if (!helpers.multiLine) { stack1 = helpers.blockHelperMissing.call(depth0,stack1,options)} if (stack1 != null) { buffer += stack1; } return buffer + "
    \n"; @@ -497,7 +497,7 @@ templates['terms.html'] = template({"1":function(container,depth0,helpers,partia return container.escapeExpression(container.lambda(((stack1 = (depth0 != null ? depth0.glossary : depth0)) != null ? stack1["0"] : stack1), depth0)); },"12":function(container,depth0,helpers,partials,data) { - var stack1, alias1=depth0 != null ? depth0 : {}; + var stack1, alias1=depth0 != null ? depth0 : (container.nullContext || {}); return "
    \n
    \n" + ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.addable : depth0),{"name":"if","hash":{},"fn":container.program(13, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") @@ -516,7 +516,7 @@ templates['terms.html'] = template({"1":function(container,depth0,helpers,partia },"15":function(container,depth0,helpers,partials,data) { return " \n"; },"17":function(container,depth0,helpers,partials,data) { - var stack1, helper, options, alias1=depth0 != null ? depth0 : {}, alias2=helpers.helperMissing, alias3="function", buffer = + var stack1, helper, options, alias1=depth0 != null ? depth0 : (container.nullContext || {}), alias2=helpers.helperMissing, alias3="function", buffer = "
    "; stack1 = ((helper = (helper = helpers.kanjiLinks || (depth0 != null ? depth0.kanjiLinks : depth0)) != null ? helper : alias2),(options={"name":"kanjiLinks","hash":{},"fn":container.program(18, data, 0),"inverse":container.noop,"data":data}),(typeof helper === alias3 ? helper.call(alias1,options) : helper)); if (!helpers.kanjiLinks) { stack1 = helpers.blockHelperMissing.call(depth0,stack1,options)} @@ -527,11 +527,11 @@ templates['terms.html'] = template({"1":function(container,depth0,helpers,partia },"18":function(container,depth0,helpers,partials,data) { var helper; - return container.escapeExpression(((helper = (helper = helpers.expression || (depth0 != null ? depth0.expression : depth0)) != null ? helper : helpers.helperMissing),(typeof helper === "function" ? helper.call(depth0 != null ? depth0 : {},{"name":"expression","hash":{},"data":data}) : helper))); + return container.escapeExpression(((helper = (helper = helpers.expression || (depth0 != null ? depth0.expression : depth0)) != null ? helper : helpers.helperMissing),(typeof helper === "function" ? helper.call(depth0 != null ? depth0 : (container.nullContext || {}),{"name":"expression","hash":{},"data":data}) : helper))); },"20":function(container,depth0,helpers,partials,data) { var stack1, helper, options, buffer = "
    "; - stack1 = ((helper = (helper = helpers.kanjiLinks || (depth0 != null ? depth0.kanjiLinks : depth0)) != null ? helper : helpers.helperMissing),(options={"name":"kanjiLinks","hash":{},"fn":container.program(18, data, 0),"inverse":container.noop,"data":data}),(typeof helper === "function" ? helper.call(depth0 != null ? depth0 : {},options) : helper)); + stack1 = ((helper = (helper = helpers.kanjiLinks || (depth0 != null ? depth0.kanjiLinks : depth0)) != null ? helper : helpers.helperMissing),(options={"name":"kanjiLinks","hash":{},"fn":container.program(18, data, 0),"inverse":container.noop,"data":data}),(typeof helper === "function" ? helper.call(depth0 != null ? depth0 : (container.nullContext || {}),options) : helper)); if (!helpers.kanjiLinks) { stack1 = helpers.blockHelperMissing.call(depth0,stack1,options)} if (stack1 != null) { buffer += stack1; } return buffer + "
    \n"; @@ -539,7 +539,7 @@ templates['terms.html'] = template({"1":function(container,depth0,helpers,partia var stack1; return "
    \n" - + ((stack1 = helpers.each.call(depth0 != null ? depth0 : {},(depth0 != null ? depth0.reasons : depth0),{"name":"each","hash":{},"fn":container.program(23, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + + ((stack1 = helpers.each.call(depth0 != null ? depth0 : (container.nullContext || {}),(depth0 != null ? depth0.reasons : depth0),{"name":"each","hash":{},"fn":container.program(23, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + "
    \n"; },"23":function(container,depth0,helpers,partials,data) { var stack1; @@ -547,19 +547,19 @@ templates['terms.html'] = template({"1":function(container,depth0,helpers,partia return " " + container.escapeExpression(container.lambda(depth0, depth0)) + " " - + ((stack1 = helpers.unless.call(depth0 != null ? depth0 : {},(data && data.last),{"name":"unless","hash":{},"fn":container.program(24, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + + ((stack1 = helpers.unless.call(depth0 != null ? depth0 : (container.nullContext || {}),(data && data.last),{"name":"unless","hash":{},"fn":container.program(24, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + "\n"; },"24":function(container,depth0,helpers,partials,data) { return "«"; },"26":function(container,depth0,helpers,partials,data) { var stack1; - return ((stack1 = helpers["if"].call(depth0 != null ? depth0 : {},((stack1 = (depth0 != null ? depth0.definitions : depth0)) != null ? stack1["1"] : stack1),{"name":"if","hash":{},"fn":container.program(27, data, 0),"inverse":container.program(30, data, 0),"data":data})) != null ? stack1 : ""); + return ((stack1 = helpers["if"].call(depth0 != null ? depth0 : (container.nullContext || {}),((stack1 = (depth0 != null ? depth0.definitions : depth0)) != null ? stack1["1"] : stack1),{"name":"if","hash":{},"fn":container.program(27, data, 0),"inverse":container.program(30, data, 0),"data":data})) != null ? stack1 : ""); },"27":function(container,depth0,helpers,partials,data) { var stack1; return "
      \n" - + ((stack1 = helpers.each.call(depth0 != null ? depth0 : {},(depth0 != null ? depth0.definitions : depth0),{"name":"each","hash":{},"fn":container.program(28, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + + ((stack1 = helpers.each.call(depth0 != null ? depth0 : (container.nullContext || {}),(depth0 != null ? depth0.definitions : depth0),{"name":"each","hash":{},"fn":container.program(28, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + "
    \n"; },"28":function(container,depth0,helpers,partials,data) { var stack1; @@ -578,7 +578,7 @@ templates['terms.html'] = template({"1":function(container,depth0,helpers,partia },"34":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(35, data, 0),"inverse":container.noop,"data":data}),(typeof helper === "function" ? helper.call(depth0 != null ? depth0 : {},options) : helper));
    +  stack1 = ((helper = (helper = helpers.dumpObject || (depth0 != null ? depth0.dumpObject : depth0)) != null ? helper : helpers.helperMissing),(options={"name":"dumpObject","hash":{},"fn":container.program(35, 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"; @@ -589,11 +589,11 @@ templates['terms.html'] = template({"1":function(container,depth0,helpers,partia },"37":function(container,depth0,helpers,partials,data,blockParams,depths) { var stack1; - return ((stack1 = helpers.each.call(depth0 != null ? depth0 : {},(depth0 != null ? depth0.definitions : depth0),{"name":"each","hash":{},"fn":container.program(38, data, 0, blockParams, depths),"inverse":container.noop,"data":data})) != null ? stack1 : ""); + return ((stack1 = helpers.each.call(depth0 != null ? depth0 : (container.nullContext || {}),(depth0 != null ? depth0.definitions : depth0),{"name":"each","hash":{},"fn":container.program(38, data, 0, blockParams, depths),"inverse":container.noop,"data":data})) != null ? stack1 : ""); },"38":function(container,depth0,helpers,partials,data,blockParams,depths) { var stack1; - return ((stack1 = helpers.unless.call(depth0 != null ? depth0 : {},(data && data.first),{"name":"unless","hash":{},"fn":container.program(39, 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(39, data, 0, blockParams, depths),"inverse":container.noop,"data":data})) != null ? stack1 : "") + "\n" + ((stack1 = container.invokePartial(partials.term,depth0,{"name":"term","hash":{"playback":(depths[1] != null ? depths[1].playback : depths[1]),"addable":(depths[1] != null ? depths[1].addable : depths[1]),"grouped":(depths[1] != null ? depths[1].grouped : depths[1]),"debug":(depths[1] != null ? depths[1].debug : depths[1])},"data":data,"helpers":helpers,"partials":partials,"decorators":container.decorators})) != null ? stack1 : ""); },"39":function(container,depth0,helpers,partials,data) { @@ -604,7 +604,7 @@ templates['terms.html'] = template({"1":function(container,depth0,helpers,partia var stack1; return "\n\n" - + ((stack1 = helpers["if"].call(depth0 != null ? depth0 : {},(depth0 != null ? depth0.definitions : depth0),{"name":"if","hash":{},"fn":container.program(37, data, 0, blockParams, depths),"inverse":container.program(41, 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(37, data, 0, blockParams, depths),"inverse":container.program(41, 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/ext/bg/search.html b/ext/bg/search.html index 7cbae392..655d7819 100644 --- a/ext/bg/search.html +++ b/ext/bg/search.html @@ -32,13 +32,19 @@
    + + + + - - + + - + + + diff --git a/ext/fg/frame.html b/ext/fg/frame.html index 9ff2c585..e0b9eac5 100644 --- a/ext/fg/frame.html +++ b/ext/fg/frame.html @@ -30,12 +30,13 @@
    - - + + - - + + + -- cgit v1.2.3 From ef43b742b0b0705e8fe44792933878c161d55d8f Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sun, 30 Jul 2017 17:32:36 -0700 Subject: Updating README.md --- README.md | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 4d467f82..1ed2bb77 100644 --- a/README.md +++ b/README.md @@ -132,7 +132,6 @@ Flashcard fields can be configured with the following steps: `{expression}` | Term expressed as Kanji (will be displayed in Kana if Kanji is not available). `{furigana}` | Term expressed as Kanji with Furigana displayed above it (e.g. 日本語にほんご). `{glossary}` | List of definitions for the term (output format depends on whether running in *grouped* mode). - `{glossary-brief}` | Shorter version of `{glossary}`, without `{tags}`. `{reading}` | Kana reading for the term (empty for terms where the expression is the reading). `{sentence}` | Sentence, quote, or phrase in which the term appears in the source content. `{tags}` | Grammar and usage tags providing information about the term (unavailable in *grouped* mode). @@ -290,4 +289,16 @@ exact versions used for distribution. ## License ## -GPL +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . + -- cgit v1.2.3 From b2003a0a560a9bd469e23e860c39dc5b21412021 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sat, 5 Aug 2017 13:13:06 -0700 Subject: cleanup --- ext/bg/js/api.js | 102 ++++++++++++++------------------------------------- ext/bg/js/backend.js | 50 ++++++++++++++++++++++++- ext/bg/js/popup.js | 8 ++-- 3 files changed, 79 insertions(+), 81 deletions(-) diff --git a/ext/bg/js/api.js b/ext/bg/js/api.js index ac53a7a3..103d247c 100644 --- a/ext/bg/js/api.js +++ b/ext/bg/js/api.js @@ -21,81 +21,6 @@ * Helpers */ -function utilMessageDispatch({action, params}, sender, callback) { - const forward = (promise, callback) => { - return promise.then(result => { - callback({result}); - }).catch(error => { - callback({error}); - }); - }; - - const handlers = { - optionsGet: ({callback}) => { - forward(optionsLoad(), callback); - }, - - kanjiFind: ({text, callback}) => { - forward(apiKanjiFind(text), callback); - }, - - termsFind: ({text, callback}) => { - forward(apiTermsFind(text), callback); - }, - - templateRender: ({template, data, callback}) => { - forward(apiTemplateRender(template, data), callback); - }, - - definitionAdd: ({definition, mode, callback}) => { - forward(apiDefinitionAdd(definition, mode), callback); - }, - - definitionsAddable: ({definitions, modes, callback}) => { - forward(apiDefinitionsAddable(definitions, modes), callback); - }, - - noteView: ({noteId}) => { - forward(apiNoteView(noteId), callback); - } - }; - - const handler = handlers[action]; - if (handler) { - params.callback = callback; - handler(params); - } - - return true; -} - -function utilCommandDispatch(command) { - const handlers = { - search: () => { - chrome.tabs.create({url: chrome.extension.getURL('/bg/search.html')}); - }, - - help: () => { - chrome.tabs.create({url: 'https://foosoft.net/projects/yomichan/'}); - }, - - options: () => { - chrome.runtime.openOptionsPage(); - }, - - toggle: () => { - const options = chrome.extension.getBackgroundPage().yomichan.options; - options.general.enable = !options.general.enable; - optionsSave(options).then(() => apiOptionsSet(options)); - } - }; - - const handler = handlers[command]; - if (handler) { - handler(); - } -} - function utilNoteFormat(definition, mode) { const options = chrome.extension.getBackgroundPage().yomichan.options; const note = {fields: {}, tags: options.anki.tags}; @@ -142,6 +67,33 @@ function utilNoteFormat(definition, mode) { * API */ +async function apiCommandExec(command) { + const handlers = { + search: () => { + chrome.tabs.create({url: chrome.extension.getURL('/bg/search.html')}); + }, + + help: () => { + chrome.tabs.create({url: 'https://foosoft.net/projects/yomichan/'}); + }, + + options: () => { + chrome.runtime.openOptionsPage(); + }, + + toggle: () => { + const options = chrome.extension.getBackgroundPage().yomichan.options; + options.general.enable = !options.general.enable; + optionsSave(options).then(() => apiOptionsSet(options)); + } + }; + + const handler = handlers[command]; + if (handler) { + handler(); + } +} + async function apiOptionsSet(options) { // In Firefox, setting options from the options UI somehow carries references // to the DOM across to the background page, causing the options object to diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index f7823047..2f60b4b7 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -26,8 +26,54 @@ window.yomichan = new class { this.translator.prepare().then(optionsLoad).then(options => { apiOptionsSet(options); - chrome.commands.onCommand.addListener(utilCommandDispatch); - chrome.runtime.onMessage.addListener(utilMessageDispatch); + chrome.commands.onCommand.addListener(apiCommandExec); + chrome.runtime.onMessage.addListener(({action, params}, sender, callback) => { + const forward = (promise, callback) => { + return promise.then(result => { + callback({result}); + }).catch(error => { + callback({error}); + }); + }; + + const handlers = { + optionsGet: ({callback}) => { + forward(optionsLoad(), callback); + }, + + kanjiFind: ({text, callback}) => { + forward(apiKanjiFind(text), callback); + }, + + termsFind: ({text, callback}) => { + forward(apiTermsFind(text), callback); + }, + + templateRender: ({template, data, callback}) => { + forward(apiTemplateRender(template, data), callback); + }, + + definitionAdd: ({definition, mode, callback}) => { + forward(apiDefinitionAdd(definition, mode), callback); + }, + + definitionsAddable: ({definitions, modes, callback}) => { + forward(apiDefinitionsAddable(definitions, modes), callback); + }, + + noteView: ({noteId}) => { + forward(apiNoteView(noteId), callback); + } + }; + + const handler = handlers[action]; + if (handler) { + params.callback = callback; + handler(params); + } + + return true; + }); if (options.general.showGuide) { chrome.tabs.create({url: chrome.extension.getURL('/bg/guide.html')}); diff --git a/ext/bg/js/popup.js b/ext/bg/js/popup.js index 712839d9..77cb5166 100644 --- a/ext/bg/js/popup.js +++ b/ext/bg/js/popup.js @@ -18,14 +18,14 @@ $(document).ready(() => { - $('#open-search').click(() => utilCommandDispatch('search')); - $('#open-options').click(() => utilCommandDispatch('options')); - $('#open-help').click(() => utilCommandDispatch('help')); + $('#open-search').click(() => apiCommandExec('search')); + $('#open-options').click(() => apiCommandExec('options')); + $('#open-help').click(() => apiCommandExec('help')); optionsLoad().then(options => { const toggle = $('#enable-search'); toggle.prop('checked', options.general.enable).change(); toggle.bootstrapToggle(); - toggle.change(() => utilCommandDispatch('toggle')); + toggle.change(() => apiCommandExec('toggle')); }); }); -- cgit v1.2.3 From 257c864bb5ca5ef9afb4d92b9d29a7f479bc50b4 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sat, 5 Aug 2017 14:57:33 -0700 Subject: fix anki --- ext/bg/js/api.js | 60 +++++-------------------------------------------- ext/bg/js/dictionary.js | 40 +++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 54 deletions(-) diff --git a/ext/bg/js/api.js b/ext/bg/js/api.js index 103d247c..36e283b9 100644 --- a/ext/bg/js/api.js +++ b/ext/bg/js/api.js @@ -17,56 +17,6 @@ */ -/* - * Helpers - */ - -function utilNoteFormat(definition, mode) { - const options = chrome.extension.getBackgroundPage().yomichan.options; - const note = {fields: {}, tags: options.anki.tags}; - let fields = []; - - if (mode === 'kanji') { - fields = options.anki.kanji.fields; - note.deckName = options.anki.kanji.deck; - note.modelName = options.anki.kanji.model; - } else { - fields = options.anki.terms.fields; - note.deckName = options.anki.terms.deck; - note.modelName = options.anki.terms.model; - - if (definition.audio) { - const audio = { - url: definition.audio.url, - filename: definition.audio.filename, - skipHash: '7e2c2f954ef6051373ba916f000168dc', - fields: [] - }; - - for (const name in fields) { - if (fields[name].includes('{audio}')) { - audio.fields.push(name); - } - } - - if (audio.fields.length > 0) { - note.audio = audio; - } - } - } - - for (const name in fields) { - note.fields[name] = dictFieldFormat(fields[name], definition, mode, options); - } - - return note; -} - - -/* - * API - */ - async function apiCommandExec(command) { const handlers = { search: () => { @@ -159,9 +109,9 @@ async function apiKanjiFind(text) { async function apiDefinitionAdd(definition, mode) { const yomichan = chrome.extension.getBackgroundPage().yomichan; + const options = yomichan.options; if (mode !== 'kanji') { - const options = yomichan.options; await audioInject( definition, options.anki.terms.fields, @@ -169,18 +119,20 @@ async function apiDefinitionAdd(definition, mode) { ); } - return yomichan.anki.addNote(utilNoteFormat(definition, mode)); + return yomichan.anki.addNote(dictNoteFormat(definition, mode, options)); } async function apiDefinitionsAddable(definitions, modes) { + const yomichan = chrome.extension.getBackgroundPage().yomichan; + const options = yomichan.options; + const notes = []; for (const definition of definitions) { for (const mode of modes) { - notes.push(utilNoteFormat(definition, mode)); + notes.push(dictNoteFormat(definition, mode, options)); } } - const yomichan = chrome.extension.getBackgroundPage().yomichan; const results = await yomichan.anki.canAddNotes(notes); const states = []; for (let resultBase = 0; resultBase < results.length; resultBase += modes.length) { diff --git a/ext/bg/js/dictionary.js b/ext/bg/js/dictionary.js index 73efe7d1..6f9b30e4 100644 --- a/ext/bg/js/dictionary.js +++ b/ext/bg/js/dictionary.js @@ -231,3 +231,43 @@ function dictFieldFormat(field, definition, mode, options) { return field; } + +function dictNoteFormat(definition, mode, options) { + const note = {fields: {}, tags: options.anki.tags}; + let fields = []; + + if (mode === 'kanji') { + fields = options.anki.kanji.fields; + note.deckName = options.anki.kanji.deck; + note.modelName = options.anki.kanji.model; + } else { + fields = options.anki.terms.fields; + note.deckName = options.anki.terms.deck; + note.modelName = options.anki.terms.model; + + if (definition.audio) { + const audio = { + url: definition.audio.url, + filename: definition.audio.filename, + skipHash: '7e2c2f954ef6051373ba916f000168dc', + fields: [] + }; + + for (const name in fields) { + if (fields[name].includes('{audio}')) { + audio.fields.push(name); + } + } + + if (audio.fields.length > 0) { + note.audio = audio; + } + } + } + + for (const name in fields) { + note.fields[name] = dictFieldFormat(fields[name], definition, mode, options); + } + + return note; +} -- cgit v1.2.3 From 8e1c6776d1a6149c222ac0bda6b7b0d373453d11 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sat, 5 Aug 2017 19:02:03 -0700 Subject: more cleanup --- ext/bg/js/api.js | 113 +++++++++++++++++---------------------- ext/bg/js/backend.js | 143 +++++++++++++++++++++++++++++++------------------- ext/bg/js/settings.js | 18 +++---- 3 files changed, 144 insertions(+), 130 deletions(-) diff --git a/ext/bg/js/api.js b/ext/bg/js/api.js index 36e283b9..9dbb69c1 100644 --- a/ext/bg/js/api.js +++ b/ext/bg/js/api.js @@ -17,72 +17,34 @@ */ -async function apiCommandExec(command) { - const handlers = { - search: () => { - chrome.tabs.create({url: chrome.extension.getURL('/bg/search.html')}); - }, - - help: () => { - chrome.tabs.create({url: 'https://foosoft.net/projects/yomichan/'}); - }, +/* + * Backend + */ - options: () => { - chrome.runtime.openOptionsPage(); - }, +function backend() { + return chrome.extension.getBackgroundPage().yomichan_backend; +} - toggle: () => { - const options = chrome.extension.getBackgroundPage().yomichan.options; - options.general.enable = !options.general.enable; - optionsSave(options).then(() => apiOptionsSet(options)); - } - }; - const handler = handlers[command]; - if (handler) { - handler(); - } -} +/* + * API + */ async function apiOptionsSet(options) { // In Firefox, setting options from the options UI somehow carries references // to the DOM across to the background page, causing the options object to // become a "DeadObject" after the options page is closed. The workaround used // here is to create a deep copy of the options object. - const yomichan = chrome.extension.getBackgroundPage().yomichan; - yomichan.options = JSON.parse(JSON.stringify(options)); - - if (!options.general.enable) { - chrome.browserAction.setBadgeBackgroundColor({color: '#d9534f'}); - chrome.browserAction.setBadgeText({text: 'off'}); - } else if (!dictConfigured(options)) { - chrome.browserAction.setBadgeBackgroundColor({color: '#f0ad4e'}); - chrome.browserAction.setBadgeText({text: '!'}); - } else { - chrome.browserAction.setBadgeText({text: ''}); - } - - if (options.anki.enable) { - yomichan.anki = new AnkiConnect(options.anki.server); - } else { - yomichan.anki = new AnkiNull(); - } - - chrome.tabs.query({}, tabs => { - for (const tab of tabs) { - chrome.tabs.sendMessage(tab.id, {action: 'optionsSet', params: options}, () => null); - } - }); + backend().optionsSet(JSON.parse(JSON.stringify(options))); } async function apiOptionsGet() { - return chrome.extension.getBackgroundPage().yomichan.options; + return backend().options; } async function apiTermsFind(text) { - const yomichan = chrome.extension.getBackgroundPage().yomichan; - const options = yomichan.options; - const translator = yomichan.translator; + const options = backend().options; + const translator = backend().translator; const searcher = options.general.groupResults ? translator.findTermsGrouped.bind(translator) : @@ -101,15 +63,13 @@ async function apiTermsFind(text) { } async function apiKanjiFind(text) { - const yomichan = chrome.extension.getBackgroundPage().yomichan; - const options = yomichan.options; - const definitions = await yomichan.translator.findKanji(text, dictEnabledSet(options)); + const options = backend().options; + const definitions = await backend().translator.findKanji(text, dictEnabledSet(options)); return definitions.slice(0, options.general.maxResults); } async function apiDefinitionAdd(definition, mode) { - const yomichan = chrome.extension.getBackgroundPage().yomichan; - const options = yomichan.options; + const options = backend().options; if (mode !== 'kanji') { await audioInject( @@ -119,21 +79,18 @@ async function apiDefinitionAdd(definition, mode) { ); } - return yomichan.anki.addNote(dictNoteFormat(definition, mode, options)); + return backend().anki.addNote(dictNoteFormat(definition, mode, options)); } async function apiDefinitionsAddable(definitions, modes) { - const yomichan = chrome.extension.getBackgroundPage().yomichan; - const options = yomichan.options; - const notes = []; for (const definition of definitions) { for (const mode of modes) { - notes.push(dictNoteFormat(definition, mode, options)); + notes.push(dictNoteFormat(definition, mode, backend().options)); } } - const results = await yomichan.anki.canAddNotes(notes); + const results = await backend().anki.canAddNotes(notes); const states = []; for (let resultBase = 0; resultBase < results.length; resultBase += modes.length) { const state = {}; @@ -148,10 +105,38 @@ async function apiDefinitionsAddable(definitions, modes) { } async function apiNoteView(noteId) { - const yomichan = chrome.extension.getBackgroundPage().yomichan; - return yomichan.anki.guiBrowse(`nid:${noteId}`); + return backend().anki.guiBrowse(`nid:${noteId}`); } async function apiTemplateRender(template, data) { return handlebarsRender(template, data); } + +async function apiCommandExec(command) { + const handlers = { + search: () => { + chrome.tabs.create({url: chrome.extension.getURL('/bg/search.html')}); + }, + + help: () => { + chrome.tabs.create({url: 'https://foosoft.net/projects/yomichan/'}); + }, + + options: () => { + chrome.runtime.openOptionsPage(); + }, + + toggle: async () => { + const options = backend().options; + options.general.enable = !options.general.enable; + await optionsSave(options); + await apiOptionsSet(options); + } + }; + + const handler = handlers[command]; + if (handler) { + handler(); + } +} + diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index 2f60b4b7..6b345e6d 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -17,67 +17,102 @@ */ -window.yomichan = new class { +window.yomichan_backend = new class { constructor() { this.translator = new Translator(); this.anki = new AnkiNull(); this.options = null; + } - this.translator.prepare().then(optionsLoad).then(options => { - apiOptionsSet(options); - - chrome.commands.onCommand.addListener(apiCommandExec); - chrome.runtime.onMessage.addListener(({action, params}, sender, callback) => { - const forward = (promise, callback) => { - return promise.then(result => { - callback({result}); - }).catch(error => { - callback({error}); - }); - }; - - const handlers = { - optionsGet: ({callback}) => { - forward(optionsLoad(), callback); - }, - - kanjiFind: ({text, callback}) => { - forward(apiKanjiFind(text), callback); - }, - - termsFind: ({text, callback}) => { - forward(apiTermsFind(text), callback); - }, - - templateRender: ({template, data, callback}) => { - forward(apiTemplateRender(template, data), callback); - }, - - definitionAdd: ({definition, mode, callback}) => { - forward(apiDefinitionAdd(definition, mode), callback); - }, - - definitionsAddable: ({definitions, modes, callback}) => { - forward(apiDefinitionsAddable(definitions, modes), callback); - }, - - noteView: ({noteId}) => { - forward(apiNoteView(noteId), callback); - } - }; - - const handler = handlers[action]; - if (handler) { - params.callback = callback; - handler(params); - } - - return true; - }); + async prepare() { + await this.translator.prepare(); + await apiOptionsSet(await optionsLoad()); + + chrome.commands.onCommand.addListener(this.onCommand.bind(this)); + chrome.runtime.onMessage.addListener(this.onMessage.bind(this)); + + if (this.options.general.showGuide) { + chrome.tabs.create({url: chrome.extension.getURL('/bg/guide.html')}); + } + } + + optionsSet(options) { + this.options = options; - if (options.general.showGuide) { - chrome.tabs.create({url: chrome.extension.getURL('/bg/guide.html')}); + if (!options.general.enable) { + chrome.browserAction.setBadgeBackgroundColor({color: '#d9534f'}); + chrome.browserAction.setBadgeText({text: 'off'}); + } else if (!dictConfigured(options)) { + chrome.browserAction.setBadgeBackgroundColor({color: '#f0ad4e'}); + chrome.browserAction.setBadgeText({text: '!'}); + } else { + chrome.browserAction.setBadgeText({text: ''}); + } + + if (options.anki.enable) { + backend().anki = new AnkiConnect(options.anki.server); + } else { + backend().anki = new AnkiNull(); + } + + chrome.tabs.query({}, tabs => { + for (const tab of tabs) { + chrome.tabs.sendMessage(tab.id, {action: 'optionsSet', params: options}, () => null); } }); } + + onCommand(command) { + apiCommandExec(command); + } + + onMessage({action, params}, sender, callback) { + const forward = (promise, callback) => { + return promise.then(result => { + callback({result}); + }).catch(error => { + callback({error}); + }); + }; + + const handlers = { + optionsGet: ({callback}) => { + forward(optionsLoad(), callback); + }, + + kanjiFind: ({text, callback}) => { + forward(apiKanjiFind(text), callback); + }, + + termsFind: ({text, callback}) => { + forward(apiTermsFind(text), callback); + }, + + templateRender: ({template, data, callback}) => { + forward(apiTemplateRender(template, data), callback); + }, + + definitionAdd: ({definition, mode, callback}) => { + forward(apiDefinitionAdd(definition, mode), callback); + }, + + definitionsAddable: ({definitions, modes, callback}) => { + forward(apiDefinitionsAddable(definitions, modes), callback); + }, + + noteView: ({noteId}) => { + forward(apiNoteView(noteId), callback); + } + }; + + const handler = handlers[action]; + if (handler) { + params.callback = callback; + handler(params); + } + + return true; + } }; + +window.yomichan_backend.prepare(); diff --git a/ext/bg/js/settings.js b/ext/bg/js/settings.js index fa44c8da..1edbab01 100644 --- a/ext/bg/js/settings.js +++ b/ext/bg/js/settings.js @@ -22,33 +22,27 @@ */ function utilAnkiGetModelNames() { - const yomichan = chrome.extension.getBackgroundPage().yomichan; - return yomichan.anki.getModelNames(); + return backend().anki.getModelNames(); } function utilAnkiGetDeckNames() { - const yomichan = chrome.extension.getBackgroundPage().yomichan; - return yomichan.anki.getDeckNames(); + return backend().anki.getDeckNames(); } function utilAnkiGetModelFieldNames(modelName) { - const yomichan = chrome.extension.getBackgroundPage().yomichan; - return yomichan.anki.getModelFieldNames(modelName); + return backend().anki.getModelFieldNames(modelName); } function utilDatabaseGetDictionaries() { - const yomichan = chrome.extension.getBackgroundPage().yomichan; - return yomichan.translator.database.getDictionaries(); + return backend().translator.database.getDictionaries(); } function utilDatabasePurge() { - const yomichan = chrome.extension.getBackgroundPage().yomichan; - return yomichan.translator.database.purge(); + return backend().translator.database.purge(); } function utilDatabaseImport(data, progress) { - const yomichan = chrome.extension.getBackgroundPage().yomichan; - return yomichan.translator.database.importDictionary(data, progress); + return backend().translator.database.importDictionary(data, progress); } -- cgit v1.2.3 From dfecef1f23435c933237794de6665f6fbacf90ec Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sat, 5 Aug 2017 19:11:06 -0700 Subject: more cleanup --- ext/bg/js/api.js | 2 +- ext/bg/js/backend.js | 2 +- ext/bg/js/display-window.js | 19 +++++++++++-------- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/ext/bg/js/api.js b/ext/bg/js/api.js index 9dbb69c1..bc2693e5 100644 --- a/ext/bg/js/api.js +++ b/ext/bg/js/api.js @@ -35,7 +35,7 @@ async function apiOptionsSet(options) { // to the DOM across to the background page, causing the options object to // become a "DeadObject" after the options page is closed. The workaround used // here is to create a deep copy of the options object. - backend().optionsSet(JSON.parse(JSON.stringify(options))); + backend().onOptionsUpdated(JSON.parse(JSON.stringify(options))); } async function apiOptionsGet() { diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index 6b345e6d..1c058433 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -36,7 +36,7 @@ window.yomichan_backend = new class { } } - optionsSet(options) { + onOptionsUpdated(options) { this.options = options; if (!options.general.enable) { diff --git a/ext/bg/js/display-window.js b/ext/bg/js/display-window.js index 1f607510..52c0cafa 100644 --- a/ext/bg/js/display-window.js +++ b/ext/bg/js/display-window.js @@ -21,12 +21,11 @@ window.displayWindow = new class extends Display { constructor() { super($('#spinner'), $('#content')); - const search = $('#search'); - search.click(this.onSearch.bind(this)); + this.search = $('#search').click(this.onSearch.bind(this)); + this.query = $('#query').on('input', this.inputSearch.bind(this)); + this.intro = $('#intro'); - const query = $('#query'); - query.on('input', () => search.prop('disabled', query.val().length === 0)); - window.wanakana.bind(query.get(0)); + window.wanakana.bind(this.query.get(0)); } handleError(error) { @@ -34,15 +33,19 @@ window.displayWindow = new class extends Display { } clearSearch() { - $('#query').focus().select(); + this.query.focus().select(); + } + + inputSearch() { + this.search.prop('disabled', this.query.val().length === 0); } async onSearch(e) { e.preventDefault(); try { - $('#intro').slideUp(); - const {length, definitions} = await apiTermsFind($('#query').val()); + this.intro.slideUp(); + const {length, definitions} = await apiTermsFind(this.query.val()); super.showTermDefs(definitions, await apiOptionsGet()); } catch (e) { this.handleError(e); -- cgit v1.2.3 From 7e635d6382b0d96f596e2440b93e5935230367aa Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sat, 5 Aug 2017 19:23:17 -0700 Subject: more cleanup --- ext/bg/background.html | 1 + ext/bg/js/api.js | 37 ++++++++++++------------------------- ext/bg/js/backend.js | 4 ++-- ext/bg/js/settings.js | 33 --------------------------------- ext/bg/js/util.js | 46 ++++++++++++++++++++++++++++++++++++++++++++++ ext/bg/settings.html | 1 + 6 files changed, 62 insertions(+), 60 deletions(-) create mode 100644 ext/bg/js/util.js diff --git a/ext/bg/background.html b/ext/bg/background.html index 1e9f3809..40f37b11 100644 --- a/ext/bg/background.html +++ b/ext/bg/background.html @@ -19,6 +19,7 @@ + diff --git a/ext/bg/js/api.js b/ext/bg/js/api.js index bc2693e5..024ba75e 100644 --- a/ext/bg/js/api.js +++ b/ext/bg/js/api.js @@ -17,34 +17,21 @@ */ -/* - * Backend - */ - -function backend() { - return chrome.extension.getBackgroundPage().yomichan_backend; -} - - -/* - * API - */ - async function apiOptionsSet(options) { // In Firefox, setting options from the options UI somehow carries references // to the DOM across to the background page, causing the options object to // become a "DeadObject" after the options page is closed. The workaround used // here is to create a deep copy of the options object. - backend().onOptionsUpdated(JSON.parse(JSON.stringify(options))); + utilBackend().onOptionsUpdated(JSON.parse(JSON.stringify(options))); } async function apiOptionsGet() { - return backend().options; + return utilBackend().options; } async function apiTermsFind(text) { - const options = backend().options; - const translator = backend().translator; + const options = utilBackend().options; + const translator = utilBackend().translator; const searcher = options.general.groupResults ? translator.findTermsGrouped.bind(translator) : @@ -63,13 +50,13 @@ async function apiTermsFind(text) { } async function apiKanjiFind(text) { - const options = backend().options; - const definitions = await backend().translator.findKanji(text, dictEnabledSet(options)); + const options = utilBackend().options; + const definitions = await utilBackend().translator.findKanji(text, dictEnabledSet(options)); return definitions.slice(0, options.general.maxResults); } async function apiDefinitionAdd(definition, mode) { - const options = backend().options; + const options = utilBackend().options; if (mode !== 'kanji') { await audioInject( @@ -79,18 +66,18 @@ async function apiDefinitionAdd(definition, mode) { ); } - return backend().anki.addNote(dictNoteFormat(definition, mode, options)); + return utilBackend().anki.addNote(dictNoteFormat(definition, mode, options)); } async function apiDefinitionsAddable(definitions, modes) { const notes = []; for (const definition of definitions) { for (const mode of modes) { - notes.push(dictNoteFormat(definition, mode, backend().options)); + notes.push(dictNoteFormat(definition, mode, utilBackend().options)); } } - const results = await backend().anki.canAddNotes(notes); + const results = await utilBackend().anki.canAddNotes(notes); const states = []; for (let resultBase = 0; resultBase < results.length; resultBase += modes.length) { const state = {}; @@ -105,7 +92,7 @@ async function apiDefinitionsAddable(definitions, modes) { } async function apiNoteView(noteId) { - return backend().anki.guiBrowse(`nid:${noteId}`); + return utilBackend().anki.guiBrowse(`nid:${noteId}`); } async function apiTemplateRender(template, data) { @@ -127,7 +114,7 @@ async function apiCommandExec(command) { }, toggle: async () => { - const options = backend().options; + const options = utilBackend().options; options.general.enable = !options.general.enable; await optionsSave(options); await apiOptionsSet(options); diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index 1c058433..f61e9742 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -50,9 +50,9 @@ window.yomichan_backend = new class { } if (options.anki.enable) { - backend().anki = new AnkiConnect(options.anki.server); + this.anki = new AnkiConnect(options.anki.server); } else { - backend().anki = new AnkiNull(); + this.anki = new AnkiNull(); } chrome.tabs.query({}, tabs => { diff --git a/ext/bg/js/settings.js b/ext/bg/js/settings.js index 1edbab01..ce9e14a2 100644 --- a/ext/bg/js/settings.js +++ b/ext/bg/js/settings.js @@ -17,39 +17,6 @@ */ -/* - * Utilities - */ - -function utilAnkiGetModelNames() { - return backend().anki.getModelNames(); -} - -function utilAnkiGetDeckNames() { - return backend().anki.getDeckNames(); -} - -function utilAnkiGetModelFieldNames(modelName) { - return backend().anki.getModelFieldNames(modelName); -} - -function utilDatabaseGetDictionaries() { - return backend().translator.database.getDictionaries(); -} - -function utilDatabasePurge() { - return backend().translator.database.purge(); -} - -function utilDatabaseImport(data, progress) { - return backend().translator.database.importDictionary(data, progress); -} - - -/* - * General - */ - async function formRead() { const optionsOld = await optionsLoad(); const optionsNew = $.extend(true, {}, optionsOld); diff --git a/ext/bg/js/util.js b/ext/bg/js/util.js new file mode 100644 index 00000000..9dc57950 --- /dev/null +++ b/ext/bg/js/util.js @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2016 Alex Yatskov + * Author: Alex Yatskov + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +function utilBackend() { + return chrome.extension.getBackgroundPage().yomichan_backend; +} + +function utilAnkiGetModelNames() { + return utilBackend().anki.getModelNames(); +} + +function utilAnkiGetDeckNames() { + return utilBackend().anki.getDeckNames(); +} + +function utilAnkiGetModelFieldNames(modelName) { + return utilBackend().anki.getModelFieldNames(modelName); +} + +function utilDatabaseGetDictionaries() { + return utilBackend().translator.database.getDictionaries(); +} + +function utilDatabasePurge() { + return utilBackend().translator.database.purge(); +} + +function utilDatabaseImport(data, progress) { + return utilBackend().translator.database.importDictionary(data, progress); +} diff --git a/ext/bg/settings.html b/ext/bg/settings.html index 7833a21d..719c67a2 100644 --- a/ext/bg/settings.html +++ b/ext/bg/settings.html @@ -283,6 +283,7 @@ + -- cgit v1.2.3 From 7fbe2ddaf33bad05fb26aec759806e0f6ae250d2 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sat, 5 Aug 2017 20:20:22 -0700 Subject: more cleanup --- ext/fg/frame.html | 1 + ext/fg/js/api.js | 31 +++++++------------------------ ext/fg/js/util.js | 35 +++++++++++++++++++++++++++++++++++ ext/manifest.json | 8 +++++--- 4 files changed, 48 insertions(+), 27 deletions(-) create mode 100644 ext/fg/js/util.js diff --git a/ext/fg/frame.html b/ext/fg/frame.html index e0b9eac5..3fe42eb2 100644 --- a/ext/fg/frame.html +++ b/ext/fg/frame.html @@ -32,6 +32,7 @@ + diff --git a/ext/fg/js/api.js b/ext/fg/js/api.js index e252637e..b4d75c3c 100644 --- a/ext/fg/js/api.js +++ b/ext/fg/js/api.js @@ -17,47 +17,30 @@ */ -function apiInvoke(action, params={}) { - return new Promise((resolve, reject) => { - try { - chrome.runtime.sendMessage({action, params}, ({result, error}) => { - if (error) { - reject(error); - } else { - resolve(result); - } - }); - } catch (e) { - window.yomichanOrphaned = true; - reject(e.message); - } - }); -} - function apiOptionsGet() { - return apiInvoke('optionsGet'); + return utilInvoke('optionsGet'); } function apiTermsFind(text) { - return apiInvoke('termsFind', {text}); + return utilInvoke('termsFind', {text}); } function apiKanjiFind(text) { - return apiInvoke('kanjiFind', {text}); + return utilInvoke('kanjiFind', {text}); } function apiTemplateRender(template, data) { - return apiInvoke('templateRender', {data, template}); + return utilInvoke('templateRender', {data, template}); } function apiDefinitionsAddable(definitions, modes) { - return apiInvoke('definitionsAddable', {definitions, modes}).catch(() => null); + return utilInvoke('definitionsAddable', {definitions, modes}).catch(() => null); } function apiDefinitionAdd(definition, mode) { - return apiInvoke('definitionAdd', {definition, mode}); + return utilInvoke('definitionAdd', {definition, mode}); } function apiNoteView(noteId) { - return apiInvoke('noteView', {noteId}); + return utilInvoke('noteView', {noteId}); } diff --git a/ext/fg/js/util.js b/ext/fg/js/util.js new file mode 100644 index 00000000..311fc065 --- /dev/null +++ b/ext/fg/js/util.js @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2016 Alex Yatskov + * Author: Alex Yatskov + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +function utilInvoke(action, params={}) { + return new Promise((resolve, reject) => { + try { + chrome.runtime.sendMessage({action, params}, ({result, error}) => { + if (error) { + reject(error); + } else { + resolve(result); + } + }); + } catch (e) { + window.yomichanOrphaned = true; + reject(e.message); + } + }); +} diff --git a/ext/manifest.json b/ext/manifest.json index 288976f3..48308b17 100644 --- a/ext/manifest.json +++ b/ext/manifest.json @@ -15,11 +15,13 @@ "content_scripts": [{ "matches": ["http://*/*", "https://*/*", "file://*/*"], "js": [ - "fg/js/document.js", - "fg/js/source-range.js", - "fg/js/source-element.js", "fg/js/api.js", + "fg/js/document.js", "fg/js/popup.js", + "fg/js/source-element.js", + "fg/js/source-range.js", + "fg/js/util.js", + "fg/js/frontend.js" ], "css": ["fg/css/client.css"] -- cgit v1.2.3 From aac2a58b5f821c6f90c95bb19f3b0a755d5e1739 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sun, 13 Aug 2017 16:11:51 -0700 Subject: wip --- ext/bg/background.html | 1 + ext/bg/js/display-window.js | 14 +- ext/bg/search.html | 2 +- ext/fg/frame.html | 4 +- ext/fg/js/display-frame.js | 58 +++----- ext/fg/js/frontend.js | 141 +++++++++--------- ext/fg/js/util.js | 2 +- ext/mixed/js/display.js | 344 ++++++++++++++++++++++++-------------------- 8 files changed, 291 insertions(+), 275 deletions(-) diff --git a/ext/bg/background.html b/ext/bg/background.html index 40f37b11..fd3b7dd1 100644 --- a/ext/bg/background.html +++ b/ext/bg/background.html @@ -20,6 +20,7 @@ + diff --git a/ext/bg/js/display-window.js b/ext/bg/js/display-window.js index 52c0cafa..cbb96681 100644 --- a/ext/bg/js/display-window.js +++ b/ext/bg/js/display-window.js @@ -17,26 +17,26 @@ */ -window.displayWindow = new class extends Display { +window.yomichan_window = new class extends Display { constructor() { super($('#spinner'), $('#content')); this.search = $('#search').click(this.onSearch.bind(this)); - this.query = $('#query').on('input', this.inputSearch.bind(this)); + this.query = $('#query').on('input', this.onSearchInput.bind(this)); this.intro = $('#intro'); window.wanakana.bind(this.query.get(0)); } - handleError(error) { + onError(error) { window.alert(`Error: ${error}`); } - clearSearch() { + onSearchClear() { this.query.focus().select(); } - inputSearch() { + onSearchInput() { this.search.prop('disabled', this.query.val().length === 0); } @@ -46,9 +46,9 @@ window.displayWindow = new class extends Display { try { this.intro.slideUp(); const {length, definitions} = await apiTermsFind(this.query.val()); - super.showTermDefs(definitions, await apiOptionsGet()); + super.termsShow(definitions, await apiOptionsGet()); } catch (e) { - this.handleError(e); + this.onError(e); } } }; diff --git a/ext/bg/search.html b/ext/bg/search.html index 655d7819..fe44d74e 100644 --- a/ext/bg/search.html +++ b/ext/bg/search.html @@ -40,10 +40,10 @@ + - diff --git a/ext/fg/frame.html b/ext/fg/frame.html index 3fe42eb2..dda3ef06 100644 --- a/ext/fg/frame.html +++ b/ext/fg/frame.html @@ -18,9 +18,9 @@ -
    +
    -
    +

    Yomichan Updated!

    diff --git a/ext/fg/js/display-frame.js b/ext/fg/js/display-frame.js index 09bd9255..5ea376c2 100644 --- a/ext/fg/js/display-frame.js +++ b/ext/fg/js/display-frame.js @@ -17,65 +17,45 @@ */ -window.displayFrame = new class extends Display { +window.yomichan_frame = new class extends Display { constructor() { super($('#spinner'), $('#content')); $(window).on('message', this.onMessage.bind(this)); } - definitionAdd(definition, mode) { - return apiDefinitionAdd(definition, mode); - } - - definitionsAddable(definitions, modes) { - return apiDefinitionsAddable(definitions, modes); - } - - noteView(noteId) { - return apiNoteView(noteId); - } - - templateRender(template, data) { - return apiTemplateRender(template, data); - } - - kanjiFind(character) { - return apiKanjiFind(character); - } - - handleError(error) { - if (window.yomichanOrphaned) { - this.showOrphaned(); + onError(error) { + if (window.yomichan_orphaned) { + this.onOrphaned(); } else { window.alert(`Error: ${error}`); } } - clearSearch() { - window.parent.postMessage('popupClose', '*'); + onOrphaned() { + $('#definitions').hide(); + $('#error-orphaned').show(); } - selectionCopy() { - window.parent.postMessage('selectionCopy', '*'); + onSearchClear() { + window.parent.postMessage('popupClose', '*'); } - showOrphaned() { - $('#content').hide(); - $('#orphan').show(); + onSelectionCopy() { + window.parent.postMessage('selectionCopy', '*'); } onMessage(e) { const handlers = { - showTermDefs: ({definitions, options, context}) => { - this.showTermDefs(definitions, options, context); + termsShow: ({definitions, options, context}) => { + this.termsShow(definitions, options, context); }, - showKanjiDefs: ({definitions, options, context}) => { - this.showKanjiDefs(definitions, options, context); + kanjiShow: ({definitions, options, context}) => { + this.kanjiShow(definitions, options, context); }, - showOrphaned: () => { - this.showOrphaned(); + orphaned: () => { + this.onOrphaned(); } }; @@ -89,8 +69,8 @@ window.displayFrame = new class extends Display { onKeyDown(e) { const handlers = { 67: /* c */ () => { - if (e.ctrlKey && window.getSelection().toString() === '') { - this.selectionCopy(); + if (e.ctrlKey && !window.getSelection().toString()) { + this.onSelectionCopy(); return true; } } diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index 9974d878..37389766 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -17,7 +17,7 @@ */ -window.yomichanFrontend = new class { +window.yomichan_frontend = new class { constructor() { this.popup = new Popup(); this.popupTimer = null; @@ -27,17 +27,23 @@ window.yomichanFrontend = new class { this.lastTextSource = null; this.pendingLookup = false; this.options = null; + } + + async prepare() { + try { + this.options = await apiOptionsGet(); + } catch (e) { + this.onError(e); + } - apiOptionsGet().then(options => { - this.options = options; - window.addEventListener('mouseover', this.onMouseOver.bind(this)); - window.addEventListener('mousedown', this.onMouseDown.bind(this)); - window.addEventListener('mouseup', this.onMouseUp.bind(this)); - window.addEventListener('mousemove', this.onMouseMove.bind(this)); - window.addEventListener('resize', e => this.searchClear()); - window.addEventListener('message', this.onFrameMessage.bind(this)); - chrome.runtime.onMessage.addListener(this.onBgMessage.bind(this)); - }).catch(this.handleError.bind(this)); + window.addEventListener('message', this.onFrameMessage.bind(this)); + window.addEventListener('mousedown', this.onMouseDown.bind(this)); + window.addEventListener('mousemove', this.onMouseMove.bind(this)); + window.addEventListener('mouseover', this.onMouseOver.bind(this)); + window.addEventListener('mouseup', this.onMouseUp.bind(this)); + window.addEventListener('resize', this.onResize.bind(this)); + + chrome.runtime.onMessage.addListener(this.onBgMessage.bind(this)); } popupTimerSet(callback) { @@ -144,7 +150,11 @@ window.yomichanFrontend = new class { callback(); } - searchAt(point) { + onResize() { + this.onSearchClear(); + } + + async searchAt(point) { if (this.pendingLookup) { return; } @@ -160,70 +170,69 @@ window.yomichanFrontend = new class { } this.pendingLookup = true; - this.searchTerms(textSource).then(found => { - if (!found) { - return this.searchKanji(textSource); + + try { + if (!await this.searchTerms(textSource)) { + await this.searchKanji(textSource); } - }).catch(error => { - this.handleError(error, textSource); - }).then(() => { - docImposterDestroy(); - this.pendingLookup = false; - }); + } catch (e) { + this.onError(e); + } + + docImposterDestroy(); + this.pendingLookup = false; } - searchTerms(textSource) { + async searchTerms(textSource) { textSource.setEndOffset(this.options.scanning.length); - return apiTermsFind(textSource.text()).then(({definitions, length}) => { - if (definitions.length === 0) { - return false; - } else { - textSource.setEndOffset(length); - - const sentence = docSentenceExtract(textSource, this.options.anki.sentenceExt); - const url = window.location.href; - this.popup.showTermDefs( - textSource.getRect(), - definitions, - this.options, - {sentence, url} - ); - - this.lastTextSource = textSource; - if (this.options.scanning.selectText) { - textSource.select(); - } + const {definitions, length} = await apiTermsFind(textSource.text()); + if (definitions.length === 0) { + return false; + } - return true; - } - }); + textSource.setEndOffset(length); + + const sentence = docSentenceExtract(textSource, this.options.anki.sentenceExt); + const url = window.location.href; + this.popup.termsShow( + textSource.getRect(), + definitions, + this.options, + {sentence, url} + ); + + this.lastTextSource = textSource; + if (this.options.scanning.selectText) { + textSource.select(); + } + + return true; } - searchKanji(textSource) { + async searchKanji(textSource) { textSource.setEndOffset(1); - return apiKanjiFind(textSource.text()).then(definitions => { - if (definitions.length === 0) { - return false; - } else { - const sentence = docSentenceExtract(textSource, this.options.anki.sentenceExt); - const url = window.location.href; - this.popup.showKanjiDefs( - textSource.getRect(), - definitions, - this.options, - {sentence, url} - ); - - this.lastTextSource = textSource; - if (this.options.scanning.selectText) { - textSource.select(); - } + const definitions = await apiKanjiFind(textSource.text()); + if (definitions.length === 0) { + return false; + } - return true; - } - }); + const sentence = docSentenceExtract(textSource, this.options.anki.sentenceExt); + const url = window.location.href; + this.popup.showKanji( + textSource.getRect(), + definitions, + this.options, + {sentence, url} + ); + + this.lastTextSource = textSource; + if (this.options.scanning.selectText) { + textSource.select(); + } + + return true; } searchClear() { @@ -238,7 +247,7 @@ window.yomichanFrontend = new class { } handleError(error, textSource) { - if (window.yomichanOrphaned) { + if (window.yomichan_orphaned) { if (textSource && this.options.scanning.modifier !== 'none') { this.popup.showOrphaned(textSource.getRect(), this.options); } diff --git a/ext/fg/js/util.js b/ext/fg/js/util.js index 311fc065..afa895ba 100644 --- a/ext/fg/js/util.js +++ b/ext/fg/js/util.js @@ -28,7 +28,7 @@ function utilInvoke(action, params={}) { } }); } catch (e) { - window.yomichanOrphaned = true; + window.yomichan_orphaned = true; reject(e.message); } }); diff --git a/ext/mixed/js/display.js b/ext/mixed/js/display.js index f408fb25..97dd7d5c 100644 --- a/ext/mixed/js/display.js +++ b/ext/mixed/js/display.js @@ -32,171 +32,57 @@ class Display { $(document).keydown(this.onKeyDown.bind(this)); } - handleError(error) { + onError(error) { throw 'override me'; } - clearSearch() { + onSearchClear() { throw 'override me'; } - showTermDefs(definitions, options, context) { - window.focus(); - - this.spinner.hide(); - this.definitions = definitions; - this.options = options; - this.context = context; - - const sequence = ++this.sequence; - const params = { - definitions, - addable: options.anki.enable, - grouped: options.general.groupResults, - playback: options.general.audioSource !== 'disabled', - debug: options.general.debugInfo - }; - - if (context) { - for (const definition of definitions) { - if (context.sentence) { - definition.cloze = Display.clozeBuild(context.sentence, definition.source); - } - - definition.url = context.url; - } - } - - apiTemplateRender('terms.html', params).then(content => { - this.container.html(content); - this.entryScroll(context && context.index || 0); - - $('.action-add-note').click(this.onAddNote.bind(this)); - $('.action-view-note').click(this.onViewNote.bind(this)); - $('.action-play-audio').click(this.onPlayAudio.bind(this)); - $('.kanji-link').click(this.onKanjiLookup.bind(this)); - - return this.adderButtonsUpdate(['term-kanji', 'term-kana'], sequence); - }).catch(this.handleError.bind(this)); - } - - showKanjiDefs(definitions, options, context) { - window.focus(); - - this.spinner.hide(); - this.definitions = definitions; - this.options = options; - this.context = context; - - const sequence = ++this.sequence; - const params = { - definitions, - source: context && context.source, - addable: options.anki.enable, - debug: options.general.debugInfo - }; - - if (context) { - for (const definition of definitions) { - if (context.sentence) { - definition.cloze = Display.clozeBuild(context.sentence); - } - - definition.url = context.url; - } - } - - apiTemplateRender('kanji.html', params).then(content => { - this.container.html(content); - this.entryScroll(context && context.index || 0); - - $('.action-add-note').click(this.onAddNote.bind(this)); - $('.source-term').click(this.onSourceTerm.bind(this)); - - return this.adderButtonsUpdate(['kanji'], sequence); - }).catch(this.handleError.bind(this)); - } - - adderButtonsUpdate(modes, sequence) { - return apiDefinitionsAddable(this.definitions, modes).then(states => { - if (!states || sequence !== this.sequence) { - return; - } - - states.forEach((state, index) => { - for (const mode in state) { - const button = Display.adderButtonFind(index, mode); - if (state[mode]) { - button.removeClass('disabled'); - } else { - button.addClass('disabled'); - } - - button.removeClass('pending'); - } - }); - }); - } - - entryScroll(index, smooth) { - index = Math.min(index, this.definitions.length - 1); - index = Math.max(index, 0); - - $('.current').hide().eq(index).show(); - - const container = $('html,body').stop(); - const entry = $('.entry').eq(index); - const target = index === 0 ? 0 : entry.offset().top; - - if (smooth) { - container.animate({scrollTop: target}, 200); - } else { - container.scrollTop(target); - } - - this.index = index; - } - - onSourceTerm(e) { + onSourceTermView(e) { e.preventDefault(); this.sourceBack(); } - onKanjiLookup(e) { - e.preventDefault(); + async onKanjiLookup(e) { + try { + e.preventDefault(); - const link = $(e.target); - const context = { - source: { - definitions: this.definitions, - index: Display.entryIndexFind(link) + const link = $(e.target); + const context = { + source: { + definitions: this.definitions, + index: Display.entryIndexFind(link) + } + }; + + if (this.context) { + context.sentence = this.context.sentence; + context.url = this.context.url; } - }; - if (this.context) { - context.sentence = this.context.sentence; - context.url = this.context.url; + const kanjiDefs = await apiKanjiFind(link.text()); + this.kanjiShow(kanjiDefs, this.options, context); + } catch (e) { + this.onError(e); } - - apiKanjiFind(link.text()).then(kanjiDefs => { - this.showKanjiDefs(kanjiDefs, this.options, context); - }).catch(this.handleError.bind(this)); } - onPlayAudio(e) { + onAudioPlay(e) { e.preventDefault(); const index = Display.entryIndexFind($(e.currentTarget)); this.audioPlay(this.definitions[index]); } - onAddNote(e) { + onNoteAdd(e) { e.preventDefault(); const link = $(e.currentTarget); const index = Display.entryIndexFind(link); this.noteAdd(this.definitions[index], link.data('mode')); } - onViewNote(e) { + onNoteView(e) { e.preventDefault(); const link = $(e.currentTarget); const index = Display.entryIndexFind(link); @@ -220,48 +106,48 @@ class Display { const handlers = { 27: /* escape */ () => { - this.clearSearch(); + this.onSearchClear(); return true; }, 33: /* page up */ () => { if (e.altKey) { - this.entryScroll(this.index - 3, true); + this.entryScrollIntoView(this.index - 3, true); return true; } }, 34: /* page down */ () => { if (e.altKey) { - this.entryScroll(this.index + 3, true); + this.entryScrollIntoView(this.index + 3, true); return true; } }, 35: /* end */ () => { if (e.altKey) { - this.entryScroll(this.definitions.length - 1, true); + this.entryScrollIntoView(this.definitions.length - 1, true); return true; } }, 36: /* home */ () => { if (e.altKey) { - this.entryScroll(0, true); + this.entryScrollIntoView(0, true); return true; } }, 38: /* up */ () => { if (e.altKey) { - this.entryScroll(this.index - 1, true); + this.entryScrollIntoView(this.index - 1, true); return true; } }, 40: /* down */ () => { if (e.altKey) { - this.entryScroll(this.index + 1, true); + this.entryScrollIntoView(this.index + 1, true); return true; } }, @@ -317,6 +203,135 @@ class Display { } } + async termsShow(definitions, options, context) { + try { + window.focus(); + + this.definitions = definitions; + this.options = options; + this.context = context; + + const sequence = ++this.sequence; + const params = { + definitions, + addable: options.anki.enable, + grouped: options.general.groupResults, + playback: options.general.audioSource !== 'disabled', + debug: options.general.debugInfo + }; + + if (context) { + for (const definition of definitions) { + if (context.sentence) { + definition.cloze = Display.clozeBuild(context.sentence, definition.source); + } + + definition.url = context.url; + } + } + + const content = await apiTemplateRender('terms.html', params); + this.container.html(content); + this.entryScrollIntoView(context && context.index || 0); + + $('.action-add-note').click(this.onNoteAdd.bind(this)); + $('.action-view-note').click(this.onNoteView.bind(this)); + $('.action-play-audio').click(this.onAudioPlay.bind(this)); + $('.kanji-link').click(this.onKanjiLookup.bind(this)); + + await this.adderButtonUpdate(['term-kanji', 'term-kana'], sequence); + } catch (e) { + this.onError(e); + } + } + + async kanjiShow(definitions, options, context) { + try { + window.focus(); + + this.definitions = definitions; + this.options = options; + this.context = context; + + const sequence = ++this.sequence; + const params = { + definitions, + source: context && context.source, + addable: options.anki.enable, + debug: options.general.debugInfo + }; + + if (context) { + for (const definition of definitions) { + if (context.sentence) { + definition.cloze = Display.clozeBuild(context.sentence); + } + + definition.url = context.url; + } + } + + const content = await apiTemplateRender('kanji.html', params); + this.container.html(content); + this.entryScrollIntoView(context && context.index || 0); + + $('.action-add-note').click(this.onNoteAdd.bind(this)); + $('.source-term').click(this.onSourceTermView.bind(this)); + + await this.adderButtonUpdate(['kanji'], sequence); + } catch (e) { + this.onError(e); + } + } + + async adderButtonUpdate(modes, sequence) { + try { + this.spinner.show(); + + const states = apiDefinitionsAddable(this.definitions, modes); + if (!states || sequence !== this.sequence) { + return; + } + + for (let i = 0; i < states.length; ++i) { + const state = states[i]; + for (const mode in state) { + const button = Display.adderButtonFind(i, mode); + if (state[mode]) { + button.removeClass('disabled'); + } else { + button.addClass('disabled'); + } + + button.removeClass('pending'); + } + } + } catch (e) { + this.onError(e); + } finally { + this.spinner.hide(); + } + } + + entryScrollIntoView(index, smooth) { + index = Math.min(index, this.definitions.length - 1); + index = Math.max(index, 0); + + $('.current').hide().eq(index).show(); + + const container = $('html,body').stop(); + const entry = $('.entry').eq(index); + const target = index === 0 ? 0 : entry.offset().top; + + if (smooth) { + container.animate({scrollTop: target}, 200); + } else { + container.scrollTop(target); + } + + this.index = index; + } + sourceBack() { if (this.context && this.context.source) { const context = { @@ -325,35 +340,42 @@ class Display { index: this.context.source.index }; - this.showTermDefs(this.context.source.definitions, this.options, context); + this.termsShow(this.context.source.definitions, this.options, context); } } - noteAdd(definition, mode) { - this.spinner.show(); - return apiDefinitionAdd(definition, mode).then(noteId => { + async noteAdd(definition, mode) { + try { + this.spinner.show(); + + const noteId = await apiDefinitionAdd(definition, mode); if (noteId) { const index = this.definitions.indexOf(definition); Display.adderButtonFind(index, mode).addClass('disabled'); Display.viewerButtonFind(index).removeClass('pending disabled').data('noteId', noteId); } else { - this.handleError('note could not be added'); + throw 'note could note be added'; } - }).catch(this.handleError.bind(this)).then(() => this.spinner.hide()); + } catch (e) { + this.onError(e); + } finally { + this.spinner.hide(); + } } - audioPlay(definition) { - this.spinner.show(); - - for (const key in this.audioCache) { - this.audioCache[key].pause(); - } + async audioPlay(definition) { + try { + this.spinner.show(); - audioBuildUrl(definition, this.options.general.audioSource, this.responseCache).then(url => { + let url = await audioBuildUrl(definition, this.options.general.audioSource, this.responseCache); if (!url) { url = '/mixed/mp3/button.mp3'; } + for (const key in this.audioCache) { + this.audioCache[key].pause(); + } + let audio = this.audioCache[url]; if (audio) { audio.currentTime = 0; @@ -371,7 +393,11 @@ class Display { audio.play(); }; } - }).catch(this.handleError.bind(this)).then(() => this.spinner.hide()); + } catch (e) { + this.onError(e); + } finally { + this.spinner.hide(); + } } static clozeBuild(sentence, source) { -- cgit v1.2.3 From 3ca28a93746ed0860bf19ede83e3e9bac979bfb5 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sun, 13 Aug 2017 16:42:22 -0700 Subject: wip --- ext/fg/js/frontend.js | 102 ++++++++++++++++++++++++------------------------ ext/fg/js/popup.js | 105 ++++++++++++++++++++++++-------------------------- 2 files changed, 103 insertions(+), 104 deletions(-) diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index 37389766..de5fa953 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -32,29 +32,17 @@ window.yomichan_frontend = new class { async prepare() { try { this.options = await apiOptionsGet(); - } catch (e) { - this.onError(e); - } - - window.addEventListener('message', this.onFrameMessage.bind(this)); - window.addEventListener('mousedown', this.onMouseDown.bind(this)); - window.addEventListener('mousemove', this.onMouseMove.bind(this)); - window.addEventListener('mouseover', this.onMouseOver.bind(this)); - window.addEventListener('mouseup', this.onMouseUp.bind(this)); - window.addEventListener('resize', this.onResize.bind(this)); - chrome.runtime.onMessage.addListener(this.onBgMessage.bind(this)); - } - - popupTimerSet(callback) { - this.popupTimerClear(); - this.popupTimer = window.setTimeout(callback, this.options.scanning.delay); - } + window.addEventListener('message', e => this.onFrameMessage(e)); + window.addEventListener('mousedown', e => this.onMouseDown(e)); + window.addEventListener('mousemove', e => this.onMouseMove(e)); + window.addEventListener('mouseover', e => this.onMouseOver(e)); + window.addEventListener('mouseup', e => this.onMouseUp(e)); + window.addEventListener('resize', e => this.onResize(e)); - popupTimerClear() { - if (this.popupTimer) { - window.clearTimeout(this.popupTimer); - this.popupTimer = null; + chrome.runtime.onMessage.addListener(({action, params}, sender, callback) => this.onBgMessage(action, params, sender, callback)); + } catch (e) { + this.onError(e); } } @@ -132,7 +120,11 @@ window.yomichan_frontend = new class { } } - onBgMessage({action, params}, sender, callback) { + onResize() { + this.onSearchClear(); + } + + onBgMessage(action, params, sender, callback) { const handlers = { optionsSet: options => { this.options = options; @@ -150,37 +142,55 @@ window.yomichan_frontend = new class { callback(); } - onResize() { - this.onSearchClear(); + onError(error) { + if (window.yomichan_orphaned) { + if (this.lastTextSource && this.options.scanning.modifier !== 'none') { + this.popup.showOrphaned(this.lastTextSource.getRect(), this.options); + } + } else { + window.alert(`Error: ${error}`); + } } - async searchAt(point) { - if (this.pendingLookup) { - return; - } + popupTimerSet(callback) { + this.popupTimerClear(); + this.popupTimer = window.setTimeout(callback, this.options.scanning.delay); + } - const textSource = docRangeFromPoint(point); - if (!textSource || !textSource.containsPoint(point)) { - docImposterDestroy(); - return; + popupTimerClear() { + if (this.popupTimer) { + window.clearTimeout(this.popupTimer); + this.popupTimer = null; } + } - if (this.lastTextSource && this.lastTextSource.equals(textSource)) { - return; - } + async searchAt(point) { + try { + if (this.pendingLookup) { + return; + } - this.pendingLookup = true; + const textSource = docRangeFromPoint(point); + if (!textSource || !textSource.containsPoint(point)) { + docImposterDestroy(); + return; + } + + if (this.lastTextSource && this.lastTextSource.equals(textSource)) { + return; + } + + this.pendingLookup = true; - try { if (!await this.searchTerms(textSource)) { await this.searchKanji(textSource); } } catch (e) { this.onError(e); + } finally { + docImposterDestroy(); + this.pendingLookup = false; } - - docImposterDestroy(); - this.pendingLookup = false; } async searchTerms(textSource) { @@ -245,14 +255,6 @@ window.yomichan_frontend = new class { this.lastTextSource = null; } +}(); - handleError(error, textSource) { - if (window.yomichan_orphaned) { - if (textSource && this.options.scanning.modifier !== 'none') { - this.popup.showOrphaned(textSource.getRect(), this.options); - } - } else { - window.alert(`Error: ${error}`); - } - } -}; +window.yomichan_frontend.prepare(); diff --git a/ext/fg/js/popup.js b/ext/fg/js/popup.js index cd7e846a..ba3289d4 100644 --- a/ext/fg/js/popup.js +++ b/ext/fg/js/popup.js @@ -40,51 +40,51 @@ class Popup { return this.injected; } - show(elementRect, options) { - return this.inject().then(() => { - const containerStyle = window.getComputedStyle(this.container); - const containerHeight = parseInt(containerStyle.height); - const containerWidth = parseInt(containerStyle.width); - - const limitX = document.body.clientWidth; - const limitY = window.innerHeight; - - let x = elementRect.left; - let width = Math.max(containerWidth, options.general.popupWidth); - const overflowX = Math.max(x + width - limitX, 0); - if (overflowX > 0) { - if (x >= overflowX) { - x -= overflowX; - } else { - width = limitX; - x = 0; - } - } + async show(elementRect, options) { + await this.inject(); + + const containerStyle = window.getComputedStyle(this.container); + const containerHeight = parseInt(containerStyle.height); + const containerWidth = parseInt(containerStyle.width); + + const limitX = document.body.clientWidth; + const limitY = window.innerHeight; - let y = 0; - let height = Math.max(containerHeight, options.general.popupHeight); - const yBelow = elementRect.bottom + options.general.popupOffset; - const yAbove = elementRect.top - options.general.popupOffset; - const overflowBelow = Math.max(yBelow + height - limitY, 0); - const overflowAbove = Math.max(height - yAbove, 0); - if (overflowBelow > 0 || overflowAbove > 0) { - if (overflowBelow < overflowAbove) { - height = Math.max(height - overflowBelow, 0); - y = yBelow; - } else { - height = Math.max(height - overflowAbove, 0); - y = Math.max(yAbove - height, 0); - } + let x = elementRect.left; + let width = Math.max(containerWidth, options.general.popupWidth); + const overflowX = Math.max(x + width - limitX, 0); + if (overflowX > 0) { + if (x >= overflowX) { + x -= overflowX; } else { + width = limitX; + x = 0; + } + } + + let y = 0; + let height = Math.max(containerHeight, options.general.popupHeight); + const yBelow = elementRect.bottom + options.general.popupOffset; + const yAbove = elementRect.top - options.general.popupOffset; + const overflowBelow = Math.max(yBelow + height - limitY, 0); + const overflowAbove = Math.max(height - yAbove, 0); + if (overflowBelow > 0 || overflowAbove > 0) { + if (overflowBelow < overflowAbove) { + height = Math.max(height - overflowBelow, 0); y = yBelow; + } else { + height = Math.max(height - overflowAbove, 0); + y = Math.max(yAbove - height, 0); } + } else { + y = yBelow; + } - this.container.style.left = `${x}px`; - this.container.style.top = `${y}px`; - this.container.style.width = `${width}px`; - this.container.style.height = `${height}px`; - this.container.style.visibility = 'visible'; - }); + this.container.style.left = `${x}px`; + this.container.style.top = `${y}px`; + this.container.style.width = `${width}px`; + this.container.style.height = `${height}px`; + this.container.style.visibility = 'visible'; } hide() { @@ -95,25 +95,22 @@ class Popup { return this.injected && this.container.style.visibility !== 'hidden'; } - showTermDefs(elementRect, definitions, options, context) { - this.show(elementRect, options).then(() => { - this.invokeApi('showTermDefs', {definitions, options, context}); - }); - } - - showKanjiDefs(elementRect, definitions, options, context) { - this.show(elementRect, options).then(() => { - this.invokeApi('showKanjiDefs', {definitions, options, context}); - }); + async termsShow(elementRect, definitions, options, context) { + await this.show(elementRect, options); + this.invokeApi('termsShow', {definitions, options, context}); } - showOrphaned(elementRect, options) { - this.show(elementRect, options).then(() => { - this.invokeApi('showOrphaned'); - }); + async kanjiShow(elementRect, definitions, options, context) { + await this.show(elementRect, options); + this.invokeApi('termsShow', {definitions, options, context}); } invokeApi(action, params={}) { this.container.contentWindow.postMessage({action, params}, '*'); } + + async onOrphaned(elementRect, options) { + await this.show(elementRect, options); + this.invokeApi('orphaned'); + } } -- cgit v1.2.3 From a202817b987e0af82607d814f775bde26947747a Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sun, 13 Aug 2017 20:50:43 -0700 Subject: wip --- ext/bg/popup.html | 3 ++- ext/fg/js/display-frame.js | 2 +- ext/fg/js/frontend.js | 7 ++++--- ext/mixed/css/frame.css | 2 +- 4 files changed, 8 insertions(+), 6 deletions(-) diff --git a/ext/bg/popup.html b/ext/bg/popup.html index 4113b008..60253d6f 100644 --- a/ext/bg/popup.html +++ b/ext/bg/popup.html @@ -35,8 +35,9 @@ - + + diff --git a/ext/fg/js/display-frame.js b/ext/fg/js/display-frame.js index 5ea376c2..e3f3e692 100644 --- a/ext/fg/js/display-frame.js +++ b/ext/fg/js/display-frame.js @@ -19,7 +19,7 @@ window.yomichan_frame = new class extends Display { constructor() { - super($('#spinner'), $('#content')); + super($('#spinner'), $('#definitions')); $(window).on('message', this.onMessage.bind(this)); } diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index de5fa953..005139e6 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -17,7 +17,7 @@ */ -window.yomichan_frontend = new class { +class Frontend { constructor() { this.popup = new Popup(); this.popupTimer = null; @@ -121,7 +121,7 @@ window.yomichan_frontend = new class { } onResize() { - this.onSearchClear(); + this.searchClear(); } onBgMessage(action, params, sender, callback) { @@ -255,6 +255,7 @@ window.yomichan_frontend = new class { this.lastTextSource = null; } -}(); +} +window.yomichan_frontend = new Frontend(); window.yomichan_frontend.prepare(); diff --git a/ext/mixed/css/frame.css b/ext/mixed/css/frame.css index a0b45fa4..8b1819bd 100644 --- a/ext/mixed/css/frame.css +++ b/ext/mixed/css/frame.css @@ -42,7 +42,7 @@ hr { right: 5px; } -#orphan { +#error-orphaned { display: none; } -- cgit v1.2.3 From 8b50dfe1e9a8be7b8d2a7c69b25bc04babfc1c0c Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sun, 13 Aug 2017 21:11:10 -0700 Subject: unify files --- ext/bg/background.html | 4 +- ext/bg/js/anki-connect.js | 69 ------------- ext/bg/js/anki-null.js | 44 --------- ext/bg/js/anki.js | 95 ++++++++++++++++++ ext/bg/js/backend.js | 5 +- ext/fg/js/source-element.js | 77 --------------- ext/fg/js/source-range.js | 176 --------------------------------- ext/fg/js/source.js | 236 ++++++++++++++++++++++++++++++++++++++++++++ ext/manifest.json | 3 +- 9 files changed, 336 insertions(+), 373 deletions(-) delete mode 100644 ext/bg/js/anki-connect.js delete mode 100644 ext/bg/js/anki-null.js create mode 100644 ext/bg/js/anki.js delete mode 100644 ext/fg/js/source-element.js delete mode 100644 ext/fg/js/source-range.js create mode 100644 ext/fg/js/source.js diff --git a/ext/bg/background.html b/ext/bg/background.html index fd3b7dd1..90ad9709 100644 --- a/ext/bg/background.html +++ b/ext/bg/background.html @@ -9,8 +9,7 @@ - - + @@ -20,7 +19,6 @@ - diff --git a/ext/bg/js/anki-connect.js b/ext/bg/js/anki-connect.js deleted file mode 100644 index 80c075fd..00000000 --- a/ext/bg/js/anki-connect.js +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Copyright (C) 2016 Alex Yatskov - * Author: Alex Yatskov - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - - -class AnkiConnect { - constructor(server) { - this.server = server; - this.localVersion = 2; - this.remoteVersion = 0; - } - - async addNote(note) { - await this.checkVersion(); - return await this.ankiInvoke('addNote', {note}); - } - - async canAddNotes(notes) { - await this.checkVersion(); - return await this.ankiInvoke('canAddNotes', {notes}); - } - - async getDeckNames() { - await this.checkVersion(); - return await this.ankiInvoke('deckNames'); - } - - async getModelNames() { - await this.checkVersion(); - return await this.ankiInvoke('modelNames'); - } - - async getModelFieldNames(modelName) { - await this.checkVersion(); - return await this.ankiInvoke('modelFieldNames', {modelName}); - } - - async guiBrowse(query) { - await this.checkVersion(); - return await this.ankiInvoke('guiBrowse', {query}); - } - - async checkVersion() { - if (this.remoteVersion < this.localVersion) { - this.remoteVersion = await this.ankiInvoke('version'); - if (this.remoteVersion < this.localVersion) { - throw 'extension and plugin versions incompatible'; - } - } - } - - ankiInvoke(action, params) { - return requestJson(this.server, 'POST', {action, params, version: this.localVersion}); - } -} diff --git a/ext/bg/js/anki-null.js b/ext/bg/js/anki-null.js deleted file mode 100644 index d82f0e68..00000000 --- a/ext/bg/js/anki-null.js +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright (C) 2016 Alex Yatskov - * Author: Alex Yatskov - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - - -class AnkiNull { - async addNote(note) { - return null; - } - - async canAddNotes(notes) { - return []; - } - - async getDeckNames() { - return []; - } - - async getModelNames() { - return []; - } - - async getModelFieldNames(modelName) { - return []; - } - - async guiBrowse(query) { - return []; - } -} diff --git a/ext/bg/js/anki.js b/ext/bg/js/anki.js new file mode 100644 index 00000000..f0ec4571 --- /dev/null +++ b/ext/bg/js/anki.js @@ -0,0 +1,95 @@ +/* + * Copyright (C) 2016 Alex Yatskov + * Author: Alex Yatskov + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +class AnkiConnect { + constructor(server) { + this.server = server; + this.localVersion = 2; + this.remoteVersion = 0; + } + + async addNote(note) { + await this.checkVersion(); + return await this.ankiInvoke('addNote', {note}); + } + + async canAddNotes(notes) { + await this.checkVersion(); + return await this.ankiInvoke('canAddNotes', {notes}); + } + + async getDeckNames() { + await this.checkVersion(); + return await this.ankiInvoke('deckNames'); + } + + async getModelNames() { + await this.checkVersion(); + return await this.ankiInvoke('modelNames'); + } + + async getModelFieldNames(modelName) { + await this.checkVersion(); + return await this.ankiInvoke('modelFieldNames', {modelName}); + } + + async guiBrowse(query) { + await this.checkVersion(); + return await this.ankiInvoke('guiBrowse', {query}); + } + + async checkVersion() { + if (this.remoteVersion < this.localVersion) { + this.remoteVersion = await this.ankiInvoke('version'); + if (this.remoteVersion < this.localVersion) { + throw 'extension and plugin versions incompatible'; + } + } + } + + ankiInvoke(action, params) { + return requestJson(this.server, 'POST', {action, params, version: this.localVersion}); + } +} + +class AnkiNull { + async addNote(note) { + return null; + } + + async canAddNotes(notes) { + return []; + } + + async getDeckNames() { + return []; + } + + async getModelNames() { + return []; + } + + async getModelFieldNames(modelName) { + return []; + } + + async guiBrowse(query) { + return []; + } +} diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index f61e9742..e8c9452c 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -17,7 +17,7 @@ */ -window.yomichan_backend = new class { +class Backend { constructor() { this.translator = new Translator(); this.anki = new AnkiNull(); @@ -113,6 +113,7 @@ window.yomichan_backend = new class { return true; } -}; +} +window.yomichan_backend = new Backend(); window.yomichan_backend.prepare(); diff --git a/ext/fg/js/source-element.js b/ext/fg/js/source-element.js deleted file mode 100644 index a8101382..00000000 --- a/ext/fg/js/source-element.js +++ /dev/null @@ -1,77 +0,0 @@ -/* - * Copyright (C) 2016 Alex Yatskov - * Author: Alex Yatskov - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - - -class TextSourceElement { - constructor(element, content='') { - this.element = element; - this.content = content; - } - - clone() { - return new TextSourceElement(this.element, this.content); - } - - text() { - return this.content; - } - - setEndOffset(length) { - switch (this.element.nodeName) { - case 'BUTTON': - this.content = this.element.innerHTML; - break; - case 'IMG': - this.content = this.element.getAttribute('alt'); - break; - default: - this.content = this.element.value; - break; - } - - this.content = this.content || ''; - this.content = this.content.substring(0, length); - - return this.content.length; - } - - setStartOffset(length) { - return 0; - } - - containsPoint(point) { - const rect = this.getRect(); - return point.x >= rect.left && point.x <= rect.right; - } - - getRect() { - return this.element.getBoundingClientRect(); - } - - select() { - // NOP - } - - deselect() { - // NOP - } - - equals(other) { - return other.element === this.element && other.content === this.content; - } -} diff --git a/ext/fg/js/source-range.js b/ext/fg/js/source-range.js deleted file mode 100644 index fa73b0a4..00000000 --- a/ext/fg/js/source-range.js +++ /dev/null @@ -1,176 +0,0 @@ -/* - * Copyright (C) 2016 Alex Yatskov - * Author: Alex Yatskov - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - - -class TextSourceRange { - constructor(range, content='') { - this.range = range; - this.content = content; - } - - clone() { - return new TextSourceRange(this.range.cloneRange(), this.content); - } - - text() { - return this.content; - } - - setEndOffset(length) { - const state = TextSourceRange.seekForward(this.range.startContainer, this.range.startOffset, length); - this.range.setEnd(state.node, state.offset); - this.content = state.content; - return length - state.remainder; - } - - setStartOffset(length) { - const state = TextSourceRange.seekBackward(this.range.startContainer, this.range.startOffset, length); - this.range.setStart(state.node, state.offset); - this.content = state.content; - return length - state.remainder; - } - - containsPoint(point) { - const rect = this.getPaddedRect(); - return point.x >= rect.left && point.x <= rect.right; - } - - getRect() { - return this.range.getBoundingClientRect(); - } - - getPaddedRect() { - const range = this.range.cloneRange(); - const startOffset = range.startOffset; - const endOffset = range.endOffset; - const node = range.startContainer; - - range.setStart(node, Math.max(0, startOffset - 1)); - range.setEnd(node, Math.min(node.length, endOffset + 1)); - - return range.getBoundingClientRect(); - } - - select() { - const selection = window.getSelection(); - selection.removeAllRanges(); - selection.addRange(this.range); - } - - deselect() { - const selection = window.getSelection(); - selection.removeAllRanges(); - } - - equals(other) { - return other.range && other.range.compareBoundaryPoints(Range.START_TO_START, this.range) === 0; - } - - static shouldEnter(node) { - if (node.nodeType !== 1) { - return false; - } - - const skip = ['RT', 'SCRIPT', 'STYLE']; - if (skip.includes(node.nodeName)) { - return false; - } - - const style = window.getComputedStyle(node); - const hidden = - style.visibility === 'hidden' || - style.display === 'none' || - parseFloat(style.fontSize) === 0; - - return !hidden; - } - - static seekForward(node, offset, length) { - const state = {node, offset, remainder: length, content: ''}; - if (!TextSourceRange.seekForwardHelper(node, state)) { - return state; - } - - for (let current = node; current !== null; current = current.parentElement) { - for (let sibling = current.nextSibling; sibling !== null; sibling = sibling.nextSibling) { - if (!TextSourceRange.seekForwardHelper(sibling, state)) { - return state; - } - } - } - - return state; - } - - static seekForwardHelper(node, state) { - if (node.nodeType === 3 && node.parentElement && TextSourceRange.shouldEnter(node.parentElement)) { - const offset = state.node === node ? state.offset : 0; - const remaining = node.length - offset; - const consumed = Math.min(remaining, state.remainder); - state.content = state.content + node.nodeValue.substring(offset, offset + consumed); - state.node = node; - state.offset = offset + consumed; - state.remainder -= consumed; - } else if (TextSourceRange.shouldEnter(node)) { - for (let i = 0; i < node.childNodes.length; ++i) { - if (!TextSourceRange.seekForwardHelper(node.childNodes[i], state)) { - break; - } - } - } - - return state.remainder > 0; - } - - static seekBackward(node, offset, length) { - const state = {node, offset, remainder: length, content: ''}; - if (!TextSourceRange.seekBackwardHelper(node, state)) { - return state; - } - - for (let current = node; current !== null; current = current.parentElement) { - for (let sibling = current.previousSibling; sibling !== null; sibling = sibling.previousSibling) { - if (!TextSourceRange.seekBackwardHelper(sibling, state)) { - return state; - } - } - } - - return state; - } - - static seekBackwardHelper(node, state) { - if (node.nodeType === 3 && node.parentElement && TextSourceRange.shouldEnter(node.parentElement)) { - const offset = state.node === node ? state.offset : node.length; - const remaining = offset; - const consumed = Math.min(remaining, state.remainder); - state.content = node.nodeValue.substring(offset - consumed, offset) + state.content; - state.node = node; - state.offset = offset - consumed; - state.remainder -= consumed; - } else if (TextSourceRange.shouldEnter(node)) { - for (let i = node.childNodes.length - 1; i >= 0; --i) { - if (!TextSourceRange.seekBackwardHelper(node.childNodes[i], state)) { - break; - } - } - } - - return state.remainder > 0; - } -} diff --git a/ext/fg/js/source.js b/ext/fg/js/source.js new file mode 100644 index 00000000..210dda12 --- /dev/null +++ b/ext/fg/js/source.js @@ -0,0 +1,236 @@ +/* + * Copyright (C) 2016 Alex Yatskov + * Author: Alex Yatskov + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +class TextSourceRange { + constructor(range, content='') { + this.range = range; + this.content = content; + } + + clone() { + return new TextSourceRange(this.range.cloneRange(), this.content); + } + + text() { + return this.content; + } + + setEndOffset(length) { + const state = TextSourceRange.seekForward(this.range.startContainer, this.range.startOffset, length); + this.range.setEnd(state.node, state.offset); + this.content = state.content; + return length - state.remainder; + } + + setStartOffset(length) { + const state = TextSourceRange.seekBackward(this.range.startContainer, this.range.startOffset, length); + this.range.setStart(state.node, state.offset); + this.content = state.content; + return length - state.remainder; + } + + containsPoint(point) { + const rect = this.getPaddedRect(); + return point.x >= rect.left && point.x <= rect.right; + } + + getRect() { + return this.range.getBoundingClientRect(); + } + + getPaddedRect() { + const range = this.range.cloneRange(); + const startOffset = range.startOffset; + const endOffset = range.endOffset; + const node = range.startContainer; + + range.setStart(node, Math.max(0, startOffset - 1)); + range.setEnd(node, Math.min(node.length, endOffset + 1)); + + return range.getBoundingClientRect(); + } + + select() { + const selection = window.getSelection(); + selection.removeAllRanges(); + selection.addRange(this.range); + } + + deselect() { + const selection = window.getSelection(); + selection.removeAllRanges(); + } + + equals(other) { + return other.range && other.range.compareBoundaryPoints(Range.START_TO_START, this.range) === 0; + } + + static shouldEnter(node) { + if (node.nodeType !== 1) { + return false; + } + + const skip = ['RT', 'SCRIPT', 'STYLE']; + if (skip.includes(node.nodeName)) { + return false; + } + + const style = window.getComputedStyle(node); + const hidden = + style.visibility === 'hidden' || + style.display === 'none' || + parseFloat(style.fontSize) === 0; + + return !hidden; + } + + static seekForward(node, offset, length) { + const state = {node, offset, remainder: length, content: ''}; + if (!TextSourceRange.seekForwardHelper(node, state)) { + return state; + } + + for (let current = node; current !== null; current = current.parentElement) { + for (let sibling = current.nextSibling; sibling !== null; sibling = sibling.nextSibling) { + if (!TextSourceRange.seekForwardHelper(sibling, state)) { + return state; + } + } + } + + return state; + } + + static seekForwardHelper(node, state) { + if (node.nodeType === 3 && node.parentElement && TextSourceRange.shouldEnter(node.parentElement)) { + const offset = state.node === node ? state.offset : 0; + const remaining = node.length - offset; + const consumed = Math.min(remaining, state.remainder); + state.content = state.content + node.nodeValue.substring(offset, offset + consumed); + state.node = node; + state.offset = offset + consumed; + state.remainder -= consumed; + } else if (TextSourceRange.shouldEnter(node)) { + for (let i = 0; i < node.childNodes.length; ++i) { + if (!TextSourceRange.seekForwardHelper(node.childNodes[i], state)) { + break; + } + } + } + + return state.remainder > 0; + } + + static seekBackward(node, offset, length) { + const state = {node, offset, remainder: length, content: ''}; + if (!TextSourceRange.seekBackwardHelper(node, state)) { + return state; + } + + for (let current = node; current !== null; current = current.parentElement) { + for (let sibling = current.previousSibling; sibling !== null; sibling = sibling.previousSibling) { + if (!TextSourceRange.seekBackwardHelper(sibling, state)) { + return state; + } + } + } + + return state; + } + + static seekBackwardHelper(node, state) { + if (node.nodeType === 3 && node.parentElement && TextSourceRange.shouldEnter(node.parentElement)) { + const offset = state.node === node ? state.offset : node.length; + const remaining = offset; + const consumed = Math.min(remaining, state.remainder); + state.content = node.nodeValue.substring(offset - consumed, offset) + state.content; + state.node = node; + state.offset = offset - consumed; + state.remainder -= consumed; + } else if (TextSourceRange.shouldEnter(node)) { + for (let i = node.childNodes.length - 1; i >= 0; --i) { + if (!TextSourceRange.seekBackwardHelper(node.childNodes[i], state)) { + break; + } + } + } + + return state.remainder > 0; + } +} + + +class TextSourceElement { + constructor(element, content='') { + this.element = element; + this.content = content; + } + + clone() { + return new TextSourceElement(this.element, this.content); + } + + text() { + return this.content; + } + + setEndOffset(length) { + switch (this.element.nodeName) { + case 'BUTTON': + this.content = this.element.innerHTML; + break; + case 'IMG': + this.content = this.element.getAttribute('alt'); + break; + default: + this.content = this.element.value; + break; + } + + this.content = this.content || ''; + this.content = this.content.substring(0, length); + + return this.content.length; + } + + setStartOffset(length) { + return 0; + } + + containsPoint(point) { + const rect = this.getRect(); + return point.x >= rect.left && point.x <= rect.right; + } + + getRect() { + return this.element.getBoundingClientRect(); + } + + select() { + // NOP + } + + deselect() { + // NOP + } + + equals(other) { + return other.element === this.element && other.content === this.content; + } +} diff --git a/ext/manifest.json b/ext/manifest.json index 48308b17..ee3bd2ac 100644 --- a/ext/manifest.json +++ b/ext/manifest.json @@ -18,8 +18,7 @@ "fg/js/api.js", "fg/js/document.js", "fg/js/popup.js", - "fg/js/source-element.js", - "fg/js/source-range.js", + "fg/js/source.js", "fg/js/util.js", "fg/js/frontend.js" -- cgit v1.2.3 From 82863cd86156d48b9f18dc10a560bedb82641862 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Mon, 14 Aug 2017 19:55:04 -0700 Subject: renaming files --- ext/bg/context.html | 43 +++++++++++++ ext/bg/js/context.js | 31 ++++++++++ ext/bg/js/display-window.js | 54 ---------------- ext/bg/js/popup.js | 31 ---------- ext/bg/js/search.js | 56 +++++++++++++++++ ext/bg/popup.html | 43 ------------- ext/bg/search.html | 4 +- ext/fg/css/client.css | 2 +- ext/fg/float.html | 43 +++++++++++++ ext/fg/frame.html | 43 ------------- ext/fg/js/display-frame.js | 86 -------------------------- ext/fg/js/float.js | 88 ++++++++++++++++++++++++++ ext/fg/js/popup.js | 4 +- ext/manifest.json | 5 +- ext/mixed/css/display.css | 146 ++++++++++++++++++++++++++++++++++++++++++++ ext/mixed/css/frame.css | 146 -------------------------------------------- 16 files changed, 414 insertions(+), 411 deletions(-) create mode 100644 ext/bg/context.html create mode 100644 ext/bg/js/context.js delete mode 100644 ext/bg/js/display-window.js delete mode 100644 ext/bg/js/popup.js create mode 100644 ext/bg/js/search.js delete mode 100644 ext/bg/popup.html create mode 100644 ext/fg/float.html delete mode 100644 ext/fg/frame.html delete mode 100644 ext/fg/js/display-frame.js create mode 100644 ext/fg/js/float.js create mode 100644 ext/mixed/css/display.css delete mode 100644 ext/mixed/css/frame.css diff --git a/ext/bg/context.html b/ext/bg/context.html new file mode 100644 index 00000000..3828c9fe --- /dev/null +++ b/ext/bg/context.html @@ -0,0 +1,43 @@ + + + + + + + + + + +

    + +

    +

    +

    + + + +
    +

    + + + + + + + + + + + + + + diff --git a/ext/bg/js/context.js b/ext/bg/js/context.js new file mode 100644 index 00000000..77cb5166 --- /dev/null +++ b/ext/bg/js/context.js @@ -0,0 +1,31 @@ +/* + * Copyright (C) 2017 Alex Yatskov + * Author: Alex Yatskov + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +$(document).ready(() => { + $('#open-search').click(() => apiCommandExec('search')); + $('#open-options').click(() => apiCommandExec('options')); + $('#open-help').click(() => apiCommandExec('help')); + + optionsLoad().then(options => { + const toggle = $('#enable-search'); + toggle.prop('checked', options.general.enable).change(); + toggle.bootstrapToggle(); + toggle.change(() => apiCommandExec('toggle')); + }); +}); diff --git a/ext/bg/js/display-window.js b/ext/bg/js/display-window.js deleted file mode 100644 index cbb96681..00000000 --- a/ext/bg/js/display-window.js +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright (C) 2016 Alex Yatskov - * Author: Alex Yatskov - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - - -window.yomichan_window = new class extends Display { - constructor() { - super($('#spinner'), $('#content')); - - this.search = $('#search').click(this.onSearch.bind(this)); - this.query = $('#query').on('input', this.onSearchInput.bind(this)); - this.intro = $('#intro'); - - window.wanakana.bind(this.query.get(0)); - } - - onError(error) { - window.alert(`Error: ${error}`); - } - - onSearchClear() { - this.query.focus().select(); - } - - onSearchInput() { - this.search.prop('disabled', this.query.val().length === 0); - } - - async onSearch(e) { - e.preventDefault(); - - try { - this.intro.slideUp(); - const {length, definitions} = await apiTermsFind(this.query.val()); - super.termsShow(definitions, await apiOptionsGet()); - } catch (e) { - this.onError(e); - } - } -}; diff --git a/ext/bg/js/popup.js b/ext/bg/js/popup.js deleted file mode 100644 index 77cb5166..00000000 --- a/ext/bg/js/popup.js +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright (C) 2017 Alex Yatskov - * Author: Alex Yatskov - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - - -$(document).ready(() => { - $('#open-search').click(() => apiCommandExec('search')); - $('#open-options').click(() => apiCommandExec('options')); - $('#open-help').click(() => apiCommandExec('help')); - - optionsLoad().then(options => { - const toggle = $('#enable-search'); - toggle.prop('checked', options.general.enable).change(); - toggle.bootstrapToggle(); - toggle.change(() => apiCommandExec('toggle')); - }); -}); diff --git a/ext/bg/js/search.js b/ext/bg/js/search.js new file mode 100644 index 00000000..87f50c32 --- /dev/null +++ b/ext/bg/js/search.js @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2016 Alex Yatskov + * Author: Alex Yatskov + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +class DisplaySearch extends Display { + constructor() { + super($('#spinner'), $('#content')); + + this.search = $('#search').click(this.onSearch.bind(this)); + this.query = $('#query').on('input', this.onSearchInput.bind(this)); + this.intro = $('#intro'); + + window.wanakana.bind(this.query.get(0)); + } + + onError(error) { + window.alert(`Error: ${error}`); + } + + onSearchClear() { + this.query.focus().select(); + } + + onSearchInput() { + this.search.prop('disabled', this.query.val().length === 0); + } + + async onSearch(e) { + e.preventDefault(); + + try { + this.intro.slideUp(); + const {length, definitions} = await apiTermsFind(this.query.val()); + super.termsShow(definitions, await apiOptionsGet()); + } catch (e) { + this.onError(e); + } + } +} + +window.yomichan_search = new DisplaySearch(); diff --git a/ext/bg/popup.html b/ext/bg/popup.html deleted file mode 100644 index 60253d6f..00000000 --- a/ext/bg/popup.html +++ /dev/null @@ -1,43 +0,0 @@ - - - - - - - - - - -

    - -

    -

    -

    - - - -
    -

    - - - - - - - - - - - - - - diff --git a/ext/bg/search.html b/ext/bg/search.html index fe44d74e..d10530f9 100644 --- a/ext/bg/search.html +++ b/ext/bg/search.html @@ -5,7 +5,7 @@ Yomichan Search - +
    @@ -45,6 +45,6 @@ - + diff --git a/ext/fg/css/client.css b/ext/fg/css/client.css index 9f566480..b5b1f6bd 100644 --- a/ext/fg/css/client.css +++ b/ext/fg/css/client.css @@ -17,7 +17,7 @@ */ -iframe#yomichan-popup { +iframe#yomichan-float { all: initial; background-color: #fff; border: 1px solid #999; diff --git a/ext/fg/float.html b/ext/fg/float.html new file mode 100644 index 00000000..a3b66c92 --- /dev/null +++ b/ext/fg/float.html @@ -0,0 +1,43 @@ + + + + + + + + + + + +
    + +
    + +
    + +
    +
    +

    Yomichan Updated!

    +

    + The Yomichan extension has been updated to a new version! In order to continue + viewing definitions on this page you must reload this tab or restart your browser. +

    +
    +
    + + + + + + + + + + + + diff --git a/ext/fg/frame.html b/ext/fg/frame.html deleted file mode 100644 index dda3ef06..00000000 --- a/ext/fg/frame.html +++ /dev/null @@ -1,43 +0,0 @@ - - - - - - - - - - - -
    - -
    - -
    - -
    -
    -

    Yomichan Updated!

    -

    - The Yomichan extension has been updated to a new version! In order to continue - viewing definitions on this page you must reload this tab or restart your browser. -

    -
    -
    - - - - - - - - - - - - diff --git a/ext/fg/js/display-frame.js b/ext/fg/js/display-frame.js deleted file mode 100644 index e3f3e692..00000000 --- a/ext/fg/js/display-frame.js +++ /dev/null @@ -1,86 +0,0 @@ -/* - * Copyright (C) 2016 Alex Yatskov - * Author: Alex Yatskov - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - - -window.yomichan_frame = new class extends Display { - constructor() { - super($('#spinner'), $('#definitions')); - $(window).on('message', this.onMessage.bind(this)); - } - - onError(error) { - if (window.yomichan_orphaned) { - this.onOrphaned(); - } else { - window.alert(`Error: ${error}`); - } - } - - onOrphaned() { - $('#definitions').hide(); - $('#error-orphaned').show(); - } - - onSearchClear() { - window.parent.postMessage('popupClose', '*'); - } - - onSelectionCopy() { - window.parent.postMessage('selectionCopy', '*'); - } - - onMessage(e) { - const handlers = { - termsShow: ({definitions, options, context}) => { - this.termsShow(definitions, options, context); - }, - - kanjiShow: ({definitions, options, context}) => { - this.kanjiShow(definitions, options, context); - }, - - orphaned: () => { - this.onOrphaned(); - } - }; - - const {action, params} = e.originalEvent.data; - const handler = handlers[action]; - if (handler) { - handler(params); - } - } - - onKeyDown(e) { - const handlers = { - 67: /* c */ () => { - if (e.ctrlKey && !window.getSelection().toString()) { - this.onSelectionCopy(); - return true; - } - } - }; - - const handler = handlers[e.keyCode]; - if (handler && handler()) { - e.preventDefault(); - } else { - super.onKeyDown(e); - } - } -}; diff --git a/ext/fg/js/float.js b/ext/fg/js/float.js new file mode 100644 index 00000000..59293239 --- /dev/null +++ b/ext/fg/js/float.js @@ -0,0 +1,88 @@ +/* + * Copyright (C) 2016 Alex Yatskov + * Author: Alex Yatskov + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +class DisplayFloat extends Display { + constructor() { + super($('#spinner'), $('#definitions')); + $(window).on('message', e => this.onMessage(e)); + } + + onError(error) { + if (window.yomichan_orphaned) { + this.onOrphaned(); + } else { + window.alert(`Error: ${error}`); + } + } + + onOrphaned() { + $('#definitions').hide(); + $('#error-orphaned').show(); + } + + onSearchClear() { + window.parent.postMessage('popupClose', '*'); + } + + onSelectionCopy() { + window.parent.postMessage('selectionCopy', '*'); + } + + onMessage(e) { + const handlers = { + termsShow: ({definitions, options, context}) => { + this.termsShow(definitions, options, context); + }, + + kanjiShow: ({definitions, options, context}) => { + this.kanjiShow(definitions, options, context); + }, + + orphaned: () => { + this.onOrphaned(); + } + }; + + const {action, params} = e.originalEvent.data; + const handler = handlers[action]; + if (handler) { + handler(params); + } + } + + onKeyDown(e) { + const handlers = { + 67: /* c */ () => { + if (e.ctrlKey && !window.getSelection().toString()) { + this.onSelectionCopy(); + return true; + } + } + }; + + const handler = handlers[e.keyCode]; + if (handler && handler()) { + e.preventDefault(); + } else { + super.onKeyDown(e); + } + } +} + +window.yomichan_display = new DisplayFloat(); diff --git a/ext/fg/js/popup.js b/ext/fg/js/popup.js index ba3289d4..8e61169a 100644 --- a/ext/fg/js/popup.js +++ b/ext/fg/js/popup.js @@ -20,10 +20,10 @@ class Popup { constructor() { this.container = document.createElement('iframe'); - this.container.id = 'yomichan-popup'; + this.container.id = 'yomichan-float'; this.container.addEventListener('mousedown', e => e.stopPropagation()); this.container.addEventListener('scroll', e => e.stopPropagation()); - this.container.setAttribute('src', chrome.extension.getURL('/fg/frame.html')); + this.container.setAttribute('src', chrome.extension.getURL('/fg/float.html')); this.container.style.width = '0px'; this.container.style.height = '0px'; this.injected = null; diff --git a/ext/manifest.json b/ext/manifest.json index ee3bd2ac..a8b68bfb 100644 --- a/ext/manifest.json +++ b/ext/manifest.json @@ -7,7 +7,7 @@ "icons": {"16": "mixed/img/icon16.png", "48": "mixed/img/icon48.png", "128": "mixed/img/icon128.png"}, "browser_action": { "default_icon": {"19": "mixed/img/icon19.png", "38": "mixed/img/icon38.png"}, - "default_popup": "bg/popup.html" + "default_popup": "bg/context.html" }, "author": "Alex Yatskov", @@ -20,7 +20,6 @@ "fg/js/popup.js", "fg/js/source.js", "fg/js/util.js", - "fg/js/frontend.js" ], "css": ["fg/css/client.css"] @@ -48,7 +47,7 @@ "description": "Open search window" } }, - "web_accessible_resources": ["fg/frame.html"], + "web_accessible_resources": ["fg/float.html"], "applications": { "gecko": { "id": "yomichan-live@foosoft.net", diff --git a/ext/mixed/css/display.css b/ext/mixed/css/display.css new file mode 100644 index 00000000..8b1819bd --- /dev/null +++ b/ext/mixed/css/display.css @@ -0,0 +1,146 @@ +/* + * Copyright (C) 2016 Alex Yatskov + * Author: Alex Yatskov + * + * This program is free software: you can redistribute it and/or modify + * it under the entrys of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +/* + * Fonts + */ + +@font-face { + font-family: kanji-stroke-orders; + src: url('/mixed/ttf/kanji-stroke-orders.ttf'); +} + +/* + * General + */ + +hr { + padding: 0px; + margin: 0px; +} + +#spinner { + bottom: 5px; + display: none; + position: fixed; + right: 5px; +} + +#error-orphaned { + display: none; +} + + +/* + * Entries + */ + +.entry, .note { + padding-top: 10px; + padding-bottom: 10px; +} + +.tag-default { + background-color: #8a8a91; +} + +.tag-name { + background-color: #5cb85c; +} + +.tag-expression { + background-color: #f0ad4e; +} + +.tag-popular { + background-color: #0275d8; +} + +.tag-frequent { + background-color: #5bc0de; +} + +.tag-archaism { + background-color: #d9534f; +} + +.tag-dictionary { + background-color: #aa66cc; +} + +.actions .disabled { + pointer-events: none; + cursor: default; +} + +.actions .disabled img { + -webkit-filter: grayscale(100%); + opacity: 0.25; +} + +.actions .pending { + visibility: hidden; +} + +.actions { + display: inline-block; + float: right; +} + +.actions:after { + clear: both; + content: ''; + display: block; +} + +.expression { + display: inline-block; + font-size: 24px; +} + +.expression a { + border-bottom: 1px #777 dashed; + color: #333; + text-decoration: none; +} + +.reasons { + color: #777; + display: inline-block; +} + +.glossary ol, .glossary ul { + padding-left: 1.4em; +} + +.glossary li { + color: #777; +} + +.glossary-item { + color: #000; +} + +.glyph { + font-family: kanji-stroke-orders; + font-size: 120px; + line-height: 120px; + padding: 0.01em; + vertical-align: top; +} diff --git a/ext/mixed/css/frame.css b/ext/mixed/css/frame.css deleted file mode 100644 index 8b1819bd..00000000 --- a/ext/mixed/css/frame.css +++ /dev/null @@ -1,146 +0,0 @@ -/* - * Copyright (C) 2016 Alex Yatskov - * Author: Alex Yatskov - * - * This program is free software: you can redistribute it and/or modify - * it under the entrys of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - - -/* - * Fonts - */ - -@font-face { - font-family: kanji-stroke-orders; - src: url('/mixed/ttf/kanji-stroke-orders.ttf'); -} - -/* - * General - */ - -hr { - padding: 0px; - margin: 0px; -} - -#spinner { - bottom: 5px; - display: none; - position: fixed; - right: 5px; -} - -#error-orphaned { - display: none; -} - - -/* - * Entries - */ - -.entry, .note { - padding-top: 10px; - padding-bottom: 10px; -} - -.tag-default { - background-color: #8a8a91; -} - -.tag-name { - background-color: #5cb85c; -} - -.tag-expression { - background-color: #f0ad4e; -} - -.tag-popular { - background-color: #0275d8; -} - -.tag-frequent { - background-color: #5bc0de; -} - -.tag-archaism { - background-color: #d9534f; -} - -.tag-dictionary { - background-color: #aa66cc; -} - -.actions .disabled { - pointer-events: none; - cursor: default; -} - -.actions .disabled img { - -webkit-filter: grayscale(100%); - opacity: 0.25; -} - -.actions .pending { - visibility: hidden; -} - -.actions { - display: inline-block; - float: right; -} - -.actions:after { - clear: both; - content: ''; - display: block; -} - -.expression { - display: inline-block; - font-size: 24px; -} - -.expression a { - border-bottom: 1px #777 dashed; - color: #333; - text-decoration: none; -} - -.reasons { - color: #777; - display: inline-block; -} - -.glossary ol, .glossary ul { - padding-left: 1.4em; -} - -.glossary li { - color: #777; -} - -.glossary-item { - color: #000; -} - -.glyph { - font-family: kanji-stroke-orders; - font-size: 120px; - line-height: 120px; - padding: 0.01em; - vertical-align: top; -} -- cgit v1.2.3 From bdf231082f4b4ca7c4c90d8b0cd40b6c4201723d Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Mon, 14 Aug 2017 21:43:09 -0700 Subject: lots of fixes to backend --- ext/bg/context.html | 5 +---- ext/bg/js/anki.js | 9 +++++++++ ext/bg/js/api.js | 2 +- ext/bg/js/backend.js | 20 +++++++++++++------ ext/bg/js/context.js | 4 ++-- ext/bg/js/database.js | 2 +- ext/bg/js/deinflector.js | 2 +- ext/bg/js/dictionary.js | 2 +- ext/bg/js/handlebars.js | 2 +- ext/bg/js/search.js | 5 ++--- ext/bg/js/settings.js | 50 ++++++++++++++++++++++++------------------------ ext/bg/js/util.js | 7 ++++++- ext/bg/settings.html | 3 +-- ext/fg/js/api.js | 22 ++++++++++++++------- ext/fg/js/frontend.js | 2 +- ext/fg/js/popup.js | 2 +- ext/mixed/js/display.js | 9 +++++---- 17 files changed, 87 insertions(+), 61 deletions(-) diff --git a/ext/bg/context.html b/ext/bg/context.html index 3828c9fe..8a72acc7 100644 --- a/ext/bg/context.html +++ b/ext/bg/context.html @@ -30,13 +30,10 @@ - - - - + diff --git a/ext/bg/js/anki.js b/ext/bg/js/anki.js index f0ec4571..c327969f 100644 --- a/ext/bg/js/anki.js +++ b/ext/bg/js/anki.js @@ -17,6 +17,10 @@ */ +/* + * AnkiConnect + */ + class AnkiConnect { constructor(server) { this.server = server; @@ -68,6 +72,11 @@ class AnkiConnect { } } + +/* + * AnkiNull + */ + class AnkiNull { async addNote(note) { return null; diff --git a/ext/bg/js/api.js b/ext/bg/js/api.js index 024ba75e..b55e306f 100644 --- a/ext/bg/js/api.js +++ b/ext/bg/js/api.js @@ -1,5 +1,5 @@ /* - * Copyright (C) 2016 Alex Yatskov + * Copyright (C) 2016-2017 Alex Yatskov * Author: Alex Yatskov * * This program is free software: you can redistribute it and/or modify diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index e8c9452c..97e5602a 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -1,5 +1,5 @@ /* - * Copyright (C) 2016 Alex Yatskov + * Copyright (C) 2016-2017 Alex Yatskov * Author: Alex Yatskov * * This program is free software: you can redistribute it and/or modify @@ -77,7 +77,11 @@ class Backend { const handlers = { optionsGet: ({callback}) => { - forward(optionsLoad(), callback); + forward(apiOptionsGet(), callback); + }, + + optionsSet: ({options, callback}) => { + forward(apiOptionsSet(options), callback); }, kanjiFind: ({text, callback}) => { @@ -88,10 +92,6 @@ class Backend { forward(apiTermsFind(text), callback); }, - templateRender: ({template, data, callback}) => { - forward(apiTemplateRender(template, data), callback); - }, - definitionAdd: ({definition, mode, callback}) => { forward(apiDefinitionAdd(definition, mode), callback); }, @@ -102,6 +102,14 @@ class Backend { noteView: ({noteId}) => { forward(apiNoteView(noteId), callback); + }, + + templateRender: ({template, data, callback}) => { + forward(apiTemplateRender(template, data), callback); + }, + + commandExec: ({command, callback}) => { + forward(apiCommandExec(command), callback); } }; diff --git a/ext/bg/js/context.js b/ext/bg/js/context.js index 77cb5166..689d6863 100644 --- a/ext/bg/js/context.js +++ b/ext/bg/js/context.js @@ -17,7 +17,7 @@ */ -$(document).ready(() => { +$(document).ready(utilAsync(() => { $('#open-search').click(() => apiCommandExec('search')); $('#open-options').click(() => apiCommandExec('options')); $('#open-help').click(() => apiCommandExec('help')); @@ -28,4 +28,4 @@ $(document).ready(() => { toggle.bootstrapToggle(); toggle.change(() => apiCommandExec('toggle')); }); -}); +})); diff --git a/ext/bg/js/database.js b/ext/bg/js/database.js index b38e00db..e00cb7a3 100644 --- a/ext/bg/js/database.js +++ b/ext/bg/js/database.js @@ -1,5 +1,5 @@ /* - * Copyright (C) 2016 Alex Yatskov + * Copyright (C) 2016-2017 Alex Yatskov * Author: Alex Yatskov * * This program is free software: you can redistribute it and/or modify diff --git a/ext/bg/js/deinflector.js b/ext/bg/js/deinflector.js index 8b67761b..0abde99d 100644 --- a/ext/bg/js/deinflector.js +++ b/ext/bg/js/deinflector.js @@ -1,5 +1,5 @@ /* - * Copyright (C) 2016 Alex Yatskov + * Copyright (C) 2016-2017 Alex Yatskov * Author: Alex Yatskov * * This program is free software: you can redistribute it and/or modify diff --git a/ext/bg/js/dictionary.js b/ext/bg/js/dictionary.js index 6f9b30e4..c8d431b9 100644 --- a/ext/bg/js/dictionary.js +++ b/ext/bg/js/dictionary.js @@ -1,5 +1,5 @@ /* - * Copyright (C) 2016 Alex Yatskov + * Copyright (C) 2016-2017 Alex Yatskov * Author: Alex Yatskov * * This program is free software: you can redistribute it and/or modify diff --git a/ext/bg/js/handlebars.js b/ext/bg/js/handlebars.js index df98bef1..debb0690 100644 --- a/ext/bg/js/handlebars.js +++ b/ext/bg/js/handlebars.js @@ -1,5 +1,5 @@ /* - * Copyright (C) 2016 Alex Yatskov + * Copyright (C) 2016-2017 Alex Yatskov * Author: Alex Yatskov * * This program is free software: you can redistribute it and/or modify diff --git a/ext/bg/js/search.js b/ext/bg/js/search.js index 87f50c32..54cda8ec 100644 --- a/ext/bg/js/search.js +++ b/ext/bg/js/search.js @@ -1,5 +1,5 @@ /* - * Copyright (C) 2016 Alex Yatskov + * Copyright (C) 2016-2017 Alex Yatskov * Author: Alex Yatskov * * This program is free software: you can redistribute it and/or modify @@ -41,9 +41,8 @@ class DisplaySearch extends Display { } async onSearch(e) { - e.preventDefault(); - try { + e.preventDefault(); this.intro.slideUp(); const {length, definitions} = await apiTermsFind(this.query.val()); super.termsShow(definitions, await apiOptionsGet()); diff --git a/ext/bg/js/settings.js b/ext/bg/js/settings.js index ce9e14a2..89b1581f 100644 --- a/ext/bg/js/settings.js +++ b/ext/bg/js/settings.js @@ -1,5 +1,5 @@ /* - * Copyright (C) 2016 Alex Yatskov + * Copyright (C) 2016-2017 Alex Yatskov * Author: Alex Yatskov * * This program is free software: you can redistribute it and/or modify @@ -90,12 +90,12 @@ function formUpdateVisibility(options) { } } -async function onFormOptionsChanged(e) {(async () => { - if (!e.originalEvent && !e.isTrigger) { - return; - } - +async function onFormOptionsChanged(e) { try { + if (!e.originalEvent && !e.isTrigger) { + return; + } + ankiErrorShow(); ankiSpinnerShow(true); @@ -116,9 +116,9 @@ async function onFormOptionsChanged(e) {(async () => { } finally { ankiSpinnerShow(false); } -})();} +} -function onReady() {(async () => { +async function onReady() { const options = await optionsLoad(); $('#show-usage-guide').prop('checked', options.general.showGuide); @@ -139,16 +139,16 @@ function onReady() {(async () => { $('#scan-length').val(options.scanning.length); $('#scan-modifier-key').val(options.scanning.modifier); - $('#dict-purge').click(onDictionaryPurge); - $('#dict-file').change(onDictionaryImport); + $('#dict-purge').click(utilAsync(onDictionaryPurge)); + $('#dict-file').change(utilAsync(onDictionaryImport)); $('#anki-enable').prop('checked', options.anki.enable); $('#card-tags').val(options.anki.tags.join(' ')); $('#generate-html-cards').prop('checked', options.anki.htmlCards); $('#sentence-detection-extent').val(options.anki.sentenceExt); $('#interface-server').val(options.anki.server); - $('input, select').not('.anki-model').change(onFormOptionsChanged); - $('.anki-model').change(onAnkiModelChanged); + $('input, select').not('.anki-model').change(utilAsync(onFormOptionsChanged)); + $('.anki-model').change(utilAsync(onAnkiModelChanged)); try { await dictionaryGroupsPopulate(options); @@ -163,9 +163,9 @@ function onReady() {(async () => { } formUpdateVisibility(options); -})();} +} -$(document).ready(onReady); +$(document).ready(utilAsync(onReady)); /* @@ -237,7 +237,7 @@ async function dictionaryGroupsPopulate(options) { }); } -async function onDictionaryPurge(e) {(async () => { +async function onDictionaryPurge(e) { e.preventDefault(); const dictControls = $('#dict-importer, #dict-groups').hide(); @@ -261,9 +261,9 @@ async function onDictionaryPurge(e) {(async () => { dictControls.show(); dictProgress.hide(); } -})();} +} -function onDictionaryImport(e) {(async () => { +async function onDictionaryImport(e) { const dictFile = $('#dict-file'); const dictControls = $('#dict-importer').hide(); const dictProgress = $('#dict-import-progress').show(); @@ -291,7 +291,7 @@ function onDictionaryImport(e) {(async () => { dictControls.show(); dictProgress.hide(); } -})();} +} /* @@ -398,7 +398,7 @@ async function ankiFieldsPopulate(element, options) { container.append($(html)); } - tab.find('.anki-field-value').change(onFormOptionsChanged); + tab.find('.anki-field-value').change(utilAsync(onFormOptionsChanged)); tab.find('.marker-link').click(onAnkiMarkerClicked); } @@ -408,12 +408,12 @@ function onAnkiMarkerClicked(e) { $(link).closest('.input-group').find('.anki-field-value').val(`{${link.text}}`).trigger('change'); } -function onAnkiModelChanged(e) {(async () => { - if (!e.originalEvent) { - return; - } - +async function onAnkiModelChanged(e) { try { + if (!e.originalEvent) { + return; + } + ankiErrorShow(); ankiSpinnerShow(true); @@ -431,4 +431,4 @@ function onAnkiModelChanged(e) {(async () => { } finally { ankiSpinnerShow(false); } -})();} +} diff --git a/ext/bg/js/util.js b/ext/bg/js/util.js index 9dc57950..11ec23eb 100644 --- a/ext/bg/js/util.js +++ b/ext/bg/js/util.js @@ -1,5 +1,5 @@ /* - * Copyright (C) 2016 Alex Yatskov + * Copyright (C) 2016-2017 Alex Yatskov * Author: Alex Yatskov * * This program is free software: you can redistribute it and/or modify @@ -16,6 +16,11 @@ * along with this program. If not, see . */ +function utilAsync(func) { + return function(...args) { + func.apply(this, args); + }; +} function utilBackend() { return chrome.extension.getBackgroundPage().yomichan_backend; diff --git a/ext/bg/settings.html b/ext/bg/settings.html index 719c67a2..237750b3 100644 --- a/ext/bg/settings.html +++ b/ext/bg/settings.html @@ -276,8 +276,7 @@ - - + diff --git a/ext/fg/js/api.js b/ext/fg/js/api.js index b4d75c3c..174531ba 100644 --- a/ext/fg/js/api.js +++ b/ext/fg/js/api.js @@ -1,5 +1,5 @@ /* - * Copyright (C) 2016 Alex Yatskov + * Copyright (C) 2016-2017 Alex Yatskov * Author: Alex Yatskov * * This program is free software: you can redistribute it and/or modify @@ -17,6 +17,10 @@ */ +function apiOptionsSet(options) { + return utilInvoke('optionsSet', {options}); +} + function apiOptionsGet() { return utilInvoke('optionsGet'); } @@ -29,18 +33,22 @@ function apiKanjiFind(text) { return utilInvoke('kanjiFind', {text}); } -function apiTemplateRender(template, data) { - return utilInvoke('templateRender', {data, template}); +function apiDefinitionAdd(definition, mode) { + return utilInvoke('definitionAdd', {definition, mode}); } function apiDefinitionsAddable(definitions, modes) { return utilInvoke('definitionsAddable', {definitions, modes}).catch(() => null); } -function apiDefinitionAdd(definition, mode) { - return utilInvoke('definitionAdd', {definition, mode}); -} - function apiNoteView(noteId) { return utilInvoke('noteView', {noteId}); } + +function apiTemplateRender(template, data) { + return utilInvoke('templateRender', {data, template}); +} + +function apiCommandExec(command) { + return utilInvoke('commandExec', {command}); +} diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index 005139e6..cc4d99c8 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -230,7 +230,7 @@ class Frontend { const sentence = docSentenceExtract(textSource, this.options.anki.sentenceExt); const url = window.location.href; - this.popup.showKanji( + this.popup.kanjiShow( textSource.getRect(), definitions, this.options, diff --git a/ext/fg/js/popup.js b/ext/fg/js/popup.js index 8e61169a..8cb16b5a 100644 --- a/ext/fg/js/popup.js +++ b/ext/fg/js/popup.js @@ -102,7 +102,7 @@ class Popup { async kanjiShow(elementRect, definitions, options, context) { await this.show(elementRect, options); - this.invokeApi('termsShow', {definitions, options, context}); + this.invokeApi('kanjiShow', {definitions, options, context}); } invokeApi(action, params={}) { diff --git a/ext/mixed/js/display.js b/ext/mixed/js/display.js index 97dd7d5c..21748f5d 100644 --- a/ext/mixed/js/display.js +++ b/ext/mixed/js/display.js @@ -42,7 +42,7 @@ class Display { onSourceTermView(e) { e.preventDefault(); - this.sourceBack(); + this.sourceTermView(); } async onKanjiLookup(e) { @@ -154,7 +154,7 @@ class Display { 66: /* b */ () => { if (e.altKey) { - this.sourceBack(); + this.sourceTermView(); return true; } }, @@ -276,6 +276,7 @@ class Display { this.entryScrollIntoView(context && context.index || 0); $('.action-add-note').click(this.onNoteAdd.bind(this)); + $('.action-view-note').click(this.onNoteView.bind(this)); $('.source-term').click(this.onSourceTermView.bind(this)); await this.adderButtonUpdate(['kanji'], sequence); @@ -288,7 +289,7 @@ class Display { try { this.spinner.show(); - const states = apiDefinitionsAddable(this.definitions, modes); + const states = await apiDefinitionsAddable(this.definitions, modes); if (!states || sequence !== this.sequence) { return; } @@ -332,7 +333,7 @@ class Display { this.index = index; } - sourceBack() { + sourceTermView() { if (this.context && this.context.source) { const context = { url: this.context.source.url, -- cgit v1.2.3 From b1ec7cdddd6caab87f0f0cbc9d597d00fe5d5b55 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Mon, 14 Aug 2017 21:47:39 -0700 Subject: tweaks --- ext/bg/legal.html | 2 +- ext/bg/settings.html | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/ext/bg/legal.html b/ext/bg/legal.html index fe9a007c..a289281d 100644 --- a/ext/bg/legal.html +++ b/ext/bg/legal.html @@ -10,7 +10,7 @@

    Yomichan License

    -Copyright (C) 2016  Alex Yatskov
    +Copyright (C) 2016-2017  Alex Yatskov
     
     This program is free software: you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
    diff --git a/ext/bg/settings.html b/ext/bg/settings.html
    index 237750b3..218f9f7d 100644
    --- a/ext/bg/settings.html
    +++ b/ext/bg/settings.html
    @@ -283,8 +283,6 @@
             
             
             
    -        
    -        
     
             
         
    -- 
    cgit v1.2.3
    
    
    From c9623276e1dfdf6014d2bf791180e88e8df500dd Mon Sep 17 00:00:00 2001
    From: Alex Yatskov 
    Date: Mon, 14 Aug 2017 22:58:03 -0700
    Subject: change color for "off" state, fixes #67
    
    ---
     ext/bg/js/backend.js | 2 +-
     1 file changed, 1 insertion(+), 1 deletion(-)
    
    diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js
    index 97e5602a..6c77ba7e 100644
    --- a/ext/bg/js/backend.js
    +++ b/ext/bg/js/backend.js
    @@ -40,7 +40,7 @@ class Backend {
             this.options = options;
     
             if (!options.general.enable) {
    -            chrome.browserAction.setBadgeBackgroundColor({color: '#d9534f'});
    +            chrome.browserAction.setBadgeBackgroundColor({color: '#555555'});
                 chrome.browserAction.setBadgeText({text: 'off'});
             } else if (!dictConfigured(options)) {
                 chrome.browserAction.setBadgeBackgroundColor({color: '#f0ad4e'});
    -- 
    cgit v1.2.3
    
    
    From 61dde5b3b74030d7b01c195751a54e9dcacdb6bb Mon Sep 17 00:00:00 2001
    From: Alex Yatskov 
    Date: Mon, 14 Aug 2017 23:10:59 -0700
    Subject: upgrade to wanikana 2.2.3 (fixes #42)
    
    ---
     ext/mixed/js/japanese.js      | 11 ++++++++++-
     ext/mixed/lib/wanakana.min.js |  2 +-
     2 files changed, 11 insertions(+), 2 deletions(-)
    
    diff --git a/ext/mixed/js/japanese.js b/ext/mixed/js/japanese.js
    index 779e3d35..c11e955b 100644
    --- a/ext/mixed/js/japanese.js
    +++ b/ext/mixed/js/japanese.js
    @@ -27,5 +27,14 @@ function jpIsKana(c) {
     }
     
     function jpKatakanaToHiragana(text) {
    -    return wanakana._katakanaToHiragana(text);
    +    let result = '';
    +    for (const c of text) {
    +        if (wanakana.isKatakana(c)) {
    +            result += wanakana.toHiragana(c);
    +        } else {
    +            result += c;
    +        }
    +    }
    +
    +    return result;
     }
    diff --git a/ext/mixed/lib/wanakana.min.js b/ext/mixed/lib/wanakana.min.js
    index 6c954785..bfc36d56 100644
    --- a/ext/mixed/lib/wanakana.min.js
    +++ b/ext/mixed/lib/wanakana.min.js
    @@ -1 +1 @@
    -var wanakana,__indexOf=[].indexOf||function(a){for(var b=0,c=this.length;c>b;b++)if(b in this&&this[b]===a)return b;return-1};wanakana=wanakana||{},wanakana.version="1.3.9","function"==typeof define&&define.amd&&define("wanakana",[],function(){return wanakana}),wanakana.LOWERCASE_START=97,wanakana.LOWERCASE_END=122,wanakana.UPPERCASE_START=65,wanakana.UPPERCASE_END=90,wanakana.HIRAGANA_START=12353,wanakana.HIRAGANA_END=12438,wanakana.KATAKANA_START=12449,wanakana.KATAKANA_END=12538,wanakana.LOWERCASE_FULLWIDTH_START=65345,wanakana.LOWERCASE_FULLWIDTH_END=65370,wanakana.UPPERCASE_FULLWIDTH_START=65313,wanakana.UPPERCASE_FULLWIDTH_END=65338,wanakana.defaultOptions={useObseleteKana:!1,IMEMode:!1,convertKatakanaToUppercase:!1},wanakana.bind=function(a){return a.addEventListener("input",wanakana._onInput)},wanakana.unbind=function(a){return a.removeEventListener("input",wanakana._onInput)},wanakana._onInput=function(a){var b,c,d,e,f,g;if(b=a.target,f=b.selectionStart,g=b.value.length,d=wanakana._convertFullwidthCharsToASCII(b.value),c=wanakana.toKana(d,{IMEMode:!0}),d!==c){if(b.value=c,"number"==typeof b.selectionStart)return b.selectionStart=b.selectionEnd=b.value.length;if("undefined"!=typeof b.createTextRange)return b.focus(),e=b.createTextRange(),e.collapse(!1),e.select()}},wanakana._extend=function(a,b){var c;if(null==a)return b;for(c in b)null==a[c]&&null!=b[c]&&(a[c]=b[c]);return a},wanakana._isCharInRange=function(a,b,c){var d;return d=a.charCodeAt(0),d>=b&&c>=d},wanakana._isCharVowel=function(a,b){var c;return null==b&&(b=!0),c=b?/[aeiouy]/:/[aeiou]/,-1!==a.toLowerCase().charAt(0).search(c)},wanakana._isCharConsonant=function(a,b){var c;return null==b&&(b=!0),c=b?/[bcdfghjklmnpqrstvwxyz]/:/[bcdfghjklmnpqrstvwxz]/,-1!==a.toLowerCase().charAt(0).search(c)},wanakana._isCharKatakana=function(a){return wanakana._isCharInRange(a,wanakana.KATAKANA_START,wanakana.KATAKANA_END)},wanakana._isCharHiragana=function(a){return wanakana._isCharInRange(a,wanakana.HIRAGANA_START,wanakana.HIRAGANA_END)},wanakana._isCharKana=function(a){return wanakana._isCharHiragana(a)||wanakana._isCharKatakana(a)},wanakana._isCharNotKana=function(a){return!wanakana._isCharHiragana(a)&&!wanakana._isCharKatakana(a)},wanakana._convertFullwidthCharsToASCII=function(a){var b,c,d,e,f,g;for(c=a.split(""),e=f=0,g=c.length;g>f;e=++f)b=c[e],d=b.charCodeAt(0),wanakana._isCharInRange(b,wanakana.LOWERCASE_FULLWIDTH_START,wanakana.LOWERCASE_FULLWIDTH_END)&&(c[e]=String.fromCharCode(d-wanakana.LOWERCASE_FULLWIDTH_START+wanakana.LOWERCASE_START)),wanakana._isCharInRange(b,wanakana.UPPERCASE_FULLWIDTH_START,wanakana.UPPERCASE_FULLWIDTH_END)&&c[e](String.fromCharCode(d-wanakana.UPPERCASE_FULLWIDTH_START+wanakana.UPPERCASE_START));return c.join("")},wanakana._katakanaToHiragana=function(a){var b,c,d,e,f,g,h;for(c=[],h=a.split(""),f=0,g=h.length;g>f;f++)e=h[f],wanakana._isCharKatakana(e)?(b=e.charCodeAt(0),b+=wanakana.HIRAGANA_START-wanakana.KATAKANA_START,d=String.fromCharCode(b),c.push(d)):c.push(e);return c.join("")},wanakana._hiraganaToKatakana=function(a){var b,c,d,e,f,g,h;for(d=[],h=a.split(""),f=0,g=h.length;g>f;f++)c=h[f],wanakana._isCharHiragana(c)?(b=c.charCodeAt(0),b+=wanakana.KATAKANA_START-wanakana.HIRAGANA_START,e=String.fromCharCode(b),d.push(e)):d.push(c);return d.join("")},wanakana._hiraganaToRomaji=function(a,b){var c,d,e,f,g,h,i,j,k,l,m;for(b=wanakana._extend(b,wanakana.defaultOptions),h=a.length,l=[],f=0,d=0,i=2,g=function(){return a.substr(f,d)},k=function(){return d=Math.min(i,h-f)};h>f;){for(e=!1,k();d>0;){if(c=g(),wanakana.isKatakana(c)&&(e=b.convertKatakanaToUppercase,c=wanakana._katakanaToHiragana(c)),"っ"===c.charAt(0)&&1===d&&h-1>f){j=!0,m="";break}if(m=wanakana.J_to_R[c],null!=m&&j&&(m=m.charAt(0).concat(m),j=!1),null!=m)break;d--}null==m&&(m=c),e&&(m=m.toUpperCase()),l.push(m),f+=d||1}return l.join("")},wanakana._romajiToHiragana=function(a,b){return wanakana._romajiToKana(a,b,!0)},wanakana._romajiToKana=function(a,b,c){var d,e,f,g,h,i,j,k,l,m;for(null==c&&(c=!1),b=wanakana._extend(b,wanakana.defaultOptions),l=a.length,j=[],g=0,m=3,h=function(){return a.substr(g,f)},i=function(a){return wanakana._isCharInRange(a,wanakana.UPPERCASE_START,wanakana.UPPERCASE_END)};l>g;){for(f=Math.min(m,l-g);f>0;){if(d=h(),e=d.toLowerCase(),__indexOf.call(wanakana.FOUR_CHARACTER_EDGE_CASES,e)>=0&&l-g>=4)f++,d=h(),e=d.toLowerCase();else{if("n"===e.charAt(0)){if(2===f){if(!b.IMEMode&&" "===e.charAt(1)){k="ん ";break}if(b.IMEMode&&"'"===e.charAt(1)){k="ん";break}}wanakana._isCharConsonant(e.charAt(1),!1)&&wanakana._isCharVowel(e.charAt(2))&&(f=1,d=h(),e=d.toLowerCase())}"n"!==e.charAt(0)&&wanakana._isCharConsonant(e.charAt(0))&&d.charAt(0)===d.charAt(1)&&(f=1,e=d=wanakana._isCharInRange(d.charAt(0),wanakana.UPPERCASE_START,wanakana.UPPERCASE_END)?"ッ":"っ")}if(k=wanakana.R_to_J[e],null!=k)break;4===f?f-=2:f--}null==k&&(d=wanakana._convertPunctuation(d),k=d),(null!=b?b.useObseleteKana:void 0)&&("wi"===e&&(k="ゐ"),"we"===e&&(k="ゑ")),b.IMEMode&&"n"===e.charAt(0)&&("y"===a.charAt(g+1).toLowerCase()&&wanakana._isCharVowel(a.charAt(g+2))===!1||g===l-1||wanakana.isKana(a.charAt(g+1)))&&(k=d.charAt(0)),c||i(d.charAt(0))&&(k=wanakana._hiraganaToKatakana(k)),j.push(k),g+=f||1}return j.join("")},wanakana._convertPunctuation=function(a,b){return" "===a?" ":"-"===a?"ー":a},wanakana.isHiragana=function(a){var b;return b=a.split(""),b.every(wanakana._isCharHiragana)},wanakana.isKatakana=function(a){var b;return b=a.split(""),b.every(wanakana._isCharKatakana)},wanakana.isKana=function(a){var b;return b=a.split(""),b.every(function(a){return wanakana.isHiragana(a)||wanakana.isKatakana(a)})},wanakana.isRomaji=function(a){var b;return b=a.split(""),b.every(function(a){return!wanakana.isHiragana(a)&&!wanakana.isKatakana(a)})},wanakana.toHiragana=function(a,b){return wanakana.isRomaji(a)?a=wanakana._romajiToHiragana(a,b):wanakana.isKatakana(a)?a=wanakana._katakanaToHiragana(a,b):a},wanakana.toKatakana=function(a,b){return wanakana.isHiragana(a)?a=wanakana._hiraganaToKatakana(a,b):wanakana.isRomaji(a)?(a=wanakana._romajiToHiragana(a,b),a=wanakana._hiraganaToKatakana(a,b)):a},wanakana.toKana=function(a,b){return a=wanakana._romajiToKana(a,b)},wanakana.toRomaji=function(a,b){return a=wanakana._hiraganaToRomaji(a,b)},wanakana.R_to_J={a:"あ",i:"い",u:"う",e:"え",o:"お",yi:"い",wu:"う",whu:"う",xa:"ぁ",xi:"ぃ",xu:"ぅ",xe:"ぇ",xo:"ぉ",xyi:"ぃ",xye:"ぇ",ye:"いぇ",wha:"うぁ",whi:"うぃ",whe:"うぇ",who:"うぉ",wi:"うぃ",we:"うぇ",va:"ゔぁ",vi:"ゔぃ",vu:"ゔ",ve:"ゔぇ",vo:"ゔぉ",vya:"ゔゃ",vyi:"ゔぃ",vyu:"ゔゅ",vye:"ゔぇ",vyo:"ゔょ",ka:"か",ki:"き",ku:"く",ke:"け",ko:"こ",lka:"ヵ",lke:"ヶ",xka:"ヵ",xke:"ヶ",kya:"きゃ",kyi:"きぃ",kyu:"きゅ",kye:"きぇ",kyo:"きょ",ca:"か",ci:"き",cu:"く",ce:"け",co:"こ",lca:"ヵ",lce:"ヶ",xca:"ヵ",xce:"ヶ",qya:"くゃ",qyu:"くゅ",qyo:"くょ",qwa:"くぁ",qwi:"くぃ",qwu:"くぅ",qwe:"くぇ",qwo:"くぉ",qa:"くぁ",qi:"くぃ",qe:"くぇ",qo:"くぉ",kwa:"くぁ",qyi:"くぃ",qye:"くぇ",ga:"が",gi:"ぎ",gu:"ぐ",ge:"げ",go:"ご",gya:"ぎゃ",gyi:"ぎぃ",gyu:"ぎゅ",gye:"ぎぇ",gyo:"ぎょ",gwa:"ぐぁ",gwi:"ぐぃ",gwu:"ぐぅ",gwe:"ぐぇ",gwo:"ぐぉ",sa:"さ",si:"し",shi:"し",su:"す",se:"せ",so:"そ",za:"ざ",zi:"じ",zu:"ず",ze:"ぜ",zo:"ぞ",ji:"じ",sya:"しゃ",syi:"しぃ",syu:"しゅ",sye:"しぇ",syo:"しょ",sha:"しゃ",shu:"しゅ",she:"しぇ",sho:"しょ",shya:"しゃ",shyu:"しゅ",shye:"しぇ",shyo:"しょ",swa:"すぁ",swi:"すぃ",swu:"すぅ",swe:"すぇ",swo:"すぉ",zya:"じゃ",zyi:"じぃ",zyu:"じゅ",zye:"じぇ",zyo:"じょ",ja:"じゃ",ju:"じゅ",je:"じぇ",jo:"じょ",jya:"じゃ",jyi:"じぃ",jyu:"じゅ",jye:"じぇ",jyo:"じょ",ta:"た",ti:"ち",tu:"つ",te:"て",to:"と",chi:"ち",tsu:"つ",ltu:"っ",xtu:"っ",tya:"ちゃ",tyi:"ちぃ",tyu:"ちゅ",tye:"ちぇ",tyo:"ちょ",cha:"ちゃ",chu:"ちゅ",che:"ちぇ",cho:"ちょ",cya:"ちゃ",cyi:"ちぃ",cyu:"ちゅ",cye:"ちぇ",cyo:"ちょ",chya:"ちゃ",chyu:"ちゅ",chye:"ちぇ",chyo:"ちょ",tsa:"つぁ",tsi:"つぃ",tse:"つぇ",tso:"つぉ",tha:"てゃ",thi:"てぃ",thu:"てゅ",the:"てぇ",tho:"てょ",twa:"とぁ",twi:"とぃ",twu:"とぅ",twe:"とぇ",two:"とぉ",da:"だ",di:"ぢ",du:"づ",de:"で","do":"ど",dya:"ぢゃ",dyi:"ぢぃ",dyu:"ぢゅ",dye:"ぢぇ",dyo:"ぢょ",dha:"でゃ",dhi:"でぃ",dhu:"でゅ",dhe:"でぇ",dho:"でょ",dwa:"どぁ",dwi:"どぃ",dwu:"どぅ",dwe:"どぇ",dwo:"どぉ",na:"な",ni:"に",nu:"ぬ",ne:"ね",no:"の",nya:"にゃ",nyi:"にぃ",nyu:"にゅ",nye:"にぇ",nyo:"にょ",ha:"は",hi:"ひ",hu:"ふ",he:"へ",ho:"ほ",fu:"ふ",hya:"ひゃ",hyi:"ひぃ",hyu:"ひゅ",hye:"ひぇ",hyo:"ひょ",fya:"ふゃ",fyu:"ふゅ",fyo:"ふょ",fwa:"ふぁ",fwi:"ふぃ",fwu:"ふぅ",fwe:"ふぇ",fwo:"ふぉ",fa:"ふぁ",fi:"ふぃ",fe:"ふぇ",fo:"ふぉ",fyi:"ふぃ",fye:"ふぇ",ba:"ば",bi:"び",bu:"ぶ",be:"べ",bo:"ぼ",bya:"びゃ",byi:"びぃ",byu:"びゅ",bye:"びぇ",byo:"びょ",pa:"ぱ",pi:"ぴ",pu:"ぷ",pe:"ぺ",po:"ぽ",pya:"ぴゃ",pyi:"ぴぃ",pyu:"ぴゅ",pye:"ぴぇ",pyo:"ぴょ",ma:"ま",mi:"み",mu:"む",me:"め",mo:"も",mya:"みゃ",myi:"みぃ",myu:"みゅ",mye:"みぇ",myo:"みょ",ya:"や",yu:"ゆ",yo:"よ",xya:"ゃ",xyu:"ゅ",xyo:"ょ",ra:"ら",ri:"り",ru:"る",re:"れ",ro:"ろ",rya:"りゃ",ryi:"りぃ",ryu:"りゅ",rye:"りぇ",ryo:"りょ",la:"ら",li:"り",lu:"る",le:"れ",lo:"ろ",lya:"りゃ",lyi:"りぃ",lyu:"りゅ",lye:"りぇ",lyo:"りょ",wa:"わ",wo:"を",lwe:"ゎ",xwa:"ゎ",n:"ん",nn:"ん","n ":"ん",xn:"ん",ltsu:"っ"},wanakana.FOUR_CHARACTER_EDGE_CASES=["lts","chy","shy"],wanakana.J_to_R={"あ":"a","い":"i","う":"u","え":"e","お":"o","ゔぁ":"va","ゔぃ":"vi","ゔ":"vu","ゔぇ":"ve","ゔぉ":"vo","か":"ka","き":"ki","きゃ":"kya","きぃ":"kyi","きゅ":"kyu","く":"ku","け":"ke","こ":"ko","が":"ga","ぎ":"gi","ぐ":"gu","げ":"ge","ご":"go","ぎゃ":"gya","ぎぃ":"gyi","ぎゅ":"gyu","ぎぇ":"gye","ぎょ":"gyo","さ":"sa","す":"su","せ":"se","そ":"so","ざ":"za","ず":"zu","ぜ":"ze","ぞ":"zo","し":"shi","しゃ":"sha","しゅ":"shu","しょ":"sho","じ":"ji","じゃ":"ja","じゅ":"ju","じょ":"jo","た":"ta","ち":"chi","ちゃ":"cha","ちゅ":"chu","ちょ":"cho","つ":"tsu","て":"te","と":"to","だ":"da","ぢ":"di","づ":"du","で":"de","ど":"do","な":"na","に":"ni","にゃ":"nya","にゅ":"nyu","にょ":"nyo","ぬ":"nu","ね":"ne","の":"no","は":"ha","ひ":"hi","ふ":"fu","へ":"he","ほ":"ho","ひゃ":"hya","ひゅ":"hyu","ひょ":"hyo","ふぁ":"fa","ふぃ":"fi","ふぇ":"fe","ふぉ":"fo","ば":"ba","び":"bi","ぶ":"bu","べ":"be","ぼ":"bo","びゃ":"bya","びゅ":"byu","びょ":"byo","ぱ":"pa","ぴ":"pi","ぷ":"pu","ぺ":"pe","ぽ":"po","ぴゃ":"pya","ぴゅ":"pyu","ぴょ":"pyo","ま":"ma","み":"mi","む":"mu","め":"me","も":"mo","みゃ":"mya","みゅ":"myu","みょ":"myo","や":"ya","ゆ":"yu","よ":"yo","ら":"ra","り":"ri","る":"ru","れ":"re","ろ":"ro","りゃ":"rya","りゅ":"ryu","りょ":"ryo","わ":"wa","を":"wo","ん":"n","ゐ":"wi","ゑ":"we","きぇ":"kye","きょ":"kyo","じぃ":"jyi","じぇ":"jye","ちぃ":"cyi","ちぇ":"che","ひぃ":"hyi","ひぇ":"hye","びぃ":"byi","びぇ":"bye","ぴぃ":"pyi","ぴぇ":"pye","みぇ":"mye","みぃ":"myi","りぃ":"ryi","りぇ":"rye","にぃ":"nyi","にぇ":"nye","しぃ":"syi","しぇ":"she","いぇ":"ye","うぁ":"wha","うぉ":"who","うぃ":"wi","うぇ":"we","ゔゃ":"vya","ゔゅ":"vyu","ゔょ":"vyo","すぁ":"swa","すぃ":"swi","すぅ":"swu","すぇ":"swe","すぉ":"swo","くゃ":"qya","くゅ":"qyu","くょ":"qyo","くぁ":"qwa","くぃ":"qwi","くぅ":"qwu","くぇ":"qwe","くぉ":"qwo","ぐぁ":"gwa","ぐぃ":"gwi","ぐぅ":"gwu","ぐぇ":"gwe","ぐぉ":"gwo","つぁ":"tsa","つぃ":"tsi","つぇ":"tse","つぉ":"tso","てゃ":"tha","てぃ":"thi","てゅ":"thu","てぇ":"the","てょ":"tho","とぁ":"twa","とぃ":"twi","とぅ":"twu","とぇ":"twe","とぉ":"two","ぢゃ":"dya","ぢぃ":"dyi","ぢゅ":"dyu","ぢぇ":"dye","ぢょ":"dyo","でゃ":"dha","でぃ":"dhi","でゅ":"dhu","でぇ":"dhe","でょ":"dho","どぁ":"dwa","どぃ":"dwi","どぅ":"dwu","どぇ":"dwe","どぉ":"dwo","ふぅ":"fwu","ふゃ":"fya","ふゅ":"fyu","ふょ":"fyo","ぁ":"a","ぃ":"i","ぇ":"e","ぅ":"u","ぉ":"o","ゃ":"ya","ゅ":"yu","ょ":"yo","っ":"","ゕ":"ka","ゖ":"ka","ゎ":"wa"," ":" ","んあ":"n'a","んい":"n'i","んう":"n'u","んえ":"n'e","んお":"n'o","んや":"n'ya","んゆ":"n'yu","んよ":"n'yo"};
    \ No newline at end of file
    +!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n(e.wanakana=e.wanakana||{})}(this,function(e){"use strict";function n(e){return"string"!=typeof e||!e.length}function t(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"",t=arguments[1],a=arguments[2];if(n(e))return!1;var r=e.charCodeAt(0);return r>=t&&a>=r}function a(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"";return[].concat(le(e)).map(function(e,n){var a=e.charCodeAt(0),r=t(e,re,oe),o=t(e,ie,ue);return r?String.fromCharCode(a-re+V):o?String.fromCharCode(a-ie+W):e}).join("")}function r(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"",t=1>=arguments.length||void 0===arguments[1]||arguments[1];if(n(e))return!1;var a=t?/[bcdfghjklmnpqrstvwxyz]/:/[bcdfghjklmnpqrstvwxz]/;return-1!==e.toLowerCase().charAt(0).search(a)}function o(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"";return!n(e)&&t(e,W,Y)}function i(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:0,n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0;return Math.min(e,n)}function u(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"",n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0,t=arguments[2];return e.slice(n,t)}function y(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"",t=1>=arguments.length||void 0===arguments[1]||arguments[1];if(n(e))return!1;var a=t?/[aeiouy]/:/[aeiou]/;return-1!==e.toLowerCase().charAt(0).search(a)}function c(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"";return!n(e)&&e.charCodeAt(0)===ye}function h(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"";return!n(e)&&e.charCodeAt(0)===ce}function s(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"";return!n(e)&&(!!c(e)||t(e,Z,$))}function f(){var e=[];return(arguments.length>0&&void 0!==arguments[0]?arguments[0]:"").split("").forEach(function(n){if(c(n)||h(n))e.push(n);else if(s(n)){var t=n.charCodeAt(0)+(ee-Z),a=String.fromCharCode(t);e.push(a)}else e.push(n)}),e.join("")}function d(){return t(arguments.length>0&&void 0!==arguments[0]?arguments[0]:"",ee,ne)}function v(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"";return!n(e)&&(s(e)||d(e))}function l(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"";return!n(e)&&[].concat(le(e)).every(v)}function g(){for(var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"",n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},a=arguments.length>2&&void 0!==arguments[2]&&arguments[2],c=Object.assign({},X,n),h=[],s=0,d=e.length,v=3,g="",w="";d>s;){var p=null;for(v=i(3,d-s);v>0;){if(g=u(e,s,s+v),w=g.toLowerCase(),se.includes(w)&&d-s>=4)w=(g=u(e,s,s+(v+=1))).toLowerCase();else{if("n"===w.charAt(0)){if(2===v){if(!c.IMEMode&&" "===w.charAt(1)){p="ん ";break}if(c.IMEMode&&"n'"===w){p="ん";break}}r(w.charAt(1),!1)&&y(w.charAt(2))&&(w=(g=u(e,s,s+(v=1))).toLowerCase())}"n"!==w.charAt(0)&&r(w.charAt(0))&&g.charAt(0)===g.charAt(1)&&(v=1,t(g.charAt(0),W,Y)?(w="ッ",g="ッ"):(w="っ",g="っ"))}if(null!=(p=fe[w]))break;v-=4===v?2:1}null==p&&(p=g),c.useObsoleteKana&&("wi"===w&&(p="ゐ"),"we"===w&&(p="ゑ")),c.IMEMode&&"n"===w.charAt(0)&&("y"===e.charAt(s+1).toLowerCase()&&!1===y(e.charAt(s+2))||s===d-1||l(e.charAt(s+1)))&&(p=g.charAt(0)),a||o(g.charAt(0))&&(p=f(p)),h.push(p),s+=v||1}return h.join("")}function w(e){var n=m(arguments.length>1&&void 0!==arguments[1]?arguments[1]:{});if(e instanceof Element&&ge.includes(e.nodeName)){var t=be();e.setAttribute("data-wanakana-id",t),e.autocapitalize="none",e.addEventListener("compositionupdate",b),e.addEventListener("input",n),we=k(n,t)}else console.warn("Input provided to wanakana.bind was not a valid input field.")}function p(e){var n=j(e);null!=n?(e.removeAttribute("data-wanakana-id"),e.removeEventListener("compositionupdate",b),e.removeEventListener("input",n.handler),we=A(n)):console.warn("Input had no listener registered.")}function m(e){var n=Object.assign({},X,e);return function(e){var t=e.target;if(me)me=!1;else{var r=a(t.value),o=g(x(r,n.IMEMode),Object.assign({},n,{IMEMode:!0}));if(r!==o){if(t.value=o,null!=t.setSelectionRange&&"number"==typeof t.selectionStart)return void t.setSelectionRange(t.value.length,t.value.length);if(null!=t.createTextRange){t.focus();var i=t.createTextRange();i.collapse(!1),i.select()}}}}}function b(e){var n=e.data||e.detail&&e.detail.data,t=n&&n.slice(-2).split("")||[],o="n"===t[0],i=t.every(function(e){return r(a(e))});me=!o&&i}function k(e,n){return we.concat({id:n,handler:e})}function j(e){return e&&we.find(function(n){return n.id===e.getAttribute("data-wanakana-id")})}function A(e){var n=e.id;return we.filter(function(e){return e.id!==n})}function x(e,n){switch(!0){case"toHiragana"===n:return e.toLowerCase();case"toKatakana"===n:return e.toUpperCase();default:return e}}function q(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"";return!n(e)&&F.test(e)}function C(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"";return!n(e)&&[].concat(le(e)).every(function(e){return q(e)})}function z(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"";return!n(e)&&[].concat(le(e)).every(function(e){return B.test(e)})}function E(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"";return!n(e)&&[].concat(le(e)).every(s)}function M(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"";return!n(e)&&[].concat(le(e)).every(d)}function K(){return t(arguments.length>0&&void 0!==arguments[0]?arguments[0]:"",te,ae)}function L(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"";return!n(e)&&[].concat(le(e)).every(K)}function O(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"",n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{passKanji:!0},t=[].concat(le(e)),a=!1;return n.passKanji||(a=t.some(L)),(t.some(E)||t.some(M))&&t.some(C)&&!a}function I(){for(var e=[],n="",t=(arguments.length>0&&void 0!==arguments[0]?arguments[0]:"").split(""),a=0;t.length>a;a+=1){var r=t[a],o=[h(r),c(r)],i=o[0],u=o[1];if(i||u&&1>a)e.push(r);else if(u&&a>0){var y=de[n].slice(-1);e.push(he[y])}else if(d(r)){var s=r.charCodeAt(0)+(Z-ee),f=String.fromCharCode(s);e.push(f),n=f}else e.push(r),n=""}return e.join("")}function R(){for(var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"",n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},t=Object.assign({},X,n),a=e.length,r=[],o=0,y=2,c="",h="",s=void 0;a>o;){y=i(2,a-o);for(var f=!1;y>0;){if(c=u(e,o,o+y),M(c)&&(f=t.upcaseKatakana,c=I(c)),"っ"===c.charAt(0)&&1===y&&a-1>o){s=!0,h="";break}if(null!=(h=de[c])&&s&&(h=h.charAt(0).concat(h),s=!1),null!=h)break;y-=1}null==h&&(h=c),f&&(h=h.toUpperCase()),r.push(h),o+=y||1}return r.join("")}function S(){return g(arguments.length>0&&void 0!==arguments[0]?arguments[0]:"",arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},!0)}function T(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"",n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},t=Object.assign({},X,n);return t.passRomaji?I(e):C(e)?S(e,t):O(e,{passKanji:!0})?S(I(e),t):I(e)}function H(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"",n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},t=Object.assign({},X,n);return f(t.passRomaji?e:C(e)||O(e)?S(e,t):e)}function P(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"";return!n(e)&&G.some(function(n){var a=ve(n,2);return t(e,a[0],a[1])})}function U(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"";return Q.some(function(n){var a=ve(n,2);return t(e,a[0],a[1])})}function D(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"";return!n(e)&&(P(e)||U(e))}function N(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"",t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{all:!1};if(n(e)||!z(e)||l(e))return e;var a=[].concat(le(e));if(t.all)return a.filter(function(e){return!v(e)}).join("");for(var r=a.reverse(),o=0,i=r.length;i>o;o+=1){var u=r[o];if(!D(u)){if(L(u))break;r[o]=""}}return r.reverse().join("")}function _(e){switch(!0){case U(e):return"japanesePunctuation";case K(e):return"kanji";case s(e):return"hiragana";case d(e):return"katakana";default:return"romaji"}}function J(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"";if(n(e))return[""];var t=[].concat(le(e)),a=t.shift(),r=_(a);return t.reduce(function(e,n){var t=_(n)===r;if(r=_(n),t){var a=e.pop();return e.concat(a.concat(n))}return e.concat(n)},[a])}var X={useObsoleteKana:!1,passRomaji:!1,upcaseKatakana:!1,IMEMode:!1},B=/[\u3000-\u303f\u3040-\u309f\u30a0-\u30ff\uff01-\uff0f\u4e00-\u9faf\u3400-\u4dbf]/,F=/[\u0000-\u007f\u0100-\u0101\u0112-\u0113\u012a-\u012b\u014c-\u014d\u016a-\u016b\u2018-\u2019\u201C-\u201D]/,G=[[33,47],[58,63],[91,96],[123,126],[8216,8217],[8220,8221]],Q=[[12289,12350],[12539,12540],[65281,65295],[65306,65311],[65339,65343],[65371,65376]],V=97,W=65,Y=90,Z=12353,$=12438,ee=12449,ne=12540,te=19968,ae=40879,re=65345,oe=65370,ie=65313,ue=65338,ye=12540,ce=12539,he={a:"あ",i:"い",u:"う",e:"え",o:"う"},se=["lts","chy","shy"],fe={".":"。",",":"、",":":":","/":"・","!":"!","?":"?","~":"〜","-":"ー","‘":"「","’":"」","“":"『","”":"』","[":"[","]":"]","(":"(",")":")","{":"{","}":"}",a:"あ",i:"い",u:"う",e:"え",o:"お",yi:"い",wu:"う",whu:"う",xa:"ぁ",xi:"ぃ",xu:"ぅ",xe:"ぇ",xo:"ぉ",xyi:"ぃ",xye:"ぇ",ye:"いぇ",wha:"うぁ",whi:"うぃ",whe:"うぇ",who:"うぉ",wi:"うぃ",we:"うぇ",va:"ゔぁ",vi:"ゔぃ",vu:"ゔ",ve:"ゔぇ",vo:"ゔぉ",vya:"ゔゃ",vyi:"ゔぃ",vyu:"ゔゅ",vye:"ゔぇ",vyo:"ゔょ",ka:"か",ki:"き",ku:"く",ke:"け",ko:"こ",lka:"ヵ",lke:"ヶ",xka:"ヵ",xke:"ヶ",kya:"きゃ",kyi:"きぃ",kyu:"きゅ",kye:"きぇ",kyo:"きょ",ca:"か",ci:"き",cu:"く",ce:"け",co:"こ",lca:"ヵ",lce:"ヶ",xca:"ヵ",xce:"ヶ",qya:"くゃ",qyu:"くゅ",qyo:"くょ",qwa:"くぁ",qwi:"くぃ",qwu:"くぅ",qwe:"くぇ",qwo:"くぉ",qa:"くぁ",qi:"くぃ",qe:"くぇ",qo:"くぉ",kwa:"くぁ",qyi:"くぃ",qye:"くぇ",ga:"が",gi:"ぎ",gu:"ぐ",ge:"げ",go:"ご",gya:"ぎゃ",gyi:"ぎぃ",gyu:"ぎゅ",gye:"ぎぇ",gyo:"ぎょ",gwa:"ぐぁ",gwi:"ぐぃ",gwu:"ぐぅ",gwe:"ぐぇ",gwo:"ぐぉ",sa:"さ",si:"し",shi:"し",su:"す",se:"せ",so:"そ",za:"ざ",zi:"じ",zu:"ず",ze:"ぜ",zo:"ぞ",ji:"じ",sya:"しゃ",syi:"しぃ",syu:"しゅ",sye:"しぇ",syo:"しょ",sha:"しゃ",shu:"しゅ",she:"しぇ",sho:"しょ",shya:"しゃ",shyu:"しゅ",shye:"しぇ",shyo:"しょ",swa:"すぁ",swi:"すぃ",swu:"すぅ",swe:"すぇ",swo:"すぉ",zya:"じゃ",zyi:"じぃ",zyu:"じゅ",zye:"じぇ",zyo:"じょ",ja:"じゃ",ju:"じゅ",je:"じぇ",jo:"じょ",jya:"じゃ",jyi:"じぃ",jyu:"じゅ",jye:"じぇ",jyo:"じょ",ta:"た",ti:"ち",tu:"つ",te:"て",to:"と",chi:"ち",tsu:"つ",ltu:"っ",xtu:"っ",tya:"ちゃ",tyi:"ちぃ",tyu:"ちゅ",tye:"ちぇ",tyo:"ちょ",cha:"ちゃ",chu:"ちゅ",che:"ちぇ",cho:"ちょ",cya:"ちゃ",cyi:"ちぃ",cyu:"ちゅ",cye:"ちぇ",cyo:"ちょ",chya:"ちゃ",chyu:"ちゅ",chye:"ちぇ",chyo:"ちょ",tsa:"つぁ",tsi:"つぃ",tse:"つぇ",tso:"つぉ",tha:"てゃ",thi:"てぃ",thu:"てゅ",the:"てぇ",tho:"てょ",twa:"とぁ",twi:"とぃ",twu:"とぅ",twe:"とぇ",two:"とぉ",da:"だ",di:"ぢ",du:"づ",de:"で",do:"ど",dya:"ぢゃ",dyi:"ぢぃ",dyu:"ぢゅ",dye:"ぢぇ",dyo:"ぢょ",dha:"でゃ",dhi:"でぃ",dhu:"でゅ",dhe:"でぇ",dho:"でょ",dwa:"どぁ",dwi:"どぃ",dwu:"どぅ",dwe:"どぇ",dwo:"どぉ",na:"な",ni:"に",nu:"ぬ",ne:"ね",no:"の",nya:"にゃ",nyi:"にぃ",nyu:"にゅ",nye:"にぇ",nyo:"にょ",ha:"は",hi:"ひ",hu:"ふ",he:"へ",ho:"ほ",fu:"ふ",hya:"ひゃ",hyi:"ひぃ",hyu:"ひゅ",hye:"ひぇ",hyo:"ひょ",fya:"ふゃ",fyu:"ふゅ",fyo:"ふょ",fwa:"ふぁ",fwi:"ふぃ",fwu:"ふぅ",fwe:"ふぇ",fwo:"ふぉ",fa:"ふぁ",fi:"ふぃ",fe:"ふぇ",fo:"ふぉ",fyi:"ふぃ",fye:"ふぇ",ba:"ば",bi:"び",bu:"ぶ",be:"べ",bo:"ぼ",bya:"びゃ",byi:"びぃ",byu:"びゅ",bye:"びぇ",byo:"びょ",pa:"ぱ",pi:"ぴ",pu:"ぷ",pe:"ぺ",po:"ぽ",pya:"ぴゃ",pyi:"ぴぃ",pyu:"ぴゅ",pye:"ぴぇ",pyo:"ぴょ",ma:"ま",mi:"み",mu:"む",me:"め",mo:"も",mya:"みゃ",myi:"みぃ",myu:"みゅ",mye:"みぇ",myo:"みょ",ya:"や",yu:"ゆ",yo:"よ",xya:"ゃ",xyu:"ゅ",xyo:"ょ",ra:"ら",ri:"り",ru:"る",re:"れ",ro:"ろ",rya:"りゃ",ryi:"りぃ",ryu:"りゅ",rye:"りぇ",ryo:"りょ",la:"ら",li:"り",lu:"る",le:"れ",lo:"ろ",lya:"りゃ",lyi:"りぃ",lyu:"りゅ",lye:"りぇ",lyo:"りょ",wa:"わ",wo:"を",lwe:"ゎ",xwa:"ゎ",n:"ん",nn:"ん","n'":"ん","n ":"ん",xn:"ん",ltsu:"っ"},de={" ":" ","!":"!","?":"?","。":".",":":":","・":"/","、":",","〜":"~","ー":"-","「":"‘","」":"’","『":"“","』":"”","[":"[","]":"]","(":"(",")":")","{":"{","}":"}","あ":"a","い":"i","う":"u","え":"e","お":"o","ゔぁ":"va","ゔぃ":"vi","ゔ":"vu","ゔぇ":"ve","ゔぉ":"vo","か":"ka","き":"ki","きゃ":"kya","きぃ":"kyi","きゅ":"kyu","く":"ku","け":"ke","こ":"ko","が":"ga","ぎ":"gi","ぐ":"gu","げ":"ge","ご":"go","ぎゃ":"gya","ぎぃ":"gyi","ぎゅ":"gyu","ぎぇ":"gye","ぎょ":"gyo","さ":"sa","す":"su","せ":"se","そ":"so","ざ":"za","ず":"zu","ぜ":"ze","ぞ":"zo","し":"shi","しゃ":"sha","しゅ":"shu","しょ":"sho","じ":"ji","じゃ":"ja","じゅ":"ju","じょ":"jo","た":"ta","ち":"chi","ちゃ":"cha","ちゅ":"chu","ちょ":"cho","つ":"tsu","て":"te","と":"to","だ":"da","ぢ":"di","づ":"du","で":"de","ど":"do","な":"na","に":"ni","にゃ":"nya","にゅ":"nyu","にょ":"nyo","ぬ":"nu","ね":"ne","の":"no","は":"ha","ひ":"hi","ふ":"fu","へ":"he","ほ":"ho","ひゃ":"hya","ひゅ":"hyu","ひょ":"hyo","ふぁ":"fa","ふぃ":"fi","ふぇ":"fe","ふぉ":"fo","ば":"ba","び":"bi","ぶ":"bu","べ":"be","ぼ":"bo","びゃ":"bya","びゅ":"byu","びょ":"byo","ぱ":"pa","ぴ":"pi","ぷ":"pu","ぺ":"pe","ぽ":"po","ぴゃ":"pya","ぴゅ":"pyu","ぴょ":"pyo","ま":"ma","み":"mi","む":"mu","め":"me","も":"mo","みゃ":"mya","みゅ":"myu","みょ":"myo","や":"ya","ゆ":"yu","よ":"yo","ら":"ra","り":"ri","る":"ru","れ":"re","ろ":"ro","りゃ":"rya","りゅ":"ryu","りょ":"ryo","わ":"wa","を":"wo","ん":"n","ゐ":"wi","ゑ":"we","きぇ":"kye","きょ":"kyo","じぃ":"jyi","じぇ":"jye","ちぃ":"cyi","ちぇ":"che","ひぃ":"hyi","ひぇ":"hye","びぃ":"byi","びぇ":"bye","ぴぃ":"pyi","ぴぇ":"pye","みぇ":"mye","みぃ":"myi","りぃ":"ryi","りぇ":"rye","にぃ":"nyi","にぇ":"nye","しぃ":"syi","しぇ":"she","いぇ":"ye","うぁ":"wha","うぉ":"who","うぃ":"wi","うぇ":"we","ゔゃ":"vya","ゔゅ":"vyu","ゔょ":"vyo","すぁ":"swa","すぃ":"swi","すぅ":"swu","すぇ":"swe","すぉ":"swo","くゃ":"qya","くゅ":"qyu","くょ":"qyo","くぁ":"qwa","くぃ":"qwi","くぅ":"qwu","くぇ":"qwe","くぉ":"qwo","ぐぁ":"gwa","ぐぃ":"gwi","ぐぅ":"gwu","ぐぇ":"gwe","ぐぉ":"gwo","つぁ":"tsa","つぃ":"tsi","つぇ":"tse","つぉ":"tso","てゃ":"tha","てぃ":"thi","てゅ":"thu","てぇ":"the","てょ":"tho","とぁ":"twa","とぃ":"twi","とぅ":"twu","とぇ":"twe","とぉ":"two","ぢゃ":"dya","ぢぃ":"dyi","ぢゅ":"dyu","ぢぇ":"dye","ぢょ":"dyo","でゃ":"dha","でぃ":"dhi","でゅ":"dhu","でぇ":"dhe","でょ":"dho","どぁ":"dwa","どぃ":"dwi","どぅ":"dwu","どぇ":"dwe","どぉ":"dwo","ふぅ":"fwu","ふゃ":"fya","ふゅ":"fyu","ふょ":"fyo","ぁ":"a","ぃ":"i","ぇ":"e","ぅ":"u","ぉ":"o","ゃ":"ya","ゅ":"yu","ょ":"yo","っ":"","ゕ":"ka","ゖ":"ka","ゎ":"wa","んあ":"n'a","んい":"n'i","んう":"n'u","んえ":"n'e","んお":"n'o","んや":"n'ya","んゆ":"n'yu","んよ":"n'yo"},ve=function(){function e(e,n){var t=[],a=!0,r=!1,o=void 0;try{for(var i,u=e[Symbol.iterator]();!(a=(i=u.next()).done)&&(t.push(i.value),!n||t.length!==n);a=!0);}catch(e){r=!0,o=e}finally{try{!a&&u.return&&u.return()}finally{if(r)throw o}}return t}return function(n,t){if(Array.isArray(n))return n;if(Symbol.iterator in Object(n))return e(n,t);throw new TypeError("Invalid attempt to destructure non-iterable instance")}}(),le=function(e){if(Array.isArray(e)){for(var n=0,t=Array(e.length);e.length>n;n++)t[n]=e[n];return t}return Array.from(e)},ge=["TEXTAREA","INPUT"],we=[],pe=0,me=!1,be=function(){return pe+=1,""+Date.now()+pe};e.bind=w,e.unbind=p,e.isRomaji=C,e.isJapanese=z,e.isKana=l,e.isHiragana=E,e.isKatakana=M,e.isMixed=O,e.isKanji=L,e.toRomaji=R,e.toKana=g,e.toHiragana=T,e.toKatakana=H,e.stripOkurigana=N,e.tokenize=J,Object.defineProperty(e,"__esModule",{value:!0})});
    -- 
    cgit v1.2.3
    
    
    From 211e5d1155e82ccbbc188dc889600459a62229fb Mon Sep 17 00:00:00 2001
    From: Alex Yatskov 
    Date: Mon, 14 Aug 2017 23:22:37 -0700
    Subject: cleanup
    
    ---
     ext/fg/js/document.js |  2 +-
     ext/fg/js/float.js    |  4 ++--
     ext/fg/js/frontend.js | 18 +++++++++---------
     ext/fg/js/util.js     |  6 ++++++
     4 files changed, 18 insertions(+), 12 deletions(-)
    
    diff --git a/ext/fg/js/document.js b/ext/fg/js/document.js
    index 582b6770..17cca613 100644
    --- a/ext/fg/js/document.js
    +++ b/ext/fg/js/document.js
    @@ -1,5 +1,5 @@
     /*
    - * Copyright (C) 2016  Alex Yatskov 
    + * Copyright (C) 2016-2017  Alex Yatskov 
      * Author: Alex Yatskov 
      *
      * This program is free software: you can redistribute it and/or modify
    diff --git a/ext/fg/js/float.js b/ext/fg/js/float.js
    index 59293239..22374f8b 100644
    --- a/ext/fg/js/float.js
    +++ b/ext/fg/js/float.js
    @@ -1,5 +1,5 @@
     /*
    - * Copyright (C) 2016  Alex Yatskov 
    + * Copyright (C) 2016-2017  Alex Yatskov 
      * Author: Alex Yatskov 
      *
      * This program is free software: you can redistribute it and/or modify
    @@ -20,7 +20,7 @@
     class DisplayFloat extends Display {
         constructor() {
             super($('#spinner'), $('#definitions'));
    -        $(window).on('message', e => this.onMessage(e));
    +        $(window).on('message', utilAsync(this.onMessage.bind(this)));
         }
     
         onError(error) {
    diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js
    index cc4d99c8..7d26f946 100644
    --- a/ext/fg/js/frontend.js
    +++ b/ext/fg/js/frontend.js
    @@ -33,14 +33,14 @@ class Frontend {
             try {
                 this.options = await apiOptionsGet();
     
    -            window.addEventListener('message', e => this.onFrameMessage(e));
    -            window.addEventListener('mousedown', e => this.onMouseDown(e));
    -            window.addEventListener('mousemove', e => this.onMouseMove(e));
    -            window.addEventListener('mouseover', e => this.onMouseOver(e));
    -            window.addEventListener('mouseup', e => this.onMouseUp(e));
    -            window.addEventListener('resize', e => this.onResize(e));
    -
    -            chrome.runtime.onMessage.addListener(({action, params}, sender, callback) => this.onBgMessage(action, params, sender, callback));
    +            window.addEventListener('message', this.onFrameMessage.bind(this));
    +            window.addEventListener('mousedown', this.onMouseDown.bind(this));
    +            window.addEventListener('mousemove', this.onMouseMove.bind(this));
    +            window.addEventListener('mouseover', this.onMouseOver.bind(this));
    +            window.addEventListener('mouseup', this.onMouseUp.bind(this));
    +            window.addEventListener('resize', this.onResize.bind(this));
    +
    +            chrome.runtime.onMessage.addListener(this.onBgMessage.bind(this));
             } catch (e) {
                 this.onError(e);
             }
    @@ -124,7 +124,7 @@ class Frontend {
             this.searchClear();
         }
     
    -    onBgMessage(action, params, sender, callback) {
    +    onBgMessage({action, params}, sender, callback) {
             const handlers = {
                 optionsSet: options => {
                     this.options = options;
    diff --git a/ext/fg/js/util.js b/ext/fg/js/util.js
    index afa895ba..3faf3b47 100644
    --- a/ext/fg/js/util.js
    +++ b/ext/fg/js/util.js
    @@ -17,6 +17,12 @@
      */
     
     
    +function utilAsync(func) {
    +    return function(...args) {
    +        func.apply(this, args);
    +    };
    +}
    +
     function utilInvoke(action, params={}) {
         return new Promise((resolve, reject) => {
             try {
    -- 
    cgit v1.2.3
    
    
    From 3362a68e06a16efa87a2ad7cc75f18f8f4b2ea25 Mon Sep 17 00:00:00 2001
    From: Alex Yatskov 
    Date: Tue, 15 Aug 2017 20:04:15 -0700
    Subject: frontend cleanup
    
    ---
     ext/fg/js/frontend.js | 44 +++++++++++++++++++++++---------------------
     ext/fg/js/popup.js    | 12 ++++++------
     ext/fg/js/source.js   | 10 +++++++++-
     ext/fg/js/util.js     |  2 +-
     4 files changed, 39 insertions(+), 29 deletions(-)
    
    diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js
    index 7d26f946..58a721bf 100644
    --- a/ext/fg/js/frontend.js
    +++ b/ext/fg/js/frontend.js
    @@ -1,5 +1,5 @@
     /*
    - * Copyright (C) 2016  Alex Yatskov 
    + * Copyright (C) 2016-2017  Alex Yatskov 
      * Author: Alex Yatskov 
      *
      * This program is free software: you can redistribute it and/or modify
    @@ -21,10 +21,10 @@ class Frontend {
         constructor() {
             this.popup = new Popup();
             this.popupTimer = null;
    -        this.lastMousePos = null;
    +        this.mousePosLast = null;
             this.mouseDownLeft = false;
             this.mouseDownMiddle = false;
    -        this.lastTextSource = null;
    +        this.textSourceLast = null;
             this.pendingLookup = false;
             this.options = null;
         }
    @@ -53,7 +53,7 @@ class Frontend {
         }
     
         onMouseMove(e) {
    -        this.lastMousePos = {x: e.clientX, y: e.clientY};
    +        this.mousePosLast = {x: e.clientX, y: e.clientY};
             this.popupTimerClear();
     
             if (!this.options.general.enable) {
    @@ -75,7 +75,7 @@ class Frontend {
                 return;
             }
     
    -        const searchFunc = () => this.searchAt(this.lastMousePos);
    +        const searchFunc = () => this.searchAt(this.mousePosLast);
             if (this.options.scanning.modifier === 'none') {
                 this.popupTimerSet(searchFunc);
             } else {
    @@ -84,7 +84,7 @@ class Frontend {
         }
     
         onMouseDown(e) {
    -        this.lastMousePos = {x: e.clientX, y: e.clientY};
    +        this.mousePosLast = {x: e.clientX, y: e.clientY};
             this.popupTimerClear();
             this.searchClear();
     
    @@ -143,13 +143,7 @@ class Frontend {
         }
     
         onError(error) {
    -        if (window.yomichan_orphaned) {
    -            if (this.lastTextSource && this.options.scanning.modifier !== 'none') {
    -                this.popup.showOrphaned(this.lastTextSource.getRect(), this.options);
    -            }
    -        } else {
    -            window.alert(`Error: ${error}`);
    -        }
    +        window.alert(`Error: ${error}`);
         }
     
         popupTimerSet(callback) {
    @@ -165,18 +159,20 @@ class Frontend {
         }
     
         async searchAt(point) {
    +        let textSource = null;
    +
             try {
                 if (this.pendingLookup) {
                     return;
                 }
     
    -            const textSource = docRangeFromPoint(point);
    +            textSource = docRangeFromPoint(point);
                 if (!textSource || !textSource.containsPoint(point)) {
                     docImposterDestroy();
                     return;
                 }
     
    -            if (this.lastTextSource && this.lastTextSource.equals(textSource)) {
    +            if (this.textSourceLast && this.textSourceLast.equals(textSource)) {
                     return;
                 }
     
    @@ -186,7 +182,13 @@ class Frontend {
                     await this.searchKanji(textSource);
                 }
             } catch (e) {
    -            this.onError(e);
    +            if (window.yomichan_orphaned) {
    +                if (textSource && this.options.scanning.modifier !== 'none') {
    +                    this.popup.showOrphaned(textSource.getRect(), this.options);
    +                }
    +            } else {
    +                this.onError(e);
    +            }
             } finally {
                 docImposterDestroy();
                 this.pendingLookup = false;
    @@ -212,7 +214,7 @@ class Frontend {
                 {sentence, url}
             );
     
    -        this.lastTextSource = textSource;
    +        this.textSourceLast = textSource;
             if (this.options.scanning.selectText) {
                 textSource.select();
             }
    @@ -237,7 +239,7 @@ class Frontend {
                 {sentence, url}
             );
     
    -        this.lastTextSource = textSource;
    +        this.textSourceLast = textSource;
             if (this.options.scanning.selectText) {
                 textSource.select();
             }
    @@ -249,11 +251,11 @@ class Frontend {
             docImposterDestroy();
             this.popup.hide();
     
    -        if (this.options.scanning.selectText && this.lastTextSource) {
    -            this.lastTextSource.deselect();
    +        if (this.options.scanning.selectText && this.textSourceLast) {
    +            this.textSourceLast.deselect();
             }
     
    -        this.lastTextSource = null;
    +        this.textSourceLast = null;
         }
     }
     
    diff --git a/ext/fg/js/popup.js b/ext/fg/js/popup.js
    index 8cb16b5a..03958832 100644
    --- a/ext/fg/js/popup.js
    +++ b/ext/fg/js/popup.js
    @@ -1,5 +1,5 @@
     /*
    - * Copyright (C) 2016  Alex Yatskov 
    + * Copyright (C) 2016-2017  Alex Yatskov 
      * Author: Alex Yatskov 
      *
      * This program is free software: you can redistribute it and/or modify
    @@ -87,6 +87,11 @@ class Popup {
             this.container.style.visibility = 'visible';
         }
     
    +    async showOrphaned(elementRect, options) {
    +        await this.show(elementRect, options);
    +        this.invokeApi('orphaned');
    +    }
    +
         hide() {
             this.container.style.visibility = 'hidden';
         }
    @@ -108,9 +113,4 @@ class Popup {
         invokeApi(action, params={}) {
             this.container.contentWindow.postMessage({action, params}, '*');
         }
    -
    -    async onOrphaned(elementRect, options) {
    -        await this.show(elementRect, options);
    -        this.invokeApi('orphaned');
    -    }
     }
    diff --git a/ext/fg/js/source.js b/ext/fg/js/source.js
    index 210dda12..3b6ecb2a 100644
    --- a/ext/fg/js/source.js
    +++ b/ext/fg/js/source.js
    @@ -1,5 +1,5 @@
     /*
    - * Copyright (C) 2016  Alex Yatskov 
    + * Copyright (C) 2016-2017  Alex Yatskov 
      * Author: Alex Yatskov 
      *
      * This program is free software: you can redistribute it and/or modify
    @@ -17,6 +17,10 @@
      */
     
     
    +/*
    + * TextSourceRange
    + */
    +
     class TextSourceRange {
         constructor(range, content='') {
             this.range = range;
    @@ -176,6 +180,10 @@ class TextSourceRange {
     }
     
     
    +/*
    + * TextSourceElement
    + */
    +
     class TextSourceElement {
         constructor(element, content='') {
             this.element = element;
    diff --git a/ext/fg/js/util.js b/ext/fg/js/util.js
    index 3faf3b47..5eff4018 100644
    --- a/ext/fg/js/util.js
    +++ b/ext/fg/js/util.js
    @@ -1,5 +1,5 @@
     /*
    - * Copyright (C) 2016  Alex Yatskov 
    + * Copyright (C) 2016-2017  Alex Yatskov 
      * Author: Alex Yatskov 
      *
      * This program is free software: you can redistribute it and/or modify
    -- 
    cgit v1.2.3
    
    
    From 5c353f41e58e530d77bf17fb0e22a655feca3dbc Mon Sep 17 00:00:00 2001
    From: Alex Yatskov 
    Date: Tue, 15 Aug 2017 20:42:27 -0700
    Subject: version bump
    
    ---
     ext/manifest.json | 2 +-
     1 file changed, 1 insertion(+), 1 deletion(-)
    
    diff --git a/ext/manifest.json b/ext/manifest.json
    index a8b68bfb..f0c67020 100644
    --- a/ext/manifest.json
    +++ b/ext/manifest.json
    @@ -1,7 +1,7 @@
     {
         "manifest_version": 2,
         "name": "Yomichan",
    -    "version": "1.2.1",
    +    "version": "1.3.0",
     
         "description": "Japanese dictionary with Anki integration",
         "icons": {"16": "mixed/img/icon16.png", "48": "mixed/img/icon48.png", "128": "mixed/img/icon128.png"},
    -- 
    cgit v1.2.3
    
    
    From e079e5f252ee39b222f3eb78e81d0a6bab6626d2 Mon Sep 17 00:00:00 2001
    From: Alex Yatskov 
    Date: Tue, 15 Aug 2017 20:58:50 -0700
    Subject: fix loss of anki settings
    
    ---
     ext/bg/js/settings.js | 10 ++++------
     1 file changed, 4 insertions(+), 6 deletions(-)
    
    diff --git a/ext/bg/js/settings.js b/ext/bg/js/settings.js
    index 89b1581f..7bdd9f84 100644
    --- a/ext/bg/js/settings.js
    +++ b/ext/bg/js/settings.js
    @@ -96,9 +96,6 @@ async function onFormOptionsChanged(e) {
                 return;
             }
     
    -        ankiErrorShow();
    -        ankiSpinnerShow(true);
    -
             const {optionsNew, optionsOld} = await formRead();
             await optionsSave(optionsNew);
     
    @@ -109,7 +106,9 @@ async function onFormOptionsChanged(e) {
                 optionsNew.anki.server !== optionsOld.anki.server;
     
             if (ankiUpdated) {
    +            ankiSpinnerShow(true);
                 await ankiDeckAndModelPopulate(optionsNew);
    +            ankiErrorShow();
             }
         } catch (e) {
             ankiErrorShow(e);
    @@ -414,9 +413,6 @@ async function onAnkiModelChanged(e) {
                 return;
             }
     
    -        ankiErrorShow();
    -        ankiSpinnerShow(true);
    -
             const element = $(this);
             const tab = element.closest('.tab-pane');
             const tabId = tab.attr('id');
    @@ -425,7 +421,9 @@ async function onAnkiModelChanged(e) {
             optionsNew.anki[tabId].fields = {};
             await optionsSave(optionsNew);
     
    +        ankiSpinnerShow(true);
             await ankiFieldsPopulate(element, optionsNew);
    +        ankiErrorShow();
         } catch (e) {
             ankiErrorShow(e);
         } finally {
    -- 
    cgit v1.2.3
    
    
    From e19933f9804abf4e64d96143bbb58f8059de5b38 Mon Sep 17 00:00:00 2001
    From: Alex Yatskov 
    Date: Tue, 15 Aug 2017 21:36:30 -0700
    Subject: jisho.org audio support
    
    ---
     ext/bg/js/api.js        |  3 +++
     ext/bg/js/backend.js    |  4 ++++
     ext/bg/settings.html    |  1 +
     ext/fg/js/api.js        |  4 ++++
     ext/mixed/js/audio.js   | 30 +++++++++++++++++++++++++++++-
     ext/mixed/js/display.js |  3 +--
     6 files changed, 42 insertions(+), 3 deletions(-)
    
    diff --git a/ext/bg/js/api.js b/ext/bg/js/api.js
    index b55e306f..8b4c3896 100644
    --- a/ext/bg/js/api.js
    +++ b/ext/bg/js/api.js
    @@ -127,3 +127,6 @@ async function apiCommandExec(command) {
         }
     }
     
    +async function apiAudioGetUrl(definition, source) {
    +    return audioBuildUrl(definition, source);
    +}
    diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js
    index 6c77ba7e..9602d385 100644
    --- a/ext/bg/js/backend.js
    +++ b/ext/bg/js/backend.js
    @@ -110,6 +110,10 @@ class Backend {
     
                 commandExec: ({command, callback}) => {
                     forward(apiCommandExec(command), callback);
    +            },
    +
    +            audioGetUrl: ({definition, source, callback}) => {
    +                forward(apiAudioGetUrl(definition, source), callback);
                 }
             };
     
    diff --git a/ext/bg/settings.html b/ext/bg/settings.html
    index 218f9f7d..ba155d4a 100644
    --- a/ext/bg/settings.html
    +++ b/ext/bg/settings.html
    @@ -48,6 +48,7 @@
                             
                             
                             
    +                        
                         
                     
    diff --git a/ext/fg/js/api.js b/ext/fg/js/api.js index 174531ba..151882eb 100644 --- a/ext/fg/js/api.js +++ b/ext/fg/js/api.js @@ -52,3 +52,7 @@ function apiTemplateRender(template, data) { function apiCommandExec(command) { return utilInvoke('commandExec', {command}); } + +function apiAudioGetUrl(definition, source) { + return utilInvoke('audioGetUrl', {definition, source}); +} diff --git a/ext/mixed/js/audio.js b/ext/mixed/js/audio.js index eb8fde94..0952887e 100644 --- a/ext/mixed/js/audio.js +++ b/ext/mixed/js/audio.js @@ -79,7 +79,35 @@ async function audioBuildUrl(definition, mode, cache={}) { } } }); - } else { + } else if (mode === 'jisho') { + return new Promise((resolve, reject) => { + const response = cache[definition.expression]; + if (response) { + resolve(response); + } 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('load', () => { + cache[definition.expression] = xhr.responseText; + resolve(xhr.responseText); + }); + + xhr.send(); + } + }).then(response => { + try { + const dom = new DOMParser().parseFromString(response, 'text/html'); + const audio = dom.getElementById(`audio_${definition.expression}:${definition.reading}`); + if (audio) { + return audio.getElementsByTagName('source').item(0).getAttribute('src'); + } + } catch (e) { + // NOP + } + }); + } + else { return Promise.resolve(); } } diff --git a/ext/mixed/js/display.js b/ext/mixed/js/display.js index 21748f5d..12950dfd 100644 --- a/ext/mixed/js/display.js +++ b/ext/mixed/js/display.js @@ -27,7 +27,6 @@ class Display { this.sequence = 0; this.index = 0; this.audioCache = {}; - this.responseCache = {}; $(document).keydown(this.onKeyDown.bind(this)); } @@ -368,7 +367,7 @@ class Display { try { this.spinner.show(); - let url = await audioBuildUrl(definition, this.options.general.audioSource, this.responseCache); + let url = await apiAudioGetUrl(definition, this.options.general.audioSource); if (!url) { url = '/mixed/mp3/button.mp3'; } -- cgit v1.2.3 From 8ed3ca6fd44d24aff9ea41c65821c6e094024d4e Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Tue, 15 Aug 2017 21:40:41 -0700 Subject: cleanup --- ext/bg/background.html | 4 +- ext/bg/js/audio.js | 154 ++++++++++++++++++++++++++++++++++++++++++++++++ ext/bg/js/request.js | 40 +++++++++++++ ext/bg/search.html | 2 +- ext/bg/settings.html | 2 +- ext/fg/float.html | 1 - ext/mixed/js/audio.js | 154 ------------------------------------------------ ext/mixed/js/request.js | 40 ------------- 8 files changed, 198 insertions(+), 199 deletions(-) create mode 100644 ext/bg/js/audio.js create mode 100644 ext/bg/js/request.js delete mode 100644 ext/mixed/js/audio.js delete mode 100644 ext/mixed/js/request.js diff --git a/ext/bg/background.html b/ext/bg/background.html index 90ad9709..97b20f46 100644 --- a/ext/bg/background.html +++ b/ext/bg/background.html @@ -11,17 +11,17 @@ + + - - diff --git a/ext/bg/js/audio.js b/ext/bg/js/audio.js new file mode 100644 index 00000000..0952887e --- /dev/null +++ b/ext/bg/js/audio.js @@ -0,0 +1,154 @@ +/* + * Copyright (C) 2017 Alex Yatskov + * Author: Alex Yatskov + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +async function audioBuildUrl(definition, mode, cache={}) { + if (mode === 'jpod101') { + let kana = definition.reading; + let kanji = definition.expression; + + if (!kana && wanakana.isHiragana(kanji)) { + kana = kanji; + kanji = null; + } + + const params = []; + if (kanji) { + params.push(`kanji=${encodeURIComponent(kanji)}`); + } + if (kana) { + params.push(`kana=${encodeURIComponent(kana)}`); + } + + const url = `https://assets.languagepod101.com/dictionary/japanese/audiomp3.php?${params.join('&')}`; + return Promise.resolve(url); + } else if (mode === 'jpod101-alternate') { + return new Promise((resolve, reject) => { + const response = cache[definition.expression]; + if (response) { + resolve(response); + } else { + const data = { + post: 'dictionary_reference', + match_type: 'exact', + search_query: definition.expression + }; + + const params = []; + for (const key in data) { + params.push(`${encodeURIComponent(key)}=${encodeURIComponent(data[key])}`); + } + + 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('load', () => { + cache[definition.expression] = xhr.responseText; + resolve(xhr.responseText); + }); + + xhr.send(params.join('&')); + } + }).then(response => { + const dom = new DOMParser().parseFromString(response, 'text/html'); + for (const row of dom.getElementsByClassName('dc-result-row')) { + try { + const url = row.getElementsByClassName('ill-onebuttonplayer').item(0).getAttribute('data-url'); + const reading = row.getElementsByClassName('dc-vocab_kana').item(0).innerText; + if (url && reading && (!definition.reading || definition.reading === reading)) { + return url; + } + } catch (e) { + // NOP + } + } + }); + } else if (mode === 'jisho') { + return new Promise((resolve, reject) => { + const response = cache[definition.expression]; + if (response) { + resolve(response); + } 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('load', () => { + cache[definition.expression] = xhr.responseText; + resolve(xhr.responseText); + }); + + xhr.send(); + } + }).then(response => { + try { + const dom = new DOMParser().parseFromString(response, 'text/html'); + const audio = dom.getElementById(`audio_${definition.expression}:${definition.reading}`); + if (audio) { + return audio.getElementsByTagName('source').item(0).getAttribute('src'); + } + } catch (e) { + // NOP + } + }); + } + else { + return Promise.resolve(); + } +} + +function audioBuildFilename(definition) { + if (definition.reading || definition.expression) { + let filename = 'yomichan'; + if (definition.reading) { + filename += `_${definition.reading}`; + } + if (definition.expression) { + filename += `_${definition.expression}`; + } + + return filename += '.mp3'; + } +} + +async function audioInject(definition, fields, mode) { + let usesAudio = false; + for (const name in fields) { + if (fields[name].includes('{audio}')) { + usesAudio = true; + break; + } + } + + if (!usesAudio) { + return true; + } + + try { + const url = await audioBuildUrl(definition, mode); + const filename = audioBuildFilename(definition); + + if (url && filename) { + definition.audio = {url, filename}; + } + + return true; + } catch (e) { + return false; + } +} diff --git a/ext/bg/js/request.js b/ext/bg/js/request.js new file mode 100644 index 00000000..94fd135a --- /dev/null +++ b/ext/bg/js/request.js @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2017 Alex Yatskov + * Author: Alex Yatskov + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +function requestJson(url, action, params) { + return new Promise((resolve, reject) => { + const xhr = new XMLHttpRequest(); + xhr.overrideMimeType('application/json'); + xhr.addEventListener('load', () => resolve(xhr.responseText)); + xhr.addEventListener('error', () => reject('failed to execute network request')); + xhr.open(action, url); + if (params) { + xhr.send(JSON.stringify(params)); + } else { + xhr.send(); + } + }).then(responseText => { + try { + return JSON.parse(responseText); + } + catch (e) { + return Promise.reject('invalid JSON response'); + } + }); +} diff --git a/ext/bg/search.html b/ext/bg/search.html index d10530f9..5fbd467a 100644 --- a/ext/bg/search.html +++ b/ext/bg/search.html @@ -37,11 +37,11 @@ + - diff --git a/ext/bg/settings.html b/ext/bg/settings.html index ba155d4a..8798aeb1 100644 --- a/ext/bg/settings.html +++ b/ext/bg/settings.html @@ -48,7 +48,7 @@ - +
    diff --git a/ext/fg/float.html b/ext/fg/float.html index a3b66c92..89872cce 100644 --- a/ext/fg/float.html +++ b/ext/fg/float.html @@ -35,7 +35,6 @@ - diff --git a/ext/mixed/js/audio.js b/ext/mixed/js/audio.js deleted file mode 100644 index 0952887e..00000000 --- a/ext/mixed/js/audio.js +++ /dev/null @@ -1,154 +0,0 @@ -/* - * Copyright (C) 2017 Alex Yatskov - * Author: Alex Yatskov - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - - -async function audioBuildUrl(definition, mode, cache={}) { - if (mode === 'jpod101') { - let kana = definition.reading; - let kanji = definition.expression; - - if (!kana && wanakana.isHiragana(kanji)) { - kana = kanji; - kanji = null; - } - - const params = []; - if (kanji) { - params.push(`kanji=${encodeURIComponent(kanji)}`); - } - if (kana) { - params.push(`kana=${encodeURIComponent(kana)}`); - } - - const url = `https://assets.languagepod101.com/dictionary/japanese/audiomp3.php?${params.join('&')}`; - return Promise.resolve(url); - } else if (mode === 'jpod101-alternate') { - return new Promise((resolve, reject) => { - const response = cache[definition.expression]; - if (response) { - resolve(response); - } else { - const data = { - post: 'dictionary_reference', - match_type: 'exact', - search_query: definition.expression - }; - - const params = []; - for (const key in data) { - params.push(`${encodeURIComponent(key)}=${encodeURIComponent(data[key])}`); - } - - 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('load', () => { - cache[definition.expression] = xhr.responseText; - resolve(xhr.responseText); - }); - - xhr.send(params.join('&')); - } - }).then(response => { - const dom = new DOMParser().parseFromString(response, 'text/html'); - for (const row of dom.getElementsByClassName('dc-result-row')) { - try { - const url = row.getElementsByClassName('ill-onebuttonplayer').item(0).getAttribute('data-url'); - const reading = row.getElementsByClassName('dc-vocab_kana').item(0).innerText; - if (url && reading && (!definition.reading || definition.reading === reading)) { - return url; - } - } catch (e) { - // NOP - } - } - }); - } else if (mode === 'jisho') { - return new Promise((resolve, reject) => { - const response = cache[definition.expression]; - if (response) { - resolve(response); - } 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('load', () => { - cache[definition.expression] = xhr.responseText; - resolve(xhr.responseText); - }); - - xhr.send(); - } - }).then(response => { - try { - const dom = new DOMParser().parseFromString(response, 'text/html'); - const audio = dom.getElementById(`audio_${definition.expression}:${definition.reading}`); - if (audio) { - return audio.getElementsByTagName('source').item(0).getAttribute('src'); - } - } catch (e) { - // NOP - } - }); - } - else { - return Promise.resolve(); - } -} - -function audioBuildFilename(definition) { - if (definition.reading || definition.expression) { - let filename = 'yomichan'; - if (definition.reading) { - filename += `_${definition.reading}`; - } - if (definition.expression) { - filename += `_${definition.expression}`; - } - - return filename += '.mp3'; - } -} - -async function audioInject(definition, fields, mode) { - let usesAudio = false; - for (const name in fields) { - if (fields[name].includes('{audio}')) { - usesAudio = true; - break; - } - } - - if (!usesAudio) { - return true; - } - - try { - const url = await audioBuildUrl(definition, mode); - const filename = audioBuildFilename(definition); - - if (url && filename) { - definition.audio = {url, filename}; - } - - return true; - } catch (e) { - return false; - } -} diff --git a/ext/mixed/js/request.js b/ext/mixed/js/request.js deleted file mode 100644 index 94fd135a..00000000 --- a/ext/mixed/js/request.js +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright (C) 2017 Alex Yatskov - * Author: Alex Yatskov - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - - -function requestJson(url, action, params) { - return new Promise((resolve, reject) => { - const xhr = new XMLHttpRequest(); - xhr.overrideMimeType('application/json'); - xhr.addEventListener('load', () => resolve(xhr.responseText)); - xhr.addEventListener('error', () => reject('failed to execute network request')); - xhr.open(action, url); - if (params) { - xhr.send(JSON.stringify(params)); - } else { - xhr.send(); - } - }).then(responseText => { - try { - return JSON.parse(responseText); - } - catch (e) { - return Promise.reject('invalid JSON response'); - } - }); -} -- cgit v1.2.3 From 84d2204d966342fa03635b4b8a860bb48a418bc0 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Tue, 15 Aug 2017 21:51:48 -0700 Subject: firefox fixes --- ext/bg/js/api.js | 8 ++------ ext/bg/js/util.js | 4 ++++ 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/ext/bg/js/api.js b/ext/bg/js/api.js index 8b4c3896..4b6729ad 100644 --- a/ext/bg/js/api.js +++ b/ext/bg/js/api.js @@ -18,15 +18,11 @@ async function apiOptionsSet(options) { - // In Firefox, setting options from the options UI somehow carries references - // to the DOM across to the background page, causing the options object to - // become a "DeadObject" after the options page is closed. The workaround used - // here is to create a deep copy of the options object. - utilBackend().onOptionsUpdated(JSON.parse(JSON.stringify(options))); + utilBackend().onOptionsUpdated(utilIsolate(options)); } async function apiOptionsGet() { - return utilBackend().options; + return utilIsolate(utilBackend().options); } async function apiTermsFind(text) { diff --git a/ext/bg/js/util.js b/ext/bg/js/util.js index 11ec23eb..a92fd0bc 100644 --- a/ext/bg/js/util.js +++ b/ext/bg/js/util.js @@ -22,6 +22,10 @@ function utilAsync(func) { }; } +function utilIsolate(data) { + return JSON.parse(JSON.stringify(data)); +} + function utilBackend() { return chrome.extension.getBackgroundPage().yomichan_backend; } -- cgit v1.2.3 From 0c650dac828b7ab9641396268a66d3a7410d4000 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Thu, 17 Aug 2017 19:05:31 -0700 Subject: don't show busy spinner while waiting for card info smoother cursor movement in firefox --- ext/fg/js/frontend.js | 20 +++++++++++++++----- ext/mixed/js/display.js | 4 ---- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index 58a721bf..41c93f00 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -21,7 +21,6 @@ class Frontend { constructor() { this.popup = new Popup(); this.popupTimer = null; - this.mousePosLast = null; this.mouseDownLeft = false; this.mouseDownMiddle = false; this.textSourceLast = null; @@ -53,7 +52,6 @@ class Frontend { } onMouseMove(e) { - this.mousePosLast = {x: e.clientX, y: e.clientY}; this.popupTimerClear(); if (!this.options.general.enable) { @@ -64,6 +62,10 @@ class Frontend { return; } + if (this.pendingLookup) { + return; + } + const mouseScan = this.mouseDownMiddle && this.options.scanning.middleMouse; const keyScan = this.options.scanning.modifier === 'alt' && e.altKey || @@ -75,11 +77,19 @@ class Frontend { return; } - const searchFunc = () => this.searchAt(this.mousePosLast); + const search = async () => { + try { + await this.searchAt({x: e.clientX, y: e.clientY}); + this.pendingLookup = false; + } catch (e) { + this.onError(e); + } + }; + if (this.options.scanning.modifier === 'none') { - this.popupTimerSet(searchFunc); + this.popupTimerSet(search); } else { - searchFunc(); + search(); } } diff --git a/ext/mixed/js/display.js b/ext/mixed/js/display.js index 12950dfd..47efd195 100644 --- a/ext/mixed/js/display.js +++ b/ext/mixed/js/display.js @@ -286,8 +286,6 @@ class Display { async adderButtonUpdate(modes, sequence) { try { - this.spinner.show(); - const states = await apiDefinitionsAddable(this.definitions, modes); if (!states || sequence !== this.sequence) { return; @@ -308,8 +306,6 @@ class Display { } } catch (e) { this.onError(e); - } finally { - this.spinner.hide(); } } -- cgit v1.2.3 From 6367894df0ea54cd6079abd93e9e1f261482185a Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Thu, 17 Aug 2017 19:11:23 -0700 Subject: fixing broken firefox object behavior --- ext/bg/js/api.js | 4 ++-- ext/bg/js/backend.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/ext/bg/js/api.js b/ext/bg/js/api.js index 4b6729ad..2afe82a0 100644 --- a/ext/bg/js/api.js +++ b/ext/bg/js/api.js @@ -18,11 +18,11 @@ async function apiOptionsSet(options) { - utilBackend().onOptionsUpdated(utilIsolate(options)); + utilBackend().onOptionsUpdated(options); } async function apiOptionsGet() { - return utilIsolate(utilBackend().options); + return utilBackend().options; } async function apiTermsFind(text) { diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index 9602d385..6b3acaa9 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -37,7 +37,7 @@ class Backend { } onOptionsUpdated(options) { - this.options = options; + this.options = utilIsolate(options); if (!options.general.enable) { chrome.browserAction.setBadgeBackgroundColor({color: '#555555'}); -- cgit v1.2.3 From 191336522c220b0a3cfe41515ed23946b3462217 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Thu, 17 Aug 2017 19:14:06 -0700 Subject: fix flicker on form elements on mouseover (fixes #56) --- ext/fg/js/document.js | 1 + 1 file changed, 1 insertion(+) diff --git a/ext/fg/js/document.js b/ext/fg/js/document.js index 17cca613..26c85b40 100644 --- a/ext/fg/js/document.js +++ b/ext/fg/js/document.js @@ -46,6 +46,7 @@ function docImposterCreate(element) { imposter.style.position = 'absolute'; imposter.style.top = `${offset.top}px`; imposter.style.left = `${offset.left}px`; + imposter.style.opacity = 0; imposter.style.zIndex = 2147483646; if (element.nodeName === 'TEXTAREA' && styleProps.overflow === 'visible') { imposter.style.overflow = 'auto'; -- cgit v1.2.3
    Kunyomi:\n" + ((stack1 = helpers.each.call(alias1,(depth0 != null ? depth0.kunyomi : depth0),{"name":"each","hash":{},"fn":container.program(6, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") @@ -332,7 +332,7 @@ templates['kanji.html'] = template({"1":function(container,depth0,helpers,partia + ((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"; },"2":function(container,depth0,helpers,partials,data) { - return " \n"; + return " \n \n"; },"4":function(container,depth0,helpers,partials,data) { return " \n"; },"6":function(container,depth0,helpers,partials,data) { @@ -486,10 +486,10 @@ templates['terms.html'] = template({"1":function(container,depth0,helpers,partia },"12":function(container,depth0,helpers,partials,data) { var stack1, alias1=depth0 != null ? depth0 : {}; - return "
    \n
    \n \n" + return "
    \n
    \n" + ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.addable : depth0),{"name":"if","hash":{},"fn":container.program(13, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.playback : depth0),{"name":"if","hash":{},"fn":container.program(15, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") - + "
    \n\n" + + " \n
    \n\n" + ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.reading : depth0),{"name":"if","hash":{},"fn":container.program(17, data, 0),"inverse":container.program(20, data, 0),"data":data})) != null ? stack1 : "") + "\n" + ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.reasons : depth0),{"name":"if","hash":{},"fn":container.program(22, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") @@ -499,7 +499,7 @@ templates['terms.html'] = template({"1":function(container,depth0,helpers,partia + ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.debug : depth0),{"name":"if","hash":{},"fn":container.program(34, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + "
    \n"; },"13":function(container,depth0,helpers,partials,data) { - return " \n \n"; + return " \n \n \n"; },"15":function(container,depth0,helpers,partials,data) { return " \n"; },"17":function(container,depth0,helpers,partials,data) { diff --git a/ext/bg/js/yomichan.js b/ext/bg/js/yomichan.js index b4beb179..e0bdabc3 100644 --- a/ext/bg/js/yomichan.js +++ b/ext/bg/js/yomichan.js @@ -157,6 +157,10 @@ window.yomichan = new class { }); } + noteView(noteId) { + return this.anki.guiBrowse(`nid:${noteId}`); + } + templateRender(template, data) { return Promise.resolve(handlebarsRender(template, data)); } @@ -211,6 +215,10 @@ window.yomichan = new class { definitionsAddable: ({definitions, modes, callback}) => { promiseCallback(this.definitionsAddable(definitions, modes), callback); + }, + + noteView: ({noteId}) => { + promiseCallback(this.noteView(noteId), callback); } }; diff --git a/ext/fg/js/display-frame.js b/ext/fg/js/display-frame.js index 9fd09e74..b29a0379 100644 --- a/ext/fg/js/display-frame.js +++ b/ext/fg/js/display-frame.js @@ -31,6 +31,10 @@ window.displayFrame = new class extends Display { return bgDefinitionsAddable(definitions, modes); } + noteView(noteId) { + return bgNoteView(noteId); + } + templateRender(template, data) { return bgTemplateRender(template, data); } diff --git a/ext/fg/js/util.js b/ext/fg/js/util.js index c6270ce6..e1b0e080 100644 --- a/ext/fg/js/util.js +++ b/ext/fg/js/util.js @@ -62,6 +62,9 @@ function bgDefinitionAdd(definition, mode) { return bgInvoke('definitionAdd', {definition, mode}); } +function bgNoteView(noteId) { + return bgInvoke('noteView', {noteId}); +} /* * Document diff --git a/ext/mixed/img/view-note.png b/ext/mixed/img/view-note.png new file mode 100644 index 00000000..7d863f94 Binary files /dev/null and b/ext/mixed/img/view-note.png differ diff --git a/ext/mixed/js/display.js b/ext/mixed/js/display.js index 7982c69f..da0cd351 100644 --- a/ext/mixed/js/display.js +++ b/ext/mixed/js/display.js @@ -40,6 +40,10 @@ class Display { throw 'override me'; } + noteView(noteId) { + throw 'override me'; + } + templateRender(template, data) { throw 'override me'; } @@ -88,6 +92,7 @@ class Display { this.entryScroll(context && context.index || 0); $('.action-add-note').click(this.onAddNote.bind(this)); + $('.action-view-note').click(this.onViewNote.bind(this)); $('.action-play-audio').click(this.onPlayAudio.bind(this)); $('.kanji-link').click(this.onKanjiLookup.bind(this)); @@ -134,7 +139,7 @@ class Display { adderButtonsUpdate(modes, sequence) { return this.definitionsAddable(this.definitions, modes).then(states => { - if (states === null || sequence !== this.sequence) { + if (!states || sequence !== this.sequence) { return; } @@ -211,6 +216,13 @@ class Display { this.noteAdd(this.definitions[index], link.data('mode')); } + onViewNote(e) { + e.preventDefault(); + const link = $(e.currentTarget); + const index = Display.entryIndexFind(link); + this.noteView(link.data('noteId')); + } + onKeyDown(e) { const noteTryAdd = mode => { const button = Display.adderButtonFind(this.index, mode); @@ -219,6 +231,13 @@ class Display { } }; + const noteTryView = mode => { + const button = Display.viewerButtonFind(this.index); + if (button.length !== 0 && !button.hasClass('disabled')) { + this.noteView(button.data('noteId')); + } + }; + const handlers = { 27: /* escape */ () => { this.clearSearch(); @@ -303,6 +322,12 @@ class Display { return true; } + }, + + 86: /* v */ () => { + if (e.altKey) { + noteTryView(); + } } }; @@ -326,10 +351,11 @@ class Display { noteAdd(definition, mode) { this.spinner.show(); - return this.definitionAdd(definition, mode).then(success => { - if (success) { + return this.definitionAdd(definition, mode).then(noteId => { + if (noteId) { const index = this.definitions.indexOf(definition); Display.adderButtonFind(index, mode).addClass('disabled'); + Display.viewerButtonFind(index).removeClass('pending disabled').data('noteId', noteId); } else { this.handleError('note could not be added'); } @@ -375,4 +401,8 @@ class Display { static adderButtonFind(index, mode) { return $('.entry').eq(index).find(`.action-add-note[data-mode="${mode}"]`); } + + static viewerButtonFind(index) { + return $('.entry').eq(index).find('.action-view-note'); + } } diff --git a/tmpl/kanji.html b/tmpl/kanji.html index 28e4b8a4..bb62f488 100644 --- a/tmpl/kanji.html +++ b/tmpl/kanji.html @@ -1,13 +1,14 @@ {{#*inline "kanji"}}
    - {{#if addable}} + {{/if}} {{#if source}} {{/if}} +
    {{character}}
    diff --git a/tmpl/terms.html b/tmpl/terms.html index bf656cfb..db36e5cc 100644 --- a/tmpl/terms.html +++ b/tmpl/terms.html @@ -20,14 +20,15 @@ {{#*inline "term"}}
    - {{#if addable}} + {{/if}} {{#if playback}} {{/if}} +
    {{#if reading}} -- cgit v1.2.3 From b3984ccd54340195fc352033f61d33e4b5f492ea Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sun, 9 Jul 2017 15:23:11 -0700 Subject: cleanup --- ext/bg/js/database.js | 14 +++++++------- ext/bg/js/options.js | 2 +- ext/fg/js/util.js | 18 +++++++----------- 3 files changed, 15 insertions(+), 19 deletions(-) diff --git a/ext/bg/js/database.js b/ext/bg/js/database.js index 70aeb0d7..400ebd6a 100644 --- a/ext/bg/js/database.js +++ b/ext/bg/js/database.js @@ -35,7 +35,7 @@ class Database { } prepare() { - if (this.db !== null) { + if (!this.db) { return Promise.reject('database already initialized'); } @@ -53,7 +53,7 @@ class Database { } purge() { - if (this.db === null) { + if (!this.db) { return Promise.reject('database not initialized'); } @@ -66,7 +66,7 @@ class Database { } findTerms(term, dictionaries) { - if (this.db === null) { + if (!this.db) { return Promise.reject('database not initialized'); } @@ -96,7 +96,7 @@ class Database { } findKanji(kanji, dictionaries) { - if (this.db === null) { + if (!this.db) { return Promise.reject('database not initialized'); } @@ -124,7 +124,7 @@ class Database { } cacheTagMeta(dictionaries) { - if (this.db === null) { + if (!this.db) { return Promise.reject('database not initialized'); } @@ -148,7 +148,7 @@ class Database { } getDictionaries() { - if (this.db === null) { + if (!this.db) { return Promise.reject('database not initialized'); } @@ -156,7 +156,7 @@ class Database { } importDictionary(archive, callback) { - if (this.db === null) { + if (!this.db) { return Promise.reject('database not initialized'); } diff --git a/ext/bg/js/options.js b/ext/bg/js/options.js index 5aa18366..728ddae4 100644 --- a/ext/bg/js/options.js +++ b/ext/bg/js/options.js @@ -337,7 +337,7 @@ function ankiFieldsPopulate(element, options) { const container = tab.find('tbody').empty(); const modelName = element.val(); - if (modelName === null) { + if (!modelName) { return Promise.resolve(); } diff --git a/ext/fg/js/util.js b/ext/fg/js/util.js index e1b0e080..2acd81c4 100644 --- a/ext/fg/js/util.js +++ b/ext/fg/js/util.js @@ -117,7 +117,7 @@ function docImposterDestroy() { function docRangeFromPoint(point) { const element = document.elementFromPoint(point.x, point.y); - if (element !== null) { + if (element) { if (element.nodeName === 'IMG' || element.nodeName === 'BUTTON') { return new TextSourceElement(element); } else if (element.nodeName === 'INPUT' || element.nodeName === 'TEXTAREA') { @@ -128,23 +128,19 @@ function docRangeFromPoint(point) { if (!document.caretRangeFromPoint) { document.caretRangeFromPoint = (x, y) => { const position = document.caretPositionFromPoint(x,y); - if (position === null) { - return null; + if (position) { + const range = document.createRange(); + range.setStart(position.offsetNode, position.offset); + range.setEnd(position.offsetNode, position.offset); + return range; } - - const range = document.createRange(); - range.setStart(position.offsetNode, position.offset); - range.setEnd(position.offsetNode, position.offset); - return range; }; } const range = document.caretRangeFromPoint(point.x, point.y); - if (range !== null) { + if (range) { return new TextSourceRange(range); } - - return null; } function docSentenceExtract(source, extent) { -- cgit v1.2.3 From 6e986bf1f5957be12ad610c627060f4d86eca98c Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sun, 9 Jul 2017 16:29:52 -0700 Subject: cleanup --- ext/bg/js/database.js | 2 +- ext/bg/js/translator.js | 2 +- ext/bg/js/util.js | 4 ++++ 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/ext/bg/js/database.js b/ext/bg/js/database.js index 400ebd6a..69a3bbdd 100644 --- a/ext/bg/js/database.js +++ b/ext/bg/js/database.js @@ -35,7 +35,7 @@ class Database { } prepare() { - if (!this.db) { + if (this.db) { return Promise.reject('database already initialized'); } diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js index 8d65a0cd..f1858247 100644 --- a/ext/bg/js/translator.js +++ b/ext/bg/js/translator.js @@ -53,7 +53,7 @@ class Translator { } return this.findTermsDeinflected(text, titles, cache).then(deinfLiteral => { - const textHiragana = wanakana._katakanaToHiragana(text); + const textHiragana = jpKatakanaToHiragana(text); if (text === textHiragana) { return deinfLiteral; } else { diff --git a/ext/bg/js/util.js b/ext/bg/js/util.js index cdd5ec31..b39b4b2f 100644 --- a/ext/bg/js/util.js +++ b/ext/bg/js/util.js @@ -43,6 +43,10 @@ function jpIsKana(c) { return wanakana.isKana(c); } +function jpKatakanaToHiragana(text) { + return wanakana._katakanaToHiragana(text); +} + /* * Commands -- cgit v1.2.3 From f49a69c993425cce57aea16e19adab378f8cbba9 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Mon, 10 Jul 2017 13:16:24 -0700 Subject: move database to use async --- ext/bg/js/database.js | 200 +++++++++++++++++++++++++------------------------- 1 file changed, 98 insertions(+), 102 deletions(-) diff --git a/ext/bg/js/database.js b/ext/bg/js/database.js index 69a3bbdd..06312438 100644 --- a/ext/bg/js/database.js +++ b/ext/bg/js/database.js @@ -20,58 +20,62 @@ class Database { constructor() { this.db = null; - this.dbVersion = 2; - this.tagMetaCache = {}; + this.version = 2; + this.tagCache = {}; } - sanitize() { - const db = new Dexie('dict'); - return db.open().then(() => { + async sanitize() { + try { + const db = new Dexie('dict'); + await db.open(); db.close(); - if (db.verno !== this.dbVersion) { - return db.delete(); + if (db.verno !== this.version) { + await db.delete(); } - }).catch(() => {}); + } + catch(error) { + // NOP + } } - prepare() { + async prepare() { if (this.db) { - return Promise.reject('database already initialized'); + throw 'database already initialized'; } - return this.sanitize().then(() => { - this.db = new Dexie('dict'); - this.db.version(this.dbVersion).stores({ - terms: '++id,dictionary,expression,reading', - kanji: '++,dictionary,character', - tagMeta: '++,dictionary', - dictionaries: '++,title,version' - }); + await this.sanitize(); - return this.db.open(); + this.db = new Dexie('dict'); + this.db.version(this.version).stores({ + terms: '++id,dictionary,expression,reading', + kanji: '++,dictionary,character', + tagMeta: '++,dictionary', + dictionaries: '++,title,version' }); + + await this.db.open(); } - purge() { + async purge() { if (!this.db) { - return Promise.reject('database not initialized'); + throw 'database not initialized'; } this.db.close(); - return this.db.delete().then(() => { - this.db = null; - this.tagMetaCache = {}; - return this.prepare(); - }); + await this.db.delete(); + this.db = null; + this.tagCache = {}; + + await this.prepare(); } - findTerms(term, dictionaries) { + async findTerms(term, dictionaries) { if (!this.db) { - return Promise.reject('database not initialized'); + throw 'database not initialized'; } const results = []; - return this.db.terms.where('expression').equals(term).or('reading').equals(term).each(row => { + await this.db.terms.where('expression').equals(term).or('reading').equals(term).each(row => { if (dictionaries.includes(row.dictionary)) { results.push({ expression: row.expression, @@ -84,24 +88,23 @@ class Database { id: row.id }); } - }).then(() => { - return this.cacheTagMeta(dictionaries); - }).then(() => { - for (const result of results) { - result.tagMeta = this.tagMetaCache[result.dictionary] || {}; - } - - return results; }); + + await this.cacheTagMeta(dictionaries); + for (const result of results) { + result.tagMeta = this.tagCache[result.dictionary] || {}; + } + + return results; } - findKanji(kanji, dictionaries) { + async findKanji(kanji, dictionaries) { if (!this.db) { return Promise.reject('database not initialized'); } const results = []; - return this.db.kanji.where('character').equals(kanji).each(row => { + await this.db.kanji.where('character').equals(kanji).each(row => { if (dictionaries.includes(row.dictionary)) { results.push({ character: row.character, @@ -112,83 +115,79 @@ class Database { dictionary: row.dictionary }); } - }).then(() => { - return this.cacheTagMeta(dictionaries); - }).then(() => { - for (const result of results) { - result.tagMeta = this.tagMetaCache[result.dictionary] || {}; - } - - return results; }); + + await this.cacheTagMeta(dictionaries); + for (const result of results) { + result.tagMeta = this.tagCache[result.dictionary] || {}; + } + + return results; } - cacheTagMeta(dictionaries) { + async cacheTagMeta(dictionaries) { if (!this.db) { - return Promise.reject('database not initialized'); + throw 'database not initialized'; } - const promises = []; for (const dictionary of dictionaries) { - if (this.tagMetaCache[dictionary]) { - continue; - } - - const tagMeta = {}; - promises.push( - this.db.tagMeta.where('dictionary').equals(dictionary).each(row => { + if (!this.tagCache[dictionary]) { + const tagMeta = {}; + await this.db.tagMeta.where('dictionary').equals(dictionary).each(row => { tagMeta[row.name] = {category: row.category, notes: row.notes, order: row.order}; - }).then(() => { - this.tagMetaCache[dictionary] = tagMeta; - }) - ); - } + }); - return Promise.all(promises); + this.tagCache[dictionary] = tagMeta; + } + } } - getDictionaries() { + async getDictionaries() { if (!this.db) { - return Promise.reject('database not initialized'); + throw 'database not initialized'; } return this.db.dictionaries.toArray(); } - importDictionary(archive, callback) { + async importDictionary(archive, callback) { if (!this.db) { return Promise.reject('database not initialized'); } let summary = null; - const indexLoaded = (title, version, revision, tagMeta, hasTerms, hasKanji) => { + const indexLoaded = async (title, version, revision, tagMeta, hasTerms, hasKanji) => { summary = {title, version, revision, hasTerms, hasKanji}; - return this.db.dictionaries.where('title').equals(title).count().then(count => { - if (count > 0) { - return Promise.reject(`dictionary "${title}" is already imported`); - } - - return this.db.dictionaries.add({title, version, revision, hasTerms, hasKanji}).then(() => { - 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); - } - - return this.db.tagMeta.bulkAdd(rows); + + const count = await this.db.dictionaries.where('title').equals(title).count(); + if (count > 0) { + throw `dictionary "${title}" is already imported`; + } + + await this.db.dictionaries.add({title, version, revision, hasTerms, hasKanji}); + + 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 termsLoaded = (title, entries, total, current) => { + const termsLoaded = async (title, entries, total, current) => { + if (callback) { + callback(total, current); + } + const rows = []; for (const [expression, reading, tags, rules, score, ...glossary] of entries) { rows.push({ @@ -202,14 +201,14 @@ class Database { }); } - return this.db.terms.bulkAdd(rows).then(() => { - if (callback) { - callback(total, current); - } - }); + await this.db.terms.bulkAdd(rows); }; - const kanjiLoaded = (title, entries, total, current) => { + const kanjiLoaded = async (title, entries, total, current) => { + if (callback) { + callback(total, current); + } + const rows = []; for (const [character, onyomi, kunyomi, tags, ...meanings] of entries) { rows.push({ @@ -222,13 +221,10 @@ class Database { }); } - return this.db.kanji.bulkAdd(rows).then(() => { - if (callback) { - callback(total, current); - } - }); + await this.db.kanji.bulkAdd(rows); }; - return zipLoadDb(archive, indexLoaded, termsLoaded, kanjiLoaded).then(() => summary); + await zipLoadDb(archive, indexLoaded, termsLoaded, kanjiLoaded); + return summary; } } -- cgit v1.2.3 From b6f3919ef63f969769fbf030e8fd8b14b0e1c214 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Mon, 10 Jul 2017 14:10:58 -0700 Subject: move translator to async --- ext/bg/js/database.js | 28 ++++----- ext/bg/js/translator.js | 160 ++++++++++++++++++++---------------------------- 2 files changed, 80 insertions(+), 108 deletions(-) diff --git a/ext/bg/js/database.js b/ext/bg/js/database.js index 06312438..9eed8ea3 100644 --- a/ext/bg/js/database.js +++ b/ext/bg/js/database.js @@ -69,14 +69,14 @@ class Database { await this.prepare(); } - async findTerms(term, dictionaries) { + async findTerms(term, titles) { if (!this.db) { throw 'database not initialized'; } const results = []; await this.db.terms.where('expression').equals(term).or('reading').equals(term).each(row => { - if (dictionaries.includes(row.dictionary)) { + if (titles.includes(row.dictionary)) { results.push({ expression: row.expression, reading: row.reading, @@ -90,7 +90,7 @@ class Database { } }); - await this.cacheTagMeta(dictionaries); + await this.cacheTagMeta(titles); for (const result of results) { result.tagMeta = this.tagCache[result.dictionary] || {}; } @@ -98,14 +98,14 @@ class Database { return results; } - async findKanji(kanji, dictionaries) { + async findKanji(kanji, titles) { if (!this.db) { return Promise.reject('database not initialized'); } const results = []; await this.db.kanji.where('character').equals(kanji).each(row => { - if (dictionaries.includes(row.dictionary)) { + if (titles.includes(row.dictionary)) { results.push({ character: row.character, onyomi: dictFieldSplit(row.onyomi), @@ -117,7 +117,7 @@ class Database { } }); - await this.cacheTagMeta(dictionaries); + await this.cacheTagMeta(titles); for (const result of results) { result.tagMeta = this.tagCache[result.dictionary] || {}; } @@ -125,29 +125,29 @@ class Database { return results; } - async cacheTagMeta(dictionaries) { + async cacheTagMeta(titles) { if (!this.db) { throw 'database not initialized'; } - for (const dictionary of dictionaries) { - if (!this.tagCache[dictionary]) { + for (const title of titles) { + if (!this.tagCache[title]) { const tagMeta = {}; - await this.db.tagMeta.where('dictionary').equals(dictionary).each(row => { + await this.db.tagMeta.where('dictionary').equals(title).each(row => { tagMeta[row.name] = {category: row.category, notes: row.notes, order: row.order}; }); - this.tagCache[dictionary] = tagMeta; + this.tagCache[title] = tagMeta; } } } async getDictionaries() { - if (!this.db) { + if (this.db) { + return this.db.dictionaries.toArray(); + } else { throw 'database not initialized'; } - - return this.db.dictionaries.toArray(); } async importDictionary(archive, callback) { diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js index f1858247..35b49608 100644 --- a/ext/bg/js/translator.js +++ b/ext/bg/js/translator.js @@ -19,135 +19,107 @@ class Translator { constructor() { - this.loaded = false; - this.ruleMeta = null; this.database = new Database(); this.deinflector = new Deinflector(); + this.loaded = false; } - prepare() { - if (this.loaded) { - return Promise.resolve(); - } - - const promises = [ - jsonLoadInt('/bg/lang/deinflect.json'), - this.database.prepare() - ]; - - return Promise.all(promises).then(([reasons]) => { + async prepare() { + if (!this.loaded) { + const reasons = await jsonLoadInt('/bg/lang/deinflect.json'); this.deinflector.setReasons(reasons); + await this.database.prepare(); this.loaded = true; - }); + } } - findTerms(text, dictionaries, alphanumeric) { - const titles = Object.keys(dictionaries); - const cache = {}; + async findTermsGrouped(text, dictionaries, alphanumeric) { + const {length, definitions} = await this.findTerms(text, dictionaries, alphanumeric); + return {length, definitions: dictTermsGroup(definitions, dictionaries)}; + } + async findTerms(text, dictionaries, alphanumeric) { if (!alphanumeric && text.length > 0) { const c = text[0]; if (!jpIsKana(c) && !jpIsKanji(c)) { - return Promise.resolve({length: 0, definitions: []}); + return {length: 0, definitions: []}; } } - return this.findTermsDeinflected(text, titles, cache).then(deinfLiteral => { - const textHiragana = jpKatakanaToHiragana(text); - if (text === textHiragana) { - return deinfLiteral; - } else { - return this.findTermsDeinflected(textHiragana, titles, cache).then(deinfHiragana => deinfLiteral.concat(deinfHiragana)); - } - }).then(deinflections => { - let definitions = []; - for (const deinflection of deinflections) { - for (const definition of deinflection.definitions) { - const tags = definition.tags.map(tag => dictTagBuild(tag, definition.tagMeta)); - tags.push(dictTagBuildSource(definition.dictionary)); - definitions.push({ - source: deinflection.source, - reasons: deinflection.reasons, - score: definition.score, - id: definition.id, - dictionary: definition.dictionary, - expression: definition.expression, - reading: definition.reading, - glossary: definition.glossary, - tags: dictTagsSort(tags) - }); - } - } - - definitions = dictTermsUndupe(definitions); - definitions = dictTermsSort(definitions, dictionaries); - - let length = 0; - for (const definition of definitions) { - length = Math.max(length, definition.source.length); - } - - return {length, definitions}; - }); - } - - findTermsGrouped(text, dictionaries, alphanumeric) { - return this.findTerms(text, dictionaries, alphanumeric).then(({length, definitions}) => { - return {length, definitions: dictTermsGroup(definitions, dictionaries)}; - }); - } - - findKanji(text, dictionaries) { + const cache = {}; const titles = Object.keys(dictionaries); - const processed = {}; - const promises = []; - - for (const c of text) { - if (!processed[c]) { - promises.push(this.database.findKanji(c, titles)); - processed[c] = true; - } + let deinflections = await this.findTermsDeinflected(text, titles, cache); + const textHiragana = jpKatakanaToHiragana(text); + if (text !== textHiragana) { + deinflections = deinflections.concat(await this.findTermsDeinflected(textHiragana, titles, cache)); } - return Promise.all(promises).then(defSets => { - const definitions = defSets.reduce((a, b) => a.concat(b), []); - for (const definition of definitions) { + let definitions = []; + for (const deinflection of deinflections) { + for (const definition of deinflection.definitions) { const tags = definition.tags.map(tag => dictTagBuild(tag, definition.tagMeta)); tags.push(dictTagBuildSource(definition.dictionary)); - definition.tags = dictTagsSort(tags); + definitions.push({ + source: deinflection.source, + reasons: deinflection.reasons, + score: definition.score, + id: definition.id, + dictionary: definition.dictionary, + expression: definition.expression, + reading: definition.reading, + glossary: definition.glossary, + tags: dictTagsSort(tags) + }); } + } + + definitions = dictTermsUndupe(definitions); + definitions = dictTermsSort(definitions, dictionaries); - return definitions; - }); + let length = 0; + for (const definition of definitions) { + length = Math.max(length, definition.source.length); + } + + return {length, definitions}; } - findTermsDeinflected(text, dictionaries, cache) { - const definer = term => { + async findTermsDeinflected(text, dictionaries, cache) { + await this.prepare(); + + const definer = async term => { if (cache.hasOwnProperty(term)) { - return Promise.resolve(cache[term]); + return cache[term]; + } else { + return cache[term] = await this.database.findTerms(term, dictionaries); } - - return this.database.findTerms(term, dictionaries).then(definitions => cache[term] = definitions); }; - const promises = []; + let deinflections = []; for (let i = text.length; i > 0; --i) { - promises.push(this.deinflector.deinflect(text.slice(0, i), definer)); + const textSlice = text.slice(0, i); + deinflections = deinflections.concat(await this.deinflector.deinflect(textSlice, definer)); } - return Promise.all(promises).then(results => { - let deinflections = []; - for (const result of results) { - deinflections = deinflections.concat(result); - } - - return deinflections; - }); + return deinflections; } - processKanji(definitions) { + async findKanji(text, dictionaries) { + await this.prepare(); + + let definitions = []; + const processed = {}; + const titles = Object.keys(dictionaries); + for (const c of text) { + if (!processed[c]) { + definitions = definitions.concat(await this.database.findKanji(c, titles)); + processed[c] = true; + } + } + for (const definition of definitions) { const tags = definition.tags.map(tag => dictTagBuild(tag, definition.tagMeta)); + tags.push(dictTagBuildSource(definition.dictionary)); definition.tags = dictTagsSort(tags); } -- cgit v1.2.3 From 49352c5fa1baea4a6ed7d71d1353c13a56b00bca Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Mon, 10 Jul 2017 14:30:34 -0700 Subject: move deinflector to async --- ext/bg/js/deinflector.js | 64 +++++++++++++++++++++--------------------------- ext/bg/js/translator.js | 16 ++++++------ 2 files changed, 37 insertions(+), 43 deletions(-) diff --git a/ext/bg/js/deinflector.js b/ext/bg/js/deinflector.js index 6484f953..8b67761b 100644 --- a/ext/bg/js/deinflector.js +++ b/ext/bg/js/deinflector.js @@ -26,26 +26,7 @@ class Deinflection { this.children = []; } - deinflect(definer, reasons) { - const define = () => { - return definer(this.term).then(definitions => { - if (this.rules.length === 0) { - this.definitions = definitions; - } else { - for (const rule of this.rules) { - for (const definition of definitions) { - if (definition.rules.includes(rule)) { - this.definitions.push(definition); - } - } - } - } - - return this.definitions.length > 0; - }); - }; - - const promises = []; + async deinflect(definer, reasons) { for (const reason in reasons) { for (const variant of reasons[reason]) { let accept = this.rules.length === 0; @@ -68,20 +49,31 @@ class Deinflection { } const child = new Deinflection(term, {reason, rules: variant.rulesOut}); - promises.push( - child.deinflect(definer, reasons).then(valid => valid && this.children.push(child)) - ); + if (await child.deinflect(definer, reasons)) { + this.children.push(child); + } } } - return Promise.all(promises).then(define).then(valid => { - if (valid && this.children.length > 0) { - const child = new Deinflection(this.term, {rules: this.rules, definitions: this.definitions}); - this.children.push(child); + const definitions = await definer(this.term); + if (this.rules.length === 0) { + this.definitions = definitions; + } else { + for (const rule of this.rules) { + for (const definition of definitions) { + if (definition.rules.includes(rule)) { + this.definitions.push(definition); + } + } } + } + + if (this.definitions.length > 0 && this.children.length > 0) { + const child = new Deinflection(this.term, {rules: this.rules, definitions: this.definitions}); + this.children.push(child); + } - return valid || this.children.length > 0; - }); + return this.definitions.length > 0 || this.children.length > 0; } gather() { @@ -112,16 +104,16 @@ class Deinflection { class Deinflector { - constructor() { - this.reasons = {}; - } - - setReasons(reasons) { + constructor(reasons) { this.reasons = reasons; } - deinflect(term, definer) { + async deinflect(term, definer) { const node = new Deinflection(term); - return node.deinflect(definer, this.reasons).then(success => success ? node.gather() : []); + if (await node.deinflect(definer, this.reasons)) { + return node.gather(); + } else { + return []; + } } } diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js index 35b49608..84a6e1d7 100644 --- a/ext/bg/js/translator.js +++ b/ext/bg/js/translator.js @@ -19,17 +19,19 @@ class Translator { constructor() { - this.database = new Database(); - this.deinflector = new Deinflector(); - this.loaded = false; + this.database = null; + this.deinflector = null; } async prepare() { - if (!this.loaded) { - const reasons = await jsonLoadInt('/bg/lang/deinflect.json'); - this.deinflector.setReasons(reasons); + if (!this.database) { + this.database = new Database(); await this.database.prepare(); - this.loaded = true; + } + + if (!this.deinflector) { + const reasons = await jsonLoadInt('/bg/lang/deinflect.json'); + this.deinflector = new Deinflector(reasons); } } -- cgit v1.2.3 From f694026827ab2ce3a884206f7494b98335137709 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Mon, 10 Jul 2017 14:53:06 -0700 Subject: move zip import to async --- ext/bg/js/database.js | 48 +++++++++++++++++++++++++++++++++++++- ext/bg/js/util.js | 64 --------------------------------------------------- 2 files changed, 47 insertions(+), 65 deletions(-) diff --git a/ext/bg/js/database.js b/ext/bg/js/database.js index 9eed8ea3..45ba3d08 100644 --- a/ext/bg/js/database.js +++ b/ext/bg/js/database.js @@ -224,7 +224,53 @@ class Database { await this.db.kanji.bulkAdd(rows); }; - await zipLoadDb(archive, indexLoaded, termsLoaded, kanjiLoaded); + await Database.importDictionaryZip(archive, indexLoaded, termsLoaded, kanjiLoaded); return summary; } + + static async importDictionaryZip(archive, indexLoaded, termsLoaded, kanjiLoaded) { + const files = (await JSZip.loadAsync(archive)).files; + + const indexFile = files['index.json']; + if (!indexFile) { + throw 'no dictionary index found in archive'; + } + + const index = JSON.parse(await indexFile.async('string')); + if (!index.title || !index.version || !index.revision) { + 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; + + 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'; + } + } + + 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'; + } + } + } } diff --git a/ext/bg/js/util.js b/ext/bg/js/util.js index b39b4b2f..1954e83b 100644 --- a/ext/bg/js/util.js +++ b/ext/bg/js/util.js @@ -455,70 +455,6 @@ function jsonLoadInt(url) { return jsonLoad(chrome.extension.getURL(url)); } -/* - * 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'); - } - - 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, - index.revision, - index.tagMeta || {}, - 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++); - })); - } - - for (let i = 1; i <= index.kanjiBanks; ++i) { - const bankFile = files[`kanji_bank_${i}.json`]; - if (!bankFile) { - return Promise.reject('missing kanji bank file'); - } - - 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); - } - - return chain; - }); - }); -} - /* * Helpers */ -- cgit v1.2.3 From 28bc1449d1b2260df2970318982385b0d8456c54 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Mon, 10 Jul 2017 15:00:38 -0700 Subject: cleanup --- ext/bg/js/translator.js | 22 +++++++++++++++++++++- ext/bg/js/util.js | 26 -------------------------- 2 files changed, 21 insertions(+), 27 deletions(-) diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js index 84a6e1d7..dfe54623 100644 --- a/ext/bg/js/translator.js +++ b/ext/bg/js/translator.js @@ -30,7 +30,8 @@ class Translator { } if (!this.deinflector) { - const reasons = await jsonLoadInt('/bg/lang/deinflect.json'); + const url = chrome.extension.getURL('/bg/lang/deinflect.json'); + const reasons = await Translator.loadRules(url); this.deinflector = new Deinflector(reasons); } } @@ -127,4 +128,23 @@ class Translator { return definitions; } + + + static loadRules(url) { + return new Promise((resolve, reject) => { + const xhr = new XMLHttpRequest(); + xhr.overrideMimeType('application/json'); + xhr.addEventListener('load', () => resolve(xhr.responseText)); + xhr.addEventListener('error', () => reject('failed to execute network request')); + xhr.open('GET', url); + xhr.send(); + }).then(responseText => { + try { + return JSON.parse(responseText); + } + catch (e) { + return Promise.reject('invalid JSON response'); + } + }); + } } diff --git a/ext/bg/js/util.js b/ext/bg/js/util.js index 1954e83b..b8a60217 100644 --- a/ext/bg/js/util.js +++ b/ext/bg/js/util.js @@ -429,32 +429,6 @@ function dictFieldFormat(field, definition, mode, options) { } -/* - * Json - */ - -function jsonLoad(url) { - return new Promise((resolve, reject) => { - const xhr = new XMLHttpRequest(); - xhr.overrideMimeType('application/json'); - xhr.addEventListener('load', () => resolve(xhr.responseText)); - xhr.addEventListener('error', () => reject('failed to execute network request')); - xhr.open('GET', url); - xhr.send(); - }).then(responseText => { - try { - return JSON.parse(responseText); - } - catch (e) { - return Promise.reject('invalid JSON response'); - } - }); -} - -function jsonLoadInt(url) { - return jsonLoad(chrome.extension.getURL(url)); -} - /* * Helpers */ -- cgit v1.2.3 From a73b8fbab747fc5d7d2802b88d4007421cda4d87 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Mon, 10 Jul 2017 15:20:07 -0700 Subject: cleanup --- ext/bg/js/translator.js | 9 ++------- ext/bg/js/yomichan.js | 5 ++++- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js index dfe54623..aa11ea63 100644 --- a/ext/bg/js/translator.js +++ b/ext/bg/js/translator.js @@ -87,14 +87,12 @@ class Translator { return {length, definitions}; } - async findTermsDeinflected(text, dictionaries, cache) { - await this.prepare(); - + async findTermsDeinflected(text, titles, cache) { const definer = async term => { if (cache.hasOwnProperty(term)) { return cache[term]; } else { - return cache[term] = await this.database.findTerms(term, dictionaries); + return cache[term] = await this.database.findTerms(term, titles); } }; @@ -108,8 +106,6 @@ class Translator { } async findKanji(text, dictionaries) { - await this.prepare(); - let definitions = []; const processed = {}; const titles = Object.keys(dictionaries); @@ -129,7 +125,6 @@ class Translator { return definitions; } - static loadRules(url) { return new Promise((resolve, reject) => { const xhr = new XMLHttpRequest(); diff --git a/ext/bg/js/yomichan.js b/ext/bg/js/yomichan.js index e0bdabc3..acc560ce 100644 --- a/ext/bg/js/yomichan.js +++ b/ext/bg/js/yomichan.js @@ -25,9 +25,12 @@ window.yomichan = new class { this.anki = new AnkiNull(); this.options = null; - this.translator.prepare().then(optionsLoad).then(this.optionsSet.bind(this)).then(() => { + this.translator.prepare().then(optionsLoad).then(options => { + this.optionsSet(options); + chrome.commands.onCommand.addListener(this.onCommand.bind(this)); chrome.runtime.onMessage.addListener(this.onMessage.bind(this)); + if (this.options.general.showGuide) { chrome.tabs.create({url: chrome.extension.getURL('/bg/guide.html')}); } -- cgit v1.2.3 From efb5ed2af8c0bcf1c0dbfdbeef7c89dad2a106b9 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Mon, 10 Jul 2017 15:22:19 -0700 Subject: cleanup --- ext/manifest.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ext/manifest.json b/ext/manifest.json index ef0be459..b472fb1f 100644 --- a/ext/manifest.json +++ b/ext/manifest.json @@ -23,7 +23,7 @@ ], "css": ["fg/css/client.css"] }], - "minimum_chrome_version": "45.0.0.0", + "minimum_chrome_version": "57.0.0.0", "options_ui": { "page": "bg/options.html" }, @@ -50,7 +50,7 @@ "applications": { "gecko": { "id": "yomichan-live@foosoft.net", - "strict_min_version": "51.0", + "strict_min_version": "52.0", "update_url": "https://foosoft.net/projects/yomichan/dl/updates.json" } } -- cgit v1.2.3 From b0cdf59bd8dae8f44362b14bdf19b243514e31d3 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Mon, 10 Jul 2017 16:24:31 -0700 Subject: move anki to async --- ext/bg/js/anki-connect.js | 69 +++++++++++++++++------------------------------ ext/bg/js/anki-null.js | 24 ++++++++--------- ext/bg/js/translator.js | 20 +------------- ext/bg/js/util.js | 25 +++++++++++++++++ 4 files changed, 63 insertions(+), 75 deletions(-) diff --git a/ext/bg/js/anki-connect.js b/ext/bg/js/anki-connect.js index 173feefd..a4d8ba3f 100644 --- a/ext/bg/js/anki-connect.js +++ b/ext/bg/js/anki-connect.js @@ -20,69 +20,50 @@ class AnkiConnect { constructor(server) { this.server = server; - this.asyncPools = {}; this.localVersion = 2; - this.remoteVersion = null; + this.remoteVersion = 0; } - addNote(note) { - return this.checkVersion().then(() => this.ankiInvoke('addNote', {note})); + async addNote(note) { + await this.checkVersion(); + return await this.ankiInvoke('addNote', {note}); } - canAddNotes(notes) { - return this.checkVersion().then(() => this.ankiInvoke('canAddNotes', {notes}, 'notes')); + async canAddNotes(notes) { + await this.checkVersion(); + return await this.ankiInvoke('canAddNotes', {notes}); } - getDeckNames() { - return this.checkVersion().then(() => this.ankiInvoke('deckNames', {})); + async getDeckNames() { + await this.checkVersion(); + return await this.ankiInvoke('deckNames'); } - getModelNames() { - return this.checkVersion().then(() => this.ankiInvoke('modelNames', {})); + async getModelNames() { + await this.checkVersion(); + return await this.ankiInvoke('modelNames'); } - getModelFieldNames(modelName) { - return this.checkVersion().then(() => this.ankiInvoke('modelFieldNames', {modelName})); + async getModelFieldNames(modelName) { + await this.checkVersion(); + return await this.ankiInvoke('modelFieldNames', {modelName}); } - guiBrowse(query) { - return this.checkVersion().then(() => this.ankiInvoke('guiBrowse', {query})); + async guiBrowse(query) { + await this.checkVersion(); + return await this.ankiInvoke('guiBrowse', {query}); } - checkVersion() { - if (this.localVersion === this.remoteVersion) { - return Promise.resolve(true); - } - - return this.ankiInvoke('version', {}, null).then(version => { - this.remoteVersion = version; + async checkVersion() { + if (this.remoteVersion < this.localVersion) { + this.remoteVersion = await this.ankiInvoke('version'); if (this.remoteVersion < this.localVersion) { return Promise.reject('extension and plugin versions incompatible'); } - }); + } } - ankiInvoke(action, params, pool) { - return new Promise((resolve, reject) => { - if (pool && this.asyncPools.hasOwnProperty(pool)) { - this.asyncPools[pool].abort(); - } - - const xhr = new XMLHttpRequest(); - xhr.addEventListener('loadend', () => { - if (pool) { - delete this.asyncPools[pool]; - } - - if (xhr.responseText) { - resolve(JSON.parse(xhr.responseText)); - } else { - reject('unable to connect to plugin'); - } - }); - - xhr.open('POST', this.server); - xhr.send(JSON.stringify({action, params})); - }); + ankiInvoke(action, params) { + return jsonRequest(this.server, 'POST', {action, params, version: this.localVersion}); } } diff --git a/ext/bg/js/anki-null.js b/ext/bg/js/anki-null.js index 8dad6915..d82f0e68 100644 --- a/ext/bg/js/anki-null.js +++ b/ext/bg/js/anki-null.js @@ -18,27 +18,27 @@ class AnkiNull { - addNote(note) { - return Promise.reject('unsupported action'); + async addNote(note) { + return null; } - canAddNotes(notes) { - return Promise.resolve([]); + async canAddNotes(notes) { + return []; } - getDeckNames() { - return Promise.resolve([]); + async getDeckNames() { + return []; } - getModelNames() { - return Promise.resolve([]); + async getModelNames() { + return []; } - getModelFieldNames(modelName) { - return Promise.resolve([]); + async getModelFieldNames(modelName) { + return []; } - guiBrowse(query) { - return Promise.resolve([]); + async guiBrowse(query) { + return []; } } diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js index aa11ea63..9232e529 100644 --- a/ext/bg/js/translator.js +++ b/ext/bg/js/translator.js @@ -31,7 +31,7 @@ class Translator { if (!this.deinflector) { const url = chrome.extension.getURL('/bg/lang/deinflect.json'); - const reasons = await Translator.loadRules(url); + const reasons = await jsonRequest(url, 'GET'); this.deinflector = new Deinflector(reasons); } } @@ -124,22 +124,4 @@ class Translator { return definitions; } - - static loadRules(url) { - return new Promise((resolve, reject) => { - const xhr = new XMLHttpRequest(); - xhr.overrideMimeType('application/json'); - xhr.addEventListener('load', () => resolve(xhr.responseText)); - xhr.addEventListener('error', () => reject('failed to execute network request')); - xhr.open('GET', url); - xhr.send(); - }).then(responseText => { - try { - return JSON.parse(responseText); - } - catch (e) { - return Promise.reject('invalid JSON response'); - } - }); - } } diff --git a/ext/bg/js/util.js b/ext/bg/js/util.js index b8a60217..4f907923 100644 --- a/ext/bg/js/util.js +++ b/ext/bg/js/util.js @@ -428,6 +428,31 @@ function dictFieldFormat(field, definition, mode, options) { return field; } +/* + * JSON + */ + +function jsonRequest(url, action, params) { + return new Promise((resolve, reject) => { + const xhr = new XMLHttpRequest(); + xhr.overrideMimeType('application/json'); + xhr.addEventListener('load', () => resolve(xhr.responseText)); + xhr.addEventListener('error', () => reject('failed to execute network request')); + xhr.open(action, url); + if (params) { + xhr.send(JSON.stringify(params)); + } else { + xhr.send(); + } + }).then(responseText => { + try { + return JSON.parse(responseText); + } + catch (e) { + return Promise.reject('invalid JSON response'); + } + }); +} /* * Helpers -- cgit v1.2.3 From e2373345a4a615ec59a45a6c3c75408826fef9c6 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Mon, 10 Jul 2017 16:30:28 -0700 Subject: version bump --- ext/manifest.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/manifest.json b/ext/manifest.json index b472fb1f..ab180faa 100644 --- a/ext/manifest.json +++ b/ext/manifest.json @@ -1,7 +1,7 @@ { "manifest_version": 2, "name": "Yomichan", - "version": "1.2.0", + "version": "1.2.1", "description": "Japanese dictionary with Anki integration", "icons": {"16": "mixed/img/icon16.png", "48": "mixed/img/icon48.png", "128": "mixed/img/icon128.png"}, -- cgit v1.2.3 From 1ed8997240ba5d8ee4fe57062d2dbe8ba46436e2 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Mon, 10 Jul 2017 16:48:26 -0700 Subject: work on audio --- ext/bg/js/database.js | 3 +-- ext/mixed/js/util.js | 31 +++++++++++++++---------------- 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/ext/bg/js/database.js b/ext/bg/js/database.js index 45ba3d08..b38e00db 100644 --- a/ext/bg/js/database.js +++ b/ext/bg/js/database.js @@ -32,8 +32,7 @@ class Database { if (db.verno !== this.version) { await db.delete(); } - } - catch(error) { + } catch(e) { // NOP } } diff --git a/ext/mixed/js/util.js b/ext/mixed/js/util.js index 5cf62000..0a8c914d 100644 --- a/ext/mixed/js/util.js +++ b/ext/mixed/js/util.js @@ -40,7 +40,7 @@ function clozeBuild(sentence, source) { * Audio */ -function audioBuildUrl(definition, mode, cache={}) { +async function audioBuildUrl(definition, mode, cache={}) { if (mode === 'jpod101') { let kana = definition.reading; let kanji = definition.expression; @@ -103,7 +103,7 @@ function audioBuildUrl(definition, mode, cache={}) { } }); } else { - return Promise.reject('unsupported audio source'); + return Promise.resolve(); } } @@ -121,16 +121,7 @@ function audioBuildFilename(definition) { } } -function audioInject(definition, fields, mode) { - if (mode === 'disabled') { - return Promise.resolve(true); - } - - const filename = audioBuildFilename(definition); - if (!filename) { - return Promise.resolve(true); - } - +async function audioInject(definition, fields, mode) { let usesAudio = false; for (const name in fields) { if (fields[name].includes('{audio}')) { @@ -140,11 +131,19 @@ function audioInject(definition, fields, mode) { } if (!usesAudio) { - return Promise.resolve(true); + return true; } - return audioBuildUrl(definition, mode).then(url => { - definition.audio = {url, filename}; + try { + const url = await audioBuildUrl(definition, mode); + const filename = audioBuildFilename(definition); + + if (url && filename) { + definition.audio = {url, filename}; + } + return true; - }).catch(() => false); + } catch (e) { + return false; + } } -- cgit v1.2.3 From ede139097c670f16ca3c332d8a79e9009b23bfac Mon Sep 17 00:00:00 2001 From: dequis Date: Sun, 16 Jul 2017 05:05:23 -0300 Subject: Add glossary-brief anki field, like glossary but without tags --- README.md | 1 + ext/bg/js/options.js | 1 + ext/bg/js/templates.js | 239 ++++++++++++++++++++++++++----------------------- ext/bg/js/util.js | 1 + tmpl/fields.html | 20 +++-- 5 files changed, 143 insertions(+), 119 deletions(-) diff --git a/README.md b/README.md index c32c867f..6ac97eea 100644 --- a/README.md +++ b/README.md @@ -132,6 +132,7 @@ Flashcard fields can be configured with the following steps: `{expression}` | Term expressed as Kanji (will be displayed in Kana if Kanji is not available). `{furigana}` | Term expressed as Kanji with Furigana displayed above it (e.g. 日本語にほんご). `{glossary}` | List of definitions for the term (output format depends on whether running in *grouped* mode). + `{glossary-brief}` | Shorter version of `{glossary}`, without `{tags}` `{reading}` | Kana reading for the term (empty for terms where the expression is the reading). `{sentence}` | Sentence, quote, or phrase in which the term appears in the source content. `{tags}` | Grammar and usage tags providing information about the term (unavailable in *grouped* mode). diff --git a/ext/bg/js/options.js b/ext/bg/js/options.js index 728ddae4..e105f1b2 100644 --- a/ext/bg/js/options.js +++ b/ext/bg/js/options.js @@ -351,6 +351,7 @@ function ankiFieldsPopulate(element, options) { 'expression', 'furigana', 'glossary', + 'glossary-brief', 'reading', 'sentence', 'tags', diff --git a/ext/bg/js/templates.js b/ext/bg/js/templates.js index 50686ed4..6fa1dad0 100644 --- a/ext/bg/js/templates.js +++ b/ext/bg/js/templates.js @@ -24,106 +24,114 @@ templates['dictionary.html'] = template({"1":function(container,depth0,helpers,p templates['fields.html'] = template({"1":function(container,depth0,helpers,partials,data) { var stack1; - return ((stack1 = helpers["if"].call(depth0 != null ? depth0 : {},(depth0 != null ? depth0.html : depth0),{"name":"if","hash":{},"fn":container.program(2, data, 0),"inverse":container.program(14, data, 0),"data":data})) != null ? stack1 : ""); + return ((stack1 = helpers["if"].call(depth0 != null ? depth0 : {},(depth0 != null ? depth0.html : depth0),{"name":"if","hash":{},"fn":container.program(2, data, 0),"inverse":container.program(15, data, 0),"data":data})) != null ? stack1 : ""); },"2":function(container,depth0,helpers,partials,data) { var stack1, alias1=depth0 != null ? depth0 : {}; - return ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.tags : depth0),{"name":"if","hash":{},"fn":container.program(3, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") - + ((stack1 = helpers["if"].call(alias1,((stack1 = (depth0 != null ? depth0.glossary : depth0)) != null ? stack1["1"] : stack1),{"name":"if","hash":{},"fn":container.program(7, data, 0),"inverse":container.program(11, data, 0),"data":data})) != null ? stack1 : ""); + return ((stack1 = helpers.unless.call(alias1,(depth0 != null ? depth0.brief : depth0),{"name":"unless","hash":{},"fn":container.program(3, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + + ((stack1 = helpers["if"].call(alias1,((stack1 = (depth0 != null ? depth0.glossary : depth0)) != null ? stack1["1"] : stack1),{"name":"if","hash":{},"fn":container.program(8, data, 0),"inverse":container.program(12, data, 0),"data":data})) != null ? stack1 : ""); },"3":function(container,depth0,helpers,partials,data) { var stack1; + return ((stack1 = helpers["if"].call(depth0 != null ? depth0 : {},(depth0 != null ? depth0.tags : depth0),{"name":"if","hash":{},"fn":container.program(4, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : ""); +},"4":function(container,depth0,helpers,partials,data) { + var stack1; + return "(" - + ((stack1 = helpers.each.call(depth0 != null ? depth0 : {},(depth0 != null ? depth0.tags : depth0),{"name":"each","hash":{},"fn":container.program(4, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + + ((stack1 = helpers.each.call(depth0 != null ? depth0 : {},(depth0 != null ? depth0.tags : depth0),{"name":"each","hash":{},"fn":container.program(5, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + ") "; -},"4":function(container,depth0,helpers,partials,data) { +},"5":function(container,depth0,helpers,partials,data) { var stack1, helper, alias1=depth0 != null ? depth0 : {}; return container.escapeExpression(((helper = (helper = helpers.name || (depth0 != null ? depth0.name : depth0)) != null ? helper : helpers.helperMissing),(typeof helper === "function" ? helper.call(alias1,{"name":"name","hash":{},"data":data}) : helper))) - + ((stack1 = helpers.unless.call(alias1,(data && data.last),{"name":"unless","hash":{},"fn":container.program(5, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : ""); -},"5":function(container,depth0,helpers,partials,data) { + + ((stack1 = helpers.unless.call(alias1,(data && data.last),{"name":"unless","hash":{},"fn":container.program(6, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : ""); +},"6":function(container,depth0,helpers,partials,data) { return ", "; -},"7":function(container,depth0,helpers,partials,data) { +},"8":function(container,depth0,helpers,partials,data) { var stack1; return "
      " - + ((stack1 = helpers.each.call(depth0 != null ? depth0 : {},(depth0 != null ? depth0.glossary : depth0),{"name":"each","hash":{},"fn":container.program(8, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + + ((stack1 = helpers.each.call(depth0 != null ? depth0 : {},(depth0 != null ? depth0.glossary : depth0),{"name":"each","hash":{},"fn":container.program(9, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + "
    "; -},"8":function(container,depth0,helpers,partials,data) { +},"9":function(container,depth0,helpers,partials,data) { var stack1, helper, options, buffer = "
  • "; - stack1 = ((helper = (helper = helpers.multiLine || (depth0 != null ? depth0.multiLine : depth0)) != null ? helper : helpers.helperMissing),(options={"name":"multiLine","hash":{},"fn":container.program(9, data, 0),"inverse":container.noop,"data":data}),(typeof helper === "function" ? helper.call(depth0 != null ? depth0 : {},options) : helper)); + stack1 = ((helper = (helper = helpers.multiLine || (depth0 != null ? depth0.multiLine : depth0)) != null ? helper : helpers.helperMissing),(options={"name":"multiLine","hash":{},"fn":container.program(10, data, 0),"inverse":container.noop,"data":data}),(typeof helper === "function" ? helper.call(depth0 != null ? depth0 : {},options) : helper)); if (!helpers.multiLine) { stack1 = helpers.blockHelperMissing.call(depth0,stack1,options)} if (stack1 != null) { buffer += stack1; } return buffer + "
  • "; -},"9":function(container,depth0,helpers,partials,data) { +},"10":function(container,depth0,helpers,partials,data) { return container.escapeExpression(container.lambda(depth0, depth0)); -},"11":function(container,depth0,helpers,partials,data) { +},"12":function(container,depth0,helpers,partials,data) { var stack1, helper, options, buffer = ""; - stack1 = ((helper = (helper = helpers.multiLine || (depth0 != null ? depth0.multiLine : depth0)) != null ? helper : helpers.helperMissing),(options={"name":"multiLine","hash":{},"fn":container.program(12, data, 0),"inverse":container.noop,"data":data}),(typeof helper === "function" ? helper.call(depth0 != null ? depth0 : {},options) : helper)); + stack1 = ((helper = (helper = helpers.multiLine || (depth0 != null ? depth0.multiLine : depth0)) != null ? helper : helpers.helperMissing),(options={"name":"multiLine","hash":{},"fn":container.program(13, data, 0),"inverse":container.noop,"data":data}),(typeof helper === "function" ? helper.call(depth0 != null ? depth0 : {},options) : helper)); if (!helpers.multiLine) { stack1 = helpers.blockHelperMissing.call(depth0,stack1,options)} if (stack1 != null) { buffer += stack1; } return buffer; -},"12":function(container,depth0,helpers,partials,data) { +},"13":function(container,depth0,helpers,partials,data) { var stack1; return container.escapeExpression(container.lambda(((stack1 = (depth0 != null ? depth0.glossary : depth0)) != null ? stack1["0"] : stack1), depth0)); -},"14":function(container,depth0,helpers,partials,data) { +},"15":function(container,depth0,helpers,partials,data) { var stack1, alias1=depth0 != null ? depth0 : {}; - return ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.tags : depth0),{"name":"if","hash":{},"fn":container.program(15, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") - + ((stack1 = helpers["if"].call(alias1,((stack1 = (depth0 != null ? depth0.glossary : depth0)) != null ? stack1["1"] : stack1),{"name":"if","hash":{},"fn":container.program(17, data, 0),"inverse":container.program(12, data, 0),"data":data})) != null ? stack1 : ""); -},"15":function(container,depth0,helpers,partials,data) { + return ((stack1 = helpers.unless.call(alias1,(depth0 != null ? depth0.brief : depth0),{"name":"unless","hash":{},"fn":container.program(16, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + + ((stack1 = helpers["if"].call(alias1,((stack1 = (depth0 != null ? depth0.glossary : depth0)) != null ? stack1["1"] : stack1),{"name":"if","hash":{},"fn":container.program(19, data, 0),"inverse":container.program(13, data, 0),"data":data})) != null ? stack1 : ""); +},"16":function(container,depth0,helpers,partials,data) { + var stack1; + + return ((stack1 = helpers["if"].call(depth0 != null ? depth0 : {},(depth0 != null ? depth0.tags : depth0),{"name":"if","hash":{},"fn":container.program(17, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : ""); +},"17":function(container,depth0,helpers,partials,data) { var stack1; return "(" - + ((stack1 = helpers.each.call(depth0 != null ? depth0 : {},(depth0 != null ? depth0.tags : depth0),{"name":"each","hash":{},"fn":container.program(4, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + + ((stack1 = helpers.each.call(depth0 != null ? depth0 : {},(depth0 != null ? depth0.tags : depth0),{"name":"each","hash":{},"fn":container.program(5, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + ") "; -},"17":function(container,depth0,helpers,partials,data) { +},"19":function(container,depth0,helpers,partials,data) { var stack1; - return ((stack1 = helpers.each.call(depth0 != null ? depth0 : {},(depth0 != null ? depth0.glossary : depth0),{"name":"each","hash":{},"fn":container.program(18, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : ""); -},"18":function(container,depth0,helpers,partials,data) { + return ((stack1 = helpers.each.call(depth0 != null ? depth0 : {},(depth0 != null ? depth0.glossary : depth0),{"name":"each","hash":{},"fn":container.program(20, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : ""); +},"20":function(container,depth0,helpers,partials,data) { var stack1; return container.escapeExpression(container.lambda(depth0, depth0)) - + ((stack1 = helpers.unless.call(depth0 != null ? depth0 : {},(data && data.last),{"name":"unless","hash":{},"fn":container.program(5, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : ""); -},"20":function(container,depth0,helpers,partials,data) { - return ""; + + ((stack1 = helpers.unless.call(depth0 != null ? depth0 : {},(data && data.last),{"name":"unless","hash":{},"fn":container.program(6, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : ""); },"22":function(container,depth0,helpers,partials,data) { + return ""; +},"24":function(container,depth0,helpers,partials,data) { var stack1; return container.escapeExpression(container.lambda(((stack1 = (depth0 != null ? depth0.definition : depth0)) != null ? stack1.character : stack1), depth0)); -},"24":function(container,depth0,helpers,partials,data) { +},"26":function(container,depth0,helpers,partials,data) { var stack1; return container.escapeExpression(container.lambda(((stack1 = (depth0 != null ? depth0.definition : depth0)) != null ? stack1.dictionary : stack1), depth0)); -},"26":function(container,depth0,helpers,partials,data) { +},"28":function(container,depth0,helpers,partials,data) { var stack1; - return ((stack1 = helpers["if"].call(depth0 != null ? depth0 : {},(depth0 != null ? depth0.modeTermKana : depth0),{"name":"if","hash":{},"fn":container.program(27, data, 0),"inverse":container.program(30, data, 0),"data":data})) != null ? stack1 : ""); -},"27":function(container,depth0,helpers,partials,data) { + return ((stack1 = helpers["if"].call(depth0 != null ? depth0 : {},(depth0 != null ? depth0.modeTermKana : depth0),{"name":"if","hash":{},"fn":container.program(29, data, 0),"inverse":container.program(32, data, 0),"data":data})) != null ? stack1 : ""); +},"29":function(container,depth0,helpers,partials,data) { var stack1; - return ((stack1 = helpers["if"].call(depth0 != null ? depth0 : {},((stack1 = (depth0 != null ? depth0.definition : depth0)) != null ? stack1.reading : stack1),{"name":"if","hash":{},"fn":container.program(28, data, 0),"inverse":container.program(30, data, 0),"data":data})) != null ? stack1 : ""); -},"28":function(container,depth0,helpers,partials,data) { + return ((stack1 = helpers["if"].call(depth0 != null ? depth0 : {},((stack1 = (depth0 != null ? depth0.definition : depth0)) != null ? stack1.reading : stack1),{"name":"if","hash":{},"fn":container.program(30, data, 0),"inverse":container.program(32, data, 0),"data":data})) != null ? stack1 : ""); +},"30":function(container,depth0,helpers,partials,data) { var stack1; return container.escapeExpression(container.lambda(((stack1 = (depth0 != null ? depth0.definition : depth0)) != null ? stack1.reading : stack1), depth0)); -},"30":function(container,depth0,helpers,partials,data) { +},"32":function(container,depth0,helpers,partials,data) { var stack1; return container.escapeExpression(container.lambda(((stack1 = (depth0 != null ? depth0.definition : depth0)) != null ? stack1.expression : stack1), depth0)); -},"32":function(container,depth0,helpers,partials,data) { +},"34":function(container,depth0,helpers,partials,data) { var stack1; - return ((stack1 = helpers["if"].call(depth0 != null ? depth0 : {},(depth0 != null ? depth0.html : depth0),{"name":"if","hash":{},"fn":container.program(33, data, 0),"inverse":container.program(36, data, 0),"data":data})) != null ? stack1 : ""); -},"33":function(container,depth0,helpers,partials,data) { + return ((stack1 = helpers["if"].call(depth0 != null ? depth0 : {},(depth0 != null ? depth0.html : depth0),{"name":"if","hash":{},"fn":container.program(35, data, 0),"inverse":container.program(38, data, 0),"data":data})) != null ? stack1 : ""); +},"35":function(container,depth0,helpers,partials,data) { var stack1; - return ((stack1 = helpers["if"].call(depth0 != null ? depth0 : {},((stack1 = (depth0 != null ? depth0.definition : depth0)) != null ? stack1.reading : stack1),{"name":"if","hash":{},"fn":container.program(34, data, 0),"inverse":container.program(30, data, 0),"data":data})) != null ? stack1 : ""); -},"34":function(container,depth0,helpers,partials,data) { + return ((stack1 = helpers["if"].call(depth0 != null ? depth0 : {},((stack1 = (depth0 != null ? depth0.definition : depth0)) != null ? stack1.reading : stack1),{"name":"if","hash":{},"fn":container.program(36, data, 0),"inverse":container.program(32, data, 0),"data":data})) != null ? stack1 : ""); +},"36":function(container,depth0,helpers,partials,data) { var stack1, alias1=container.lambda, alias2=container.escapeExpression; return "" @@ -131,147 +139,151 @@ templates['fields.html'] = template({"1":function(container,depth0,helpers,parti + "" + alias2(alias1(((stack1 = (depth0 != null ? depth0.definition : depth0)) != null ? stack1.reading : stack1), depth0)) + ""; -},"36":function(container,depth0,helpers,partials,data) { +},"38":function(container,depth0,helpers,partials,data) { var stack1; - return ((stack1 = helpers["if"].call(depth0 != null ? depth0 : {},((stack1 = (depth0 != null ? depth0.definition : depth0)) != null ? stack1.reading : stack1),{"name":"if","hash":{},"fn":container.program(37, data, 0),"inverse":container.program(30, data, 0),"data":data})) != null ? stack1 : ""); -},"37":function(container,depth0,helpers,partials,data) { + return ((stack1 = helpers["if"].call(depth0 != null ? depth0 : {},((stack1 = (depth0 != null ? depth0.definition : depth0)) != null ? stack1.reading : stack1),{"name":"if","hash":{},"fn":container.program(39, data, 0),"inverse":container.program(32, data, 0),"data":data})) != null ? stack1 : ""); +},"39":function(container,depth0,helpers,partials,data) { var stack1, alias1=container.lambda, alias2=container.escapeExpression; return alias2(alias1(((stack1 = (depth0 != null ? depth0.definition : depth0)) != null ? stack1.expression : stack1), depth0)) + " [" + alias2(alias1(((stack1 = (depth0 != null ? depth0.definition : depth0)) != null ? stack1.reading : stack1), depth0)) + "]"; -},"39":function(container,depth0,helpers,partials,data,blockParams,depths) { +},"41":function(container,depth0,helpers,partials,data,blockParams,depths) { var stack1, alias1=depth0 != null ? depth0 : {}; - return ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.html : depth0),{"name":"if","hash":{},"fn":container.program(40, data, 0, blockParams, depths),"inverse":container.noop,"data":data})) != null ? stack1 : "") - + ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.modeKanji : depth0),{"name":"if","hash":{},"fn":container.program(42, data, 0, blockParams, depths),"inverse":container.program(51, data, 0, blockParams, depths),"data":data})) != null ? stack1 : "") - + ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.html : depth0),{"name":"if","hash":{},"fn":container.program(64, data, 0, blockParams, depths),"inverse":container.noop,"data":data})) != null ? stack1 : ""); -},"40":function(container,depth0,helpers,partials,data) { - return "
    "; + return ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.html : depth0),{"name":"if","hash":{},"fn":container.program(42, data, 0, blockParams, depths),"inverse":container.noop,"data":data})) != null ? stack1 : "") + + ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.modeKanji : depth0),{"name":"if","hash":{},"fn":container.program(44, data, 0, blockParams, depths),"inverse":container.program(53, data, 0, blockParams, depths),"data":data})) != null ? stack1 : "") + + ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.html : depth0),{"name":"if","hash":{},"fn":container.program(66, data, 0, blockParams, depths),"inverse":container.noop,"data":data})) != null ? stack1 : ""); },"42":function(container,depth0,helpers,partials,data) { + return "
    "; +},"44":function(container,depth0,helpers,partials,data) { var stack1; - return ((stack1 = helpers["if"].call(depth0 != null ? depth0 : {},((stack1 = ((stack1 = (depth0 != null ? depth0.definition : depth0)) != null ? stack1.glossary : stack1)) != null ? stack1["1"] : stack1),{"name":"if","hash":{},"fn":container.program(43, data, 0),"inverse":container.program(49, data, 0),"data":data})) != null ? stack1 : ""); -},"43":function(container,depth0,helpers,partials,data) { + return ((stack1 = helpers["if"].call(depth0 != null ? depth0 : {},((stack1 = ((stack1 = (depth0 != null ? depth0.definition : depth0)) != null ? stack1.glossary : stack1)) != null ? stack1["1"] : stack1),{"name":"if","hash":{},"fn":container.program(45, data, 0),"inverse":container.program(51, data, 0),"data":data})) != null ? stack1 : ""); +},"45":function(container,depth0,helpers,partials,data) { var stack1; - return ((stack1 = helpers["if"].call(depth0 != null ? depth0 : {},(depth0 != null ? depth0.html : depth0),{"name":"if","hash":{},"fn":container.program(44, data, 0),"inverse":container.program(47, data, 0),"data":data})) != null ? stack1 : ""); -},"44":function(container,depth0,helpers,partials,data) { + return ((stack1 = helpers["if"].call(depth0 != null ? depth0 : {},(depth0 != null ? depth0.html : depth0),{"name":"if","hash":{},"fn":container.program(46, data, 0),"inverse":container.program(49, data, 0),"data":data})) != null ? stack1 : ""); +},"46":function(container,depth0,helpers,partials,data) { var stack1; return "
      " - + ((stack1 = helpers.each.call(depth0 != null ? depth0 : {},((stack1 = (depth0 != null ? depth0.definition : depth0)) != null ? stack1.glossary : stack1),{"name":"each","hash":{},"fn":container.program(45, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + + ((stack1 = helpers.each.call(depth0 != null ? depth0 : {},((stack1 = (depth0 != null ? depth0.definition : depth0)) != null ? stack1.glossary : stack1),{"name":"each","hash":{},"fn":container.program(47, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + "
    "; -},"45":function(container,depth0,helpers,partials,data) { +},"47":function(container,depth0,helpers,partials,data) { return "
  • " + container.escapeExpression(container.lambda(depth0, depth0)) + "
  • "; -},"47":function(container,depth0,helpers,partials,data) { +},"49":function(container,depth0,helpers,partials,data) { var stack1; - return ((stack1 = helpers.each.call(depth0 != null ? depth0 : {},((stack1 = (depth0 != null ? depth0.definition : depth0)) != null ? stack1.glossary : stack1),{"name":"each","hash":{},"fn":container.program(18, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : ""); -},"49":function(container,depth0,helpers,partials,data) { + return ((stack1 = helpers.each.call(depth0 != null ? depth0 : {},((stack1 = (depth0 != null ? depth0.definition : depth0)) != null ? stack1.glossary : stack1),{"name":"each","hash":{},"fn":container.program(20, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : ""); +},"51":function(container,depth0,helpers,partials,data) { var stack1; return container.escapeExpression(container.lambda(((stack1 = ((stack1 = (depth0 != null ? depth0.definition : depth0)) != null ? stack1.glossary : stack1)) != null ? stack1["0"] : stack1), depth0)); -},"51":function(container,depth0,helpers,partials,data,blockParams,depths) { +},"53":function(container,depth0,helpers,partials,data,blockParams,depths) { var stack1; - return ((stack1 = helpers["if"].call(depth0 != null ? depth0 : {},(depth0 != null ? depth0.group : depth0),{"name":"if","hash":{},"fn":container.program(52, data, 0, blockParams, depths),"inverse":container.program(62, data, 0, blockParams, depths),"data":data})) != null ? stack1 : ""); -},"52":function(container,depth0,helpers,partials,data,blockParams,depths) { + return ((stack1 = helpers["if"].call(depth0 != null ? depth0 : {},(depth0 != null ? depth0.group : depth0),{"name":"if","hash":{},"fn":container.program(54, data, 0, blockParams, depths),"inverse":container.program(64, data, 0, blockParams, depths),"data":data})) != null ? stack1 : ""); +},"54":function(container,depth0,helpers,partials,data,blockParams,depths) { var stack1; - return ((stack1 = helpers["if"].call(depth0 != null ? depth0 : {},((stack1 = ((stack1 = (depth0 != null ? depth0.definition : depth0)) != null ? stack1.definitions : stack1)) != null ? stack1["1"] : stack1),{"name":"if","hash":{},"fn":container.program(53, data, 0, blockParams, depths),"inverse":container.program(60, data, 0, blockParams, depths),"data":data})) != null ? stack1 : ""); -},"53":function(container,depth0,helpers,partials,data,blockParams,depths) { + return ((stack1 = helpers["if"].call(depth0 != null ? depth0 : {},((stack1 = ((stack1 = (depth0 != null ? depth0.definition : depth0)) != null ? stack1.definitions : stack1)) != null ? stack1["1"] : stack1),{"name":"if","hash":{},"fn":container.program(55, data, 0, blockParams, depths),"inverse":container.program(62, data, 0, blockParams, depths),"data":data})) != null ? stack1 : ""); +},"55":function(container,depth0,helpers,partials,data,blockParams,depths) { var stack1; - return ((stack1 = helpers["if"].call(depth0 != null ? depth0 : {},(depth0 != null ? depth0.html : depth0),{"name":"if","hash":{},"fn":container.program(54, data, 0, blockParams, depths),"inverse":container.program(57, data, 0, blockParams, depths),"data":data})) != null ? stack1 : ""); -},"54":function(container,depth0,helpers,partials,data,blockParams,depths) { + return ((stack1 = helpers["if"].call(depth0 != null ? depth0 : {},(depth0 != null ? depth0.html : depth0),{"name":"if","hash":{},"fn":container.program(56, data, 0, blockParams, depths),"inverse":container.program(59, data, 0, blockParams, depths),"data":data})) != null ? stack1 : ""); +},"56":function(container,depth0,helpers,partials,data,blockParams,depths) { var stack1; return "
      " - + ((stack1 = helpers.each.call(depth0 != null ? depth0 : {},((stack1 = (depth0 != null ? depth0.definition : depth0)) != null ? stack1.definitions : stack1),{"name":"each","hash":{},"fn":container.program(55, data, 0, blockParams, depths),"inverse":container.noop,"data":data})) != null ? stack1 : "") + + ((stack1 = helpers.each.call(depth0 != null ? depth0 : {},((stack1 = (depth0 != null ? depth0.definition : depth0)) != null ? stack1.definitions : stack1),{"name":"each","hash":{},"fn":container.program(57, data, 0, blockParams, depths),"inverse":container.noop,"data":data})) != null ? stack1 : "") + "
    "; -},"55":function(container,depth0,helpers,partials,data,blockParams,depths) { +},"57":function(container,depth0,helpers,partials,data,blockParams,depths) { var stack1; return "
  • " - + ((stack1 = container.invokePartial(partials["glossary-single"],depth0,{"name":"glossary-single","hash":{"html":(depths[1] != null ? depths[1].html : depths[1])},"data":data,"helpers":helpers,"partials":partials,"decorators":container.decorators})) != null ? stack1 : "") + + ((stack1 = container.invokePartial(partials["glossary-single"],depth0,{"name":"glossary-single","hash":{"brief":(depths[1] != null ? depths[1].brief : depths[1]),"html":(depths[1] != null ? depths[1].html : depths[1])},"data":data,"helpers":helpers,"partials":partials,"decorators":container.decorators})) != null ? stack1 : "") + "
  • "; -},"57":function(container,depth0,helpers,partials,data,blockParams,depths) { +},"59":function(container,depth0,helpers,partials,data,blockParams,depths) { var stack1; - return ((stack1 = helpers.each.call(depth0 != null ? depth0 : {},((stack1 = (depth0 != null ? depth0.definition : depth0)) != null ? stack1.definitions : stack1),{"name":"each","hash":{},"fn":container.program(58, data, 0, blockParams, depths),"inverse":container.noop,"data":data})) != null ? stack1 : ""); -},"58":function(container,depth0,helpers,partials,data,blockParams,depths) { + return ((stack1 = helpers.each.call(depth0 != null ? depth0 : {},((stack1 = (depth0 != null ? depth0.definition : depth0)) != null ? stack1.definitions : stack1),{"name":"each","hash":{},"fn":container.program(60, data, 0, blockParams, depths),"inverse":container.noop,"data":data})) != null ? stack1 : ""); +},"60":function(container,depth0,helpers,partials,data,blockParams,depths) { var stack1; return " * " - + ((stack1 = container.invokePartial(partials["glossary-single"],depth0,{"name":"glossary-single","hash":{"html":(depths[1] != null ? depths[1].html : depths[1])},"data":data,"helpers":helpers,"partials":partials,"decorators":container.decorators})) != null ? stack1 : ""); -},"60":function(container,depth0,helpers,partials,data) { - var stack1; - - return ((stack1 = container.invokePartial(partials["glossary-single"],((stack1 = ((stack1 = (depth0 != null ? depth0.definition : depth0)) != null ? stack1.definitions : stack1)) != null ? stack1["0"] : stack1),{"name":"glossary-single","hash":{"html":(depth0 != null ? depth0.html : depth0)},"data":data,"helpers":helpers,"partials":partials,"decorators":container.decorators})) != null ? stack1 : ""); + + ((stack1 = container.invokePartial(partials["glossary-single"],depth0,{"name":"glossary-single","hash":{"brief":(depths[1] != null ? depths[1].brief : depths[1]),"html":(depths[1] != null ? depths[1].html : depths[1])},"data":data,"helpers":helpers,"partials":partials,"decorators":container.decorators})) != null ? stack1 : ""); },"62":function(container,depth0,helpers,partials,data) { var stack1; - return ((stack1 = container.invokePartial(partials["glossary-single"],(depth0 != null ? depth0.definition : depth0),{"name":"glossary-single","hash":{"html":(depth0 != null ? depth0.html : depth0)},"data":data,"helpers":helpers,"partials":partials,"decorators":container.decorators})) != null ? stack1 : ""); + return ((stack1 = container.invokePartial(partials["glossary-single"],((stack1 = ((stack1 = (depth0 != null ? depth0.definition : depth0)) != null ? stack1.definitions : stack1)) != null ? stack1["0"] : stack1),{"name":"glossary-single","hash":{"brief":(depth0 != null ? depth0.brief : depth0),"html":(depth0 != null ? depth0.html : depth0)},"data":data,"helpers":helpers,"partials":partials,"decorators":container.decorators})) != null ? stack1 : ""); },"64":function(container,depth0,helpers,partials,data) { - return "
    "; -},"66":function(container,depth0,helpers,partials,data) { var stack1; - return ((stack1 = helpers.each.call(depth0 != null ? depth0 : {},((stack1 = (depth0 != null ? depth0.definition : depth0)) != null ? stack1.kunyomi : stack1),{"name":"each","hash":{},"fn":container.program(18, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : ""); + return ((stack1 = container.invokePartial(partials["glossary-single"],(depth0 != null ? depth0.definition : depth0),{"name":"glossary-single","hash":{"brief":(depth0 != null ? depth0.brief : depth0),"html":(depth0 != null ? depth0.html : depth0)},"data":data,"helpers":helpers,"partials":partials,"decorators":container.decorators})) != null ? stack1 : ""); +},"66":function(container,depth0,helpers,partials,data) { + return "
    "; },"68":function(container,depth0,helpers,partials,data) { var stack1; - return ((stack1 = helpers.each.call(depth0 != null ? depth0 : {},((stack1 = (depth0 != null ? depth0.definition : depth0)) != null ? stack1.onyomi : stack1),{"name":"each","hash":{},"fn":container.program(18, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : ""); + return ((stack1 = container.invokePartial(partials.glossary,depth0,{"name":"glossary","hash":{"brief":true},"data":data,"helpers":helpers,"partials":partials,"decorators":container.decorators})) != null ? stack1 : ""); },"70":function(container,depth0,helpers,partials,data) { var stack1; - return ((stack1 = helpers.unless.call(depth0 != null ? depth0 : {},(depth0 != null ? depth0.modeTermKana : depth0),{"name":"unless","hash":{},"fn":container.program(28, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : ""); + return ((stack1 = helpers.each.call(depth0 != null ? depth0 : {},((stack1 = (depth0 != null ? depth0.definition : depth0)) != null ? stack1.kunyomi : stack1),{"name":"each","hash":{},"fn":container.program(20, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : ""); },"72":function(container,depth0,helpers,partials,data) { var stack1; - return ((stack1 = helpers["if"].call(depth0 != null ? depth0 : {},((stack1 = (depth0 != null ? depth0.definition : depth0)) != null ? stack1.cloze : stack1),{"name":"if","hash":{},"fn":container.program(73, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : ""); -},"73":function(container,depth0,helpers,partials,data) { + return ((stack1 = helpers.each.call(depth0 != null ? depth0 : {},((stack1 = (depth0 != null ? depth0.definition : depth0)) != null ? stack1.onyomi : stack1),{"name":"each","hash":{},"fn":container.program(20, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : ""); +},"74":function(container,depth0,helpers,partials,data) { + var stack1; + + return ((stack1 = helpers.unless.call(depth0 != null ? depth0 : {},(depth0 != null ? depth0.modeTermKana : depth0),{"name":"unless","hash":{},"fn":container.program(30, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : ""); +},"76":function(container,depth0,helpers,partials,data) { + var stack1; + + return ((stack1 = helpers["if"].call(depth0 != null ? depth0 : {},((stack1 = (depth0 != null ? depth0.definition : depth0)) != null ? stack1.cloze : stack1),{"name":"if","hash":{},"fn":container.program(77, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : ""); +},"77":function(container,depth0,helpers,partials,data) { var stack1; return container.escapeExpression(container.lambda(((stack1 = ((stack1 = (depth0 != null ? depth0.definition : depth0)) != null ? stack1.cloze : stack1)) != null ? stack1.sentence : stack1), depth0)); -},"75":function(container,depth0,helpers,partials,data) { +},"79":function(container,depth0,helpers,partials,data) { var stack1; - return ((stack1 = helpers["if"].call(depth0 != null ? depth0 : {},((stack1 = (depth0 != null ? depth0.definition : depth0)) != null ? stack1.cloze : stack1),{"name":"if","hash":{},"fn":container.program(76, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : ""); -},"76":function(container,depth0,helpers,partials,data) { + return ((stack1 = helpers["if"].call(depth0 != null ? depth0 : {},((stack1 = (depth0 != null ? depth0.definition : depth0)) != null ? stack1.cloze : stack1),{"name":"if","hash":{},"fn":container.program(80, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : ""); +},"80":function(container,depth0,helpers,partials,data) { var stack1; return container.escapeExpression(container.lambda(((stack1 = ((stack1 = (depth0 != null ? depth0.definition : depth0)) != null ? stack1.cloze : stack1)) != null ? stack1.prefix : stack1), depth0)); -},"78":function(container,depth0,helpers,partials,data) { +},"82":function(container,depth0,helpers,partials,data) { var stack1; - return ((stack1 = helpers["if"].call(depth0 != null ? depth0 : {},((stack1 = (depth0 != null ? depth0.definition : depth0)) != null ? stack1.cloze : stack1),{"name":"if","hash":{},"fn":container.program(79, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : ""); -},"79":function(container,depth0,helpers,partials,data) { + return ((stack1 = helpers["if"].call(depth0 != null ? depth0 : {},((stack1 = (depth0 != null ? depth0.definition : depth0)) != null ? stack1.cloze : stack1),{"name":"if","hash":{},"fn":container.program(83, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : ""); +},"83":function(container,depth0,helpers,partials,data) { var stack1; return container.escapeExpression(container.lambda(((stack1 = ((stack1 = (depth0 != null ? depth0.definition : depth0)) != null ? stack1.cloze : stack1)) != null ? stack1.body : stack1), depth0)); -},"81":function(container,depth0,helpers,partials,data) { +},"85":function(container,depth0,helpers,partials,data) { var stack1; - return ((stack1 = helpers["if"].call(depth0 != null ? depth0 : {},((stack1 = (depth0 != null ? depth0.definition : depth0)) != null ? stack1.cloze : stack1),{"name":"if","hash":{},"fn":container.program(82, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : ""); -},"82":function(container,depth0,helpers,partials,data) { + return ((stack1 = helpers["if"].call(depth0 != null ? depth0 : {},((stack1 = (depth0 != null ? depth0.definition : depth0)) != null ? stack1.cloze : stack1),{"name":"if","hash":{},"fn":container.program(86, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : ""); +},"86":function(container,depth0,helpers,partials,data) { var stack1; return container.escapeExpression(container.lambda(((stack1 = ((stack1 = (depth0 != null ? depth0.definition : depth0)) != null ? stack1.cloze : stack1)) != null ? stack1.suffix : stack1), depth0)); -},"84":function(container,depth0,helpers,partials,data) { +},"88":function(container,depth0,helpers,partials,data) { var stack1; - return ((stack1 = helpers.each.call(depth0 != null ? depth0 : {},((stack1 = (depth0 != null ? depth0.definition : depth0)) != null ? stack1.tags : stack1),{"name":"each","hash":{},"fn":container.program(4, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : ""); -},"86":function(container,depth0,helpers,partials,data) { + return ((stack1 = helpers.each.call(depth0 != null ? depth0 : {},((stack1 = (depth0 != null ? depth0.definition : depth0)) != null ? stack1.tags : stack1),{"name":"each","hash":{},"fn":container.program(5, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : ""); +},"90":function(container,depth0,helpers,partials,data) { var stack1; - return ((stack1 = helpers["if"].call(depth0 != null ? depth0 : {},(depth0 != null ? depth0.html : depth0),{"name":"if","hash":{},"fn":container.program(87, data, 0),"inverse":container.program(89, data, 0),"data":data})) != null ? stack1 : ""); -},"87":function(container,depth0,helpers,partials,data) { + return ((stack1 = helpers["if"].call(depth0 != null ? depth0 : {},(depth0 != null ? depth0.html : depth0),{"name":"if","hash":{},"fn":container.program(91, data, 0),"inverse":container.program(93, data, 0),"data":data})) != null ? stack1 : ""); +},"91":function(container,depth0,helpers,partials,data) { var stack1, alias1=container.lambda, alias2=container.escapeExpression; return "" + alias2(alias1(((stack1 = (depth0 != null ? depth0.definition : depth0)) != null ? stack1.url : stack1), depth0)) + ""; -},"89":function(container,depth0,helpers,partials,data) { +},"93":function(container,depth0,helpers,partials,data) { var stack1; return container.escapeExpression(container.lambda(((stack1 = (depth0 != null ? depth0.definition : depth0)) != null ? stack1.url : stack1), depth0)); },"compiler":[7,">= 4.0.0"],"main":function(container,depth0,helpers,partials,data,blockParams,depths) { var stack1; - return "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" + return "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" + ((stack1 = container.invokePartial(helpers.lookup.call(depth0 != null ? depth0 : {},depth0,"marker",{"name":"lookup","hash":{},"data":data}),depth0,{"data":data,"helpers":helpers,"partials":partials,"decorators":container.decorators})) != null ? stack1 : ""); },"main_d": function(fn, props, container, depth0, data, blockParams, depths) { var decorators = container.decorators; fn = decorators.inline(fn,props,container,{"name":"inline","hash":{},"fn":container.program(1, data, 0, blockParams, depths),"inverse":container.noop,"args":["glossary-single"],"data":data}) || fn; - fn = decorators.inline(fn,props,container,{"name":"inline","hash":{},"fn":container.program(20, data, 0, blockParams, depths),"inverse":container.noop,"args":["audio"],"data":data}) || fn; - fn = decorators.inline(fn,props,container,{"name":"inline","hash":{},"fn":container.program(22, data, 0, blockParams, depths),"inverse":container.noop,"args":["character"],"data":data}) || fn; - fn = decorators.inline(fn,props,container,{"name":"inline","hash":{},"fn":container.program(24, data, 0, blockParams, depths),"inverse":container.noop,"args":["dictionary"],"data":data}) || fn; - fn = decorators.inline(fn,props,container,{"name":"inline","hash":{},"fn":container.program(26, data, 0, blockParams, depths),"inverse":container.noop,"args":["expression"],"data":data}) || fn; - fn = decorators.inline(fn,props,container,{"name":"inline","hash":{},"fn":container.program(32, data, 0, blockParams, depths),"inverse":container.noop,"args":["furigana"],"data":data}) || fn; - fn = decorators.inline(fn,props,container,{"name":"inline","hash":{},"fn":container.program(39, data, 0, blockParams, depths),"inverse":container.noop,"args":["glossary"],"data":data}) || fn; - fn = decorators.inline(fn,props,container,{"name":"inline","hash":{},"fn":container.program(66, data, 0, blockParams, depths),"inverse":container.noop,"args":["kunyomi"],"data":data}) || fn; - fn = decorators.inline(fn,props,container,{"name":"inline","hash":{},"fn":container.program(68, data, 0, blockParams, depths),"inverse":container.noop,"args":["onyomi"],"data":data}) || fn; - fn = decorators.inline(fn,props,container,{"name":"inline","hash":{},"fn":container.program(70, data, 0, blockParams, depths),"inverse":container.noop,"args":["reading"],"data":data}) || fn; - fn = decorators.inline(fn,props,container,{"name":"inline","hash":{},"fn":container.program(72, data, 0, blockParams, depths),"inverse":container.noop,"args":["sentence"],"data":data}) || fn; - fn = decorators.inline(fn,props,container,{"name":"inline","hash":{},"fn":container.program(75, data, 0, blockParams, depths),"inverse":container.noop,"args":["cloze-prefix"],"data":data}) || fn; - fn = decorators.inline(fn,props,container,{"name":"inline","hash":{},"fn":container.program(78, data, 0, blockParams, depths),"inverse":container.noop,"args":["cloze-body"],"data":data}) || fn; - fn = decorators.inline(fn,props,container,{"name":"inline","hash":{},"fn":container.program(81, data, 0, blockParams, depths),"inverse":container.noop,"args":["cloze-suffix"],"data":data}) || fn; - fn = decorators.inline(fn,props,container,{"name":"inline","hash":{},"fn":container.program(84, data, 0, blockParams, depths),"inverse":container.noop,"args":["tags"],"data":data}) || fn; - fn = decorators.inline(fn,props,container,{"name":"inline","hash":{},"fn":container.program(86, data, 0, blockParams, depths),"inverse":container.noop,"args":["url"],"data":data}) || fn; + fn = decorators.inline(fn,props,container,{"name":"inline","hash":{},"fn":container.program(22, data, 0, blockParams, depths),"inverse":container.noop,"args":["audio"],"data":data}) || fn; + fn = decorators.inline(fn,props,container,{"name":"inline","hash":{},"fn":container.program(24, data, 0, blockParams, depths),"inverse":container.noop,"args":["character"],"data":data}) || fn; + fn = decorators.inline(fn,props,container,{"name":"inline","hash":{},"fn":container.program(26, data, 0, blockParams, depths),"inverse":container.noop,"args":["dictionary"],"data":data}) || fn; + fn = decorators.inline(fn,props,container,{"name":"inline","hash":{},"fn":container.program(28, data, 0, blockParams, depths),"inverse":container.noop,"args":["expression"],"data":data}) || fn; + fn = decorators.inline(fn,props,container,{"name":"inline","hash":{},"fn":container.program(34, data, 0, blockParams, depths),"inverse":container.noop,"args":["furigana"],"data":data}) || fn; + fn = decorators.inline(fn,props,container,{"name":"inline","hash":{},"fn":container.program(41, data, 0, blockParams, depths),"inverse":container.noop,"args":["glossary"],"data":data}) || fn; + fn = decorators.inline(fn,props,container,{"name":"inline","hash":{},"fn":container.program(68, data, 0, blockParams, depths),"inverse":container.noop,"args":["glossary-brief"],"data":data}) || fn; + fn = decorators.inline(fn,props,container,{"name":"inline","hash":{},"fn":container.program(70, data, 0, blockParams, depths),"inverse":container.noop,"args":["kunyomi"],"data":data}) || fn; + fn = decorators.inline(fn,props,container,{"name":"inline","hash":{},"fn":container.program(72, data, 0, blockParams, depths),"inverse":container.noop,"args":["onyomi"],"data":data}) || fn; + fn = decorators.inline(fn,props,container,{"name":"inline","hash":{},"fn":container.program(74, data, 0, blockParams, depths),"inverse":container.noop,"args":["reading"],"data":data}) || fn; + fn = decorators.inline(fn,props,container,{"name":"inline","hash":{},"fn":container.program(76, data, 0, blockParams, depths),"inverse":container.noop,"args":["sentence"],"data":data}) || fn; + fn = decorators.inline(fn,props,container,{"name":"inline","hash":{},"fn":container.program(79, data, 0, blockParams, depths),"inverse":container.noop,"args":["cloze-prefix"],"data":data}) || fn; + fn = decorators.inline(fn,props,container,{"name":"inline","hash":{},"fn":container.program(82, data, 0, blockParams, depths),"inverse":container.noop,"args":["cloze-body"],"data":data}) || fn; + fn = decorators.inline(fn,props,container,{"name":"inline","hash":{},"fn":container.program(85, data, 0, blockParams, depths),"inverse":container.noop,"args":["cloze-suffix"],"data":data}) || fn; + fn = decorators.inline(fn,props,container,{"name":"inline","hash":{},"fn":container.program(88, data, 0, blockParams, depths),"inverse":container.noop,"args":["tags"],"data":data}) || fn; + fn = decorators.inline(fn,props,container,{"name":"inline","hash":{},"fn":container.program(90, data, 0, blockParams, depths),"inverse":container.noop,"args":["url"],"data":data}) || fn; return fn; } diff --git a/ext/bg/js/util.js b/ext/bg/js/util.js index 4f907923..4d0aa27e 100644 --- a/ext/bg/js/util.js +++ b/ext/bg/js/util.js @@ -400,6 +400,7 @@ function dictFieldFormat(field, definition, mode, options) { 'expression', 'furigana', 'glossary', + 'glossary-brief', 'kunyomi', 'onyomi', 'reading', diff --git a/tmpl/fields.html b/tmpl/fields.html index ab1a9722..944a43e6 100644 --- a/tmpl/fields.html +++ b/tmpl/fields.html @@ -1,13 +1,17 @@ {{#*inline "glossary-single"}} {{~#if html~}} - {{~#if tags~}}({{#each tags}}{{name}}{{#unless @last}}, {{/unless}}{{/each}}) {{/if~}} + {{~#unless brief~}} + {{~#if tags~}}({{#each tags}}{{name}}{{#unless @last}}, {{/unless}}{{/each}}) {{/if~}} + {{~/unless~}} {{~#if glossary.[1]~}}
      {{#each glossary}}
    • {{#multiLine}}{{.}}{{/multiLine}}
    • {{/each}}
    {{~else~}} {{~#multiLine}}{{glossary.[0]}}{{/multiLine~}} {{~/if~}} {{~else~}} - {{~#if tags~}}({{#each tags}}{{name}}{{#unless @last}}, {{/unless}}{{/each}}) {{/if~}} + {{~#unless brief~}} + {{~#if tags~}}({{#each tags}}{{name}}{{#unless @last}}, {{/unless}}{{/each}}) {{/if~}} + {{~/unless~}} {{~#if glossary.[1]~}} {{#each glossary}}{{.}}{{#unless @last}}, {{/unless}}{{/each}} {{~else~}} @@ -58,18 +62,22 @@ {{~else~}} {{~#if group~}} {{~#if definition.definitions.[1]~}} - {{~#if html}}
      {{#each definition.definitions}}
    1. {{> glossary-single html=../html}}
    2. {{/each}}
    - {{~else}}{{#each definition.definitions}} * {{> glossary-single html=../html}}{{/each}}{{/if~}} + {{~#if html}}
      {{#each definition.definitions}}
    1. {{> glossary-single html=../html brief=../brief}}
    2. {{/each}}
    + {{~else}}{{#each definition.definitions}} * {{> glossary-single html=../html brief=../brief}}{{/each}}{{/if~}} {{~else~}} - {{~> glossary-single definition.definitions.[0] html=html~}} + {{~> glossary-single definition.definitions.[0] html=html brief=brief~}} {{~/if~}} {{~else~}} - {{~> glossary-single definition html=html~}} + {{~> glossary-single definition html=html brief=brief~}} {{~/if~}} {{~/if~}} {{~#if html}}
    {{/if~}} {{/inline}} +{{#*inline "glossary-brief"}} + {{~> glossary brief=true ~}} +{{/inline}} + {{#*inline "kunyomi"}} {{~#each definition.kunyomi}}{{.}}{{#unless @last}}, {{/unless}}{{/each~}} {{/inline}} -- cgit v1.2.3 From 516c7f5381700700dcfdfb06682f11de8205137b Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sun, 16 Jul 2017 12:48:27 -0700 Subject: refactor mixed/js/util.js --- ext/bg/background.html | 2 +- ext/bg/search.html | 2 +- ext/fg/frame.html | 2 +- ext/mixed/js/audio.js | 130 ++++++++++++++++++++++++++++++++++++++++++ ext/mixed/js/display.js | 18 +++++- ext/mixed/js/util.js | 149 ------------------------------------------------ 6 files changed, 149 insertions(+), 154 deletions(-) create mode 100644 ext/mixed/js/audio.js delete mode 100644 ext/mixed/js/util.js diff --git a/ext/bg/background.html b/ext/bg/background.html index 4410c249..4a2d8c8d 100644 --- a/ext/bg/background.html +++ b/ext/bg/background.html @@ -8,7 +8,7 @@ - + diff --git a/ext/bg/search.html b/ext/bg/search.html index 4c07ee61..0fc3caf7 100644 --- a/ext/bg/search.html +++ b/ext/bg/search.html @@ -34,7 +34,7 @@ - + diff --git a/ext/fg/frame.html b/ext/fg/frame.html index c20745af..ecaee323 100644 --- a/ext/fg/frame.html +++ b/ext/fg/frame.html @@ -33,7 +33,7 @@ - + diff --git a/ext/mixed/js/audio.js b/ext/mixed/js/audio.js new file mode 100644 index 00000000..451fe1d9 --- /dev/null +++ b/ext/mixed/js/audio.js @@ -0,0 +1,130 @@ +/* + * Copyright (C) 2017 Alex Yatskov + * Author: Alex Yatskov + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +/* + * Audio + */ + +async function audioBuildUrl(definition, mode, cache={}) { + if (mode === 'jpod101') { + let kana = definition.reading; + let kanji = definition.expression; + + if (!kana && wanakana.isHiragana(kanji)) { + kana = kanji; + kanji = null; + } + + const params = []; + if (kanji) { + params.push(`kanji=${encodeURIComponent(kanji)}`); + } + if (kana) { + params.push(`kana=${encodeURIComponent(kana)}`); + } + + const url = `https://assets.languagepod101.com/dictionary/japanese/audiomp3.php?${params.join('&')}`; + return Promise.resolve(url); + } else if (mode === 'jpod101-alternate') { + return new Promise((resolve, reject) => { + const response = cache[definition.expression]; + if (response) { + resolve(response); + } else { + const data = { + post: 'dictionary_reference', + match_type: 'exact', + search_query: definition.expression + }; + + const params = []; + for (const key in data) { + params.push(`${encodeURIComponent(key)}=${encodeURIComponent(data[key])}`); + } + + 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('load', () => { + cache[definition.expression] = xhr.responseText; + resolve(xhr.responseText); + }); + + xhr.send(params.join('&')); + } + }).then(response => { + const dom = new DOMParser().parseFromString(response, 'text/html'); + for (const row of dom.getElementsByClassName('dc-result-row')) { + try { + const url = row.getElementsByClassName('ill-onebuttonplayer').item(0).getAttribute('data-url'); + const reading = row.getElementsByClassName('dc-vocab_kana').item(0).innerText; + if (url && reading && (!definition.reading || definition.reading === reading)) { + return url; + } + } catch (e) { + // NOP + } + } + }); + } else { + return Promise.resolve(); + } +} + +function audioBuildFilename(definition) { + if (definition.reading || definition.expression) { + let filename = 'yomichan'; + if (definition.reading) { + filename += `_${definition.reading}`; + } + if (definition.expression) { + filename += `_${definition.expression}`; + } + + return filename += '.mp3'; + } +} + +async function audioInject(definition, fields, mode) { + let usesAudio = false; + for (const name in fields) { + if (fields[name].includes('{audio}')) { + usesAudio = true; + break; + } + } + + if (!usesAudio) { + return true; + } + + try { + const url = await audioBuildUrl(definition, mode); + const filename = audioBuildFilename(definition); + + if (url && filename) { + definition.audio = {url, filename}; + } + + return true; + } catch (e) { + return false; + } +} diff --git a/ext/mixed/js/display.js b/ext/mixed/js/display.js index da0cd351..e54bc0f9 100644 --- a/ext/mixed/js/display.js +++ b/ext/mixed/js/display.js @@ -80,7 +80,7 @@ class Display { if (context) { for (const definition of definitions) { if (context.sentence) { - definition.cloze = clozeBuild(context.sentence, definition.source); + definition.cloze = Display.clozeBuild(context.sentence, definition.source); } definition.url = context.url; @@ -119,7 +119,7 @@ class Display { if (context) { for (const definition of definitions) { if (context.sentence) { - definition.cloze = clozeBuild(context.sentence); + definition.cloze = Display.clozeBuild(context.sentence); } definition.url = context.url; @@ -394,6 +394,20 @@ class Display { }).catch(this.handleError.bind(this)).then(() => this.spinner.hide()); } + static clozeBuild(sentence, source) { + const result = { + sentence: sentence.text.trim() + }; + + if (source) { + result.prefix = sentence.text.substring(0, sentence.offset).trim(); + result.body = source.trim(); + result.suffix = sentence.text.substring(sentence.offset + source.length).trim(); + } + + return result; + } + static entryIndexFind(element) { return $('.entry').index(element.closest('.entry')); } diff --git a/ext/mixed/js/util.js b/ext/mixed/js/util.js deleted file mode 100644 index 0a8c914d..00000000 --- a/ext/mixed/js/util.js +++ /dev/null @@ -1,149 +0,0 @@ -/* - * Copyright (C) 2017 Alex Yatskov - * Author: Alex Yatskov - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - - -/* - * Cloze - */ - -function clozeBuild(sentence, source) { - const result = { - sentence: sentence.text.trim() - }; - - if (source) { - result.prefix = sentence.text.substring(0, sentence.offset).trim(); - result.body = source.trim(); - result.suffix = sentence.text.substring(sentence.offset + source.length).trim(); - } - - return result; -} - - -/* - * Audio - */ - -async function audioBuildUrl(definition, mode, cache={}) { - if (mode === 'jpod101') { - let kana = definition.reading; - let kanji = definition.expression; - - if (!kana && wanakana.isHiragana(kanji)) { - kana = kanji; - kanji = null; - } - - const params = []; - if (kanji) { - params.push(`kanji=${encodeURIComponent(kanji)}`); - } - if (kana) { - params.push(`kana=${encodeURIComponent(kana)}`); - } - - const url = `https://assets.languagepod101.com/dictionary/japanese/audiomp3.php?${params.join('&')}`; - return Promise.resolve(url); - } else if (mode === 'jpod101-alternate') { - return new Promise((resolve, reject) => { - const response = cache[definition.expression]; - if (response) { - resolve(response); - } else { - const data = { - post: 'dictionary_reference', - match_type: 'exact', - search_query: definition.expression - }; - - const params = []; - for (const key in data) { - params.push(`${encodeURIComponent(key)}=${encodeURIComponent(data[key])}`); - } - - 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('load', () => { - cache[definition.expression] = xhr.responseText; - resolve(xhr.responseText); - }); - - xhr.send(params.join('&')); - } - }).then(response => { - const dom = new DOMParser().parseFromString(response, 'text/html'); - for (const row of dom.getElementsByClassName('dc-result-row')) { - try { - const url = row.getElementsByClassName('ill-onebuttonplayer').item(0).getAttribute('data-url'); - const reading = row.getElementsByClassName('dc-vocab_kana').item(0).innerText; - if (url && reading && (!definition.reading || definition.reading === reading)) { - return url; - } - } catch (e) { - // NOP - } - } - }); - } else { - return Promise.resolve(); - } -} - -function audioBuildFilename(definition) { - if (definition.reading || definition.expression) { - let filename = 'yomichan'; - if (definition.reading) { - filename += `_${definition.reading}`; - } - if (definition.expression) { - filename += `_${definition.expression}`; - } - - return filename += '.mp3'; - } -} - -async function audioInject(definition, fields, mode) { - let usesAudio = false; - for (const name in fields) { - if (fields[name].includes('{audio}')) { - usesAudio = true; - break; - } - } - - if (!usesAudio) { - return true; - } - - try { - const url = await audioBuildUrl(definition, mode); - const filename = audioBuildFilename(definition); - - if (url && filename) { - definition.audio = {url, filename}; - } - - return true; - } catch (e) { - return false; - } -} -- cgit v1.2.3 From b26aea4d81416e95fbf12c4e8407bff3f25518f7 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sun, 16 Jul 2017 12:50:00 -0700 Subject: . --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 6ac97eea..4d467f82 100644 --- a/README.md +++ b/README.md @@ -132,7 +132,7 @@ Flashcard fields can be configured with the following steps: `{expression}` | Term expressed as Kanji (will be displayed in Kana if Kanji is not available). `{furigana}` | Term expressed as Kanji with Furigana displayed above it (e.g. 日本語にほんご). `{glossary}` | List of definitions for the term (output format depends on whether running in *grouped* mode). - `{glossary-brief}` | Shorter version of `{glossary}`, without `{tags}` + `{glossary-brief}` | Shorter version of `{glossary}`, without `{tags}`. `{reading}` | Kana reading for the term (empty for terms where the expression is the reading). `{sentence}` | Sentence, quote, or phrase in which the term appears in the source content. `{tags}` | Grammar and usage tags providing information about the term (unavailable in *grouped* mode). -- cgit v1.2.3 From 7dd75082abd31b725d9907a93ab97fe1434bab04 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sun, 16 Jul 2017 12:59:16 -0700 Subject: rename options to settings --- ext/bg/js/options.js | 405 -------------------------------------------------- ext/bg/js/settings.js | 405 ++++++++++++++++++++++++++++++++++++++++++++++++++ ext/bg/options.html | 282 ----------------------------------- ext/bg/settings.html | 282 +++++++++++++++++++++++++++++++++++ ext/manifest.json | 2 +- 5 files changed, 688 insertions(+), 688 deletions(-) delete mode 100644 ext/bg/js/options.js create mode 100644 ext/bg/js/settings.js delete mode 100644 ext/bg/options.html create mode 100644 ext/bg/settings.html diff --git a/ext/bg/js/options.js b/ext/bg/js/options.js deleted file mode 100644 index e105f1b2..00000000 --- a/ext/bg/js/options.js +++ /dev/null @@ -1,405 +0,0 @@ -/* - * Copyright (C) 2016 Alex Yatskov - * Author: Alex Yatskov - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - - -/* - * General - */ - -function formRead() { - return optionsLoad().then(optionsOld => { - const optionsNew = $.extend(true, {}, optionsOld); - - optionsNew.general.showGuide = $('#show-usage-guide').prop('checked'); - optionsNew.general.audioSource = $('#audio-playback-source').val(); - optionsNew.general.audioVolume = parseFloat($('#audio-playback-volume').val()); - optionsNew.general.groupResults = $('#group-terms-results').prop('checked'); - optionsNew.general.debugInfo = $('#show-debug-info').prop('checked'); - optionsNew.general.showAdvanced = $('#show-advanced-options').prop('checked'); - optionsNew.general.maxResults = parseInt($('#max-displayed-results').val(), 10); - optionsNew.general.popupWidth = parseInt($('#popup-width').val(), 10); - optionsNew.general.popupHeight = parseInt($('#popup-height').val(), 10); - optionsNew.general.popupOffset = parseInt($('#popup-offset').val(), 10); - - optionsNew.scanning.middleMouse = $('#middle-mouse-button-scan').prop('checked'); - optionsNew.scanning.selectText = $('#select-matched-text').prop('checked'); - optionsNew.scanning.alphanumeric = $('#search-alphanumeric').prop('checked'); - optionsNew.scanning.delay = parseInt($('#scan-delay').val(), 10); - optionsNew.scanning.length = parseInt($('#scan-length').val(), 10); - optionsNew.scanning.modifier = $('#scan-modifier-key').val(); - - optionsNew.anki.enable = $('#anki-enable').prop('checked'); - optionsNew.anki.tags = $('#card-tags').val().split(/[,; ]+/); - optionsNew.anki.htmlCards = $('#generate-html-cards').prop('checked'); - optionsNew.anki.sentenceExt = parseInt($('#sentence-detection-extent').val(), 10); - optionsNew.anki.server = $('#interface-server').val(); - - if (optionsOld.anki.enable && !$('#anki-error').is(':visible')) { - optionsNew.anki.terms.deck = $('#anki-terms-deck').val(); - optionsNew.anki.terms.model = $('#anki-terms-model').val(); - optionsNew.anki.terms.fields = ankiFieldsToDict($('#terms .anki-field-value')); - optionsNew.anki.kanji.deck = $('#anki-kanji-deck').val(); - optionsNew.anki.kanji.model = $('#anki-kanji-model').val(); - optionsNew.anki.kanji.fields = ankiFieldsToDict($('#kanji .anki-field-value')); - } - - $('.dict-group').each((index, element) => { - const dictionary = $(element); - const title = dictionary.data('title'); - const priority = parseInt(dictionary.find('.dict-priority').val(), 10); - const enabled = dictionary.find('.dict-enabled').prop('checked'); - optionsNew.dictionaries[title] = {priority, enabled}; - }); - - return {optionsNew, optionsOld}; - }); -} - -function updateVisibility(options) { - const general = $('#anki-general'); - if (options.anki.enable) { - general.show(); - } else { - general.hide(); - } - - const advanced = $('.options-advanced'); - if (options.general.showAdvanced) { - advanced.show(); - } else { - advanced.hide(); - } - - const debug = $('#debug'); - if (options.general.debugInfo) { - const text = JSON.stringify(options, null, 4); - debug.html(handlebarsEscape(text)); - debug.show(); - } else { - debug.hide(); - } -} - -function onOptionsChanged(e) { - if (!e.originalEvent && !e.isTrigger) { - return; - } - - formRead().then(({optionsNew, optionsOld}) => { - return optionsSave(optionsNew).then(() => { - updateVisibility(optionsNew); - - const ankiUpdated = - optionsNew.anki.enable !== optionsOld.anki.enable || - optionsNew.anki.server !== optionsOld.anki.server; - - if (ankiUpdated) { - ankiErrorShow(null); - ankiSpinnerShow(true); - return ankiDeckAndModelPopulate(optionsNew); - } - }); - }).catch(ankiErrorShow).then(() => ankiSpinnerShow(false)); -} - -$(document).ready(() => { - handlebarsRegister(); - - optionsLoad().then(options => { - $('#show-usage-guide').prop('checked', options.general.showGuide); - $('#audio-playback-source').val(options.general.audioSource); - $('#audio-playback-volume').val(options.general.audioVolume); - $('#group-terms-results').prop('checked', options.general.groupResults); - $('#show-debug-info').prop('checked', options.general.debugInfo); - $('#show-advanced-options').prop('checked', options.general.showAdvanced); - $('#max-displayed-results').val(options.general.maxResults); - $('#popup-width').val(options.general.popupWidth); - $('#popup-height').val(options.general.popupHeight); - $('#popup-offset').val(options.general.popupOffset); - - $('#middle-mouse-button-scan').prop('checked', options.scanning.middleMouse); - $('#select-matched-text').prop('checked', options.scanning.selectText); - $('#search-alphanumeric').prop('checked', options.scanning.alphanumeric); - $('#scan-delay').val(options.scanning.delay); - $('#scan-length').val(options.scanning.length); - $('#scan-modifier-key').val(options.scanning.modifier); - - $('#dict-purge').click(onDictionaryPurge); - $('#dict-file').change(onDictionaryImport); - - $('#anki-enable').prop('checked', options.anki.enable); - $('#card-tags').val(options.anki.tags.join(' ')); - $('#generate-html-cards').prop('checked', options.anki.htmlCards); - $('#sentence-detection-extent').val(options.anki.sentenceExt); - $('#interface-server').val(options.anki.server); - $('input, select').not('.anki-model').change(onOptionsChanged); - $('.anki-model').change(onAnkiModelChanged); - - dictionaryGroupsPopulate(options); - ankiDeckAndModelPopulate(options); - updateVisibility(options); - }); -}); - - -/* - * Dictionary - */ - -function dictionaryErrorShow(error) { - const dialog = $('#dict-error'); - if (error) { - dialog.show().find('span').text(error); - } else { - dialog.hide(); - } -} - -function dictionarySpinnerShow(show) { - const spinner = $('#dict-spinner'); - if (show) { - spinner.show(); - } else { - spinner.hide(); - } -} - -function dictionaryGroupsSort() { - const dictGroups = $('#dict-groups'); - const dictGroupChildren = dictGroups.children('.dict-group').sort((ca, cb) => { - const pa = parseInt($(ca).find('.dict-priority').val(), 10); - const pb = parseInt($(cb).find('.dict-priority').val(), 10); - if (pa < pb) { - return 1; - } else if (pa > pb) { - return -1; - } else { - return 0; - } - }); - - dictGroups.append(dictGroupChildren); -} - -function dictionaryGroupsPopulate(options) { - dictionaryErrorShow(null); - dictionarySpinnerShow(true); - - const dictGroups = $('#dict-groups').empty(); - const dictWarning = $('#dict-warning').hide(); - - return instDb().getDictionaries().then(rows => { - if (rows.length === 0) { - dictWarning.show(); - } - - for (const row of dictRowsSort(rows, options)) { - const dictOptions = options.dictionaries[row.title] || {enabled: false, priority: 0}; - const dictHtml = handlebarsRender('dictionary.html', { - title: row.title, - version: row.version, - revision: row.revision, - priority: dictOptions.priority, - enabled: dictOptions.enabled - }); - - dictGroups.append($(dictHtml)); - } - - updateVisibility(options); - - $('.dict-enabled, .dict-priority').change(e => { - dictionaryGroupsSort(); - onOptionsChanged(e); - }); - }).catch(dictionaryErrorShow).then(() => dictionarySpinnerShow(false)); -} - -function onDictionaryPurge(e) { - e.preventDefault(); - - dictionaryErrorShow(null); - dictionarySpinnerShow(true); - - const dictControls = $('#dict-importer, #dict-groups').hide(); - const dictProgress = $('#dict-purge-progress').show(); - - instDb().purge().catch(dictionaryErrorShow).then(() => { - dictionarySpinnerShow(false); - dictControls.show(); - dictProgress.hide(); - return optionsLoad(); - }).then(options => { - options.dictionaries = {}; - optionsSave(options).then(() => dictionaryGroupsPopulate(options)); - }); -} - -function onDictionaryImport(e) { - dictionaryErrorShow(null); - dictionarySpinnerShow(true); - - const dictFile = $('#dict-file'); - const dictImporter = $('#dict-importer').hide(); - const dictProgress = $('#dict-import-progress').show(); - const setProgress = percent => dictProgress.find('.progress-bar').css('width', `${percent}%`); - const updateProgress = (total, current) => setProgress(current / total * 100.0); - - setProgress(0.0); - - optionsLoad().then(options => { - return instDb().importDictionary(e.target.files[0], updateProgress).then(summary => { - options.dictionaries[summary.title] = {enabled: true, priority: 0}; - return optionsSave(options); - }).then(() => dictionaryGroupsPopulate(options)); - }).catch(dictionaryErrorShow).then(() => { - dictFile.val(''); - dictionarySpinnerShow(false); - dictProgress.hide(); - dictImporter.show(); - }); -} - -/* - * Anki - */ - -function ankiSpinnerShow(show) { - const spinner = $('#anki-spinner'); - if (show) { - spinner.show(); - } else { - spinner.hide(); - } -} - -function ankiErrorShow(error) { - const dialog = $('#anki-error'); - if (error) { - dialog.show().find('span').text(error); - } - else { - dialog.hide(); - } -} - -function ankiFieldsToDict(selection) { - const result = {}; - selection.each((index, element) => { - result[$(element).data('field')] = $(element).val(); - }); - - return result; -} - -function ankiDeckAndModelPopulate(options) { - ankiErrorShow(null); - ankiSpinnerShow(true); - - const ankiFormat = $('#anki-format').hide(); - return Promise.all([instAnki().getDeckNames(), instAnki().getModelNames()]).then(([deckNames, modelNames]) => { - const ankiDeck = $('.anki-deck'); - ankiDeck.find('option').remove(); - deckNames.sort().forEach(name => ankiDeck.append($('
    - + diff --git a/ext/bg/settings.html b/ext/bg/settings.html index 4c7198c3..9c3995eb 100644 --- a/ext/bg/settings.html +++ b/ext/bg/settings.html @@ -276,7 +276,7 @@ - + -- cgit v1.2.3 From 8bcc4ddf19a6c171ad1cf5d18b165da391089fd2 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Thu, 20 Jul 2017 21:21:22 -0700 Subject: cleanup --- ext/fg/frame.html | 3 +- ext/fg/js/background.js | 63 ++++++++++++++ ext/fg/js/document.js | 162 ++++++++++++++++++++++++++++++++++++ ext/fg/js/util.js | 215 ------------------------------------------------ ext/manifest.json | 3 +- 5 files changed, 229 insertions(+), 217 deletions(-) create mode 100644 ext/fg/js/background.js create mode 100644 ext/fg/js/document.js delete mode 100644 ext/fg/js/util.js diff --git a/ext/fg/frame.html b/ext/fg/frame.html index ecaee323..80e69967 100644 --- a/ext/fg/frame.html +++ b/ext/fg/frame.html @@ -32,9 +32,10 @@ - + + diff --git a/ext/fg/js/background.js b/ext/fg/js/background.js new file mode 100644 index 00000000..52b533be --- /dev/null +++ b/ext/fg/js/background.js @@ -0,0 +1,63 @@ +/* + * Copyright (C) 2016 Alex Yatskov + * Author: Alex Yatskov + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +function bgInvoke(action, params={}) { + return new Promise((resolve, reject) => { + try { + chrome.runtime.sendMessage({action, params}, ({result, error}) => { + if (error) { + reject(error); + } else { + resolve(result); + } + }); + } catch (e) { + window.orphaned = true; + reject(e.message); + } + }); +} + +function bgOptionsGet() { + return bgInvoke('optionsGet'); +} + +function bgTermsFind(text) { + return bgInvoke('termsFind', {text}); +} + +function bgKanjiFind(text) { + return bgInvoke('kanjiFind', {text}); +} + +function bgTemplateRender(template, data) { + return bgInvoke('templateRender', {data, template}); +} + +function bgDefinitionsAddable(definitions, modes) { + return bgInvoke('definitionsAddable', {definitions, modes}).catch(() => null); +} + +function bgDefinitionAdd(definition, mode) { + return bgInvoke('definitionAdd', {definition, mode}); +} + +function bgNoteView(noteId) { + return bgInvoke('noteView', {noteId}); +} diff --git a/ext/fg/js/document.js b/ext/fg/js/document.js new file mode 100644 index 00000000..582b6770 --- /dev/null +++ b/ext/fg/js/document.js @@ -0,0 +1,162 @@ +/* + * Copyright (C) 2016 Alex Yatskov + * Author: Alex Yatskov + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +function docOffsetCalc(element) { + const scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop; + const scrollLeft = window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft; + + const clientTop = document.documentElement.clientTop || document.body.clientTop || 0; + const clientLeft = document.documentElement.clientLeft || document.body.clientLeft || 0; + + const rect = element.getBoundingClientRect(); + const top = Math.round(rect.top + scrollTop - clientTop); + const left = Math.round(rect.left + scrollLeft - clientLeft); + + return {top, left}; +} + +function docImposterCreate(element) { + const styleProps = window.getComputedStyle(element); + const stylePairs = []; + for (const key of styleProps) { + stylePairs.push(`${key}: ${styleProps[key]};`); + } + + const offset = docOffsetCalc(element); + const imposter = document.createElement('div'); + imposter.className = 'yomichan-imposter'; + imposter.innerText = element.value; + imposter.style.cssText = stylePairs.join('\n'); + imposter.style.position = 'absolute'; + imposter.style.top = `${offset.top}px`; + imposter.style.left = `${offset.left}px`; + imposter.style.zIndex = 2147483646; + if (element.nodeName === 'TEXTAREA' && styleProps.overflow === 'visible') { + imposter.style.overflow = 'auto'; + } + + document.body.appendChild(imposter); + imposter.scrollTop = element.scrollTop; + imposter.scrollLeft = element.scrollLeft; +} + +function docImposterDestroy() { + for (const element of document.getElementsByClassName('yomichan-imposter')) { + element.parentNode.removeChild(element); + } +} + +function docRangeFromPoint(point) { + const element = document.elementFromPoint(point.x, point.y); + if (element) { + if (element.nodeName === 'IMG' || element.nodeName === 'BUTTON') { + return new TextSourceElement(element); + } else if (element.nodeName === 'INPUT' || element.nodeName === 'TEXTAREA') { + docImposterCreate(element); + } + } + + if (!document.caretRangeFromPoint) { + document.caretRangeFromPoint = (x, y) => { + const position = document.caretPositionFromPoint(x,y); + if (position) { + const range = document.createRange(); + range.setStart(position.offsetNode, position.offset); + range.setEnd(position.offsetNode, position.offset); + return range; + } + }; + } + + const range = document.caretRangeFromPoint(point.x, point.y); + if (range) { + return new TextSourceRange(range); + } +} + +function docSentenceExtract(source, extent) { + const quotesFwd = {'「': '」', '『': '』', "'": "'", '"': '"'}; + const quotesBwd = {'」': '「', '』': '『', "'": "'", '"': '"'}; + const terminators = '…。..??!!'; + + const sourceLocal = source.clone(); + const position = sourceLocal.setStartOffset(extent); + sourceLocal.setEndOffset(position + extent); + const content = sourceLocal.text(); + + let quoteStack = []; + + let startPos = 0; + for (let i = position; i >= startPos; --i) { + const c = content[i]; + + if (c === '\n') { + startPos = i + 1; + break; + } + + if (quoteStack.length === 0 && (terminators.includes(c) || c in quotesFwd)) { + startPos = i + 1; + break; + } + + if (quoteStack.length > 0 && c === quoteStack[0]) { + quoteStack.pop(); + } else if (c in quotesBwd) { + quoteStack = [quotesBwd[c]].concat(quoteStack); + } + } + + quoteStack = []; + + let endPos = content.length; + for (let i = position; i <= endPos; ++i) { + const c = content[i]; + + if (c === '\n') { + endPos = i + 1; + break; + } + + if (quoteStack.length === 0) { + if (terminators.includes(c)) { + endPos = i + 1; + break; + } + else if (c in quotesBwd) { + endPos = i; + break; + } + } + + if (quoteStack.length > 0 && c === quoteStack[0]) { + quoteStack.pop(); + } else if (c in quotesFwd) { + quoteStack = [quotesFwd[c]].concat(quoteStack); + } + } + + const text = content.substring(startPos, endPos); + const padding = text.length - text.replace(/^\s+/, '').length; + + return { + text: text.trim(), + offset: position - startPos - padding + }; +} diff --git a/ext/fg/js/util.js b/ext/fg/js/util.js deleted file mode 100644 index 2acd81c4..00000000 --- a/ext/fg/js/util.js +++ /dev/null @@ -1,215 +0,0 @@ -/* - * Copyright (C) 2016 Alex Yatskov - * Author: Alex Yatskov - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - - -/* - * Background - */ - -function bgInvoke(action, params) { - return new Promise((resolve, reject) => { - try { - chrome.runtime.sendMessage({action, params}, ({result, error}) => { - if (error) { - reject(error); - } else { - resolve(result); - } - }); - } catch (e) { - window.orphaned = true; - reject(e.message); - } - }); -} - -function bgOptionsGet() { - return bgInvoke('optionsGet', {}); -} - -function bgTermsFind(text) { - return bgInvoke('termsFind', {text}); -} - -function bgKanjiFind(text) { - return bgInvoke('kanjiFind', {text}); -} - -function bgTemplateRender(template, data) { - return bgInvoke('templateRender', {data, template}); -} - -function bgDefinitionsAddable(definitions, modes) { - return bgInvoke('definitionsAddable', {definitions, modes}).catch(() => null); -} - -function bgDefinitionAdd(definition, mode) { - return bgInvoke('definitionAdd', {definition, mode}); -} - -function bgNoteView(noteId) { - return bgInvoke('noteView', {noteId}); -} - -/* - * Document - */ - -function docOffsetCalc(element) { - const scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop; - const scrollLeft = window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft; - - const clientTop = document.documentElement.clientTop || document.body.clientTop || 0; - const clientLeft = document.documentElement.clientLeft || document.body.clientLeft || 0; - - const rect = element.getBoundingClientRect(); - const top = Math.round(rect.top + scrollTop - clientTop); - const left = Math.round(rect.left + scrollLeft - clientLeft); - - return {top, left}; -} - -function docImposterCreate(element) { - const styleProps = window.getComputedStyle(element); - const stylePairs = []; - for (const key of styleProps) { - stylePairs.push(`${key}: ${styleProps[key]};`); - } - - const offset = docOffsetCalc(element); - const imposter = document.createElement('div'); - imposter.className = 'yomichan-imposter'; - imposter.innerText = element.value; - imposter.style.cssText = stylePairs.join('\n'); - imposter.style.position = 'absolute'; - imposter.style.top = `${offset.top}px`; - imposter.style.left = `${offset.left}px`; - imposter.style.zIndex = 2147483646; - if (element.nodeName === 'TEXTAREA' && styleProps.overflow === 'visible') { - imposter.style.overflow = 'auto'; - } - - document.body.appendChild(imposter); - imposter.scrollTop = element.scrollTop; - imposter.scrollLeft = element.scrollLeft; -} - -function docImposterDestroy() { - for (const element of document.getElementsByClassName('yomichan-imposter')) { - element.parentNode.removeChild(element); - } -} - -function docRangeFromPoint(point) { - const element = document.elementFromPoint(point.x, point.y); - if (element) { - if (element.nodeName === 'IMG' || element.nodeName === 'BUTTON') { - return new TextSourceElement(element); - } else if (element.nodeName === 'INPUT' || element.nodeName === 'TEXTAREA') { - docImposterCreate(element); - } - } - - if (!document.caretRangeFromPoint) { - document.caretRangeFromPoint = (x, y) => { - const position = document.caretPositionFromPoint(x,y); - if (position) { - const range = document.createRange(); - range.setStart(position.offsetNode, position.offset); - range.setEnd(position.offsetNode, position.offset); - return range; - } - }; - } - - const range = document.caretRangeFromPoint(point.x, point.y); - if (range) { - return new TextSourceRange(range); - } -} - -function docSentenceExtract(source, extent) { - const quotesFwd = {'「': '」', '『': '』', "'": "'", '"': '"'}; - const quotesBwd = {'」': '「', '』': '『', "'": "'", '"': '"'}; - const terminators = '…。..??!!'; - - const sourceLocal = source.clone(); - const position = sourceLocal.setStartOffset(extent); - sourceLocal.setEndOffset(position + extent); - const content = sourceLocal.text(); - - let quoteStack = []; - - let startPos = 0; - for (let i = position; i >= startPos; --i) { - const c = content[i]; - - if (c === '\n') { - startPos = i + 1; - break; - } - - if (quoteStack.length === 0 && (terminators.includes(c) || c in quotesFwd)) { - startPos = i + 1; - break; - } - - if (quoteStack.length > 0 && c === quoteStack[0]) { - quoteStack.pop(); - } else if (c in quotesBwd) { - quoteStack = [quotesBwd[c]].concat(quoteStack); - } - } - - quoteStack = []; - - let endPos = content.length; - for (let i = position; i <= endPos; ++i) { - const c = content[i]; - - if (c === '\n') { - endPos = i + 1; - break; - } - - if (quoteStack.length === 0) { - if (terminators.includes(c)) { - endPos = i + 1; - break; - } - else if (c in quotesBwd) { - endPos = i; - break; - } - } - - if (quoteStack.length > 0 && c === quoteStack[0]) { - quoteStack.pop(); - } else if (c in quotesFwd) { - quoteStack = [quotesFwd[c]].concat(quoteStack); - } - } - - const text = content.substring(startPos, endPos); - const padding = text.length - text.replace(/^\s+/, '').length; - - return { - text: text.trim(), - offset: position - startPos - padding - }; -} diff --git a/ext/manifest.json b/ext/manifest.json index be9f3208..7a277fb2 100644 --- a/ext/manifest.json +++ b/ext/manifest.json @@ -15,9 +15,10 @@ "content_scripts": [{ "matches": ["http://*/*", "https://*/*", "file://*/*"], "js": [ - "fg/js/util.js", + "fg/js/document.js", "fg/js/source-range.js", "fg/js/source-element.js", + "fg/js/background.js", "fg/js/popup.js", "fg/js/driver.js" ], -- cgit v1.2.3 From d6c2f1cc38a2490afe79b0d8e1f24ad214ee9534 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Thu, 20 Jul 2017 21:29:27 -0700 Subject: cleanup --- ext/fg/js/driver.js | 249 -------------------------------------------------- ext/fg/js/frontend.js | 249 ++++++++++++++++++++++++++++++++++++++++++++++++++ ext/manifest.json | 2 +- 3 files changed, 250 insertions(+), 250 deletions(-) delete mode 100644 ext/fg/js/driver.js create mode 100644 ext/fg/js/frontend.js diff --git a/ext/fg/js/driver.js b/ext/fg/js/driver.js deleted file mode 100644 index b0cc4613..00000000 --- a/ext/fg/js/driver.js +++ /dev/null @@ -1,249 +0,0 @@ -/* - * Copyright (C) 2016 Alex Yatskov - * Author: Alex Yatskov - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - - -window.driver = new class { - constructor() { - this.popup = new Popup(); - this.popupTimer = null; - this.lastMousePos = null; - this.mouseDownLeft = false; - this.mouseDownMiddle = false; - this.lastTextSource = null; - this.pendingLookup = false; - this.options = null; - - bgOptionsGet().then(options => { - this.options = options; - window.addEventListener('mouseover', this.onMouseOver.bind(this)); - window.addEventListener('mousedown', this.onMouseDown.bind(this)); - window.addEventListener('mouseup', this.onMouseUp.bind(this)); - window.addEventListener('mousemove', this.onMouseMove.bind(this)); - window.addEventListener('resize', e => this.searchClear()); - window.addEventListener('message', this.onFrameMessage.bind(this)); - chrome.runtime.onMessage.addListener(this.onBgMessage.bind(this)); - }).catch(this.handleError.bind(this)); - } - - popupTimerSet(callback) { - this.popupTimerClear(); - this.popupTimer = window.setTimeout(callback, this.options.scanning.delay); - } - - popupTimerClear() { - if (this.popupTimer) { - window.clearTimeout(this.popupTimer); - this.popupTimer = null; - } - } - - onMouseOver(e) { - if (e.target === this.popup.container && this.popupTimer) { - this.popupTimerClear(); - } - } - - onMouseMove(e) { - this.lastMousePos = {x: e.clientX, y: e.clientY}; - this.popupTimerClear(); - - if (!this.options.general.enable) { - return; - } - - if (this.mouseDownLeft) { - return; - } - - const mouseScan = this.mouseDownMiddle && this.options.scanning.middleMouse; - const keyScan = - this.options.scanning.modifier === 'alt' && e.altKey || - this.options.scanning.modifier === 'ctrl' && e.ctrlKey || - this.options.scanning.modifier === 'shift' && e.shiftKey || - this.options.scanning.modifier === 'none'; - - if (!keyScan && !mouseScan) { - return; - } - - const searchFunc = () => this.searchAt(this.lastMousePos); - if (this.options.scanning.modifier === 'none') { - this.popupTimerSet(searchFunc); - } else { - searchFunc(); - } - } - - onMouseDown(e) { - this.lastMousePos = {x: e.clientX, y: e.clientY}; - this.popupTimerClear(); - this.searchClear(); - - if (e.which === 1) { - this.mouseDownLeft = true; - } else if (e.which === 2) { - this.mouseDownMiddle = true; - } - } - - onMouseUp(e) { - if (e.which === 1) { - this.mouseDownLeft = false; - } else if (e.which === 2) { - this.mouseDownMiddle = false; - } - } - - onFrameMessage(e) { - const handlers = { - popupClose: () => { - this.searchClear(); - }, - - selectionCopy: () => { - document.execCommand('copy'); - } - }; - - const handler = handlers[e.data]; - if (handler) { - handler(); - } - } - - onBgMessage({action, params}, sender, callback) { - const handlers = { - optionsSet: options => { - this.options = options; - if (!this.options.enable) { - this.searchClear(); - } - } - }; - - const handler = handlers[action]; - if (handler) { - handler(params); - } - - callback(); - } - - searchAt(point) { - if (this.pendingLookup) { - return; - } - - const textSource = docRangeFromPoint(point); - if (!textSource || !textSource.containsPoint(point)) { - docImposterDestroy(); - return; - } - - if (this.lastTextSource && this.lastTextSource.equals(textSource)) { - return; - } - - this.pendingLookup = true; - this.searchTerms(textSource).then(found => { - if (!found) { - return this.searchKanji(textSource); - } - }).catch(error => { - this.handleError(error, textSource); - }).then(() => { - docImposterDestroy(); - this.pendingLookup = false; - }); - } - - searchTerms(textSource) { - textSource.setEndOffset(this.options.scanning.length); - - return bgTermsFind(textSource.text()).then(({definitions, length}) => { - if (definitions.length === 0) { - return false; - } else { - textSource.setEndOffset(length); - - const sentence = docSentenceExtract(textSource, this.options.anki.sentenceExt); - const url = window.location.href; - this.popup.showTermDefs( - textSource.getRect(), - definitions, - this.options, - {sentence, url} - ); - - this.lastTextSource = textSource; - if (this.options.scanning.selectText) { - textSource.select(); - } - - return true; - } - }); - } - - searchKanji(textSource) { - textSource.setEndOffset(1); - - return bgKanjiFind(textSource.text()).then(definitions => { - if (definitions.length === 0) { - return false; - } else { - const sentence = docSentenceExtract(textSource, this.options.anki.sentenceExt); - const url = window.location.href; - this.popup.showKanjiDefs( - textSource.getRect(), - definitions, - this.options, - {sentence, url} - ); - - this.lastTextSource = textSource; - if (this.options.scanning.selectText) { - textSource.select(); - } - - return true; - } - }); - } - - searchClear() { - docImposterDestroy(); - this.popup.hide(); - - if (this.options.scanning.selectText && this.lastTextSource) { - this.lastTextSource.deselect(); - } - - this.lastTextSource = null; - } - - handleError(error, textSource) { - if (window.orphaned) { - if (textSource && this.options.scanning.modifier !== 'none') { - this.popup.showOrphaned(textSource.getRect(), this.options); - } - } else { - window.alert(`Error: ${error}`); - } - } -}; diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js new file mode 100644 index 00000000..5a51af6d --- /dev/null +++ b/ext/fg/js/frontend.js @@ -0,0 +1,249 @@ +/* + * Copyright (C) 2016 Alex Yatskov + * Author: Alex Yatskov + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +window.frontend = new class { + constructor() { + this.popup = new Popup(); + this.popupTimer = null; + this.lastMousePos = null; + this.mouseDownLeft = false; + this.mouseDownMiddle = false; + this.lastTextSource = null; + this.pendingLookup = false; + this.options = null; + + bgOptionsGet().then(options => { + this.options = options; + window.addEventListener('mouseover', this.onMouseOver.bind(this)); + window.addEventListener('mousedown', this.onMouseDown.bind(this)); + window.addEventListener('mouseup', this.onMouseUp.bind(this)); + window.addEventListener('mousemove', this.onMouseMove.bind(this)); + window.addEventListener('resize', e => this.searchClear()); + window.addEventListener('message', this.onFrameMessage.bind(this)); + chrome.runtime.onMessage.addListener(this.onBgMessage.bind(this)); + }).catch(this.handleError.bind(this)); + } + + popupTimerSet(callback) { + this.popupTimerClear(); + this.popupTimer = window.setTimeout(callback, this.options.scanning.delay); + } + + popupTimerClear() { + if (this.popupTimer) { + window.clearTimeout(this.popupTimer); + this.popupTimer = null; + } + } + + onMouseOver(e) { + if (e.target === this.popup.container && this.popupTimer) { + this.popupTimerClear(); + } + } + + onMouseMove(e) { + this.lastMousePos = {x: e.clientX, y: e.clientY}; + this.popupTimerClear(); + + if (!this.options.general.enable) { + return; + } + + if (this.mouseDownLeft) { + return; + } + + const mouseScan = this.mouseDownMiddle && this.options.scanning.middleMouse; + const keyScan = + this.options.scanning.modifier === 'alt' && e.altKey || + this.options.scanning.modifier === 'ctrl' && e.ctrlKey || + this.options.scanning.modifier === 'shift' && e.shiftKey || + this.options.scanning.modifier === 'none'; + + if (!keyScan && !mouseScan) { + return; + } + + const searchFunc = () => this.searchAt(this.lastMousePos); + if (this.options.scanning.modifier === 'none') { + this.popupTimerSet(searchFunc); + } else { + searchFunc(); + } + } + + onMouseDown(e) { + this.lastMousePos = {x: e.clientX, y: e.clientY}; + this.popupTimerClear(); + this.searchClear(); + + if (e.which === 1) { + this.mouseDownLeft = true; + } else if (e.which === 2) { + this.mouseDownMiddle = true; + } + } + + onMouseUp(e) { + if (e.which === 1) { + this.mouseDownLeft = false; + } else if (e.which === 2) { + this.mouseDownMiddle = false; + } + } + + onFrameMessage(e) { + const handlers = { + popupClose: () => { + this.searchClear(); + }, + + selectionCopy: () => { + document.execCommand('copy'); + } + }; + + const handler = handlers[e.data]; + if (handler) { + handler(); + } + } + + onBgMessage({action, params}, sender, callback) { + const handlers = { + optionsSet: options => { + this.options = options; + if (!this.options.enable) { + this.searchClear(); + } + } + }; + + const handler = handlers[action]; + if (handler) { + handler(params); + } + + callback(); + } + + searchAt(point) { + if (this.pendingLookup) { + return; + } + + const textSource = docRangeFromPoint(point); + if (!textSource || !textSource.containsPoint(point)) { + docImposterDestroy(); + return; + } + + if (this.lastTextSource && this.lastTextSource.equals(textSource)) { + return; + } + + this.pendingLookup = true; + this.searchTerms(textSource).then(found => { + if (!found) { + return this.searchKanji(textSource); + } + }).catch(error => { + this.handleError(error, textSource); + }).then(() => { + docImposterDestroy(); + this.pendingLookup = false; + }); + } + + searchTerms(textSource) { + textSource.setEndOffset(this.options.scanning.length); + + return bgTermsFind(textSource.text()).then(({definitions, length}) => { + if (definitions.length === 0) { + return false; + } else { + textSource.setEndOffset(length); + + const sentence = docSentenceExtract(textSource, this.options.anki.sentenceExt); + const url = window.location.href; + this.popup.showTermDefs( + textSource.getRect(), + definitions, + this.options, + {sentence, url} + ); + + this.lastTextSource = textSource; + if (this.options.scanning.selectText) { + textSource.select(); + } + + return true; + } + }); + } + + searchKanji(textSource) { + textSource.setEndOffset(1); + + return bgKanjiFind(textSource.text()).then(definitions => { + if (definitions.length === 0) { + return false; + } else { + const sentence = docSentenceExtract(textSource, this.options.anki.sentenceExt); + const url = window.location.href; + this.popup.showKanjiDefs( + textSource.getRect(), + definitions, + this.options, + {sentence, url} + ); + + this.lastTextSource = textSource; + if (this.options.scanning.selectText) { + textSource.select(); + } + + return true; + } + }); + } + + searchClear() { + docImposterDestroy(); + this.popup.hide(); + + if (this.options.scanning.selectText && this.lastTextSource) { + this.lastTextSource.deselect(); + } + + this.lastTextSource = null; + } + + handleError(error, textSource) { + if (window.orphaned) { + if (textSource && this.options.scanning.modifier !== 'none') { + this.popup.showOrphaned(textSource.getRect(), this.options); + } + } else { + window.alert(`Error: ${error}`); + } + } +}; diff --git a/ext/manifest.json b/ext/manifest.json index 7a277fb2..a318e539 100644 --- a/ext/manifest.json +++ b/ext/manifest.json @@ -20,7 +20,7 @@ "fg/js/source-element.js", "fg/js/background.js", "fg/js/popup.js", - "fg/js/driver.js" + "fg/js/frontend.js" ], "css": ["fg/css/client.css"] }], -- cgit v1.2.3 From a0e2d9cb721d44a89715f3a9df10135c2aebfe29 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Thu, 20 Jul 2017 21:32:17 -0700 Subject: cleanup --- ext/bg/background.html | 2 +- ext/bg/js/backend.js | 250 +++++++++++++++++++++++++++++++++++++++++++++++++ ext/bg/js/instance.js | 2 +- ext/bg/js/yomichan.js | 250 ------------------------------------------------- ext/fg/js/frontend.js | 2 +- 5 files changed, 253 insertions(+), 253 deletions(-) create mode 100644 ext/bg/js/backend.js delete mode 100644 ext/bg/js/yomichan.js diff --git a/ext/bg/background.html b/ext/bg/background.html index de3cbf20..27d9bc41 100644 --- a/ext/bg/background.html +++ b/ext/bg/background.html @@ -21,6 +21,6 @@ - + diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js new file mode 100644 index 00000000..f5415d93 --- /dev/null +++ b/ext/bg/js/backend.js @@ -0,0 +1,250 @@ +/* + * Copyright (C) 2016 Alex Yatskov + * Author: Alex Yatskov + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +window.yomichanBackend = new class { + constructor() { + handlebarsRegister(); + + this.translator = new Translator(); + this.anki = new AnkiNull(); + this.options = null; + + this.translator.prepare().then(optionsLoad).then(options => { + this.optionsSet(options); + + chrome.commands.onCommand.addListener(this.onCommand.bind(this)); + chrome.runtime.onMessage.addListener(this.onMessage.bind(this)); + + if (this.options.general.showGuide) { + chrome.tabs.create({url: chrome.extension.getURL('/bg/guide.html')}); + } + }); + } + + optionsSet(options) { + // In Firefox, setting options from the options UI somehow carries references + // to the DOM across to the background page, causing the options object to + // become a "DeadObject" after the options page is closed. The workaround used + // here is to create a deep copy of the options object. + this.options = JSON.parse(JSON.stringify(options)); + + if (!this.options.general.enable) { + chrome.browserAction.setBadgeBackgroundColor({color: '#d9534f'}); + chrome.browserAction.setBadgeText({text: 'off'}); + } else if (!dictConfigured(this.options)) { + chrome.browserAction.setBadgeBackgroundColor({color: '#f0ad4e'}); + chrome.browserAction.setBadgeText({text: '!'}); + } else { + chrome.browserAction.setBadgeText({text: ''}); + } + + if (this.options.anki.enable) { + this.anki = new AnkiConnect(this.options.anki.server); + } else { + this.anki = new AnkiNull(); + } + + chrome.tabs.query({}, tabs => { + for (const tab of tabs) { + chrome.tabs.sendMessage(tab.id, {action: 'optionsSet', params: options}, () => null); + } + }); + } + + noteFormat(definition, mode) { + const note = { + fields: {}, + tags: this.options.anki.tags + }; + + let fields = []; + if (mode === 'kanji') { + fields = this.options.anki.kanji.fields; + note.deckName = this.options.anki.kanji.deck; + note.modelName = this.options.anki.kanji.model; + } else { + fields = this.options.anki.terms.fields; + note.deckName = this.options.anki.terms.deck; + note.modelName = this.options.anki.terms.model; + + if (definition.audio) { + const audio = { + url: definition.audio.url, + filename: definition.audio.filename, + skipHash: '7e2c2f954ef6051373ba916f000168dc', + fields: [] + }; + + for (const name in fields) { + if (fields[name].includes('{audio}')) { + audio.fields.push(name); + } + } + + if (audio.fields.length > 0) { + note.audio = audio; + } + } + } + + for (const name in fields) { + note.fields[name] = dictFieldFormat( + fields[name], + definition, + mode, + this.options + ); + } + + return note; + } + + termsFind(text) { + const searcher = this.options.general.groupResults ? + this.translator.findTermsGrouped.bind(this.translator) : + this.translator.findTerms.bind(this.translator); + + return searcher(text, dictEnabledSet(this.options), this.options.scanning.alphanumeric).then(({definitions, length}) => { + return {length, definitions: definitions.slice(0, this.options.general.maxResults)}; + }); + } + + kanjiFind(text) { + return this.translator.findKanji(text, dictEnabledSet(this.options)).then(definitions => { + return definitions.slice(0, this.options.general.maxResults); + }); + } + + definitionAdd(definition, mode) { + let promise = Promise.resolve(); + if (mode !== 'kanji') { + promise = audioInject(definition, this.options.anki.terms.fields, this.options.general.audioSource); + } + + return promise.then(() => { + const note = this.noteFormat(definition, mode); + return this.anki.addNote(note); + }); + } + + definitionsAddable(definitions, modes) { + const notes = []; + for (const definition of definitions) { + for (const mode of modes) { + notes.push(this.noteFormat(definition, mode)); + } + } + + return this.anki.canAddNotes(notes).then(raw => { + const states = []; + for (let resultBase = 0; resultBase < raw.length; resultBase += modes.length) { + const state = {}; + for (let modeOffset = 0; modeOffset < modes.length; ++modeOffset) { + state[modes[modeOffset]] = raw[resultBase + modeOffset]; + } + + states.push(state); + } + + return states; + }); + } + + noteView(noteId) { + return this.anki.guiBrowse(`nid:${noteId}`); + } + + templateRender(template, data) { + return Promise.resolve(handlebarsRender(template, data)); + } + + onCommand(command) { + const handlers = { + search: () => { + chrome.tabs.create({url: chrome.extension.getURL('/bg/search.html')}); + }, + + help: () => { + chrome.tabs.create({url: 'https://foosoft.net/projects/yomichan/'}); + }, + + options: () => { + chrome.runtime.openOptionsPage(); + }, + + toggle: () => { + this.options.general.enable = !this.options.general.enable; + optionsSave(this.options).then(() => this.optionsSet(this.options)); + } + }; + + const handler = handlers[command]; + if (handler) { + handler(); + } + } + + onMessage({action, params}, sender, callback) { + const promiseCallback = (promise, callback) => { + return promise.then(result => { + callback({result}); + }).catch(error => { + callback({error}); + }); + }; + + const handlers = { + optionsGet: ({callback}) => { + promiseCallback(optionsLoad(), callback); + }, + + kanjiFind: ({text, callback}) => { + promiseCallback(this.kanjiFind(text), callback); + }, + + termsFind: ({text, callback}) => { + promiseCallback(this.termsFind(text), callback); + }, + + templateRender: ({template, data, callback}) => { + promiseCallback(this.templateRender(template, data), callback); + }, + + definitionAdd: ({definition, mode, callback}) => { + promiseCallback(this.definitionAdd(definition, mode), callback); + }, + + definitionsAddable: ({definitions, modes, callback}) => { + promiseCallback(this.definitionsAddable(definitions, modes), callback); + }, + + noteView: ({noteId}) => { + promiseCallback(this.noteView(noteId), callback); + } + }; + + const handler = handlers[action]; + if (handler) { + params.callback = callback; + handler(params); + } + + return true; + } +}; diff --git a/ext/bg/js/instance.js b/ext/bg/js/instance.js index 0df267cc..bf858fbf 100644 --- a/ext/bg/js/instance.js +++ b/ext/bg/js/instance.js @@ -18,7 +18,7 @@ function instYomi() { - return chrome.extension.getBackgroundPage().yomichan; + return chrome.extension.getBackgroundPage().yomichanBackend; } function instDb() { diff --git a/ext/bg/js/yomichan.js b/ext/bg/js/yomichan.js deleted file mode 100644 index 214bdef3..00000000 --- a/ext/bg/js/yomichan.js +++ /dev/null @@ -1,250 +0,0 @@ -/* - * Copyright (C) 2016 Alex Yatskov - * Author: Alex Yatskov - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - - -window.yomichan = new class { - constructor() { - handlebarsRegister(); - - this.translator = new Translator(); - this.anki = new AnkiNull(); - this.options = null; - - this.translator.prepare().then(optionsLoad).then(options => { - this.optionsSet(options); - - chrome.commands.onCommand.addListener(this.onCommand.bind(this)); - chrome.runtime.onMessage.addListener(this.onMessage.bind(this)); - - if (this.options.general.showGuide) { - chrome.tabs.create({url: chrome.extension.getURL('/bg/guide.html')}); - } - }); - } - - optionsSet(options) { - // In Firefox, setting options from the options UI somehow carries references - // to the DOM across to the background page, causing the options object to - // become a "DeadObject" after the options page is closed. The workaround used - // here is to create a deep copy of the options object. - this.options = JSON.parse(JSON.stringify(options)); - - if (!this.options.general.enable) { - chrome.browserAction.setBadgeBackgroundColor({color: '#d9534f'}); - chrome.browserAction.setBadgeText({text: 'off'}); - } else if (!dictConfigured(this.options)) { - chrome.browserAction.setBadgeBackgroundColor({color: '#f0ad4e'}); - chrome.browserAction.setBadgeText({text: '!'}); - } else { - chrome.browserAction.setBadgeText({text: ''}); - } - - if (this.options.anki.enable) { - this.anki = new AnkiConnect(this.options.anki.server); - } else { - this.anki = new AnkiNull(); - } - - chrome.tabs.query({}, tabs => { - for (const tab of tabs) { - chrome.tabs.sendMessage(tab.id, {action: 'optionsSet', params: options}, () => null); - } - }); - } - - noteFormat(definition, mode) { - const note = { - fields: {}, - tags: this.options.anki.tags - }; - - let fields = []; - if (mode === 'kanji') { - fields = this.options.anki.kanji.fields; - note.deckName = this.options.anki.kanji.deck; - note.modelName = this.options.anki.kanji.model; - } else { - fields = this.options.anki.terms.fields; - note.deckName = this.options.anki.terms.deck; - note.modelName = this.options.anki.terms.model; - - if (definition.audio) { - const audio = { - url: definition.audio.url, - filename: definition.audio.filename, - skipHash: '7e2c2f954ef6051373ba916f000168dc', - fields: [] - }; - - for (const name in fields) { - if (fields[name].includes('{audio}')) { - audio.fields.push(name); - } - } - - if (audio.fields.length > 0) { - note.audio = audio; - } - } - } - - for (const name in fields) { - note.fields[name] = dictFieldFormat( - fields[name], - definition, - mode, - this.options - ); - } - - return note; - } - - termsFind(text) { - const searcher = this.options.general.groupResults ? - this.translator.findTermsGrouped.bind(this.translator) : - this.translator.findTerms.bind(this.translator); - - return searcher(text, dictEnabledSet(this.options), this.options.scanning.alphanumeric).then(({definitions, length}) => { - return {length, definitions: definitions.slice(0, this.options.general.maxResults)}; - }); - } - - kanjiFind(text) { - return this.translator.findKanji(text, dictEnabledSet(this.options)).then(definitions => { - return definitions.slice(0, this.options.general.maxResults); - }); - } - - definitionAdd(definition, mode) { - let promise = Promise.resolve(); - if (mode !== 'kanji') { - promise = audioInject(definition, this.options.anki.terms.fields, this.options.general.audioSource); - } - - return promise.then(() => { - const note = this.noteFormat(definition, mode); - return this.anki.addNote(note); - }); - } - - definitionsAddable(definitions, modes) { - const notes = []; - for (const definition of definitions) { - for (const mode of modes) { - notes.push(this.noteFormat(definition, mode)); - } - } - - return this.anki.canAddNotes(notes).then(raw => { - const states = []; - for (let resultBase = 0; resultBase < raw.length; resultBase += modes.length) { - const state = {}; - for (let modeOffset = 0; modeOffset < modes.length; ++modeOffset) { - state[modes[modeOffset]] = raw[resultBase + modeOffset]; - } - - states.push(state); - } - - return states; - }); - } - - noteView(noteId) { - return this.anki.guiBrowse(`nid:${noteId}`); - } - - templateRender(template, data) { - return Promise.resolve(handlebarsRender(template, data)); - } - - onCommand(command) { - const handlers = { - search: () => { - chrome.tabs.create({url: chrome.extension.getURL('/bg/search.html')}); - }, - - help: () => { - chrome.tabs.create({url: 'https://foosoft.net/projects/yomichan/'}); - }, - - options: () => { - chrome.runtime.openOptionsPage(); - }, - - toggle: () => { - this.options.general.enable = !this.options.general.enable; - optionsSave(this.options).then(() => this.optionsSet(this.options)); - } - }; - - const handler = handlers[command]; - if (handler) { - handler(); - } - } - - onMessage({action, params}, sender, callback) { - const promiseCallback = (promise, callback) => { - return promise.then(result => { - callback({result}); - }).catch(error => { - callback({error}); - }); - }; - - const handlers = { - optionsGet: ({callback}) => { - promiseCallback(optionsLoad(), callback); - }, - - kanjiFind: ({text, callback}) => { - promiseCallback(this.kanjiFind(text), callback); - }, - - termsFind: ({text, callback}) => { - promiseCallback(this.termsFind(text), callback); - }, - - templateRender: ({template, data, callback}) => { - promiseCallback(this.templateRender(template, data), callback); - }, - - definitionAdd: ({definition, mode, callback}) => { - promiseCallback(this.definitionAdd(definition, mode), callback); - }, - - definitionsAddable: ({definitions, modes, callback}) => { - promiseCallback(this.definitionsAddable(definitions, modes), callback); - }, - - noteView: ({noteId}) => { - promiseCallback(this.noteView(noteId), callback); - } - }; - - const handler = handlers[action]; - if (handler) { - params.callback = callback; - handler(params); - } - - return true; - } -}; diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index 5a51af6d..e32a630d 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -17,7 +17,7 @@ */ -window.frontend = new class { +window.yomichanFrontend = new class { constructor() { this.popup = new Popup(); this.popupTimer = null; -- cgit v1.2.3 From 611b4420af9aa5c8ad6287996b73ae8fd2eb2f4b Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Thu, 20 Jul 2017 21:34:10 -0700 Subject: cleanup --- ext/fg/js/background.js | 2 +- ext/fg/js/display-frame.js | 2 +- ext/fg/js/frontend.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/ext/fg/js/background.js b/ext/fg/js/background.js index 52b533be..c072468b 100644 --- a/ext/fg/js/background.js +++ b/ext/fg/js/background.js @@ -28,7 +28,7 @@ function bgInvoke(action, params={}) { } }); } catch (e) { - window.orphaned = true; + window.yomichanOrphaned = true; reject(e.message); } }); diff --git a/ext/fg/js/display-frame.js b/ext/fg/js/display-frame.js index b29a0379..c7da43e8 100644 --- a/ext/fg/js/display-frame.js +++ b/ext/fg/js/display-frame.js @@ -44,7 +44,7 @@ window.displayFrame = new class extends Display { } handleError(error) { - if (window.orphaned) { + if (window.yomichanOrphaned) { this.showOrphaned(); } else { window.alert(`Error: ${error}`); diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index e32a630d..8b4c182c 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -238,7 +238,7 @@ window.yomichanFrontend = new class { } handleError(error, textSource) { - if (window.orphaned) { + if (window.yomichanOrphaned) { if (textSource && this.options.scanning.modifier !== 'none') { this.popup.showOrphaned(textSource.getRect(), this.options); } -- cgit v1.2.3 From edf1c0ff6d9eadd17c98f00ef027c27d1b89a8ee Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sat, 22 Jul 2017 23:19:38 -0700 Subject: cleanup --- ext/fg/frame.html | 2 +- ext/fg/js/api.js | 63 ++++++++++++++++++++++++++++++++++++++++++++++ ext/fg/js/background.js | 63 ---------------------------------------------- ext/fg/js/display-frame.js | 10 ++++---- ext/fg/js/frontend.js | 6 ++--- ext/manifest.json | 2 +- 6 files changed, 73 insertions(+), 73 deletions(-) create mode 100644 ext/fg/js/api.js delete mode 100644 ext/fg/js/background.js diff --git a/ext/fg/frame.html b/ext/fg/frame.html index 80e69967..9ff2c585 100644 --- a/ext/fg/frame.html +++ b/ext/fg/frame.html @@ -35,7 +35,7 @@ - + diff --git a/ext/fg/js/api.js b/ext/fg/js/api.js new file mode 100644 index 00000000..e252637e --- /dev/null +++ b/ext/fg/js/api.js @@ -0,0 +1,63 @@ +/* + * Copyright (C) 2016 Alex Yatskov + * Author: Alex Yatskov + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +function apiInvoke(action, params={}) { + return new Promise((resolve, reject) => { + try { + chrome.runtime.sendMessage({action, params}, ({result, error}) => { + if (error) { + reject(error); + } else { + resolve(result); + } + }); + } catch (e) { + window.yomichanOrphaned = true; + reject(e.message); + } + }); +} + +function apiOptionsGet() { + return apiInvoke('optionsGet'); +} + +function apiTermsFind(text) { + return apiInvoke('termsFind', {text}); +} + +function apiKanjiFind(text) { + return apiInvoke('kanjiFind', {text}); +} + +function apiTemplateRender(template, data) { + return apiInvoke('templateRender', {data, template}); +} + +function apiDefinitionsAddable(definitions, modes) { + return apiInvoke('definitionsAddable', {definitions, modes}).catch(() => null); +} + +function apiDefinitionAdd(definition, mode) { + return apiInvoke('definitionAdd', {definition, mode}); +} + +function apiNoteView(noteId) { + return apiInvoke('noteView', {noteId}); +} diff --git a/ext/fg/js/background.js b/ext/fg/js/background.js deleted file mode 100644 index c072468b..00000000 --- a/ext/fg/js/background.js +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Copyright (C) 2016 Alex Yatskov - * Author: Alex Yatskov - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - - -function bgInvoke(action, params={}) { - return new Promise((resolve, reject) => { - try { - chrome.runtime.sendMessage({action, params}, ({result, error}) => { - if (error) { - reject(error); - } else { - resolve(result); - } - }); - } catch (e) { - window.yomichanOrphaned = true; - reject(e.message); - } - }); -} - -function bgOptionsGet() { - return bgInvoke('optionsGet'); -} - -function bgTermsFind(text) { - return bgInvoke('termsFind', {text}); -} - -function bgKanjiFind(text) { - return bgInvoke('kanjiFind', {text}); -} - -function bgTemplateRender(template, data) { - return bgInvoke('templateRender', {data, template}); -} - -function bgDefinitionsAddable(definitions, modes) { - return bgInvoke('definitionsAddable', {definitions, modes}).catch(() => null); -} - -function bgDefinitionAdd(definition, mode) { - return bgInvoke('definitionAdd', {definition, mode}); -} - -function bgNoteView(noteId) { - return bgInvoke('noteView', {noteId}); -} diff --git a/ext/fg/js/display-frame.js b/ext/fg/js/display-frame.js index c7da43e8..09bd9255 100644 --- a/ext/fg/js/display-frame.js +++ b/ext/fg/js/display-frame.js @@ -24,23 +24,23 @@ window.displayFrame = new class extends Display { } definitionAdd(definition, mode) { - return bgDefinitionAdd(definition, mode); + return apiDefinitionAdd(definition, mode); } definitionsAddable(definitions, modes) { - return bgDefinitionsAddable(definitions, modes); + return apiDefinitionsAddable(definitions, modes); } noteView(noteId) { - return bgNoteView(noteId); + return apiNoteView(noteId); } templateRender(template, data) { - return bgTemplateRender(template, data); + return apiTemplateRender(template, data); } kanjiFind(character) { - return bgKanjiFind(character); + return apiKanjiFind(character); } handleError(error) { diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index 8b4c182c..9974d878 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -28,7 +28,7 @@ window.yomichanFrontend = new class { this.pendingLookup = false; this.options = null; - bgOptionsGet().then(options => { + apiOptionsGet().then(options => { this.options = options; window.addEventListener('mouseover', this.onMouseOver.bind(this)); window.addEventListener('mousedown', this.onMouseDown.bind(this)); @@ -175,7 +175,7 @@ window.yomichanFrontend = new class { searchTerms(textSource) { textSource.setEndOffset(this.options.scanning.length); - return bgTermsFind(textSource.text()).then(({definitions, length}) => { + return apiTermsFind(textSource.text()).then(({definitions, length}) => { if (definitions.length === 0) { return false; } else { @@ -203,7 +203,7 @@ window.yomichanFrontend = new class { searchKanji(textSource) { textSource.setEndOffset(1); - return bgKanjiFind(textSource.text()).then(definitions => { + return apiKanjiFind(textSource.text()).then(definitions => { if (definitions.length === 0) { return false; } else { diff --git a/ext/manifest.json b/ext/manifest.json index a318e539..288976f3 100644 --- a/ext/manifest.json +++ b/ext/manifest.json @@ -18,7 +18,7 @@ "fg/js/document.js", "fg/js/source-range.js", "fg/js/source-element.js", - "fg/js/background.js", + "fg/js/api.js", "fg/js/popup.js", "fg/js/frontend.js" ], -- cgit v1.2.3 From 32680c58b895e4c781cfb2f51a97fbff42e111b0 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sun, 23 Jul 2017 22:48:33 -0700 Subject: cleanup --- ext/bg/background.html | 22 +++-- ext/bg/js/api.js | 236 ++++++++++++++++++++++++++++++++++++++++++++ ext/bg/js/backend.js | 223 ++--------------------------------------- ext/bg/js/display-window.js | 20 ---- ext/bg/js/instance.js | 30 ------ ext/bg/js/options.js | 2 +- ext/bg/popup.html | 1 - ext/bg/search.html | 1 - ext/bg/settings.html | 1 - ext/mixed/js/display.js | 34 ++----- 10 files changed, 265 insertions(+), 305 deletions(-) create mode 100644 ext/bg/js/api.js delete mode 100644 ext/bg/js/instance.js diff --git a/ext/bg/background.html b/ext/bg/background.html index 27d9bc41..1e9f3809 100644 --- a/ext/bg/background.html +++ b/ext/bg/background.html @@ -4,23 +4,25 @@ - - + - - - - - - - - + + + + + + + + + + + diff --git a/ext/bg/js/api.js b/ext/bg/js/api.js new file mode 100644 index 00000000..3db0558b --- /dev/null +++ b/ext/bg/js/api.js @@ -0,0 +1,236 @@ +/* + * Copyright (C) 2016 Alex Yatskov + * Author: Alex Yatskov + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +function utilMessageDispatch({action, params}, sender, callback) { + const forward = (promise, callback) => { + return promise.then(result => { + callback({result}); + }).catch(error => { + callback({error}); + }); + }; + + const handlers = { + optionsGet: ({callback}) => { + forward(optionsLoad(), callback); + }, + + kanjiFind: ({text, callback}) => { + forward(apiKanjiFind(text), callback); + }, + + termsFind: ({text, callback}) => { + forward(apiTermsFind(text), callback); + }, + + templateRender: ({template, data, callback}) => { + forward(apiTemplateRender(template, data), callback); + }, + + definitionAdd: ({definition, mode, callback}) => { + forward(apiDefinitionAdd(definition, mode), callback); + }, + + definitionsAddable: ({definitions, modes, callback}) => { + forward(apiDefinitionsAddable(definitions, modes), callback); + }, + + noteView: ({noteId}) => { + forward(apiNoteView(noteId), callback); + } + }; + + const handler = handlers[action]; + if (handler) { + params.callback = callback; + handler(params); + } + + return true; +} + +function utilCommandDispatch(command) { + const handlers = { + search: () => { + chrome.tabs.create({url: chrome.extension.getURL('/bg/search.html')}); + }, + + help: () => { + chrome.tabs.create({url: 'https://foosoft.net/projects/yomichan/'}); + }, + + options: () => { + chrome.runtime.openOptionsPage(); + }, + + toggle: () => { + this.options.general.enable = !this.options.general.enable; + optionsSave(this.options).then(() => this.optionsSet(this.options)); + } + }; + + const handler = handlers[command]; + if (handler) { + handler(); + } +} + +function utilNoteFormat(definition, mode) { + const options = Backend.instance().options; + const note = {fields: {}, tags: options.anki.tags}; + let fields = []; + + if (mode === 'kanji') { + fields = options.anki.kanji.fields; + note.deckName = options.anki.kanji.deck; + note.modelName = options.anki.kanji.model; + } else { + fields = options.anki.terms.fields; + note.deckName = options.anki.terms.deck; + note.modelName = options.anki.terms.model; + + if (definition.audio) { + const audio = { + url: definition.audio.url, + filename: definition.audio.filename, + skipHash: '7e2c2f954ef6051373ba916f000168dc', + fields: [] + }; + + for (const name in fields) { + if (fields[name].includes('{audio}')) { + audio.fields.push(name); + } + } + + if (audio.fields.length > 0) { + note.audio = audio; + } + } + } + + for (const name in fields) { + note.fields[name] = dictFieldFormat(fields[name], definition, mode, options); + } + + return note; +} + +async function apiOptionsSet(options) { + // In Firefox, setting options from the options UI somehow carries references + // to the DOM across to the background page, causing the options object to + // become a "DeadObject" after the options page is closed. The workaround used + // here is to create a deep copy of the options object. + Backend.instance().options = JSON.parse(JSON.stringify(options)); + + if (!options.general.enable) { + chrome.browserAction.setBadgeBackgroundColor({color: '#d9534f'}); + chrome.browserAction.setBadgeText({text: 'off'}); + } else if (!dictConfigured(options)) { + chrome.browserAction.setBadgeBackgroundColor({color: '#f0ad4e'}); + chrome.browserAction.setBadgeText({text: '!'}); + } else { + chrome.browserAction.setBadgeText({text: ''}); + } + + if (options.anki.enable) { + Backend.instance().anki = new AnkiConnect(options.anki.server); + } else { + Backend.instance().anki = new AnkiNull(); + } + + chrome.tabs.query({}, tabs => { + for (const tab of tabs) { + chrome.tabs.sendMessage(tab.id, {action: 'optionsSet', params: options}, () => null); + } + }); +} + +async function apiOptionsGet() { + return Backend.instance().options; +} + +async function apiTermsFind(text) { + const options = Backend.instance().options; + const translator = Backend.instance().translator; + + const searcher = options.general.groupResults ? + translator.findTermsGrouped.bind(translator) : + translator.findTerms.bind(translator); + + const {definitions, length} = await searcher( + text, + dictEnabledSet(options), + options.scanning.alphanumeric + ); + + return { + length, + definitions: definitions.slice(0, options.general.maxResults) + }; +} + +async function apiKanjiFind(text) { + const options = Backend.instance().options; + const definitions = await Backend.instance().translator.findKanji(text, dictEnabledSet(options)); + return definitions.slice(0, options.general.maxResults); +} + +async function apiDefinitionAdd(definition, mode) { + if (mode !== 'kanji') { + const options = Backend.instance().options; + await audioInject( + definition, + options.anki.terms.fields, + options.general.audioSource + ); + } + + return Backend.instance().anki.addNote(utilNoteFormat(definition, mode)); +} + +async function apiDefinitionsAddable(definitions, modes) { + const notes = []; + for (const definition of definitions) { + for (const mode of modes) { + notes.push(utilNoteFormat(definition, mode)); + } + } + + const results = await Backend.instance().anki.canAddNotes(notes); + const states = []; + for (let resultBase = 0; resultBase < results.length; resultBase += modes.length) { + const state = {}; + for (let modeOffset = 0; modeOffset < modes.length; ++modeOffset) { + state[modes[modeOffset]] = results[resultBase + modeOffset]; + } + + states.push(state); + } + + return states; +} + +async function apiNoteView(noteId) { + return Backend.instance().anki.guiBrowse(`nid:${noteId}`); +} + +async function apiTemplateRender(template, data) { + return handlebarsRender(template, data); +} diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index f5415d93..e977735e 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -17,7 +17,7 @@ */ -window.yomichanBackend = new class { +class Backend { constructor() { handlebarsRegister(); @@ -26,225 +26,20 @@ window.yomichanBackend = new class { this.options = null; this.translator.prepare().then(optionsLoad).then(options => { - this.optionsSet(options); + apiOptionsSet(options); - chrome.commands.onCommand.addListener(this.onCommand.bind(this)); - chrome.runtime.onMessage.addListener(this.onMessage.bind(this)); + chrome.commands.onCommand.addListener(utilCommandDispatch); + chrome.runtime.onMessage.addListener(utilMessageDispatch); - if (this.options.general.showGuide) { + if (options.general.showGuide) { chrome.tabs.create({url: chrome.extension.getURL('/bg/guide.html')}); } }); } - optionsSet(options) { - // In Firefox, setting options from the options UI somehow carries references - // to the DOM across to the background page, causing the options object to - // become a "DeadObject" after the options page is closed. The workaround used - // here is to create a deep copy of the options object. - this.options = JSON.parse(JSON.stringify(options)); - - if (!this.options.general.enable) { - chrome.browserAction.setBadgeBackgroundColor({color: '#d9534f'}); - chrome.browserAction.setBadgeText({text: 'off'}); - } else if (!dictConfigured(this.options)) { - chrome.browserAction.setBadgeBackgroundColor({color: '#f0ad4e'}); - chrome.browserAction.setBadgeText({text: '!'}); - } else { - chrome.browserAction.setBadgeText({text: ''}); - } - - if (this.options.anki.enable) { - this.anki = new AnkiConnect(this.options.anki.server); - } else { - this.anki = new AnkiNull(); - } - - chrome.tabs.query({}, tabs => { - for (const tab of tabs) { - chrome.tabs.sendMessage(tab.id, {action: 'optionsSet', params: options}, () => null); - } - }); - } - - noteFormat(definition, mode) { - const note = { - fields: {}, - tags: this.options.anki.tags - }; - - let fields = []; - if (mode === 'kanji') { - fields = this.options.anki.kanji.fields; - note.deckName = this.options.anki.kanji.deck; - note.modelName = this.options.anki.kanji.model; - } else { - fields = this.options.anki.terms.fields; - note.deckName = this.options.anki.terms.deck; - note.modelName = this.options.anki.terms.model; - - if (definition.audio) { - const audio = { - url: definition.audio.url, - filename: definition.audio.filename, - skipHash: '7e2c2f954ef6051373ba916f000168dc', - fields: [] - }; - - for (const name in fields) { - if (fields[name].includes('{audio}')) { - audio.fields.push(name); - } - } - - if (audio.fields.length > 0) { - note.audio = audio; - } - } - } - - for (const name in fields) { - note.fields[name] = dictFieldFormat( - fields[name], - definition, - mode, - this.options - ); - } - - return note; - } - - termsFind(text) { - const searcher = this.options.general.groupResults ? - this.translator.findTermsGrouped.bind(this.translator) : - this.translator.findTerms.bind(this.translator); - - return searcher(text, dictEnabledSet(this.options), this.options.scanning.alphanumeric).then(({definitions, length}) => { - return {length, definitions: definitions.slice(0, this.options.general.maxResults)}; - }); - } - - kanjiFind(text) { - return this.translator.findKanji(text, dictEnabledSet(this.options)).then(definitions => { - return definitions.slice(0, this.options.general.maxResults); - }); - } - - definitionAdd(definition, mode) { - let promise = Promise.resolve(); - if (mode !== 'kanji') { - promise = audioInject(definition, this.options.anki.terms.fields, this.options.general.audioSource); - } - - return promise.then(() => { - const note = this.noteFormat(definition, mode); - return this.anki.addNote(note); - }); - } - - definitionsAddable(definitions, modes) { - const notes = []; - for (const definition of definitions) { - for (const mode of modes) { - notes.push(this.noteFormat(definition, mode)); - } - } - - return this.anki.canAddNotes(notes).then(raw => { - const states = []; - for (let resultBase = 0; resultBase < raw.length; resultBase += modes.length) { - const state = {}; - for (let modeOffset = 0; modeOffset < modes.length; ++modeOffset) { - state[modes[modeOffset]] = raw[resultBase + modeOffset]; - } - - states.push(state); - } - - return states; - }); - } - - noteView(noteId) { - return this.anki.guiBrowse(`nid:${noteId}`); - } - - templateRender(template, data) { - return Promise.resolve(handlebarsRender(template, data)); - } - - onCommand(command) { - const handlers = { - search: () => { - chrome.tabs.create({url: chrome.extension.getURL('/bg/search.html')}); - }, - - help: () => { - chrome.tabs.create({url: 'https://foosoft.net/projects/yomichan/'}); - }, - - options: () => { - chrome.runtime.openOptionsPage(); - }, - - toggle: () => { - this.options.general.enable = !this.options.general.enable; - optionsSave(this.options).then(() => this.optionsSet(this.options)); - } - }; - - const handler = handlers[command]; - if (handler) { - handler(); - } - } - - onMessage({action, params}, sender, callback) { - const promiseCallback = (promise, callback) => { - return promise.then(result => { - callback({result}); - }).catch(error => { - callback({error}); - }); - }; - - const handlers = { - optionsGet: ({callback}) => { - promiseCallback(optionsLoad(), callback); - }, - - kanjiFind: ({text, callback}) => { - promiseCallback(this.kanjiFind(text), callback); - }, - - termsFind: ({text, callback}) => { - promiseCallback(this.termsFind(text), callback); - }, - - templateRender: ({template, data, callback}) => { - promiseCallback(this.templateRender(template, data), callback); - }, - - definitionAdd: ({definition, mode, callback}) => { - promiseCallback(this.definitionAdd(definition, mode), callback); - }, - - definitionsAddable: ({definitions, modes, callback}) => { - promiseCallback(this.definitionsAddable(definitions, modes), callback); - }, - - noteView: ({noteId}) => { - promiseCallback(this.noteView(noteId), callback); - } - }; - - const handler = handlers[action]; - if (handler) { - params.callback = callback; - handler(params); - } - - return true; + static instance() { + return chrome.extension.getBackgroundPage().yomichanBackend; } }; + +window.yomichanBackend = new Backend(); diff --git a/ext/bg/js/display-window.js b/ext/bg/js/display-window.js index 64e56f72..e5357bf9 100644 --- a/ext/bg/js/display-window.js +++ b/ext/bg/js/display-window.js @@ -29,26 +29,6 @@ window.displayWindow = new class extends Display { window.wanakana.bind(query.get(0)); } - definitionAdd(definition, mode) { - return instYomi().definitionAdd(definition, mode); - } - - definitionsAddable(definitions, modes) { - return instYomi().definitionsAddable(definitions, modes).catch(() => []); - } - - noteView(noteId) { - return instYomi().noteView(noteId); - } - - templateRender(template, data) { - return instYomi().templateRender(template, data); - } - - kanjiFind(character) { - return instYomi().kanjiFind(character); - } - handleError(error) { window.alert(`Error: ${error}`); } diff --git a/ext/bg/js/instance.js b/ext/bg/js/instance.js deleted file mode 100644 index bf858fbf..00000000 --- a/ext/bg/js/instance.js +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright (C) 2016 Alex Yatskov - * Author: Alex Yatskov - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - - -function instYomi() { - return chrome.extension.getBackgroundPage().yomichanBackend; -} - -function instDb() { - return instYomi().translator.database; -} - -function instAnki() { - return instYomi().anki; -} diff --git a/ext/bg/js/options.js b/ext/bg/js/options.js index d611ae59..ea52d337 100644 --- a/ext/bg/js/options.js +++ b/ext/bg/js/options.js @@ -126,6 +126,6 @@ function optionsSave(options) { return new Promise((resolve, reject) => { chrome.storage.local.set({options: JSON.stringify(options)}, resolve); }).then(() => { - instYomi().optionsSet(options); + apiOptionsSet(options); }); } diff --git a/ext/bg/popup.html b/ext/bg/popup.html index baeb2ffb..26c0f0bb 100644 --- a/ext/bg/popup.html +++ b/ext/bg/popup.html @@ -30,7 +30,6 @@ - diff --git a/ext/bg/search.html b/ext/bg/search.html index 472907c2..7cbae392 100644 --- a/ext/bg/search.html +++ b/ext/bg/search.html @@ -33,7 +33,6 @@
    - diff --git a/ext/bg/settings.html b/ext/bg/settings.html index 9c3995eb..2d0b4b4c 100644 --- a/ext/bg/settings.html +++ b/ext/bg/settings.html @@ -276,7 +276,6 @@ - diff --git a/ext/mixed/js/display.js b/ext/mixed/js/display.js index e54bc0f9..f408fb25 100644 --- a/ext/mixed/js/display.js +++ b/ext/mixed/js/display.js @@ -32,26 +32,6 @@ class Display { $(document).keydown(this.onKeyDown.bind(this)); } - definitionAdd(definition, mode) { - throw 'override me'; - } - - definitionsAddable(definitions, modes) { - throw 'override me'; - } - - noteView(noteId) { - throw 'override me'; - } - - templateRender(template, data) { - throw 'override me'; - } - - kanjiFind(character) { - throw 'override me'; - } - handleError(error) { throw 'override me'; } @@ -87,7 +67,7 @@ class Display { } } - this.templateRender('terms.html', params).then(content => { + apiTemplateRender('terms.html', params).then(content => { this.container.html(content); this.entryScroll(context && context.index || 0); @@ -126,7 +106,7 @@ class Display { } } - this.templateRender('kanji.html', params).then(content => { + apiTemplateRender('kanji.html', params).then(content => { this.container.html(content); this.entryScroll(context && context.index || 0); @@ -138,7 +118,7 @@ class Display { } adderButtonsUpdate(modes, sequence) { - return this.definitionsAddable(this.definitions, modes).then(states => { + return apiDefinitionsAddable(this.definitions, modes).then(states => { if (!states || sequence !== this.sequence) { return; } @@ -198,7 +178,7 @@ class Display { context.url = this.context.url; } - this.kanjiFind(link.text()).then(kanjiDefs => { + apiKanjiFind(link.text()).then(kanjiDefs => { this.showKanjiDefs(kanjiDefs, this.options, context); }).catch(this.handleError.bind(this)); } @@ -220,7 +200,7 @@ class Display { e.preventDefault(); const link = $(e.currentTarget); const index = Display.entryIndexFind(link); - this.noteView(link.data('noteId')); + apiNoteView(link.data('noteId')); } onKeyDown(e) { @@ -234,7 +214,7 @@ class Display { const noteTryView = mode => { const button = Display.viewerButtonFind(this.index); if (button.length !== 0 && !button.hasClass('disabled')) { - this.noteView(button.data('noteId')); + apiNoteView(button.data('noteId')); } }; @@ -351,7 +331,7 @@ class Display { noteAdd(definition, mode) { this.spinner.show(); - return this.definitionAdd(definition, mode).then(noteId => { + return apiDefinitionAdd(definition, mode).then(noteId => { if (noteId) { const index = this.definitions.indexOf(definition); Display.adderButtonFind(index, mode).addClass('disabled'); -- cgit v1.2.3 From eed630e9fa3c62610bda356e9c76393e70c1a08c Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Mon, 24 Jul 2017 22:44:19 -0700 Subject: cleanup --- ext/bg/js/backend.js | 2 +- ext/bg/js/popup.js | 10 ++++------ ext/bg/popup.html | 7 +++++-- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index e977735e..0615ffae 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -40,6 +40,6 @@ class Backend { static instance() { return chrome.extension.getBackgroundPage().yomichanBackend; } -}; +} window.yomichanBackend = new Backend(); diff --git a/ext/bg/js/popup.js b/ext/bg/js/popup.js index 01994827..712839d9 100644 --- a/ext/bg/js/popup.js +++ b/ext/bg/js/popup.js @@ -18,16 +18,14 @@ $(document).ready(() => { - const commandExec = command => instYomi().onCommand(command); - - $('#open-search').click(() => commandExec('search')); - $('#open-options').click(() => commandExec('options')); - $('#open-help').click(() => commandExec('help')); + $('#open-search').click(() => utilCommandDispatch('search')); + $('#open-options').click(() => utilCommandDispatch('options')); + $('#open-help').click(() => utilCommandDispatch('help')); optionsLoad().then(options => { const toggle = $('#enable-search'); toggle.prop('checked', options.general.enable).change(); toggle.bootstrapToggle(); - toggle.change(() => commandExec('toggle')); + toggle.change(() => utilCommandDispatch('toggle')); }); }); diff --git a/ext/bg/popup.html b/ext/bg/popup.html index 26c0f0bb..4113b008 100644 --- a/ext/bg/popup.html +++ b/ext/bg/popup.html @@ -27,13 +27,16 @@

    + + + - - + + -- cgit v1.2.3 From adbc7a32cc0cb050b29443e0719e36dd8fafde58 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Thu, 27 Jul 2017 21:42:14 -0700 Subject: cleanup --- ext/bg/js/api.js | 35 ++++++++++++++++++++++------------- ext/bg/js/backend.js | 10 ++-------- ext/bg/js/settings.js | 47 +++++++++++++++++++++++++++++++++++++++++------ ext/bg/settings.html | 9 ++++++--- 4 files changed, 71 insertions(+), 30 deletions(-) diff --git a/ext/bg/js/api.js b/ext/bg/js/api.js index 3db0558b..6d6ec2ea 100644 --- a/ext/bg/js/api.js +++ b/ext/bg/js/api.js @@ -92,7 +92,8 @@ function utilCommandDispatch(command) { } function utilNoteFormat(definition, mode) { - const options = Backend.instance().options; + const yomichan = chrome.extension.getBackgroundPage().yomichanBackend; + const options = yomichan.options; const note = {fields: {}, tags: options.anki.tags}; let fields = []; @@ -137,7 +138,8 @@ async function apiOptionsSet(options) { // to the DOM across to the background page, causing the options object to // become a "DeadObject" after the options page is closed. The workaround used // here is to create a deep copy of the options object. - Backend.instance().options = JSON.parse(JSON.stringify(options)); + const yomichan = chrome.extension.getBackgroundPage().yomichanBackend; + yomichan.options = JSON.parse(JSON.stringify(options)); if (!options.general.enable) { chrome.browserAction.setBadgeBackgroundColor({color: '#d9534f'}); @@ -150,9 +152,9 @@ async function apiOptionsSet(options) { } if (options.anki.enable) { - Backend.instance().anki = new AnkiConnect(options.anki.server); + yomichan.anki = new AnkiConnect(options.anki.server); } else { - Backend.instance().anki = new AnkiNull(); + yomichan.anki = new AnkiNull(); } chrome.tabs.query({}, tabs => { @@ -163,12 +165,14 @@ async function apiOptionsSet(options) { } async function apiOptionsGet() { - return Backend.instance().options; + const yomichan = chrome.extension.getBackgroundPage().yomichanBackend; + return yomichan.options; } async function apiTermsFind(text) { - const options = Backend.instance().options; - const translator = Backend.instance().translator; + const yomichan = chrome.extension.getBackgroundPage().yomichanBackend; + const options = yomichan.options; + const translator = yomichan.translator; const searcher = options.general.groupResults ? translator.findTermsGrouped.bind(translator) : @@ -187,14 +191,17 @@ async function apiTermsFind(text) { } async function apiKanjiFind(text) { - const options = Backend.instance().options; - const definitions = await Backend.instance().translator.findKanji(text, dictEnabledSet(options)); + const yomichan = chrome.extension.getBackgroundPage().yomichanBackend; + const options = yomichan.options; + const definitions = await yomichan.translator.findKanji(text, dictEnabledSet(options)); return definitions.slice(0, options.general.maxResults); } async function apiDefinitionAdd(definition, mode) { + const yomichan = chrome.extension.getBackgroundPage().yomichanBackend; + if (mode !== 'kanji') { - const options = Backend.instance().options; + const options = yomichan.options; await audioInject( definition, options.anki.terms.fields, @@ -202,7 +209,7 @@ async function apiDefinitionAdd(definition, mode) { ); } - return Backend.instance().anki.addNote(utilNoteFormat(definition, mode)); + return yomichan.anki.addNote(utilNoteFormat(definition, mode)); } async function apiDefinitionsAddable(definitions, modes) { @@ -213,7 +220,8 @@ async function apiDefinitionsAddable(definitions, modes) { } } - const results = await Backend.instance().anki.canAddNotes(notes); + const yomichan = chrome.extension.getBackgroundPage().yomichanBackend; + const results = await yomichan.anki.canAddNotes(notes); const states = []; for (let resultBase = 0; resultBase < results.length; resultBase += modes.length) { const state = {}; @@ -228,7 +236,8 @@ async function apiDefinitionsAddable(definitions, modes) { } async function apiNoteView(noteId) { - return Backend.instance().anki.guiBrowse(`nid:${noteId}`); + const yomichan = chrome.extension.getBackgroundPage().yomichanBackend; + return yomichan.anki.guiBrowse(`nid:${noteId}`); } async function apiTemplateRender(template, data) { diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index 0615ffae..4fabe4b2 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -17,7 +17,7 @@ */ -class Backend { +window.yomichanBackend = new class { constructor() { handlebarsRegister(); @@ -36,10 +36,4 @@ class Backend { } }); } - - static instance() { - return chrome.extension.getBackgroundPage().yomichanBackend; - } -} - -window.yomichanBackend = new Backend(); +}; diff --git a/ext/bg/js/settings.js b/ext/bg/js/settings.js index 3cd64fbd..09e92249 100644 --- a/ext/bg/js/settings.js +++ b/ext/bg/js/settings.js @@ -17,6 +17,41 @@ */ +/* + * Utilities + */ + +function utilAnkiGetModelNames() { + const yomichan = chrome.extension.getBackgroundPage().yomichanBackend; + return yomichan.anki.getModelNames(); +} + +function utilAnkiGetDeckNames() { + const yomichan = chrome.extension.getBackgroundPage().yomichanBackend; + return yomichan.anki.getDeckNames(); +} + +function utilAnkiGetModelFieldNames(modelName) { + const yomichan = chrome.extension.getBackgroundPage().yomichanBackend; + return yomichan.anki.utilAnkiGetModelFieldNames(modelName); +} + +function utilDatabaseGetDictionaries() { + const yomichan = chrome.extension.getBackgroundPage().yomichanBackend; + return yomichan.translator.database.getDictionaries(); +} + +function utilDatabasePurge() { + const yomichan = chrome.extension.getBackgroundPage().yomichanBackend; + return yomichan.translator.database.purge(); +} + +function utilDatabaseImport(data, progress) { + const yomichan = chrome.extension.getBackgroundPage().yomichanBackend; + return yomichan.translator.database.importDictionary(data, progress); +} + + /* * General */ @@ -217,7 +252,7 @@ async function dictionaryGroupsPopulate(options) { const dictGroups = $('#dict-groups').empty(); const dictWarning = $('#dict-warning').hide(); - const dictRows = await instDb().getDictionaries(); + const dictRows = await utilDatabaseGetDictionaries(); if (dictRows.length === 0) { dictWarning.show(); } @@ -253,7 +288,7 @@ async function onDictionaryPurge(e) {(async () => { dictionaryErrorShow(); dictionarySpinnerShow(true); - await instDb().purge(); + await utilDatabasePurge(); const options = await optionsLoad(); options.dictionaries = {}; await optionsSave(options); @@ -283,7 +318,7 @@ function onDictionaryImport(e) {(async () => { setProgress(0.0); const options = await optionsLoad(); - const summary = await instDb().importDictionary(e.target.files[0], updateProgress); + const summary = await utilDatabaseImport(e.target.files[0], updateProgress); options.dictionaries[summary.title] = {enabled: true, priority: 0}; await optionsSave(options); @@ -344,12 +379,12 @@ async function ankiDeckAndModelPopulate(options) { $('#anki-terms-deck').val(options.anki.terms.deck); $('#anki-kanji-deck').val(options.anki.kanji.deck); - const deckNames = await instAnki().getDeckNames(); + const deckNames = await utilAnkiGetDeckNames(); const ankiDeck = $('.anki-deck'); ankiDeck.find('option').remove(); deckNames.sort().forEach(name => ankiDeck.append($('
    " + alias4(((helper = (helper = helpers.name || (depth0 != null ? depth0.name : depth0)) != null ? helper : alias2),(typeof helper === alias3 ? helper.call(alias1,{"name":"name","hash":{},"data":data}) : helper))) @@ -450,7 +450,7 @@ templates['model.html'] = template({"1":function(container,depth0,helpers,partia + " \n \n \n