2016-03-27 20:21:52 +00:00
|
|
|
/*
|
2017-08-16 03:04:15 +00:00
|
|
|
* Copyright (C) 2016-2017 Alex Yatskov <alex@foosoft.net>
|
2016-03-27 20:21:52 +00:00
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2017-08-14 03:50:43 +00:00
|
|
|
class Frontend {
|
2019-08-26 01:13:17 +00:00
|
|
|
constructor(popup, ignoreNodes) {
|
2019-08-17 22:50:48 +00:00
|
|
|
this.popup = popup;
|
2019-10-24 23:38:29 +00:00
|
|
|
this.popupTimerPromise = null;
|
2019-10-25 00:45:30 +00:00
|
|
|
this.textSourceCurrent = null;
|
2016-09-12 02:47:40 +00:00
|
|
|
this.pendingLookup = false;
|
2016-09-29 03:14:21 +00:00
|
|
|
this.options = null;
|
2019-08-26 01:13:17 +00:00
|
|
|
this.ignoreNodes = (Array.isArray(ignoreNodes) && ignoreNodes.length > 0 ? ignoreNodes.join(',') : null);
|
2019-02-11 01:44:16 +00:00
|
|
|
|
2019-09-07 17:58:19 +00:00
|
|
|
this.optionsContext = {
|
2019-09-10 23:55:14 +00:00
|
|
|
depth: popup.depth,
|
|
|
|
url: popup.url
|
2019-09-07 17:58:19 +00:00
|
|
|
};
|
|
|
|
|
2019-02-11 01:44:16 +00:00
|
|
|
this.primaryTouchIdentifier = null;
|
2019-10-25 01:16:23 +00:00
|
|
|
this.preventNextContextMenu = false;
|
|
|
|
this.preventNextMouseDown = false;
|
|
|
|
this.preventNextClick = false;
|
|
|
|
this.preventScroll = false;
|
2019-10-04 00:22:12 +00:00
|
|
|
|
|
|
|
this.enabled = false;
|
|
|
|
this.eventListeners = [];
|
2019-10-12 02:35:59 +00:00
|
|
|
|
|
|
|
this.isPreparedPromiseResolve = null;
|
|
|
|
this.isPreparedPromise = new Promise((resolve) => { this.isPreparedPromiseResolve = resolve; });
|
2019-10-12 16:59:51 +00:00
|
|
|
|
|
|
|
this.lastShowPromise = Promise.resolve();
|
2017-08-13 23:11:51 +00:00
|
|
|
}
|
|
|
|
|
2019-08-17 22:50:48 +00:00
|
|
|
static create() {
|
2019-10-16 00:23:25 +00:00
|
|
|
const data = window.frontendInitializationData || {};
|
|
|
|
const {id, depth=0, parentFrameId, ignoreNodes, url, proxy=false} = data;
|
2019-08-17 22:50:48 +00:00
|
|
|
|
2019-10-16 00:23:25 +00:00
|
|
|
const popup = proxy ? new PopupProxy(depth + 1, id, parentFrameId, url) : PopupProxyHost.instance.createPopup(null, depth);
|
2019-08-26 01:13:17 +00:00
|
|
|
const frontend = new Frontend(popup, ignoreNodes);
|
2019-08-17 22:50:48 +00:00
|
|
|
frontend.prepare();
|
|
|
|
return frontend;
|
|
|
|
}
|
|
|
|
|
2017-08-13 23:11:51 +00:00
|
|
|
async prepare() {
|
|
|
|
try {
|
2019-10-04 00:22:12 +00:00
|
|
|
await this.updateOptions();
|
2019-02-11 01:44:16 +00:00
|
|
|
|
2019-10-03 00:31:42 +00:00
|
|
|
chrome.runtime.onMessage.addListener(this.onRuntimeMessage.bind(this));
|
2019-10-12 02:35:59 +00:00
|
|
|
this.isPreparedPromiseResolve();
|
2017-08-13 23:42:22 +00:00
|
|
|
} catch (e) {
|
|
|
|
this.onError(e);
|
2016-10-09 00:39:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-12 02:35:59 +00:00
|
|
|
isPrepared() {
|
|
|
|
return this.isPreparedPromise;
|
|
|
|
}
|
|
|
|
|
2016-10-09 00:39:21 +00:00
|
|
|
onMouseOver(e) {
|
2019-10-24 23:38:29 +00:00
|
|
|
if (e.target === this.popup.container) {
|
2016-10-09 00:39:21 +00:00
|
|
|
this.popupTimerClear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-29 02:06:01 +00:00
|
|
|
onMouseMove(e) {
|
2016-10-09 00:39:21 +00:00
|
|
|
this.popupTimerClear();
|
|
|
|
|
2019-11-26 23:52:05 +00:00
|
|
|
if (this.pendingLookup || DOM.isMouseButtonDown(e, 'primary')) {
|
2017-08-18 02:05:31 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-14 18:11:06 +00:00
|
|
|
const scanningOptions = this.options.scanning;
|
|
|
|
const scanningModifier = scanningOptions.modifier;
|
|
|
|
if (!(
|
|
|
|
Frontend.isScanningModifierPressed(scanningModifier, e) ||
|
2019-11-26 23:52:05 +00:00
|
|
|
(scanningOptions.middleMouse && DOM.isMouseButtonDown(e, 'auxiliary'))
|
2019-09-14 18:11:06 +00:00
|
|
|
)) {
|
2016-10-09 00:39:21 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-18 02:05:31 +00:00
|
|
|
const search = async () => {
|
2019-10-24 23:38:29 +00:00
|
|
|
if (scanningModifier === 'none') {
|
|
|
|
if (!await this.popupTimerWait()) {
|
|
|
|
// Aborted
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-24 23:38:06 +00:00
|
|
|
await this.searchAt(e.clientX, e.clientY, 'mouse');
|
2017-08-18 02:05:31 +00:00
|
|
|
};
|
|
|
|
|
2019-10-24 23:38:29 +00:00
|
|
|
search();
|
2016-03-29 02:06:01 +00:00
|
|
|
}
|
2016-03-28 00:04:49 +00:00
|
|
|
|
2016-03-29 02:06:01 +00:00
|
|
|
onMouseDown(e) {
|
2019-10-25 01:16:23 +00:00
|
|
|
if (this.preventNextMouseDown) {
|
|
|
|
this.preventNextMouseDown = false;
|
|
|
|
this.preventNextClick = true;
|
2019-02-11 01:44:16 +00:00
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-10-26 00:01:42 +00:00
|
|
|
if (e.button === 0) {
|
|
|
|
this.popupTimerClear();
|
|
|
|
this.searchClear(true);
|
|
|
|
}
|
2016-03-28 20:00:48 +00:00
|
|
|
}
|
|
|
|
|
2019-11-25 19:35:53 +00:00
|
|
|
onMouseOut() {
|
2019-08-31 13:35:25 +00:00
|
|
|
this.popupTimerClear();
|
|
|
|
}
|
|
|
|
|
2019-10-26 00:11:44 +00:00
|
|
|
onClick(e) {
|
|
|
|
if (this.preventNextClick) {
|
|
|
|
this.preventNextClick = false;
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
return false;
|
2017-03-25 22:22:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-25 19:35:53 +00:00
|
|
|
onAuxClick() {
|
2019-10-26 00:11:44 +00:00
|
|
|
this.preventNextContextMenu = false;
|
2017-08-13 23:42:22 +00:00
|
|
|
}
|
|
|
|
|
2019-10-26 00:11:44 +00:00
|
|
|
onContextMenu(e) {
|
|
|
|
if (this.preventNextContextMenu) {
|
|
|
|
this.preventNextContextMenu = false;
|
2019-02-11 01:44:16 +00:00
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onTouchStart(e) {
|
2019-10-25 01:41:58 +00:00
|
|
|
if (this.primaryTouchIdentifier !== null || e.changedTouches.length === 0) {
|
2019-02-11 01:44:16 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-10-25 01:41:13 +00:00
|
|
|
this.preventScroll = false;
|
|
|
|
this.preventNextContextMenu = false;
|
|
|
|
this.preventNextMouseDown = false;
|
|
|
|
this.preventNextClick = false;
|
|
|
|
|
2019-10-25 01:36:18 +00:00
|
|
|
const primaryTouch = e.changedTouches[0];
|
2019-11-26 23:47:16 +00:00
|
|
|
if (DOM.isPointInSelection(primaryTouch.clientX, primaryTouch.clientY, window.getSelection())) {
|
2019-10-25 01:36:18 +00:00
|
|
|
return;
|
2019-08-10 01:45:29 +00:00
|
|
|
}
|
|
|
|
|
2019-10-25 01:38:19 +00:00
|
|
|
this.primaryTouchIdentifier = primaryTouch.identifier;
|
|
|
|
|
2019-10-25 01:41:58 +00:00
|
|
|
if (this.pendingLookup) {
|
|
|
|
return;
|
|
|
|
}
|
2019-10-25 01:39:23 +00:00
|
|
|
|
2019-10-25 01:38:19 +00:00
|
|
|
const textSourceCurrentPrevious = this.textSourceCurrent !== null ? this.textSourceCurrent.clone() : null;
|
|
|
|
|
|
|
|
this.searchAt(primaryTouch.clientX, primaryTouch.clientY, 'touchStart')
|
|
|
|
.then(() => {
|
|
|
|
if (
|
|
|
|
this.textSourceCurrent === null ||
|
|
|
|
this.textSourceCurrent.equals(textSourceCurrentPrevious)
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.preventScroll = true;
|
|
|
|
this.preventNextContextMenu = true;
|
|
|
|
this.preventNextMouseDown = true;
|
|
|
|
});
|
2019-02-11 01:44:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
onTouchEnd(e) {
|
2019-10-25 01:36:18 +00:00
|
|
|
if (
|
|
|
|
this.primaryTouchIdentifier === null ||
|
|
|
|
this.getIndexOfTouch(e.changedTouches, this.primaryTouchIdentifier) < 0
|
|
|
|
) {
|
2019-02-11 01:44:16 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-10-25 01:38:19 +00:00
|
|
|
this.primaryTouchIdentifier = null;
|
|
|
|
this.preventScroll = false;
|
|
|
|
this.preventNextClick = false;
|
|
|
|
// Don't revert context menu and mouse down prevention,
|
|
|
|
// since these events can occur after the touch has ended.
|
|
|
|
// this.preventNextContextMenu = false;
|
|
|
|
// this.preventNextMouseDown = false;
|
2019-02-11 01:44:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
onTouchCancel(e) {
|
|
|
|
this.onTouchEnd(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
onTouchMove(e) {
|
2019-10-25 01:16:23 +00:00
|
|
|
if (!this.preventScroll || !e.cancelable || this.primaryTouchIdentifier === null) {
|
2019-02-11 01:44:16 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const touches = e.changedTouches;
|
|
|
|
const index = this.getIndexOfTouch(touches, this.primaryTouchIdentifier);
|
|
|
|
if (index < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-10-25 01:36:18 +00:00
|
|
|
const primaryTouch = touches[index];
|
|
|
|
this.searchAt(primaryTouch.clientX, primaryTouch.clientY, 'touchMove');
|
2019-02-11 01:44:16 +00:00
|
|
|
|
|
|
|
e.preventDefault(); // Disable scroll
|
|
|
|
}
|
|
|
|
|
2019-10-26 00:11:44 +00:00
|
|
|
async onResize() {
|
|
|
|
if (this.textSourceCurrent !== null && await this.popup.isVisibleAsync()) {
|
|
|
|
const textSource = this.textSourceCurrent;
|
|
|
|
this.lastShowPromise = this.popup.showContent(
|
|
|
|
textSource.getRect(),
|
|
|
|
textSource.getWritingMode()
|
|
|
|
);
|
|
|
|
}
|
2019-10-26 00:01:42 +00:00
|
|
|
}
|
|
|
|
|
2019-10-26 00:11:44 +00:00
|
|
|
onWindowMessage(e) {
|
|
|
|
const action = e.data;
|
|
|
|
const handlers = Frontend.windowMessageHandlers;
|
2019-11-25 19:19:18 +00:00
|
|
|
if (hasOwn(handlers, action)) {
|
2019-10-26 00:11:44 +00:00
|
|
|
const handler = handlers[action];
|
|
|
|
handler(this);
|
2019-02-11 01:44:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-03 00:31:42 +00:00
|
|
|
onRuntimeMessage({action, params}, sender, callback) {
|
|
|
|
const handlers = Frontend.runtimeMessageHandlers;
|
2019-11-25 19:19:18 +00:00
|
|
|
if (hasOwn(handlers, action)) {
|
2019-10-03 00:31:42 +00:00
|
|
|
const handler = handlers[action];
|
2019-10-20 02:28:23 +00:00
|
|
|
const result = handler(this, params);
|
|
|
|
callback(result);
|
2016-04-06 05:18:55 +00:00
|
|
|
}
|
2016-03-28 20:00:48 +00:00
|
|
|
}
|
|
|
|
|
2017-08-13 23:42:22 +00:00
|
|
|
onError(error) {
|
2019-10-09 01:33:10 +00:00
|
|
|
logError(error, false);
|
2017-08-13 23:11:51 +00:00
|
|
|
}
|
|
|
|
|
2019-10-04 00:22:12 +00:00
|
|
|
setEnabled(enabled) {
|
|
|
|
if (enabled) {
|
|
|
|
if (!this.enabled) {
|
|
|
|
this.hookEvents();
|
|
|
|
this.enabled = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (this.enabled) {
|
|
|
|
this.clearEventListeners();
|
|
|
|
this.enabled = false;
|
|
|
|
}
|
2019-09-29 17:00:32 +00:00
|
|
|
this.searchClear(false);
|
2019-09-07 15:21:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-04 00:22:12 +00:00
|
|
|
hookEvents() {
|
|
|
|
this.addEventListener(window, 'message', this.onWindowMessage.bind(this));
|
|
|
|
this.addEventListener(window, 'mousedown', this.onMouseDown.bind(this));
|
|
|
|
this.addEventListener(window, 'mousemove', this.onMouseMove.bind(this));
|
|
|
|
this.addEventListener(window, 'mouseover', this.onMouseOver.bind(this));
|
|
|
|
this.addEventListener(window, 'mouseout', this.onMouseOut.bind(this));
|
|
|
|
this.addEventListener(window, 'resize', this.onResize.bind(this));
|
|
|
|
|
|
|
|
if (this.options.scanning.touchInputEnabled) {
|
|
|
|
this.addEventListener(window, 'click', this.onClick.bind(this));
|
2019-10-26 00:01:42 +00:00
|
|
|
this.addEventListener(window, 'auxclick', this.onAuxClick.bind(this));
|
2019-10-04 00:22:12 +00:00
|
|
|
this.addEventListener(window, 'touchstart', this.onTouchStart.bind(this));
|
|
|
|
this.addEventListener(window, 'touchend', this.onTouchEnd.bind(this));
|
|
|
|
this.addEventListener(window, 'touchcancel', this.onTouchCancel.bind(this));
|
|
|
|
this.addEventListener(window, 'touchmove', this.onTouchMove.bind(this), {passive: false});
|
|
|
|
this.addEventListener(window, 'contextmenu', this.onContextMenu.bind(this));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
addEventListener(node, type, listener, options) {
|
|
|
|
node.addEventListener(type, listener, options);
|
|
|
|
this.eventListeners.push([node, type, listener, options]);
|
|
|
|
}
|
|
|
|
|
|
|
|
clearEventListeners() {
|
|
|
|
for (const [node, type, listener, options] of this.eventListeners) {
|
|
|
|
node.removeEventListener(type, listener, options);
|
|
|
|
}
|
|
|
|
this.eventListeners = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
async updateOptions() {
|
|
|
|
this.options = await apiOptionsGet(this.getOptionsContext());
|
|
|
|
this.setEnabled(this.options.general.enable);
|
2019-10-12 18:34:45 +00:00
|
|
|
await this.popup.setOptions(this.options);
|
2019-10-04 00:22:12 +00:00
|
|
|
}
|
|
|
|
|
2019-10-24 23:38:29 +00:00
|
|
|
async popupTimerWait() {
|
2019-09-19 23:00:26 +00:00
|
|
|
const delay = this.options.scanning.delay;
|
2019-11-06 00:47:02 +00:00
|
|
|
const promise = promiseTimeout(delay, true);
|
|
|
|
this.popupTimerPromise = promise;
|
2019-10-24 23:38:29 +00:00
|
|
|
try {
|
2019-11-06 00:47:02 +00:00
|
|
|
return await promise;
|
2019-10-24 23:38:29 +00:00
|
|
|
} finally {
|
2019-11-06 00:47:02 +00:00
|
|
|
if (this.popupTimerPromise === promise) {
|
|
|
|
this.popupTimerPromise = null;
|
|
|
|
}
|
2019-09-19 23:00:26 +00:00
|
|
|
}
|
2017-08-13 23:42:22 +00:00
|
|
|
}
|
2017-01-07 20:21:47 +00:00
|
|
|
|
2017-08-13 23:42:22 +00:00
|
|
|
popupTimerClear() {
|
2019-10-24 23:38:29 +00:00
|
|
|
if (this.popupTimerPromise !== null) {
|
|
|
|
this.popupTimerPromise.resolve(false);
|
2019-11-06 00:47:02 +00:00
|
|
|
this.popupTimerPromise = null;
|
2017-01-07 20:21:47 +00:00
|
|
|
}
|
2017-08-13 23:42:22 +00:00
|
|
|
}
|
2017-01-07 20:21:47 +00:00
|
|
|
|
2019-09-14 18:31:21 +00:00
|
|
|
async searchAt(x, y, cause) {
|
2019-10-24 23:38:06 +00:00
|
|
|
try {
|
2019-10-25 01:05:29 +00:00
|
|
|
this.popupTimerClear();
|
|
|
|
|
2019-10-24 23:38:06 +00:00
|
|
|
if (this.pendingLookup || await this.popup.containsPoint(x, y)) {
|
|
|
|
return;
|
|
|
|
}
|
2017-09-17 17:09:48 +00:00
|
|
|
|
2019-10-24 23:38:06 +00:00
|
|
|
const textSource = docRangeFromPoint(x, y, this.options);
|
2019-11-03 16:06:31 +00:00
|
|
|
if (this.textSourceCurrent !== null && this.textSourceCurrent.equals(textSource)) {
|
2019-10-25 00:01:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2019-11-03 16:06:31 +00:00
|
|
|
await this.searchSource(textSource, cause);
|
2019-10-25 00:01:04 +00:00
|
|
|
} finally {
|
2019-11-03 16:06:31 +00:00
|
|
|
if (textSource !== null) {
|
|
|
|
textSource.cleanup();
|
|
|
|
}
|
2019-10-25 00:01:04 +00:00
|
|
|
}
|
2019-10-24 23:38:06 +00:00
|
|
|
} catch (e) {
|
|
|
|
this.onError(e);
|
|
|
|
}
|
2019-10-12 02:35:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async searchSource(textSource, cause) {
|
2019-10-25 00:21:21 +00:00
|
|
|
let results = null;
|
2017-08-16 03:04:15 +00:00
|
|
|
|
2017-08-13 23:42:22 +00:00
|
|
|
try {
|
2019-10-25 00:07:17 +00:00
|
|
|
this.pendingLookup = true;
|
2019-11-03 16:06:31 +00:00
|
|
|
if (textSource !== null) {
|
|
|
|
results = (
|
|
|
|
await this.findTerms(textSource) ||
|
|
|
|
await this.findKanji(textSource)
|
|
|
|
);
|
|
|
|
if (results !== null) {
|
|
|
|
const focus = (cause === 'mouse');
|
|
|
|
this.showContent(textSource, focus, results.definitions, results.type);
|
|
|
|
}
|
2019-10-25 00:21:21 +00:00
|
|
|
}
|
2017-08-13 23:11:51 +00:00
|
|
|
} catch (e) {
|
2017-08-16 03:04:15 +00:00
|
|
|
if (window.yomichan_orphaned) {
|
2019-11-03 16:06:31 +00:00
|
|
|
if (textSource !== null && this.options.scanning.modifier !== 'none') {
|
2019-10-17 01:36:10 +00:00
|
|
|
this.lastShowPromise = this.popup.showContent(
|
2019-08-31 15:51:31 +00:00
|
|
|
textSource.getRect(),
|
2019-10-17 01:36:10 +00:00
|
|
|
textSource.getWritingMode(),
|
|
|
|
'orphaned'
|
2019-08-31 15:51:31 +00:00
|
|
|
);
|
2017-08-16 03:04:15 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.onError(e);
|
|
|
|
}
|
2017-08-13 23:42:22 +00:00
|
|
|
} finally {
|
2019-10-25 00:21:21 +00:00
|
|
|
if (results === null && this.options.scanning.autoHideResults) {
|
2019-09-29 17:00:32 +00:00
|
|
|
this.searchClear(true);
|
2017-09-17 06:08:43 +00:00
|
|
|
}
|
|
|
|
|
2017-08-13 23:42:22 +00:00
|
|
|
this.pendingLookup = false;
|
2017-08-13 23:11:51 +00:00
|
|
|
}
|
2019-10-25 00:45:30 +00:00
|
|
|
|
2019-11-03 16:06:31 +00:00
|
|
|
return results;
|
2016-04-18 00:36:15 +00:00
|
|
|
}
|
|
|
|
|
2019-10-25 00:21:21 +00:00
|
|
|
showContent(textSource, focus, definitions, type) {
|
2017-08-13 23:11:51 +00:00
|
|
|
const sentence = docSentenceExtract(textSource, this.options.anki.sentenceExt);
|
|
|
|
const url = window.location.href;
|
2019-10-17 01:36:10 +00:00
|
|
|
this.lastShowPromise = this.popup.showContent(
|
2017-08-13 23:11:51 +00:00
|
|
|
textSource.getRect(),
|
2019-08-31 15:51:31 +00:00
|
|
|
textSource.getWritingMode(),
|
2019-10-25 00:21:21 +00:00
|
|
|
type,
|
2019-10-17 01:36:10 +00:00
|
|
|
{definitions, context: {sentence, url, focus}}
|
2017-08-13 23:11:51 +00:00
|
|
|
);
|
|
|
|
|
2019-10-25 00:45:30 +00:00
|
|
|
this.textSourceCurrent = textSource;
|
2017-08-13 23:11:51 +00:00
|
|
|
if (this.options.scanning.selectText) {
|
|
|
|
textSource.select();
|
|
|
|
}
|
2019-10-25 00:21:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async findTerms(textSource) {
|
|
|
|
this.setTextSourceScanLength(textSource, this.options.scanning.length);
|
|
|
|
|
|
|
|
const searchText = textSource.text();
|
|
|
|
if (searchText.length === 0) { return null; }
|
|
|
|
|
2019-11-05 01:43:40 +00:00
|
|
|
const {definitions, length} = await apiTermsFind(searchText, {}, this.getOptionsContext());
|
2019-10-25 00:21:21 +00:00
|
|
|
if (definitions.length === 0) { return null; }
|
|
|
|
|
|
|
|
textSource.setEndOffset(length);
|
2017-08-13 23:11:51 +00:00
|
|
|
|
2019-10-25 00:21:21 +00:00
|
|
|
return {definitions, type: 'terms'};
|
2016-03-27 20:21:52 +00:00
|
|
|
}
|
|
|
|
|
2019-10-25 00:21:21 +00:00
|
|
|
async findKanji(textSource) {
|
2019-08-26 01:13:17 +00:00
|
|
|
this.setTextSourceScanLength(textSource, 1);
|
2016-09-19 00:32:57 +00:00
|
|
|
|
2019-08-26 01:13:17 +00:00
|
|
|
const searchText = textSource.text();
|
2019-10-25 00:21:21 +00:00
|
|
|
if (searchText.length === 0) { return null; }
|
2019-08-26 01:13:17 +00:00
|
|
|
|
2019-09-10 23:55:14 +00:00
|
|
|
const definitions = await apiKanjiFind(searchText, this.getOptionsContext());
|
2019-10-25 00:21:21 +00:00
|
|
|
if (definitions.length === 0) { return null; }
|
2017-08-13 23:11:51 +00:00
|
|
|
|
2019-10-25 00:21:21 +00:00
|
|
|
return {definitions, type: 'kanji'};
|
2016-09-19 00:32:57 +00:00
|
|
|
}
|
|
|
|
|
2019-09-29 17:00:32 +00:00
|
|
|
searchClear(changeFocus) {
|
|
|
|
this.popup.hide(changeFocus);
|
2017-12-16 18:56:53 +00:00
|
|
|
this.popup.clearAutoPlayTimer();
|
2016-04-08 03:51:05 +00:00
|
|
|
|
2019-10-25 00:45:30 +00:00
|
|
|
if (this.textSourceCurrent !== null) {
|
|
|
|
if (this.options.scanning.selectText) {
|
|
|
|
this.textSourceCurrent.deselect();
|
|
|
|
}
|
2016-04-08 03:51:05 +00:00
|
|
|
|
2019-10-25 00:45:30 +00:00
|
|
|
this.textSourceCurrent = null;
|
|
|
|
}
|
2016-03-27 20:21:52 +00:00
|
|
|
}
|
2019-02-11 01:44:16 +00:00
|
|
|
|
|
|
|
getIndexOfTouch(touchList, identifier) {
|
2019-11-25 19:28:52 +00:00
|
|
|
for (const i in touchList) {
|
|
|
|
const t = touchList[i];
|
2019-02-11 01:44:16 +00:00
|
|
|
if (t.identifier === identifier) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-08-26 01:13:17 +00:00
|
|
|
setTextSourceScanLength(textSource, length) {
|
|
|
|
textSource.setEndOffset(length);
|
|
|
|
if (this.ignoreNodes === null || !textSource.range) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
length = textSource.text().length;
|
|
|
|
while (textSource.range && length > 0) {
|
2019-09-01 14:28:50 +00:00
|
|
|
const nodes = TextSourceRange.getNodesInRange(textSource.range);
|
|
|
|
if (!TextSourceRange.anyNodeMatchesSelector(nodes, this.ignoreNodes)) {
|
2019-08-26 01:13:17 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
--length;
|
|
|
|
textSource.setEndOffset(length);
|
|
|
|
}
|
|
|
|
}
|
2019-09-14 18:11:06 +00:00
|
|
|
|
2019-09-10 23:55:14 +00:00
|
|
|
getOptionsContext() {
|
|
|
|
this.optionsContext.url = this.popup.url;
|
|
|
|
return this.optionsContext;
|
|
|
|
}
|
|
|
|
|
2019-09-14 18:11:06 +00:00
|
|
|
static isScanningModifierPressed(scanningModifier, mouseEvent) {
|
|
|
|
switch (scanningModifier) {
|
|
|
|
case 'alt': return mouseEvent.altKey;
|
|
|
|
case 'ctrl': return mouseEvent.ctrlKey;
|
|
|
|
case 'shift': return mouseEvent.shiftKey;
|
|
|
|
case 'none': return true;
|
|
|
|
default: return false;
|
|
|
|
}
|
|
|
|
}
|
2017-08-14 03:50:43 +00:00
|
|
|
}
|
2016-03-28 00:04:49 +00:00
|
|
|
|
2019-10-03 00:31:42 +00:00
|
|
|
Frontend.windowMessageHandlers = {
|
|
|
|
popupClose: (self) => {
|
|
|
|
self.searchClear(true);
|
|
|
|
},
|
|
|
|
|
|
|
|
selectionCopy: () => {
|
|
|
|
document.execCommand('copy');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Frontend.runtimeMessageHandlers = {
|
|
|
|
optionsUpdate: (self) => {
|
|
|
|
self.updateOptions();
|
|
|
|
},
|
|
|
|
|
2019-10-06 01:38:13 +00:00
|
|
|
popupSetVisibleOverride: (self, {visible}) => {
|
|
|
|
self.popup.setVisibleOverride(visible);
|
2019-10-20 02:28:23 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
getUrl: () => {
|
|
|
|
return {url: window.location.href};
|
2019-10-03 00:31:42 +00:00
|
|
|
}
|
|
|
|
};
|