Sentence and URL support
This commit is contained in:
parent
d26ecab0b5
commit
f88f8dc97f
@ -171,7 +171,9 @@ class Yomichan {
|
|||||||
'kunyomi',
|
'kunyomi',
|
||||||
'onyomi',
|
'onyomi',
|
||||||
'reading',
|
'reading',
|
||||||
|
'sentence',
|
||||||
'tags',
|
'tags',
|
||||||
|
'url',
|
||||||
];
|
];
|
||||||
|
|
||||||
for (let tag of tags) {
|
for (let tag of tags) {
|
||||||
|
@ -19,17 +19,18 @@
|
|||||||
|
|
||||||
class Client {
|
class Client {
|
||||||
constructor() {
|
constructor() {
|
||||||
this.popup = new Popup();
|
this.popup = new Popup();
|
||||||
this.audio = {};
|
this.audio = {};
|
||||||
this.lastMousePos = null;
|
this.lastMousePos = null;
|
||||||
this.lastTextSource = null;
|
this.lastTextSource = null;
|
||||||
this.activateKey = 16;
|
this.activateKey = 16;
|
||||||
this.activateBtn = 2;
|
this.activateBtn = 2;
|
||||||
this.enabled = false;
|
this.sentenceExtent = 200;
|
||||||
this.options = {};
|
this.enabled = false;
|
||||||
this.definitions = null;
|
this.options = {};
|
||||||
this.sequence = 0;
|
this.definitions = null;
|
||||||
this.fgRoot = chrome.extension.getURL('fg');
|
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('message', this.onFrameMessage.bind(this));
|
||||||
@ -94,9 +95,15 @@ class Client {
|
|||||||
if (length === 0) {
|
if (length === 0) {
|
||||||
this.hidePopup();
|
this.hidePopup();
|
||||||
} else {
|
} else {
|
||||||
const sequence = ++this.sequence;
|
|
||||||
textSource.setEndOffset(length);
|
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(
|
bgRenderText(
|
||||||
{definitions, root: this.fgRoot, options: this.options, sequence},
|
{definitions, root: this.fgRoot, options: this.options, sequence},
|
||||||
'term-list.html',
|
'term-list.html',
|
||||||
@ -180,6 +187,10 @@ class Client {
|
|||||||
|
|
||||||
api_displayKanji(kanji) {
|
api_displayKanji(kanji) {
|
||||||
bgFindKanji(kanji, (definitions) => {
|
bgFindKanji(kanji, (definitions) => {
|
||||||
|
definitions.forEach((definition) => {
|
||||||
|
definition.url = window.location.href;
|
||||||
|
});
|
||||||
|
|
||||||
const sequence = ++this.sequence;
|
const sequence = ++this.sequence;
|
||||||
bgRenderText(
|
bgRenderText(
|
||||||
{definitions, root: this.fgRoot, options: this.options, sequence},
|
{definitions, root: this.fgRoot, options: this.options, sequence},
|
||||||
@ -219,11 +230,16 @@ class Client {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
static extractSentence(content, position) {
|
static extractSentence(source, extent) {
|
||||||
const quotesFwd = {'「': '」', '『': '』', "'": "'", '"': '"'};
|
const quotesFwd = {'「': '」', '『': '』', "'": "'", '"': '"'};
|
||||||
const quotesBwd = {'」': '「', '』': '『', "'": "'", '"': '"'};
|
const quotesBwd = {'」': '「', '』': '『', "'": "'", '"': '"'};
|
||||||
const terminators = '…。..??!!';
|
const terminators = '…。..??!!';
|
||||||
|
|
||||||
|
const sourceLocal = source.clone();
|
||||||
|
const position = sourceLocal.setStartOffset(extent);
|
||||||
|
sourceLocal.setEndOffset(position + extent);
|
||||||
|
const content = sourceLocal.text();
|
||||||
|
|
||||||
let quoteStack = [];
|
let quoteStack = [];
|
||||||
|
|
||||||
let startPos = 0;
|
let startPos = 0;
|
||||||
|
@ -18,9 +18,13 @@
|
|||||||
|
|
||||||
|
|
||||||
class TextSourceInput {
|
class TextSourceInput {
|
||||||
constructor(input) {
|
constructor(input, length=-1) {
|
||||||
this.input = input;
|
this.input = input;
|
||||||
this.length = -1;
|
this.length = length;
|
||||||
|
}
|
||||||
|
|
||||||
|
clone() {
|
||||||
|
return new TextSourceInput(this.input, this.length);
|
||||||
}
|
}
|
||||||
|
|
||||||
text() {
|
text() {
|
||||||
|
@ -22,20 +22,26 @@ class TextSourceRange {
|
|||||||
this.rng = range;
|
this.rng = range;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
clone() {
|
||||||
|
return new TextSourceRange(this.rng.cloneRange());
|
||||||
|
}
|
||||||
|
|
||||||
text() {
|
text() {
|
||||||
return this.rng.toString();
|
return this.rng.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
setEndOffset(length) {
|
setEndOffset(length) {
|
||||||
const end = TextSourceRange.seekForward(this.rng.startContainer, this.rng.startOffset + length);
|
const lengthAdj = length + this.rng.startOffset;
|
||||||
this.rng.setEnd(end.node, end.offset);
|
const state = TextSourceRange.seekForward(this.rng.startContainer, lengthAdj);
|
||||||
return length - end.length;
|
this.rng.setEnd(state.node, state.offset);
|
||||||
|
return length - state.length;
|
||||||
}
|
}
|
||||||
|
|
||||||
setStartOffset(length) {
|
setStartOffset(length) {
|
||||||
const start = TextSourceRange.seekBackward(this.rng.startContainer, length + (this.rng.startContainer.length - this.rng.startOffset));
|
const lengthAdj = length + (this.rng.startContainer.length - this.rng.startOffset);
|
||||||
this.rng.setStart(start.node, start.offset);
|
const state = TextSourceRange.seekBackward(this.rng.startContainer, lengthAdj);
|
||||||
return length - start.length;
|
this.rng.setStart(state.node, state.offset);
|
||||||
|
return length - state.length;
|
||||||
}
|
}
|
||||||
|
|
||||||
containsPoint(point) {
|
containsPoint(point) {
|
||||||
@ -48,10 +54,10 @@ class TextSourceRange {
|
|||||||
}
|
}
|
||||||
|
|
||||||
getPaddedRect() {
|
getPaddedRect() {
|
||||||
const range = this.rng.cloneRange();
|
const range = this.rng.cloneRange();
|
||||||
const startOffset = range.startOffset;
|
const startOffset = range.startOffset;
|
||||||
const endOffset = range.endOffset;
|
const endOffset = range.endOffset;
|
||||||
const node = range.startContainer;
|
const node = range.startContainer;
|
||||||
|
|
||||||
range.setStart(node, Math.max(0, startOffset - 1));
|
range.setStart(node, Math.max(0, startOffset - 1));
|
||||||
range.setEnd(node, Math.min(node.length, endOffset + 1));
|
range.setEnd(node, Math.min(node.length, endOffset + 1));
|
||||||
|
Loading…
Reference in New Issue
Block a user