From f4314497e401e94ccd2ff88358b5720c73074612 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sun, 11 Sep 2016 20:59:42 -0700 Subject: Cleanup --- ext/fg/js/util.js | 114 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 ext/fg/js/util.js (limited to 'ext/fg/js/util.js') diff --git a/ext/fg/js/util.js b/ext/fg/js/util.js new file mode 100644 index 00000000..176765fc --- /dev/null +++ b/ext/fg/js/util.js @@ -0,0 +1,114 @@ +/* + * 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 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(); +} -- cgit v1.2.3 From b969e8952c551fdc7c150dcc111eccdc90ac7408 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Thu, 15 Sep 2016 21:03:58 -0700 Subject: Cleanup --- ext/bg/js/translator.js | 6 +++--- ext/fg/js/client.js | 6 +++--- ext/fg/js/frame.js | 10 +++++++--- ext/fg/js/popup.js | 2 +- ext/fg/js/util.js | 12 ++++++------ 5 files changed, 20 insertions(+), 16 deletions(-) (limited to 'ext/fg/js/util.js') diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js index 2331bde7..e534e0cb 100644 --- a/ext/bg/js/translator.js +++ b/ext/bg/js/translator.js @@ -54,10 +54,10 @@ class Translator { percent += banks[url].loaded / banks[url].total; } - percent /= 3; + percent /= 3.0; if (callback) { - callback({state: 'update', progress: Math.ceil(100 * percent)}); + callback({state: 'update', progress: Math.ceil(100.0 * percent)}); } }; @@ -69,7 +69,7 @@ class Translator { return this.dictionary.sealDb(); }).then(() => { if (callback) { - callback({state: 'end', progress: 100}); + callback({state: 'end', progress: 100.0}); } }); }).then(() => { diff --git a/ext/fg/js/client.js b/ext/fg/js/client.js index daeabf99..717b5873 100644 --- a/ext/fg/js/client.js +++ b/ext/fg/js/client.js @@ -116,7 +116,7 @@ class Client { return canAddDefinitions(definitions, ['term_kanji', 'term_kana']); }).then(states => { if (states !== null) { - states.forEach((state, index) => this.popup.sendMessage('setActionState', {index, state, sequence })); + states.forEach((state, index) => this.popup.invokeApi('setActionState', {index, state, sequence})); } }); } @@ -158,7 +158,7 @@ class Client { const state = {[mode]: false}; addDefinition(this.definitions[index], mode).then(success => { if (success) { - this.popup.sendMessage('setActionState', {index, state, sequence: this.sequence}); + this.popup.invokeApi('setActionState', {index, state, sequence: this.sequence}); } else { alert('Note could not be added'); } @@ -195,7 +195,7 @@ class Client { return canAddDefinitions(definitions, ['kanji']); }).then(states => { if (states !== null) { - states.forEach((state, index) => this.popup.sendMessage('setActionState', {index, state, sequence})); + states.forEach((state, index) => this.popup.invokeApi('setActionState', {index, state, sequence})); } }); }); diff --git a/ext/fg/js/frame.js b/ext/fg/js/frame.js index 590646d5..8a99a405 100644 --- a/ext/fg/js/frame.js +++ b/ext/fg/js/frame.js @@ -17,11 +17,15 @@ */ +function invokeApi(action, params, target) { + target.postMessage({action, params}, '*'); +} + function registerKanjiLinks() { for (const link of Array.from(document.getElementsByClassName('kanji-link'))) { link.addEventListener('click', e => { e.preventDefault(); - window.parent.postMessage({action: 'displayKanji', params: e.target.innerHTML}, '*'); + invokeApi('displayKanji', e.target.innerHTML, window.parent); }); } } @@ -31,7 +35,7 @@ function registerAddNoteLinks() { link.addEventListener('click', e => { e.preventDefault(); const ds = e.currentTarget.dataset; - window.parent.postMessage({action: 'addNote', params: {index: ds.index, mode: ds.mode}}, '*'); + invokeApi('addNote', {index: ds.index, mode: ds.mode}, window.parent); }); } } @@ -41,7 +45,7 @@ function registerAudioLinks() { link.addEventListener('click', e => { e.preventDefault(); const ds = e.currentTarget.dataset; - window.parent.postMessage({action: 'playAudio', params: ds.index}, '*'); + invokeApi('playAudio', ds.index, window.parent); }); } } diff --git a/ext/fg/js/popup.js b/ext/fg/js/popup.js index 930df18d..88b8e4e3 100644 --- a/ext/fg/js/popup.js +++ b/ext/fg/js/popup.js @@ -68,7 +68,7 @@ class Popup { doc.close(); } - sendMessage(action, params, callback) { + invokeApi(action, params) { if (this.popup !== null) { this.popup.contentWindow.postMessage({action, params}, '*'); } diff --git a/ext/fg/js/util.js b/ext/fg/js/util.js index 176765fc..c24ad885 100644 --- a/ext/fg/js/util.js +++ b/ext/fg/js/util.js @@ -17,28 +17,28 @@ */ -function sendMessage(action, params) { +function invokeApiBg(action, params) { return new Promise((resolve, reject) => chrome.runtime.sendMessage({action, params}, resolve)); } function findTerm(text) { - return sendMessage('findTerm', {text}); + return invokeApiBg('findTerm', {text}); } function findKanji(text) { - return sendMessage('findKanji', {text}); + return invokeApiBg('findKanji', {text}); } function renderText(data, template) { - return sendMessage('renderText', {data, template}); + return invokeApiBg('renderText', {data, template}); } function canAddDefinitions(definitions, modes) { - return sendMessage('canAddDefinitions', {definitions, modes}); + return invokeApiBg('canAddDefinitions', {definitions, modes}); } function addDefinition(definition, mode) { - return sendMessage('addDefinition', {definition, mode}); + return invokeApiBg('addDefinition', {definition, mode}); } function textSourceFromPoint(point) { -- cgit v1.2.3