summaryrefslogtreecommitdiff
path: root/ext/fg/js/client.js
diff options
context:
space:
mode:
authorAlex Yatskov <alex@foosoft.net>2016-07-25 20:07:54 -0700
committerAlex Yatskov <alex@foosoft.net>2016-07-25 20:07:54 -0700
commitf88f8dc97fa4baa354a7e04c125dd6968a22c85f (patch)
treed8e6cb61234aa5bb5b55e329fad764933062657e /ext/fg/js/client.js
parentd26ecab0b521e8da19c01521e5a5a3e3ada331eb (diff)
Sentence and URL support
Diffstat (limited to 'ext/fg/js/client.js')
-rw-r--r--ext/fg/js/client.js44
1 files changed, 30 insertions, 14 deletions
diff --git a/ext/fg/js/client.js b/ext/fg/js/client.js
index 2c46e9cf..a8fbaa68 100644
--- a/ext/fg/js/client.js
+++ b/ext/fg/js/client.js
@@ -19,17 +19,18 @@
class Client {
constructor() {
- this.popup = new Popup();
- this.audio = {};
- this.lastMousePos = null;
+ this.popup = new Popup();
+ this.audio = {};
+ this.lastMousePos = null;
this.lastTextSource = null;
- this.activateKey = 16;
- this.activateBtn = 2;
- this.enabled = false;
- this.options = {};
- this.definitions = null;
- this.sequence = 0;
- this.fgRoot = chrome.extension.getURL('fg');
+ this.activateKey = 16;
+ this.activateBtn = 2;
+ this.sentenceExtent = 200;
+ this.enabled = false;
+ this.options = {};
+ this.definitions = null;
+ this.sequence = 0;
+ this.fgRoot = chrome.extension.getURL('fg');
chrome.runtime.onMessage.addListener(this.onBgMessage.bind(this));
window.addEventListener('message', this.onFrameMessage.bind(this));
@@ -94,9 +95,15 @@ class Client {
if (length === 0) {
this.hidePopup();
} else {
- const sequence = ++this.sequence;
textSource.setEndOffset(length);
+ const sentence = Client.extractSentence(textSource, this.sentenceExtent);
+ definitions.forEach((definition) => {
+ definition.url = window.location.href;
+ definition.sentence = sentence;
+ });
+
+ const sequence = ++this.sequence;
bgRenderText(
{definitions, root: this.fgRoot, options: this.options, sequence},
'term-list.html',
@@ -180,6 +187,10 @@ class Client {
api_displayKanji(kanji) {
bgFindKanji(kanji, (definitions) => {
+ definitions.forEach((definition) => {
+ definition.url = window.location.href;
+ });
+
const sequence = ++this.sequence;
bgRenderText(
{definitions, root: this.fgRoot, options: this.options, sequence},
@@ -219,11 +230,16 @@ class Client {
return null;
}
- static extractSentence(content, position) {
- const quotesFwd = {'「': '」', '『': '』', "'": "'", '"': '"'};
- const quotesBwd = {'」': '「', '』': '『', "'": "'", '"': '"'};
+ static extractSentence(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;