2016-03-27 13:21:52 -07:00
|
|
|
/*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2016-09-15 22:44:33 -07:00
|
|
|
class Driver {
|
2016-03-27 13:21:52 -07:00
|
|
|
constructor() {
|
2016-07-25 20:07:54 -07:00
|
|
|
this.popup = new Popup();
|
2016-10-08 17:39:21 -07:00
|
|
|
this.popupTimer = null;
|
2016-07-25 20:07:54 -07:00
|
|
|
this.lastMousePos = null;
|
2016-07-23 12:47:42 -07:00
|
|
|
this.lastTextSource = null;
|
2016-09-11 19:47:40 -07:00
|
|
|
this.pendingLookup = false;
|
2016-07-25 20:07:54 -07:00
|
|
|
this.enabled = false;
|
2016-09-28 20:14:21 -07:00
|
|
|
this.options = null;
|
2016-03-27 17:04:49 -07:00
|
|
|
|
2016-04-17 17:36:15 -07:00
|
|
|
chrome.runtime.onMessage.addListener(this.onBgMessage.bind(this));
|
2016-10-08 17:39:21 -07:00
|
|
|
window.addEventListener('mouseover', this.onMouseOver.bind(this));
|
2016-03-28 19:06:01 -07:00
|
|
|
window.addEventListener('mousedown', this.onMouseDown.bind(this));
|
|
|
|
window.addEventListener('mousemove', this.onMouseMove.bind(this));
|
|
|
|
window.addEventListener('keydown', this.onKeyDown.bind(this));
|
2017-01-07 18:39:36 -08:00
|
|
|
window.addEventListener('resize', e => this.searchClear());
|
2016-09-28 20:14:21 -07:00
|
|
|
|
|
|
|
getOptions().then(opts => {
|
|
|
|
this.options = opts;
|
2017-01-07 18:39:36 -08:00
|
|
|
return isEnabled();
|
2016-09-28 20:14:21 -07:00
|
|
|
}).then(enabled => {
|
|
|
|
this.enabled = enabled;
|
|
|
|
});
|
2016-03-27 13:21:52 -07:00
|
|
|
}
|
|
|
|
|
2016-10-08 17:39:21 -07:00
|
|
|
popupTimerSet(callback) {
|
|
|
|
this.popupTimerClear();
|
2017-01-13 21:06:33 -08:00
|
|
|
this.popupTimer = window.setTimeout(callback, this.options.scanning.delay);
|
2016-10-08 17:39:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
popupTimerClear() {
|
|
|
|
if (this.popupTimer !== null) {
|
|
|
|
window.clearTimeout(this.popupTimer);
|
|
|
|
this.popupTimer = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-28 19:06:01 -07:00
|
|
|
onKeyDown(e) {
|
2016-10-08 17:39:21 -07:00
|
|
|
this.popupTimerClear();
|
|
|
|
|
|
|
|
if (this.enabled && this.lastMousePos !== null && e.keyCode === 16 /* shift */) {
|
|
|
|
this.searchAt(this.lastMousePos, true);
|
2016-10-17 09:01:43 -07:00
|
|
|
} else if (e.keyCode === 27 /* esc */) {
|
2017-01-07 18:39:36 -08:00
|
|
|
this.searchClear();
|
2016-03-27 22:27:30 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-08 17:39:21 -07:00
|
|
|
onMouseOver(e) {
|
|
|
|
if (e.target === this.popup.container && this.popuptimer !== null) {
|
|
|
|
this.popupTimerClear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-28 19:06:01 -07:00
|
|
|
onMouseMove(e) {
|
2016-10-17 09:01:43 -07:00
|
|
|
this.lastMousePos = {x: e.clientX, y: e.clientY};
|
2016-10-08 17:39:21 -07:00
|
|
|
this.popupTimerClear();
|
|
|
|
|
|
|
|
if (!this.enabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-10-17 09:01:43 -07:00
|
|
|
if (e.which === 1 /* lmb */) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-13 21:06:33 -08:00
|
|
|
if (this.options.scanning.requireShift && !e.shiftKey) {
|
2016-10-08 17:39:21 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const searcher = () => this.searchAt(this.lastMousePos, false);
|
2017-01-07 18:39:36 -08:00
|
|
|
if (!this.popup.isVisible() || e.shiftKey || e.which === 2 /* mmb */) {
|
2016-10-08 17:39:21 -07:00
|
|
|
searcher();
|
|
|
|
} else {
|
|
|
|
this.popupTimerSet(searcher);
|
2016-03-27 17:04:49 -07:00
|
|
|
}
|
2016-03-28 19:06:01 -07:00
|
|
|
}
|
2016-03-27 17:04:49 -07:00
|
|
|
|
2016-03-28 19:06:01 -07:00
|
|
|
onMouseDown(e) {
|
2016-10-17 09:14:49 -07:00
|
|
|
this.lastMousePos = {x: e.clientX, y: e.clientY};
|
|
|
|
this.popupTimerClear();
|
2017-01-07 18:39:36 -08:00
|
|
|
this.searchClear();
|
2016-03-28 13:00:48 -07:00
|
|
|
}
|
|
|
|
|
2016-07-23 15:14:13 -07:00
|
|
|
onBgMessage({action, params}, sender, callback) {
|
|
|
|
const method = this['api_' + action];
|
|
|
|
if (typeof(method) === 'function') {
|
|
|
|
method.call(this, params);
|
2016-04-05 22:18:55 -07:00
|
|
|
}
|
|
|
|
|
2016-03-28 13:00:48 -07:00
|
|
|
callback();
|
|
|
|
}
|
|
|
|
|
2017-01-07 12:21:47 -08:00
|
|
|
searchAt(point, hideNotFound) {
|
|
|
|
if (this.pendingLookup) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const textSource = textSourceFromPoint(point);
|
|
|
|
if (textSource === null || !textSource.containsPoint(point)) {
|
|
|
|
if (hideNotFound) {
|
2017-01-07 18:39:36 -08:00
|
|
|
this.searchClear();
|
2017-01-07 12:21:47 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.lastTextSource !== null && this.lastTextSource.equals(textSource)) {
|
2017-01-07 18:39:36 -08:00
|
|
|
return;
|
2016-04-24 20:50:27 -07:00
|
|
|
}
|
2017-01-07 12:21:47 -08:00
|
|
|
|
|
|
|
this.pendingLookup = true;
|
|
|
|
this.searchTerms(textSource).then(found => {
|
|
|
|
if (!found) {
|
|
|
|
this.searchKanji(textSource).then(found => {
|
|
|
|
if (!found && hideNotFound) {
|
2017-01-07 18:39:36 -08:00
|
|
|
this.searchClear();
|
2017-01-07 12:21:47 -08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}).catch(error => {
|
2017-01-07 18:39:36 -08:00
|
|
|
window.alert('Error: ' + error);
|
2017-01-07 12:21:47 -08:00
|
|
|
}).then(() => {
|
|
|
|
this.pendingLookup = false;
|
|
|
|
});
|
2016-04-17 17:36:15 -07:00
|
|
|
}
|
|
|
|
|
2016-09-18 17:32:57 -07:00
|
|
|
searchTerms(textSource) {
|
2017-01-13 21:06:33 -08:00
|
|
|
textSource.setEndOffset(this.options.scanning.length);
|
2016-08-09 21:23:05 -07:00
|
|
|
|
2017-01-13 21:06:33 -08:00
|
|
|
const findFunc = this.options.general.groupResults ? findTermGrouped : findTerm;
|
2017-01-08 15:33:45 -08:00
|
|
|
return findFunc(textSource.text()).then(({definitions, length}) => {
|
2016-09-18 17:32:57 -07:00
|
|
|
if (definitions.length === 0) {
|
2016-10-08 17:39:21 -07:00
|
|
|
return false;
|
2016-09-11 20:18:34 -07:00
|
|
|
} else {
|
2016-07-24 21:18:17 -07:00
|
|
|
textSource.setEndOffset(length);
|
2016-05-21 22:59:29 -07:00
|
|
|
|
2017-01-13 21:06:33 -08:00
|
|
|
const sentence = extractSentence(textSource, this.options.anki.sentenceExt);
|
2016-09-11 19:47:40 -07:00
|
|
|
definitions.forEach(definition => {
|
2016-07-25 20:07:54 -07:00
|
|
|
definition.url = window.location.href;
|
|
|
|
definition.sentence = sentence;
|
|
|
|
});
|
|
|
|
|
2017-01-07 12:21:47 -08:00
|
|
|
this.popup.showNextTo(textSource.getRect());
|
2017-01-09 09:30:56 -08:00
|
|
|
this.popup.showTermDefs(definitions, this.options);
|
2017-01-07 18:52:51 -08:00
|
|
|
this.lastTextSource = textSource;
|
2017-01-13 21:06:33 -08:00
|
|
|
if (this.options.scanning.selectText) {
|
2017-01-07 18:52:51 -08:00
|
|
|
textSource.select();
|
|
|
|
}
|
2016-10-08 17:39:21 -07:00
|
|
|
|
2017-01-07 12:21:47 -08:00
|
|
|
return true;
|
2016-09-11 20:18:34 -07:00
|
|
|
}
|
2016-10-19 21:21:00 -07:00
|
|
|
}).catch(error => {
|
2017-01-07 18:39:36 -08:00
|
|
|
window.alert('Error: ' + error);
|
|
|
|
return false;
|
2016-09-11 20:41:41 -07:00
|
|
|
});
|
2016-03-27 13:21:52 -07:00
|
|
|
}
|
|
|
|
|
2016-09-18 17:32:57 -07:00
|
|
|
searchKanji(textSource) {
|
|
|
|
textSource.setEndOffset(1);
|
|
|
|
|
2016-10-08 17:39:21 -07:00
|
|
|
return findKanji(textSource.text()).then(definitions => {
|
2016-09-18 17:32:57 -07:00
|
|
|
if (definitions.length === 0) {
|
2016-10-08 17:39:21 -07:00
|
|
|
return false;
|
2016-09-18 17:32:57 -07:00
|
|
|
} else {
|
|
|
|
definitions.forEach(definition => definition.url = window.location.href);
|
|
|
|
|
2017-01-07 12:21:47 -08:00
|
|
|
this.popup.showNextTo(textSource.getRect());
|
2017-01-07 18:39:36 -08:00
|
|
|
this.popup.showKanjiDefs(definitions, this.options);
|
2017-01-07 18:52:51 -08:00
|
|
|
this.lastTextSource = textSource;
|
2017-01-13 21:06:33 -08:00
|
|
|
if (this.options.scanning.selectText) {
|
2017-01-07 18:52:51 -08:00
|
|
|
textSource.select();
|
|
|
|
}
|
2016-10-08 17:39:21 -07:00
|
|
|
|
2017-01-07 12:21:47 -08:00
|
|
|
return true;
|
2016-09-18 17:32:57 -07:00
|
|
|
}
|
2016-10-19 21:21:00 -07:00
|
|
|
}).catch(error => {
|
2017-01-07 18:39:36 -08:00
|
|
|
window.alert('Error: ' + error);
|
|
|
|
return false;
|
2016-09-18 17:32:57 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-01-07 18:39:36 -08:00
|
|
|
searchClear() {
|
2016-04-23 10:10:34 -07:00
|
|
|
this.popup.hide();
|
2016-04-07 20:51:05 -07:00
|
|
|
|
2017-01-13 21:06:33 -08:00
|
|
|
if (this.options.scanning.selectText && this.lastTextSource !== null) {
|
2016-07-22 22:14:59 -07:00
|
|
|
this.lastTextSource.deselect();
|
2016-04-07 22:41:16 -07:00
|
|
|
}
|
2016-04-07 20:51:05 -07:00
|
|
|
|
2016-08-09 21:23:05 -07:00
|
|
|
this.lastTextSource = null;
|
2016-03-27 13:21:52 -07:00
|
|
|
}
|
2016-03-27 17:04:49 -07:00
|
|
|
|
2016-07-23 15:14:13 -07:00
|
|
|
api_setOptions(opts) {
|
|
|
|
this.options = opts;
|
|
|
|
}
|
|
|
|
|
|
|
|
api_setEnabled(enabled) {
|
2016-03-27 17:04:49 -07:00
|
|
|
if (!(this.enabled = enabled)) {
|
2017-01-07 18:39:36 -08:00
|
|
|
this.searchClear();
|
2016-03-27 17:04:49 -07:00
|
|
|
}
|
|
|
|
}
|
2016-03-27 13:21:52 -07:00
|
|
|
}
|
|
|
|
|
2016-09-15 22:44:33 -07:00
|
|
|
window.driver = new Driver();
|