Cleanup
This commit is contained in:
parent
fa446f540a
commit
f4314497e4
@ -1,42 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) 2016 Alex Yatskov <alex@foosoft.net>
|
|
||||||
* Author: Alex Yatskov <alex@foosoft.net>
|
|
||||||
*
|
|
||||||
* 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 <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
function bgSendMessage(action, params) {
|
|
||||||
return new Promise((resolve, reject) => chrome.runtime.sendMessage({action, params}, resolve));
|
|
||||||
}
|
|
||||||
|
|
||||||
function bgFindTerm(text) {
|
|
||||||
return bgSendMessage('findTerm', {text});
|
|
||||||
}
|
|
||||||
|
|
||||||
function bgFindKanji(text) {
|
|
||||||
return bgSendMessage('findKanji', {text});
|
|
||||||
}
|
|
||||||
|
|
||||||
function bgRenderText(data, template) {
|
|
||||||
return bgSendMessage('renderText', {data, template});
|
|
||||||
}
|
|
||||||
|
|
||||||
function bgCanAddDefinitions(definitions, modes) {
|
|
||||||
return bgSendMessage('canAddDefinitions', {definitions, modes});
|
|
||||||
}
|
|
||||||
|
|
||||||
function bgAddDefinition(definition, mode) {
|
|
||||||
return bgSendMessage('addDefinition', {definition, mode});
|
|
||||||
}
|
|
@ -82,7 +82,7 @@ class Client {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const textSource = Client.textSourceFromPoint(point);
|
const textSource = textSourceFromPoint(point);
|
||||||
if (textSource === null || !textSource.containsPoint(point)) {
|
if (textSource === null || !textSource.containsPoint(point)) {
|
||||||
this.hidePopup();
|
this.hidePopup();
|
||||||
return;
|
return;
|
||||||
@ -95,25 +95,25 @@ class Client {
|
|||||||
textSource.setEndOffset(this.options.scanLength);
|
textSource.setEndOffset(this.options.scanLength);
|
||||||
|
|
||||||
this.pendingLookup = true;
|
this.pendingLookup = true;
|
||||||
bgFindTerm(textSource.text()).then(({definitions, length}) => {
|
findTerm(textSource.text()).then(({definitions, length}) => {
|
||||||
if (length === 0) {
|
if (length === 0) {
|
||||||
this.pendingLookup = false;
|
this.pendingLookup = false;
|
||||||
this.hidePopup();
|
this.hidePopup();
|
||||||
} else {
|
} else {
|
||||||
textSource.setEndOffset(length);
|
textSource.setEndOffset(length);
|
||||||
|
|
||||||
const sentence = Client.extractSentence(textSource, this.options.sentenceExtent);
|
const sentence = extractSentence(textSource, this.options.sentenceExtent);
|
||||||
definitions.forEach(definition => {
|
definitions.forEach(definition => {
|
||||||
definition.url = window.location.href;
|
definition.url = window.location.href;
|
||||||
definition.sentence = sentence;
|
definition.sentence = sentence;
|
||||||
});
|
});
|
||||||
|
|
||||||
const sequence = ++this.sequence;
|
const sequence = ++this.sequence;
|
||||||
return bgRenderText({definitions, sequence, root: this.fgRoot, options: this.options}, 'term-list.html').then(content => {
|
return renderText({definitions, sequence, root: this.fgRoot, options: this.options}, 'term-list.html').then(content => {
|
||||||
this.definitions = definitions;
|
this.definitions = definitions;
|
||||||
this.pendingLookup = false;
|
this.pendingLookup = false;
|
||||||
this.showPopup(textSource, content);
|
this.showPopup(textSource, content);
|
||||||
return bgCanAddDefinitions(definitions, ['term_kanji', 'term_kana']);
|
return canAddDefinitions(definitions, ['term_kanji', 'term_kana']);
|
||||||
}).then(states => {
|
}).then(states => {
|
||||||
if (states !== null) {
|
if (states !== null) {
|
||||||
states.forEach((state, index) => this.popup.sendMessage('setActionState', {index, state, sequence }));
|
states.forEach((state, index) => this.popup.sendMessage('setActionState', {index, state, sequence }));
|
||||||
@ -156,7 +156,7 @@ class Client {
|
|||||||
|
|
||||||
api_addNote({index, mode}) {
|
api_addNote({index, mode}) {
|
||||||
const state = {[mode]: false};
|
const state = {[mode]: false};
|
||||||
bgAddDefinition(this.definitions[index], mode).then(success => {
|
addDefinition(this.definitions[index], mode).then(success => {
|
||||||
if (success) {
|
if (success) {
|
||||||
this.popup.sendMessage('setActionState', {index, state, sequence: this.sequence});
|
this.popup.sendMessage('setActionState', {index, state, sequence: this.sequence});
|
||||||
} else {
|
} else {
|
||||||
@ -185,14 +185,14 @@ class Client {
|
|||||||
}
|
}
|
||||||
|
|
||||||
api_displayKanji(kanji) {
|
api_displayKanji(kanji) {
|
||||||
bgFindKanji(kanji).then(definitions => {
|
findKanji(kanji).then(definitions => {
|
||||||
definitions.forEach(definition => definition.url = window.location.href);
|
definitions.forEach(definition => definition.url = window.location.href);
|
||||||
|
|
||||||
const sequence = ++this.sequence;
|
const sequence = ++this.sequence;
|
||||||
return bgRenderText({definitions, sequence, root: this.fgRoot, options: this.options}, 'kanji-list.html').then(content => {
|
return renderText({definitions, sequence, root: this.fgRoot, options: this.options}, 'kanji-list.html').then(content => {
|
||||||
this.definitions = definitions;
|
this.definitions = definitions;
|
||||||
this.popup.setContent(content, definitions);
|
this.popup.setContent(content, definitions);
|
||||||
return bgCanAddDefinitions(definitions, ['kanji']);
|
return canAddDefinitions(definitions, ['kanji']);
|
||||||
}).then(states => {
|
}).then(states => {
|
||||||
if (states !== null) {
|
if (states !== null) {
|
||||||
states.forEach((state, index) => this.popup.sendMessage('setActionState', {index, state, sequence}));
|
states.forEach((state, index) => this.popup.sendMessage('setActionState', {index, state, sequence}));
|
||||||
@ -200,78 +200,6 @@ class Client {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
static textSourceFromPoint(point) {
|
|
||||||
const element = document.elementFromPoint(point.x, point.y);
|
|
||||||
if (element !== null) {
|
|
||||||
const names = ['IMG', 'INPUT', 'BUTTON', 'TEXTAREA'];
|
|
||||||
if (names.includes(element.nodeName)) {
|
|
||||||
return new TextSourceElement(element);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const range = document.caretRangeFromPoint(point.x, point.y);
|
|
||||||
if (range !== null) {
|
|
||||||
return new TextSourceRange(range);
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
for (let i = position; i >= startPos; --i) {
|
|
||||||
const c = content[i];
|
|
||||||
|
|
||||||
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 (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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return content.substring(startPos, endPos).trim();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
window.yomiClient = new Client();
|
window.yomiClient = new Client();
|
||||||
|
@ -19,7 +19,7 @@
|
|||||||
|
|
||||||
function registerKanjiLinks() {
|
function registerKanjiLinks() {
|
||||||
for (const link of Array.from(document.getElementsByClassName('kanji-link'))) {
|
for (const link of Array.from(document.getElementsByClassName('kanji-link'))) {
|
||||||
link.addEventListener('click', (e) => {
|
link.addEventListener('click', e => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
window.parent.postMessage({action: 'displayKanji', params: e.target.innerHTML}, '*');
|
window.parent.postMessage({action: 'displayKanji', params: e.target.innerHTML}, '*');
|
||||||
});
|
});
|
||||||
@ -28,7 +28,7 @@ function registerKanjiLinks() {
|
|||||||
|
|
||||||
function registerAddNoteLinks() {
|
function registerAddNoteLinks() {
|
||||||
for (const link of Array.from(document.getElementsByClassName('action-add-note'))) {
|
for (const link of Array.from(document.getElementsByClassName('action-add-note'))) {
|
||||||
link.addEventListener('click', (e) => {
|
link.addEventListener('click', e => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
const ds = e.currentTarget.dataset;
|
const ds = e.currentTarget.dataset;
|
||||||
window.parent.postMessage({action: 'addNote', params: {index: ds.index, mode: ds.mode}}, '*');
|
window.parent.postMessage({action: 'addNote', params: {index: ds.index, mode: ds.mode}}, '*');
|
||||||
@ -38,7 +38,7 @@ function registerAddNoteLinks() {
|
|||||||
|
|
||||||
function registerAudioLinks() {
|
function registerAudioLinks() {
|
||||||
for (const link of Array.from(document.getElementsByClassName('action-play-audio'))) {
|
for (const link of Array.from(document.getElementsByClassName('action-play-audio'))) {
|
||||||
link.addEventListener('click', (e) => {
|
link.addEventListener('click', e => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
const ds = e.currentTarget.dataset;
|
const ds = e.currentTarget.dataset;
|
||||||
window.parent.postMessage({action: 'playAudio', params: ds.index}, '*');
|
window.parent.postMessage({action: 'playAudio', params: ds.index}, '*');
|
||||||
|
@ -81,8 +81,8 @@ class Popup {
|
|||||||
|
|
||||||
this.popup = document.createElement('iframe');
|
this.popup = document.createElement('iframe');
|
||||||
this.popup.id = 'yomichan-popup';
|
this.popup.id = 'yomichan-popup';
|
||||||
this.popup.addEventListener('mousedown', (e) => e.stopPropagation());
|
this.popup.addEventListener('mousedown', e => e.stopPropagation());
|
||||||
this.popup.addEventListener('scroll', (e) => e.stopPropagation());
|
this.popup.addEventListener('scroll', e => e.stopPropagation());
|
||||||
|
|
||||||
document.body.appendChild(this.popup);
|
document.body.appendChild(this.popup);
|
||||||
}
|
}
|
||||||
|
114
ext/fg/js/util.js
Normal file
114
ext/fg/js/util.js
Normal file
@ -0,0 +1,114 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2016 Alex Yatskov <alex@foosoft.net>
|
||||||
|
* Author: Alex Yatskov <alex@foosoft.net>
|
||||||
|
*
|
||||||
|
* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
function sendMessage(action, params) {
|
||||||
|
return new Promise((resolve, reject) => chrome.runtime.sendMessage({action, params}, resolve));
|
||||||
|
}
|
||||||
|
|
||||||
|
function findTerm(text) {
|
||||||
|
return sendMessage('findTerm', {text});
|
||||||
|
}
|
||||||
|
|
||||||
|
function findKanji(text) {
|
||||||
|
return sendMessage('findKanji', {text});
|
||||||
|
}
|
||||||
|
|
||||||
|
function renderText(data, template) {
|
||||||
|
return sendMessage('renderText', {data, template});
|
||||||
|
}
|
||||||
|
|
||||||
|
function canAddDefinitions(definitions, modes) {
|
||||||
|
return sendMessage('canAddDefinitions', {definitions, modes});
|
||||||
|
}
|
||||||
|
|
||||||
|
function addDefinition(definition, mode) {
|
||||||
|
return sendMessage('addDefinition', {definition, mode});
|
||||||
|
}
|
||||||
|
|
||||||
|
function textSourceFromPoint(point) {
|
||||||
|
const element = document.elementFromPoint(point.x, point.y);
|
||||||
|
if (element !== null) {
|
||||||
|
const names = ['IMG', 'INPUT', 'BUTTON', 'TEXTAREA'];
|
||||||
|
if (names.includes(element.nodeName)) {
|
||||||
|
return new TextSourceElement(element);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const range = document.caretRangeFromPoint(point.x, point.y);
|
||||||
|
if (range !== null) {
|
||||||
|
return new TextSourceRange(range);
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
function 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;
|
||||||
|
for (let i = position; i >= startPos; --i) {
|
||||||
|
const c = content[i];
|
||||||
|
|
||||||
|
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 (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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return content.substring(startPos, endPos).trim();
|
||||||
|
}
|
@ -15,7 +15,7 @@
|
|||||||
"fg/js/source-range.js",
|
"fg/js/source-range.js",
|
||||||
"fg/js/source-element.js",
|
"fg/js/source-element.js",
|
||||||
"fg/js/popup.js",
|
"fg/js/popup.js",
|
||||||
"fg/js/api.js",
|
"fg/js/util.js",
|
||||||
"fg/js/client.js"
|
"fg/js/client.js"
|
||||||
],
|
],
|
||||||
"css": ["fg/css/client.css"]
|
"css": ["fg/css/client.css"]
|
||||||
|
Loading…
Reference in New Issue
Block a user