This commit is contained in:
Alex Yatskov 2017-01-07 12:21:47 -08:00
parent 86e39efe28
commit 15ebc06fb3
13 changed files with 256 additions and 359 deletions

View File

@ -31,34 +31,12 @@ templates['dictionary.html'] = template({"1":function(container,depth0,helpers,p
+ ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.enableKanji : depth0),{"name":"if","hash":{},"fn":container.program(3, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.enableKanji : depth0),{"name":"if","hash":{},"fn":container.program(3, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "")
+ "> Enable Kanji search</label>\n </div>\n</div>\n"; + "> Enable Kanji search</label>\n </div>\n</div>\n";
},"useData":true}); },"useData":true});
templates['footer.html'] = template({"compiler":[7,">= 4.0.0"],"main":function(container,depth0,helpers,partials,data) { templates['kanji.html'] = template({"1":function(container,depth0,helpers,partials,data) {
var helper; var helper;
return " <script src=\""
+ container.escapeExpression(((helper = (helper = helpers.root || (depth0 != null ? depth0.root : depth0)) != null ? helper : helpers.helperMissing),(typeof helper === "function" ? helper.call(depth0 != null ? depth0 : {},{"name":"root","hash":{},"data":data}) : helper)))
+ "/js/frame.js\"></script>\n </body>\n</html>\n";
},"useData":true});
templates['header.html'] = template({"compiler":[7,">= 4.0.0"],"main":function(container,depth0,helpers,partials,data) {
var helper, alias1=depth0 != null ? depth0 : {}, alias2=helpers.helperMissing, alias3="function", alias4=container.escapeExpression;
return "<!DOCTYPE html>\n<html lang=\"en\">\n <head>\n <meta charset=\"UTF-8\">\n <title></title>\n <style>\n @font-face {\n font-family: kanji-stroke-orders;\n src: url('"
+ alias4(((helper = (helper = helpers.root || (depth0 != null ? depth0.root : depth0)) != null ? helper : alias2),(typeof helper === alias3 ? helper.call(alias1,{"name":"root","hash":{},"data":data}) : helper)))
+ "/ttf/kanji-stroke-orders.ttf');\n }\n @font-face {\n font-family: vl-gothic-regular;\n src: url('"
+ alias4(((helper = (helper = helpers.root || (depth0 != null ? depth0.root : depth0)) != null ? helper : alias2),(typeof helper === alias3 ? helper.call(alias1,{"name":"root","hash":{},"data":data}) : helper)))
+ "/ttf/vl-gothic-regular.ttf');\n }\n </style>\n <link rel=\"stylesheet\" href=\""
+ alias4(((helper = (helper = helpers.root || (depth0 != null ? depth0.root : depth0)) != null ? helper : alias2),(typeof helper === alias3 ? helper.call(alias1,{"name":"root","hash":{},"data":data}) : helper)))
+ "/css/frame.css\">\n </head>\n <body>\n\n <div class=\"spinner\">\n <img src=\""
+ alias4(((helper = (helper = helpers.root || (depth0 != null ? depth0.root : depth0)) != null ? helper : alias2),(typeof helper === alias3 ? helper.call(alias1,{"name":"root","hash":{},"data":data}) : helper)))
+ "/img/spinner.gif\">\n </div>\n";
},"useData":true});
templates['kanji.html'] = template({"1":function(container,depth0,helpers,partials,data) {
var helper, alias1=depth0 != null ? depth0 : {}, alias2=helpers.helperMissing, alias3="function", alias4=container.escapeExpression;
return " <a href=\"#\" title=\"Add Kanji\" class=\"action-add-note pending disabled\" data-mode=\"kanji\" data-index=\"" return " <a href=\"#\" title=\"Add Kanji\" class=\"action-add-note pending disabled\" data-mode=\"kanji\" data-index=\""
+ alias4(((helper = (helper = helpers.index || (data && data.index)) != null ? helper : alias2),(typeof helper === alias3 ? helper.call(alias1,{"name":"index","hash":{},"data":data}) : helper))) + container.escapeExpression(((helper = (helper = helpers.index || (data && data.index)) != null ? helper : helpers.helperMissing),(typeof helper === "function" ? helper.call(depth0 != null ? depth0 : {},{"name":"index","hash":{},"data":data}) : helper)))
+ "\"><img src=\"" + "\"><img src=\"img/add_kanji.png\"></a>\n";
+ alias4(((helper = (helper = helpers.root || (depth0 != null ? depth0.root : depth0)) != null ? helper : alias2),(typeof helper === alias3 ? helper.call(alias1,{"name":"root","hash":{},"data":data}) : helper)))
+ "/img/add_kanji.png\"></a>\n";
},"3":function(container,depth0,helpers,partials,data) { },"3":function(container,depth0,helpers,partials,data) {
var stack1; var stack1;
@ -95,14 +73,12 @@ templates['kanji.html'] = template({"1":function(container,depth0,helpers,partia
+ container.escapeExpression(container.lambda(((stack1 = (depth0 != null ? depth0.glossary : depth0)) != null ? stack1["0"] : stack1), depth0)) + container.escapeExpression(container.lambda(((stack1 = (depth0 != null ? depth0.glossary : depth0)) != null ? stack1["0"] : stack1), depth0))
+ "\n </p>\n"; + "\n </p>\n";
},"compiler":[7,">= 4.0.0"],"main":function(container,depth0,helpers,partials,data) { },"compiler":[7,">= 4.0.0"],"main":function(container,depth0,helpers,partials,data) {
var stack1, helper, alias1=depth0 != null ? depth0 : {}, alias2=helpers.helperMissing, alias3="function", alias4=container.escapeExpression; var stack1, helper, alias1=depth0 != null ? depth0 : {};
return "<div class=\"kanji-definition\">\n <div class=\"action-bar\" data-sequence=\"" return "<div class=\"kanji-definition\">\n <div class=\"action-bar\">\n"
+ alias4(((helper = (helper = helpers.sequence || (depth0 != null ? depth0.sequence : depth0)) != null ? helper : alias2),(typeof helper === alias3 ? helper.call(alias1,{"name":"sequence","hash":{},"data":data}) : helper)))
+ "\">\n"
+ ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.addable : depth0),{"name":"if","hash":{},"fn":container.program(1, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.addable : depth0),{"name":"if","hash":{},"fn":container.program(1, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "")
+ " </div>\n\n <div class=\"kanji-glyph\">" + " </div>\n\n <div class=\"kanji-glyph\">"
+ alias4(((helper = (helper = helpers.character || (depth0 != null ? depth0.character : depth0)) != null ? helper : alias2),(typeof helper === alias3 ? helper.call(alias1,{"name":"character","hash":{},"data":data}) : helper))) + 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)))
+ "</div>\n\n <div class=\"kanji-reading\">\n <table>\n <tr>\n <th>Kunyomi:</th>\n <td>\n" + "</div>\n\n <div class=\"kanji-reading\">\n <table>\n <tr>\n <th>Kunyomi:</th>\n <td>\n"
+ ((stack1 = helpers.each.call(alias1,(depth0 != null ? depth0.kunyomi : depth0),{"name":"each","hash":{},"fn":container.program(3, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + ((stack1 = helpers.each.call(alias1,(depth0 != null ? depth0.kunyomi : depth0),{"name":"each","hash":{},"fn":container.program(3, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "")
+ " </td>\n </tr>\n <tr>\n <th>Onyomi:</th>\n <td>\n" + " </td>\n </tr>\n <tr>\n <th>Onyomi:</th>\n <td>\n"
@ -127,15 +103,13 @@ templates['kanji-list.html'] = template({"1":function(container,depth0,helpers,p
},"2":function(container,depth0,helpers,partials,data,blockParams,depths) { },"2":function(container,depth0,helpers,partials,data,blockParams,depths) {
var stack1; var stack1;
return ((stack1 = container.invokePartial(partials["kanji.html"],depth0,{"name":"kanji.html","hash":{"sequence":(depths[1] != null ? depths[1].sequence : depths[1]),"options":(depths[1] != null ? depths[1].options : depths[1]),"root":(depths[1] != null ? depths[1].root : depths[1]),"addable":(depths[1] != null ? depths[1].addable : depths[1])},"data":data,"indent":" ","helpers":helpers,"partials":partials,"decorators":container.decorators})) != null ? stack1 : ""); return ((stack1 = container.invokePartial(partials["kanji.html"],depth0,{"name":"kanji.html","hash":{"root":(depths[1] != null ? depths[1].root : depths[1]),"addable":(depths[1] != null ? depths[1].addable : depths[1])},"data":data,"indent":" ","helpers":helpers,"partials":partials,"decorators":container.decorators})) != null ? stack1 : "");
},"4":function(container,depth0,helpers,partials,data) { },"4":function(container,depth0,helpers,partials,data) {
return " <p>No results found</p>\n"; return " <p>No results found</p>\n";
},"compiler":[7,">= 4.0.0"],"main":function(container,depth0,helpers,partials,data,blockParams,depths) { },"compiler":[7,">= 4.0.0"],"main":function(container,depth0,helpers,partials,data,blockParams,depths) {
var stack1; var stack1;
return ((stack1 = container.invokePartial(partials["header.html"],depth0,{"name":"header.html","data":data,"helpers":helpers,"partials":partials,"decorators":container.decorators})) != null ? stack1 : "") return ((stack1 = helpers["if"].call(depth0 != null ? depth0 : {},(depth0 != null ? depth0.definitions : depth0),{"name":"if","hash":{},"fn":container.program(1, data, 0, blockParams, depths),"inverse":container.program(4, data, 0, blockParams, depths),"data":data})) != null ? stack1 : "");
+ ((stack1 = helpers["if"].call(depth0 != null ? depth0 : {},(depth0 != null ? depth0.definitions : depth0),{"name":"if","hash":{},"fn":container.program(1, data, 0, blockParams, depths),"inverse":container.program(4, data, 0, blockParams, depths),"data":data})) != null ? stack1 : "")
+ ((stack1 = container.invokePartial(partials["footer.html"],depth0,{"name":"footer.html","data":data,"helpers":helpers,"partials":partials,"decorators":container.decorators})) != null ? stack1 : "");
},"usePartial":true,"useData":true,"useDepths":true}); },"usePartial":true,"useData":true,"useDepths":true});
templates['model.html'] = template({"1":function(container,depth0,helpers,partials,data) { templates['model.html'] = template({"1":function(container,depth0,helpers,partials,data) {
return " <li><a class=\"marker-link\" href=\"#\">" return " <li><a class=\"marker-link\" href=\"#\">"
@ -159,21 +133,15 @@ templates['term.html'] = template({"1":function(container,depth0,helpers,partial
return " <a href=\"#\" title=\"Add term as expression\" class=\"action-add-note pending disabled\" data-mode=\"term_kanji\" data-index=\"" return " <a href=\"#\" title=\"Add term as expression\" class=\"action-add-note pending disabled\" data-mode=\"term_kanji\" data-index=\""
+ alias4(((helper = (helper = helpers.index || (data && data.index)) != null ? helper : alias2),(typeof helper === alias3 ? helper.call(alias1,{"name":"index","hash":{},"data":data}) : helper))) + alias4(((helper = (helper = helpers.index || (data && data.index)) != null ? helper : alias2),(typeof helper === alias3 ? helper.call(alias1,{"name":"index","hash":{},"data":data}) : helper)))
+ "\"><img src=\"" + "\"><img src=\"img/add_term_kanji.png\"></a>\n <a href=\"#\" title=\"Add term as reading\" class=\"action-add-note pending disabled\" data-mode=\"term_kana\" data-index=\""
+ alias4(((helper = (helper = helpers.root || (depth0 != null ? depth0.root : depth0)) != null ? helper : alias2),(typeof helper === alias3 ? helper.call(alias1,{"name":"root","hash":{},"data":data}) : helper)))
+ "/img/add_term_kanji.png\"></a>\n <a href=\"#\" title=\"Add term as reading\" class=\"action-add-note pending disabled\" data-mode=\"term_kana\" data-index=\""
+ alias4(((helper = (helper = helpers.index || (data && data.index)) != null ? helper : alias2),(typeof helper === alias3 ? helper.call(alias1,{"name":"index","hash":{},"data":data}) : helper))) + alias4(((helper = (helper = helpers.index || (data && data.index)) != null ? helper : alias2),(typeof helper === alias3 ? helper.call(alias1,{"name":"index","hash":{},"data":data}) : helper)))
+ "\"><img src=\"" + "\"><img src=\"img/add_term_kana.png\"></a>\n";
+ alias4(((helper = (helper = helpers.root || (depth0 != null ? depth0.root : depth0)) != null ? helper : alias2),(typeof helper === alias3 ? helper.call(alias1,{"name":"root","hash":{},"data":data}) : helper)))
+ "/img/add_term_kana.png\"></a>\n";
},"3":function(container,depth0,helpers,partials,data) { },"3":function(container,depth0,helpers,partials,data) {
var helper, alias1=depth0 != null ? depth0 : {}, alias2=helpers.helperMissing, alias3="function", alias4=container.escapeExpression; var helper;
return " <a href=\"#\" title=\"Play audio\" class=\"action-play-audio\" data-index=\"" return " <a href=\"#\" title=\"Play audio\" class=\"action-play-audio\" data-index=\""
+ alias4(((helper = (helper = helpers.index || (data && data.index)) != null ? helper : alias2),(typeof helper === alias3 ? helper.call(alias1,{"name":"index","hash":{},"data":data}) : helper))) + container.escapeExpression(((helper = (helper = helpers.index || (data && data.index)) != null ? helper : helpers.helperMissing),(typeof helper === "function" ? helper.call(depth0 != null ? depth0 : {},{"name":"index","hash":{},"data":data}) : helper)))
+ "\"><img src=\"" + "\"><img src=\"img/play_audio.png\"></a>\n";
+ alias4(((helper = (helper = helpers.root || (depth0 != null ? depth0.root : depth0)) != null ? helper : alias2),(typeof helper === alias3 ? helper.call(alias1,{"name":"root","hash":{},"data":data}) : helper)))
+ "/img/play_audio.png\"></a>\n";
},"5":function(container,depth0,helpers,partials,data) { },"5":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 : {}, alias2=helpers.helperMissing, alias3="function", buffer =
" <div class=\"term-expression\"><ruby>"; " <div class=\"term-expression\"><ruby>";
@ -231,13 +199,11 @@ templates['term.html'] = template({"1":function(container,depth0,helpers,partial
+ container.escapeExpression(container.lambda(((stack1 = (depth0 != null ? depth0.glossary : depth0)) != null ? stack1["0"] : stack1), depth0)) + container.escapeExpression(container.lambda(((stack1 = (depth0 != null ? depth0.glossary : depth0)) != null ? stack1["0"] : stack1), depth0))
+ "</p>\n"; + "</p>\n";
},"compiler":[7,">= 4.0.0"],"main":function(container,depth0,helpers,partials,data) { },"compiler":[7,">= 4.0.0"],"main":function(container,depth0,helpers,partials,data) {
var stack1, helper, alias1=depth0 != null ? depth0 : {}; var stack1, alias1=depth0 != null ? depth0 : {};
return "<div class=\"term-definition\">\n <div class=\"action-bar\" data-sequence=\"" return "<div class=\"term-definition\">\n <div class=\"action-bar\">\n"
+ container.escapeExpression(((helper = (helper = helpers.sequence || (depth0 != null ? depth0.sequence : depth0)) != null ? helper : helpers.helperMissing),(typeof helper === "function" ? helper.call(alias1,{"name":"sequence","hash":{},"data":data}) : helper)))
+ "\">\n"
+ ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.addable : depth0),{"name":"if","hash":{},"fn":container.program(1, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.addable : depth0),{"name":"if","hash":{},"fn":container.program(1, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "")
+ ((stack1 = helpers["if"].call(alias1,((stack1 = (depth0 != null ? depth0.options : depth0)) != null ? stack1.enableAudioPlayback : stack1),{"name":"if","hash":{},"fn":container.program(3, 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(3, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "")
+ " </div>\n\n" + " </div>\n\n"
+ ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.reading : depth0),{"name":"if","hash":{},"fn":container.program(5, data, 0),"inverse":container.program(8, data, 0),"data":data})) != null ? stack1 : "") + ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.reading : depth0),{"name":"if","hash":{},"fn":container.program(5, data, 0),"inverse":container.program(8, data, 0),"data":data})) != null ? stack1 : "")
+ "\n <div class=\"term-reasons\">\n" + "\n <div class=\"term-reasons\">\n"
@ -255,14 +221,12 @@ templates['term-list.html'] = template({"1":function(container,depth0,helpers,pa
},"2":function(container,depth0,helpers,partials,data,blockParams,depths) { },"2":function(container,depth0,helpers,partials,data,blockParams,depths) {
var stack1; var stack1;
return ((stack1 = container.invokePartial(partials["term.html"],depth0,{"name":"term.html","hash":{"sequence":(depths[1] != null ? depths[1].sequence : depths[1]),"options":(depths[1] != null ? depths[1].options : depths[1]),"root":(depths[1] != null ? depths[1].root : depths[1]),"addable":(depths[1] != null ? depths[1].addable : depths[1])},"data":data,"indent":" ","helpers":helpers,"partials":partials,"decorators":container.decorators})) != null ? stack1 : ""); return ((stack1 = container.invokePartial(partials["term.html"],depth0,{"name":"term.html","hash":{"playback":(depths[1] != null ? depths[1].playback : depths[1]),"addable":(depths[1] != null ? depths[1].addable : depths[1])},"data":data,"indent":" ","helpers":helpers,"partials":partials,"decorators":container.decorators})) != null ? stack1 : "");
},"4":function(container,depth0,helpers,partials,data) { },"4":function(container,depth0,helpers,partials,data) {
return " <p>No results found</p>\n"; return " <p>No results found</p>\n";
},"compiler":[7,">= 4.0.0"],"main":function(container,depth0,helpers,partials,data,blockParams,depths) { },"compiler":[7,">= 4.0.0"],"main":function(container,depth0,helpers,partials,data,blockParams,depths) {
var stack1; var stack1;
return ((stack1 = container.invokePartial(partials["header.html"],depth0,{"name":"header.html","data":data,"helpers":helpers,"partials":partials,"decorators":container.decorators})) != null ? stack1 : "") return ((stack1 = helpers["if"].call(depth0 != null ? depth0 : {},(depth0 != null ? depth0.definitions : depth0),{"name":"if","hash":{},"fn":container.program(1, data, 0, blockParams, depths),"inverse":container.program(4, data, 0, blockParams, depths),"data":data})) != null ? stack1 : "");
+ ((stack1 = helpers["if"].call(depth0 != null ? depth0 : {},(depth0 != null ? depth0.definitions : depth0),{"name":"if","hash":{},"fn":container.program(1, data, 0, blockParams, depths),"inverse":container.program(4, data, 0, blockParams, depths),"data":data})) != null ? stack1 : "")
+ ((stack1 = container.invokePartial(partials["footer.html"],depth0,{"name":"footer.html","data":data,"helpers":helpers,"partials":partials,"decorators":container.decorators})) != null ? stack1 : "");
},"usePartial":true,"useData":true,"useDepths":true}); },"usePartial":true,"useData":true,"useDepths":true});
})(); })();

View File

@ -18,6 +18,15 @@
/* common styles */ /* common styles */
@font-face {
font-family: kanji-stroke-orders;
src: url('../ttf/kanji-stroke-orders.ttf');
}
@font-face {
font-family: vl-gothic-regular;
src: url('../ttf/vl-gothic-regular.ttf');
}
body { body {
background-color: #fff; background-color: #fff;
color: #333; color: #333;

18
ext/fg/frame.html Normal file
View File

@ -0,0 +1,18 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" href="css/frame.css">
</head>
<body>
<div class="spinner">
<img src="img/spinner.gif">
</div>
<div class="content"></div>
<script src="../lib/jquery-2.2.2.min.js"></script>
<script src="js/frame.js"></script>
</body>
</html>

View File

@ -21,18 +21,13 @@ class Driver {
constructor() { constructor() {
this.popup = new Popup(); this.popup = new Popup();
this.popupTimer = null; this.popupTimer = null;
this.audio = {};
this.lastMousePos = null; this.lastMousePos = null;
this.lastTextSource = null; this.lastTextSource = null;
this.pendingLookup = false; this.pendingLookup = false;
this.enabled = false; this.enabled = false;
this.options = null; this.options = null;
this.definitions = null;
this.sequence = 0;
this.fgRoot = chrome.extension.getURL('fg');
chrome.runtime.onMessage.addListener(this.onBgMessage.bind(this)); chrome.runtime.onMessage.addListener(this.onBgMessage.bind(this));
window.addEventListener('message', this.onFrameMessage.bind(this));
window.addEventListener('mouseover', this.onMouseOver.bind(this)); window.addEventListener('mouseover', this.onMouseOver.bind(this));
window.addEventListener('mousedown', this.onMouseDown.bind(this)); window.addEventListener('mousedown', this.onMouseDown.bind(this));
window.addEventListener('mousemove', this.onMouseMove.bind(this)); window.addEventListener('mousemove', this.onMouseMove.bind(this));
@ -114,101 +109,6 @@ class Driver {
callback(); callback();
} }
onFrameMessage(e) {
const {action, params} = e.data, method = this['api_' + action];
if (typeof(method) === 'function') {
method.call(this, params);
}
}
searchTerms(textSource) {
textSource.setEndOffset(this.options.scanLength);
this.pendingLookup = true;
return findTerm(textSource.text()).then(({definitions, length}) => {
if (definitions.length === 0) {
this.pendingLookup = false;
return false;
} else {
textSource.setEndOffset(length);
const sentence = extractSentence(textSource, this.options.sentenceExtent);
definitions.forEach(definition => {
definition.url = window.location.href;
definition.sentence = sentence;
});
const sequence = ++this.sequence;
const context = {
definitions,
sequence,
addable: this.options.ankiMethod !== 'disabled',
root: this.fgRoot,
options: this.options
};
return renderText(context, 'term-list.html').then(content => {
this.definitions = definitions;
this.pendingLookup = false;
this.showPopup(textSource, content);
return canAddDefinitions(definitions, ['term_kanji', 'term_kana']);
}).then(states => {
if (states !== null) {
states.forEach((state, index) => this.popup.invokeApi(
'setActionState',
{index, state, sequence}
));
}
return true;
});
}
}).catch(error => {
alert('Error: ' + error);
});
}
searchKanji(textSource) {
textSource.setEndOffset(1);
this.pendingLookup = true;
return findKanji(textSource.text()).then(definitions => {
if (definitions.length === 0) {
this.pendingLookup = false;
return false;
} else {
definitions.forEach(definition => definition.url = window.location.href);
const sequence = ++this.sequence;
const context = {
definitions,
sequence,
addable: this.options.ankiMethod !== 'disabled',
root: this.fgRoot,
options: this.options
};
return renderText(context, 'kanji-list.html').then(content => {
this.definitions = definitions;
this.pendingLookup = false;
this.showPopup(textSource, content);
return canAddDefinitions(definitions, ['kanji']);
}).then(states => {
if (states !== null) {
states.forEach((state, index) => this.popup.invokeApi(
'setActionState',
{index, state, sequence}
));
}
return true;
});
}
}).catch(error => {
alert('Error: ' + error);
});
}
searchAt(point, hideNotFound) { searchAt(point, hideNotFound) {
if (this.pendingLookup) { if (this.pendingLookup) {
return; return;
@ -227,6 +127,7 @@ class Driver {
return true; return true;
} }
this.pendingLookup = true;
this.searchTerms(textSource).then(found => { this.searchTerms(textSource).then(found => {
if (!found) { if (!found) {
this.searchKanji(textSource).then(found => { this.searchKanji(textSource).then(found => {
@ -237,17 +138,53 @@ class Driver {
} }
}).catch(error => { }).catch(error => {
alert('Error: ' + error); alert('Error: ' + error);
}).then(() => {
this.pendingLookup = false;
}); });
} }
showPopup(textSource, content) { searchTerms(textSource) {
this.popup.showNextTo(textSource.getRect(), content); textSource.setEndOffset(this.options.scanLength);
if (this.options.selectMatchedText) { return findTerm(textSource.text()).then(({definitions, length}) => {
textSource.select(); if (definitions.length === 0) {
return false;
} else {
textSource.setEndOffset(length);
const sentence = extractSentence(textSource, this.options.sentenceExtent);
definitions.forEach(definition => {
definition.url = window.location.href;
definition.sentence = sentence;
});
this.popup.showNextTo(textSource.getRect());
this.popup.invoke('showTermDefs', {definitions, options: this.options});
return true;
}
}).catch(error => {
alert('Error: ' + error);
});
} }
this.lastTextSource = textSource; searchKanji(textSource) {
textSource.setEndOffset(1);
return findKanji(textSource.text()).then(definitions => {
if (definitions.length === 0) {
return false;
} else {
definitions.forEach(definition => definition.url = window.location.href);
this.popup.showNextTo(textSource.getRect());
this.popup.invoke('showKanjiDefs', {definitions, options: this.options});
return true;
}
}).catch(error => {
alert('Error: ' + error);
});
} }
hidePopup() { hidePopup() {
@ -270,67 +207,6 @@ class Driver {
this.hidePopup(); this.hidePopup();
} }
} }
api_addNote({index, mode}) {
const state = {[mode]: false};
addDefinition(this.definitions[index], mode).then(success => {
if (success) {
this.popup.invokeApi('setActionState', {index, state, sequence: this.sequence});
} else {
alert('Note could not be added');
}
this.popup.invokeApi('addNoteComplete');
}).catch(error => {
alert('Error: ' + error);
});
}
api_playAudio(index) {
const definition = this.definitions[index];
let url = `https://assets.languagepod101.com/dictionary/japanese/audiomp3.php?kanji=${encodeURIComponent(definition.expression)}`;
if (definition.reading) {
url += `&kana=${encodeURIComponent(definition.reading)}`;
}
for (const key in this.audio) {
this.audio[key].pause();
}
const audio = this.audio[url] || new Audio(url);
audio.currentTime = 0;
audio.play();
this.audio[url] = audio;
}
api_displayKanji(kanji) {
findKanji(kanji).then(definitions => {
definitions.forEach(definition => definition.url = window.location.href);
const sequence = ++this.sequence;
const context = {
definitions,
sequence,
addable: this.options.ankiMethod !== 'disabled',
root: this.fgRoot,
options: this.options
};
return renderText(context, 'kanji-list.html').then(content => {
this.definitions = definitions;
this.popup.setContent(content, definitions);
return canAddDefinitions(definitions, ['kanji']);
}).then(states => {
if (states !== null) {
states.forEach((state, index) => this.popup.invokeApi('setActionState', {index, state, sequence}));
}
});
}).catch(error => {
alert('Error: ' + error);
});
}
} }
window.driver = new Driver(); window.driver = new Driver();

View File

@ -17,76 +17,157 @@
*/ */
function invokeApi(action, params, target) { // function renderText(data, template) {
target.postMessage({action, params}, '*'); // return invokeApiBg('renderText', {data, template});
} // }
function showSpinner(show) { // function canAddDefinitions(definitions, modes) {
const spinner = document.querySelector('.spinner'); // return invokeApiBg('canAddDefinitions', {definitions, modes}).catch(() => null);
spinner.style.visibility = show ? 'visible' : 'hidden'; // }
}
function registerKanjiLinks() { // function addDefinition(definition, mode) {
for (const link of Array.from(document.getElementsByClassName('kanji-link'))) { // return invokeApiBg('addDefinition', {definition, mode});
link.addEventListener('click', e => { // }
e.preventDefault();
invokeApi('displayKanji', e.target.innerHTML, window.parent);
});
}
}
function registerAddNoteLinks() { // function invokeApi(action, params, target) {
for (const link of Array.from(document.getElementsByClassName('action-add-note'))) { // target.postMessage({action, params}, '*');
link.addEventListener('click', e => { // }
e.preventDefault();
const ds = e.currentTarget.dataset;
invokeApi('addNote', {index: ds.index, mode: ds.mode}, window.parent);
showSpinner(true);
});
}
}
function registerAudioLinks() { // function showSpinner(show) {
for (const link of Array.from(document.getElementsByClassName('action-play-audio'))) { // const spinner = document.querySelector('.spinner');
link.addEventListener('click', e => { // spinner.style.visibility = show ? 'visible' : 'hidden';
e.preventDefault(); // }
const ds = e.currentTarget.dataset;
invokeApi('playAudio', ds.index, window.parent);
});
}
}
function api_addNoteComplete() { // function registerKanjiLinks() {
showSpinner(false); // for (const link of Array.from(document.getElementsByClassName('kanji-link'))) {
} // link.addEventListener('click', e => {
// e.preventDefault();
// invokeApi('displayKanji', e.target.innerHTML, window.parent);
// });
// }
// }
function api_setActionState({index, state, sequence}) { // function registerAddNoteLinks() {
for (const mode in state) { // for (const link of Array.from(document.getElementsByClassName('action-add-note'))) {
const matches = document.querySelectorAll(`.action-bar[data-sequence="${sequence}"] .action-add-note[data-index="${index}"][data-mode="${mode}"]`); // link.addEventListener('click', e => {
if (matches.length === 0) { // e.preventDefault();
return; // const ds = e.currentTarget.dataset;
} // invokeApi('addNote', {index: ds.index, mode: ds.mode}, window.parent);
// showSpinner(true);
// });
// }
// }
const classes = matches[0].classList; // function registerAudioLinks() {
if (state[mode]) { // for (const link of Array.from(document.getElementsByClassName('action-play-audio'))) {
classes.remove('disabled'); // link.addEventListener('click', e => {
} else { // e.preventDefault();
classes.add('disabled'); // const ds = e.currentTarget.dataset;
} // invokeApi('playAudio', ds.index, window.parent);
// });
// }
// }
classes.remove('pending'); // function api_setActionState({index, state, sequence}) {
} // for (const mode in state) {
} // const matches = document.querySelectorAll(`.action-bar[data-sequence="${sequence}"] .action-add-note[data-index="${index}"][data-mode="${mode}"]`);
// if (matches.length === 0) {
// return;
// }
document.addEventListener('DOMContentLoaded', () => { // const classes = matches[0].classList;
registerKanjiLinks(); // if (state[mode]) {
registerAddNoteLinks(); // classes.remove('disabled');
registerAudioLinks(); // } else {
}); // classes.add('disabled');
// }
window.addEventListener('message', e => { // classes.remove('pending');
const {action, params} = e.data, method = window['api_' + action]; // }
// }
class FrameContext {
constructor() {
$(window).on('message', e => {
const {action, params} = e.originalEvent.data, method = this['api_' + action];
if (typeof(method) === 'function') { if (typeof(method) === 'function') {
method(params); method.call(this, params);
} }
}); });
}
api_showTermDefs({definitions, options}) {
const context = {
definitions,
addable: options.ankiMethod !== 'disabled',
playback: options.enableAudioPlayback
};
this.renderText('term-list.html', context).then(content => {
$('.content').html(content);
});
}
api_showKanjiDefs({definitions, options}) {
const context = {
definitions,
addable: options.ankiMethod !== 'disabled'
};
this.renderText('kanji-list.html', context).then(content => {
$('.content').html(content);
});
}
renderText(template, data) {
return this.invokeBgApi('renderText', {data, template});
}
invokeBgApi(action, params) {
return new Promise((resolve, reject) => {
chrome.runtime.sendMessage({action, params}, ({result, error}) => {
if (error) {
reject(error);
} else {
resolve(result);
}
});
});
}
}
window.frameContext = new FrameContext();
// api_addNote({index, mode}) {
// const state = {[mode]: false};
// addDefinition(this.definitions[index], mode).then(success => {
// if (success) {
// this.popup.invokeApi('setActionState', {index, state, sequence: this.sequence});
// } else {
// alert('Note could not be added');
// }
// this.popup.invokeApi('addNoteComplete');
// }).catch(error => {
// alert('Error: ' + error);
// });
// }
// api_playAudio(index) {
// const definition = this.definitions[index];
// let url = `https://assets.languagepod101.com/dictionary/japanese/audiomp3.php?kanji=${encodeURIComponent(definition.expression)}`;
// if (definition.reading) {
// url += `&kana=${encodeURIComponent(definition.reading)}`;
// }
// for (const key in this.audio) {
// this.audio[key].pause();
// }
// const audio = this.audio[url] || new Audio(url);
// audio.currentTime = 0;
// audio.play();
// this.audio[url] = audio;
// }

View File

@ -19,29 +19,32 @@
class Popup { class Popup {
constructor() { constructor() {
this.container = null;
this.offset = 10; this.offset = 10;
this.container = document.createElement('iframe');
this.container.id = 'yomichan-popup';
this.container.addEventListener('mousedown', e => e.stopPropagation());
this.container.addEventListener('scroll', e => e.stopPropagation());
this.container.setAttribute('src', chrome.extension.getURL('fg/frame.html'));
document.body.appendChild(this.container);
} }
show(rect, content) { show(rect) {
this.inject();
this.container.style.left = rect.x + 'px'; this.container.style.left = rect.x + 'px';
this.container.style.top = rect.y + 'px'; this.container.style.top = rect.y + 'px';
this.container.style.height = rect.height + 'px'; this.container.style.height = rect.height + 'px';
this.container.style.width = rect.width + 'px'; this.container.style.width = rect.width + 'px';
this.container.style.visibility = 'visible'; this.container.style.visibility = 'visible';
this.setContent(content);
} }
showNextTo(elementRect, content) { showNextTo(elementRect) {
this.inject(); const containerStyle = window.getComputedStyle(this.container);
const containerHeight = parseInt(containerStyle.height);
const containerRect = this.container.getBoundingClientRect(); const containerWidth = parseInt(containerStyle.width);
let x = elementRect.left; let x = elementRect.left;
let width = containerRect.width; let width = containerWidth;
if (x + width >= window.innerWidth) { if (x + width >= window.innerWidth) {
const widthMax = window.innerWidth - x; const widthMax = window.innerWidth - x;
width = Math.min(width, widthMax); width = Math.min(width, widthMax);
@ -49,14 +52,14 @@ class Popup {
} }
let y = elementRect.bottom + this.offset; let y = elementRect.bottom + this.offset;
let height = containerRect.height; let height = containerHeight;
if (y + height >= window.innerHeight) { if (y + height >= window.innerHeight) {
const heightMax = window.innerHeight - y - this.offset; const heightMax = window.innerHeight - y - this.offset;
height = Math.min(height, heightMax); height = Math.min(height, heightMax);
y = elementRect.top - height - this.offset; y = elementRect.top - height - this.offset;
} }
this.show({x, y, width, height}, content); this.show({x, y, width, height});
} }
visible() { visible() {
@ -69,35 +72,9 @@ class Popup {
} }
} }
setContent(content) { invoke(action, params) {
if (this.container === null) {
return;
}
this.container.contentWindow.scrollTo(0, 0);
const doc = this.container.contentDocument;
doc.open();
doc.write(content);
doc.close();
}
invokeApi(action, params) {
if (this.container !== null) { if (this.container !== null) {
this.container.contentWindow.postMessage({action, params}, '*'); this.container.contentWindow.postMessage({action, params}, '*');
} }
} }
inject() {
if (this.container !== null) {
return;
}
this.container = document.createElement('iframe');
this.container.id = 'yomichan-popup';
this.container.addEventListener('mousedown', e => e.stopPropagation());
this.container.addEventListener('scroll', e => e.stopPropagation());
document.body.appendChild(this.container);
}
} }

View File

@ -40,6 +40,7 @@
"fg/img/play_audio.png", "fg/img/play_audio.png",
"fg/img/spinner.gif", "fg/img/spinner.gif",
"fg/js/frame.js", "fg/js/frame.js",
"fg/frame.html",
"fg/ttf/kanji-stroke-orders.ttf", "fg/ttf/kanji-stroke-orders.ttf",
"fg/ttf/vl-gothic-regular.ttf" "fg/ttf/vl-gothic-regular.ttf"
] ]

View File

@ -1,3 +0,0 @@
<script src="{{root}}/js/frame.js"></script>
</body>
</html>

View File

@ -1,22 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<style>
@font-face {
font-family: kanji-stroke-orders;
src: url('{{root}}/ttf/kanji-stroke-orders.ttf');
}
@font-face {
font-family: vl-gothic-regular;
src: url('{{root}}/ttf/vl-gothic-regular.ttf');
}
</style>
<link rel="stylesheet" href="{{root}}/css/frame.css">
</head>
<body>
<div class="spinner">
<img src="{{root}}/img/spinner.gif">
</div>

View File

@ -1,9 +1,7 @@
{{> header.html}}
{{#if definitions}} {{#if definitions}}
{{#each definitions}} {{#each definitions}}
{{> kanji.html addable=../addable root=../root options=../options sequence=../sequence}} {{> kanji.html addable=../addable root=../root}}
{{/each}} {{/each}}
{{else}} {{else}}
<p>No results found</p> <p>No results found</p>
{{/if}} {{/if}}
{{> footer.html}}

View File

@ -1,7 +1,7 @@
<div class="kanji-definition"> <div class="kanji-definition">
<div class="action-bar" data-sequence="{{sequence}}"> <div class="action-bar">
{{#if addable}} {{#if addable}}
<a href="#" title="Add Kanji" class="action-add-note pending disabled" data-mode="kanji" data-index="{{@index}}"><img src="{{root}}/img/add_kanji.png"></a> <a href="#" title="Add Kanji" class="action-add-note pending disabled" data-mode="kanji" data-index="{{@index}}"><img src="img/add_kanji.png"></a>
{{/if}} {{/if}}
</div> </div>

View File

@ -1,9 +1,7 @@
{{> header.html}}
{{#if definitions}} {{#if definitions}}
{{#each definitions}} {{#each definitions}}
{{> term.html addable=../addable root=../root options=../options sequence=../sequence}} {{> term.html addable=../addable playback=../playback}}
{{/each}} {{/each}}
{{else}} {{else}}
<p>No results found</p> <p>No results found</p>
{{/if}} {{/if}}
{{> footer.html}}

View File

@ -1,11 +1,11 @@
<div class="term-definition"> <div class="term-definition">
<div class="action-bar" data-sequence="{{sequence}}"> <div class="action-bar">
{{#if addable}} {{#if addable}}
<a href="#" title="Add term as expression" class="action-add-note pending disabled" data-mode="term_kanji" data-index="{{@index}}"><img src="{{root}}/img/add_term_kanji.png"></a> <a href="#" title="Add term as expression" class="action-add-note pending disabled" data-mode="term_kanji" data-index="{{@index}}"><img src="img/add_term_kanji.png"></a>
<a href="#" title="Add term as reading" class="action-add-note pending disabled" data-mode="term_kana" data-index="{{@index}}"><img src="{{root}}/img/add_term_kana.png"></a> <a href="#" title="Add term as reading" class="action-add-note pending disabled" data-mode="term_kana" data-index="{{@index}}"><img src="img/add_term_kana.png"></a>
{{/if}} {{/if}}
{{#if options.enableAudioPlayback}} {{#if playback}}
<a href="#" title="Play audio" class="action-play-audio" data-index="{{@index}}"><img src="{{root}}/img/play_audio.png"></a> <a href="#" title="Play audio" class="action-play-audio" data-index="{{@index}}"><img src="img/play_audio.png"></a>
{{/if}} {{/if}}
</div> </div>