2017-03-05 01:56:10 +00:00
|
|
|
/*
|
2021-01-01 19:50:41 +00:00
|
|
|
* Copyright (C) 2016-2021 Yomichan Authors
|
2017-03-05 01:56:10 +00:00
|
|
|
*
|
|
|
|
* 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
|
2020-01-01 17:00:31 +00:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2017-03-05 01:56:10 +00:00
|
|
|
*/
|
|
|
|
|
2020-03-11 02:30:36 +00:00
|
|
|
/* global
|
|
|
|
* ClipboardMonitor
|
|
|
|
* Display
|
2020-05-24 17:30:40 +00:00
|
|
|
* api
|
2020-04-21 02:18:37 +00:00
|
|
|
* wanakana
|
2020-03-11 02:30:36 +00:00
|
|
|
*/
|
2020-02-01 20:00:34 +00:00
|
|
|
|
2017-08-15 02:55:04 +00:00
|
|
|
class DisplaySearch extends Display {
|
2021-01-17 21:55:45 +00:00
|
|
|
constructor(japaneseUtil, documentFocusController, hotkeyHandler) {
|
|
|
|
super('search', japaneseUtil, documentFocusController, hotkeyHandler);
|
2020-11-08 17:34:23 +00:00
|
|
|
this._searchButton = document.querySelector('#search-button');
|
|
|
|
this._queryInput = document.querySelector('#search-textbox');
|
2020-08-10 01:03:11 +00:00
|
|
|
this._introElement = document.querySelector('#intro');
|
|
|
|
this._clipboardMonitorEnableCheckbox = document.querySelector('#clipboard-monitor-enable');
|
|
|
|
this._wanakanaEnableCheckbox = document.querySelector('#wanakana-enable');
|
2020-11-08 17:34:23 +00:00
|
|
|
this._queryInputEvents = new EventListenerCollection();
|
|
|
|
this._wanakanaEnabled = false;
|
2020-03-14 00:51:39 +00:00
|
|
|
this._isPrepared = false;
|
2020-07-03 19:58:29 +00:00
|
|
|
this._introVisible = true;
|
|
|
|
this._introAnimationTimer = null;
|
2020-08-10 01:03:11 +00:00
|
|
|
this._clipboardMonitorEnabled = false;
|
2020-07-03 19:58:29 +00:00
|
|
|
this._clipboardMonitor = new ClipboardMonitor({
|
2020-11-29 18:09:02 +00:00
|
|
|
japaneseUtil,
|
2020-09-20 19:10:57 +00:00
|
|
|
clipboardReader: {
|
|
|
|
getText: async () => (await api.clipboardGet())
|
|
|
|
}
|
2020-07-03 16:02:21 +00:00
|
|
|
});
|
2020-11-22 20:29:51 +00:00
|
|
|
this.autoPlayAudioDelay = 0;
|
2021-01-16 01:19:56 +00:00
|
|
|
|
2021-01-16 19:54:35 +00:00
|
|
|
this.hotkeyHandler.registerActions([
|
2021-01-16 01:19:56 +00:00
|
|
|
['focusSearchBox', this._onActionFocusSearchBox.bind(this)]
|
|
|
|
]);
|
2019-10-12 18:20:54 +00:00
|
|
|
}
|
2019-08-03 12:06:28 +00:00
|
|
|
|
2019-10-12 18:20:54 +00:00
|
|
|
async prepare() {
|
2020-06-21 20:14:05 +00:00
|
|
|
await super.prepare();
|
|
|
|
await this.updateOptions();
|
2020-11-24 01:31:48 +00:00
|
|
|
yomichan.on('optionsUpdated', this._onOptionsUpdated.bind(this));
|
2020-07-24 20:03:11 +00:00
|
|
|
|
2020-07-26 20:51:54 +00:00
|
|
|
this.on('contentUpdating', this._onContentUpdating.bind(this));
|
2020-08-09 17:11:41 +00:00
|
|
|
this.on('modeChange', this._onModeChange.bind(this));
|
|
|
|
|
|
|
|
this.registerMessageHandlers([
|
|
|
|
['updateSearchQuery', {async: false, handler: this._onExternalSearchUpdate.bind(this)}]
|
|
|
|
]);
|
2020-07-26 20:51:54 +00:00
|
|
|
|
2020-08-01 20:22:00 +00:00
|
|
|
this.queryParserVisible = true;
|
2020-07-26 20:51:54 +00:00
|
|
|
this.setHistorySettings({useBrowserHistory: true});
|
2020-07-24 20:03:11 +00:00
|
|
|
|
2020-11-08 17:34:23 +00:00
|
|
|
const enableWanakana = !!this.getOptions().general.enableWanakana;
|
|
|
|
this._wanakanaEnableCheckbox.checked = enableWanakana;
|
|
|
|
this._setWanakanaEnabled(enableWanakana);
|
2020-02-05 11:26:25 +00:00
|
|
|
|
2020-08-10 01:03:11 +00:00
|
|
|
this._searchButton.addEventListener('click', this._onSearch.bind(this), false);
|
|
|
|
this._wanakanaEnableCheckbox.addEventListener('change', this._onWanakanaEnableChange.bind(this));
|
2020-07-03 19:58:29 +00:00
|
|
|
window.addEventListener('copy', this._onCopy.bind(this));
|
|
|
|
this._clipboardMonitor.on('change', this._onExternalSearchUpdate.bind(this));
|
2020-08-10 01:03:11 +00:00
|
|
|
this._clipboardMonitorEnableCheckbox.addEventListener('change', this._onClipboardMonitorEnableChange.bind(this));
|
2021-01-16 19:54:35 +00:00
|
|
|
this.hotkeyHandler.on('keydownNonHotkey', this._onKeyDown.bind(this));
|
2020-02-09 23:16:06 +00:00
|
|
|
|
2020-08-09 17:11:41 +00:00
|
|
|
this._onModeChange();
|
2019-10-26 13:39:43 +00:00
|
|
|
|
2020-07-26 20:51:54 +00:00
|
|
|
this.initializeState();
|
|
|
|
|
2020-06-21 20:14:05 +00:00
|
|
|
this._isPrepared = true;
|
2017-03-05 01:56:10 +00:00
|
|
|
}
|
|
|
|
|
2020-08-01 20:22:00 +00:00
|
|
|
postProcessQuery(query) {
|
2020-11-08 17:34:23 +00:00
|
|
|
if (this._wanakanaEnabled) {
|
2020-08-01 20:22:00 +00:00
|
|
|
try {
|
2020-11-29 18:09:02 +00:00
|
|
|
query = this._japaneseUtil.convertToKana(query);
|
2020-08-01 20:22:00 +00:00
|
|
|
} catch (e) {
|
|
|
|
// NOP
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return query;
|
|
|
|
}
|
|
|
|
|
2021-01-16 01:19:56 +00:00
|
|
|
// Actions
|
|
|
|
|
|
|
|
_onActionFocusSearchBox() {
|
|
|
|
if (this._queryInput === null) { return; }
|
|
|
|
this._queryInput.focus();
|
|
|
|
this._queryInput.select();
|
|
|
|
}
|
|
|
|
|
2020-07-26 20:51:54 +00:00
|
|
|
// Private
|
2020-07-24 18:54:54 +00:00
|
|
|
|
2021-01-16 19:54:35 +00:00
|
|
|
_onKeyDown(e) {
|
|
|
|
if (
|
|
|
|
document.activeElement !== this._queryInput &&
|
|
|
|
!e.ctrlKey &&
|
|
|
|
!e.metaKey &&
|
|
|
|
!e.altKey &&
|
|
|
|
e.key.length === 1
|
|
|
|
) {
|
|
|
|
this._queryInput.focus({preventScroll: true});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-24 01:31:48 +00:00
|
|
|
async _onOptionsUpdated() {
|
|
|
|
await this.updateOptions();
|
|
|
|
const query = this._queryInput.value;
|
|
|
|
if (query) {
|
2020-12-22 00:19:59 +00:00
|
|
|
this.searchLast();
|
2020-11-24 01:31:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-01 20:22:00 +00:00
|
|
|
_onContentUpdating({type, content, source}) {
|
2020-07-26 20:51:54 +00:00
|
|
|
let animate = false;
|
|
|
|
let valid = false;
|
|
|
|
switch (type) {
|
|
|
|
case 'terms':
|
|
|
|
case 'kanji':
|
2020-11-24 16:54:08 +00:00
|
|
|
animate = !!content.animate;
|
2020-07-26 20:51:54 +00:00
|
|
|
valid = content.definitions.length > 0;
|
2020-11-22 02:17:39 +00:00
|
|
|
this.blurElement(this._queryInput);
|
2020-07-26 20:51:54 +00:00
|
|
|
break;
|
|
|
|
case 'clear':
|
|
|
|
valid = false;
|
|
|
|
animate = true;
|
|
|
|
source = '';
|
|
|
|
break;
|
|
|
|
}
|
2020-07-26 23:29:12 +00:00
|
|
|
|
2020-07-26 20:51:54 +00:00
|
|
|
if (typeof source !== 'string') { source = ''; }
|
2020-07-26 23:29:12 +00:00
|
|
|
|
2020-08-10 01:03:11 +00:00
|
|
|
this._queryInput.value = source;
|
2020-11-08 17:34:23 +00:00
|
|
|
this._updateSearchHeight();
|
2020-07-26 20:51:54 +00:00
|
|
|
this._setIntroVisible(!valid, animate);
|
2020-07-03 19:58:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_onSearchInput() {
|
2020-11-08 17:34:23 +00:00
|
|
|
this._updateSearchHeight();
|
2017-03-25 22:59:33 +00:00
|
|
|
}
|
|
|
|
|
2020-11-08 17:34:23 +00:00
|
|
|
_onSearchKeydown(e) {
|
2021-01-23 01:46:48 +00:00
|
|
|
const {code} = e;
|
|
|
|
if (!((code === 'Enter' || code === 'NumpadEnter') && !e.shiftKey)) { return; }
|
2019-09-15 16:12:30 +00:00
|
|
|
|
2020-11-08 17:34:23 +00:00
|
|
|
// Search
|
2019-10-26 22:26:17 +00:00
|
|
|
e.preventDefault();
|
2020-11-08 17:34:23 +00:00
|
|
|
e.stopImmediatePropagation();
|
2020-11-22 02:17:39 +00:00
|
|
|
this.blurElement(e.currentTarget);
|
2020-11-24 16:54:08 +00:00
|
|
|
this._search(true, true);
|
2020-11-08 17:34:23 +00:00
|
|
|
}
|
2019-10-08 23:49:08 +00:00
|
|
|
|
2020-11-08 17:34:23 +00:00
|
|
|
_onSearch(e) {
|
|
|
|
e.preventDefault();
|
2020-11-24 16:54:08 +00:00
|
|
|
this._search(true, true);
|
2019-10-08 23:49:08 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 19:58:29 +00:00
|
|
|
_onCopy() {
|
2020-01-26 01:02:33 +00:00
|
|
|
// ignore copy from search page
|
2020-07-03 19:58:29 +00:00
|
|
|
this._clipboardMonitor.setPreviousText(window.getSelection().toString().trim());
|
2020-01-26 01:02:33 +00:00
|
|
|
}
|
|
|
|
|
2020-07-19 00:30:10 +00:00
|
|
|
_onExternalSearchUpdate({text, animate=true}) {
|
2020-12-18 16:24:43 +00:00
|
|
|
const {general: {maximumClipboardSearchLength}} = this.getOptions();
|
|
|
|
if (text.length > maximumClipboardSearchLength) {
|
|
|
|
text = text.substring(0, maximumClipboardSearchLength);
|
|
|
|
}
|
2020-11-24 01:31:48 +00:00
|
|
|
this._queryInput.value = text;
|
2020-11-24 16:54:08 +00:00
|
|
|
this._search(animate, false);
|
2017-03-05 01:56:10 +00:00
|
|
|
}
|
2019-09-15 16:04:21 +00:00
|
|
|
|
2020-07-03 19:58:29 +00:00
|
|
|
_onWanakanaEnableChange(e) {
|
2020-05-06 23:32:28 +00:00
|
|
|
const value = e.target.checked;
|
2020-11-08 17:34:23 +00:00
|
|
|
this._setWanakanaEnabled(value);
|
2020-05-24 17:30:40 +00:00
|
|
|
api.modifySettings([{
|
2020-05-06 23:32:28 +00:00
|
|
|
action: 'set',
|
|
|
|
path: 'general.enableWanakana',
|
|
|
|
value,
|
|
|
|
scope: 'profile',
|
|
|
|
optionsContext: this.getOptionsContext()
|
|
|
|
}], 'search');
|
2020-02-27 00:04:21 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 19:58:29 +00:00
|
|
|
_onClipboardMonitorEnableChange(e) {
|
2020-08-09 17:11:41 +00:00
|
|
|
const enabled = e.target.checked;
|
|
|
|
this._setClipboardMonitorEnabled(enabled);
|
|
|
|
}
|
|
|
|
|
|
|
|
_onModeChange() {
|
|
|
|
let mode = this.mode;
|
|
|
|
if (mode === null) { mode = ''; }
|
|
|
|
document.documentElement.dataset.searchMode = mode;
|
|
|
|
this._updateClipboardMonitorEnabled();
|
2020-02-27 00:04:21 +00:00
|
|
|
}
|
|
|
|
|
2020-11-08 17:34:23 +00:00
|
|
|
_setWanakanaEnabled(enabled) {
|
|
|
|
const input = this._queryInput;
|
|
|
|
this._queryInputEvents.removeAllEventListeners();
|
|
|
|
|
|
|
|
this._queryInputEvents.addEventListener(input, 'keydown', this._onSearchKeydown.bind(this), false);
|
|
|
|
|
|
|
|
if (this._wanakanaEnabled !== enabled) {
|
|
|
|
this._wanakanaEnabled = enabled;
|
|
|
|
if (enabled) {
|
|
|
|
wanakana.bind(input);
|
|
|
|
} else {
|
|
|
|
wanakana.unbind(input);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this._queryInputEvents.addEventListener(input, 'input', this._onSearchInput.bind(this), false);
|
2019-10-26 22:26:17 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 19:58:29 +00:00
|
|
|
_setIntroVisible(visible, animate) {
|
|
|
|
if (this._introVisible === visible) {
|
2019-09-15 16:04:21 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-03 19:58:29 +00:00
|
|
|
this._introVisible = visible;
|
2019-09-15 16:04:21 +00:00
|
|
|
|
2020-08-10 01:03:11 +00:00
|
|
|
if (this._introElement === null) {
|
2019-09-15 16:04:21 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-03 19:58:29 +00:00
|
|
|
if (this._introAnimationTimer !== null) {
|
|
|
|
clearTimeout(this._introAnimationTimer);
|
|
|
|
this._introAnimationTimer = null;
|
2019-10-08 23:49:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (visible) {
|
2020-07-03 19:58:29 +00:00
|
|
|
this._showIntro(animate);
|
2019-10-08 23:49:08 +00:00
|
|
|
} else {
|
2020-07-03 19:58:29 +00:00
|
|
|
this._hideIntro(animate);
|
2019-10-08 23:49:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-03 19:58:29 +00:00
|
|
|
_showIntro(animate) {
|
2019-10-08 23:49:08 +00:00
|
|
|
if (animate) {
|
|
|
|
const duration = 0.4;
|
2020-08-10 01:03:11 +00:00
|
|
|
this._introElement.style.transition = '';
|
|
|
|
this._introElement.style.height = '';
|
|
|
|
const size = this._introElement.getBoundingClientRect();
|
|
|
|
this._introElement.style.height = '0px';
|
|
|
|
this._introElement.style.transition = `height ${duration}s ease-in-out 0s`;
|
|
|
|
window.getComputedStyle(this._introElement).getPropertyValue('height'); // Commits height so next line can start animation
|
|
|
|
this._introElement.style.height = `${size.height}px`;
|
2020-07-03 19:58:29 +00:00
|
|
|
this._introAnimationTimer = setTimeout(() => {
|
2020-08-10 01:03:11 +00:00
|
|
|
this._introElement.style.height = '';
|
2020-07-03 19:58:29 +00:00
|
|
|
this._introAnimationTimer = null;
|
2019-10-08 23:49:08 +00:00
|
|
|
}, duration * 1000);
|
|
|
|
} else {
|
2020-08-10 01:03:11 +00:00
|
|
|
this._introElement.style.transition = '';
|
|
|
|
this._introElement.style.height = '';
|
2019-10-08 23:49:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-03 19:58:29 +00:00
|
|
|
_hideIntro(animate) {
|
2019-10-08 23:49:08 +00:00
|
|
|
if (animate) {
|
|
|
|
const duration = 0.4;
|
2020-08-10 01:03:11 +00:00
|
|
|
const size = this._introElement.getBoundingClientRect();
|
|
|
|
this._introElement.style.height = `${size.height}px`;
|
|
|
|
this._introElement.style.transition = `height ${duration}s ease-in-out 0s`;
|
|
|
|
window.getComputedStyle(this._introElement).getPropertyValue('height'); // Commits height so next line can start animation
|
2019-10-08 23:49:08 +00:00
|
|
|
} else {
|
2020-08-10 01:03:11 +00:00
|
|
|
this._introElement.style.transition = '';
|
2019-10-08 23:49:08 +00:00
|
|
|
}
|
2020-08-10 01:03:11 +00:00
|
|
|
this._introElement.style.height = '0';
|
2019-09-15 16:04:21 +00:00
|
|
|
}
|
2019-10-08 23:49:08 +00:00
|
|
|
|
2020-08-09 17:11:41 +00:00
|
|
|
async _setClipboardMonitorEnabled(value) {
|
|
|
|
let modify = true;
|
|
|
|
if (value) {
|
|
|
|
value = await this._requestPermissions(['clipboardRead']);
|
|
|
|
modify = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._clipboardMonitorEnabled = value;
|
|
|
|
this._updateClipboardMonitorEnabled();
|
|
|
|
|
|
|
|
if (!modify) { return; }
|
|
|
|
|
|
|
|
await api.modifySettings([{
|
|
|
|
action: 'set',
|
|
|
|
path: 'general.enableClipboardMonitor',
|
|
|
|
value,
|
|
|
|
scope: 'profile',
|
|
|
|
optionsContext: this.getOptionsContext()
|
|
|
|
}], 'search');
|
|
|
|
}
|
|
|
|
|
|
|
|
_updateClipboardMonitorEnabled() {
|
|
|
|
const mode = this.mode;
|
|
|
|
const enabled = this._clipboardMonitorEnabled;
|
2020-08-10 01:03:11 +00:00
|
|
|
this._clipboardMonitorEnableCheckbox.checked = enabled;
|
2020-08-09 17:11:41 +00:00
|
|
|
if (enabled && mode !== 'popup') {
|
|
|
|
this._clipboardMonitor.start();
|
|
|
|
} else {
|
|
|
|
this._clipboardMonitor.stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_requestPermissions(permissions) {
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
chrome.permissions.request(
|
|
|
|
{permissions},
|
|
|
|
(granted) => {
|
|
|
|
const e = chrome.runtime.lastError;
|
|
|
|
resolve(!e && granted);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
2020-11-08 17:34:23 +00:00
|
|
|
|
2020-11-24 16:54:08 +00:00
|
|
|
_search(animate, history) {
|
2020-11-08 17:34:23 +00:00
|
|
|
const query = this._queryInput.value;
|
2021-01-12 23:04:26 +00:00
|
|
|
const depth = this.depth;
|
|
|
|
const url = window.location.href;
|
|
|
|
const documentTitle = document.title;
|
2020-11-24 01:31:48 +00:00
|
|
|
const details = {
|
|
|
|
focus: false,
|
2020-11-24 16:54:08 +00:00
|
|
|
history,
|
2020-11-24 01:31:48 +00:00
|
|
|
params: {
|
|
|
|
query
|
|
|
|
},
|
|
|
|
state: {
|
|
|
|
focusEntry: 0,
|
2021-01-12 23:04:26 +00:00
|
|
|
optionsContext: {depth, url},
|
|
|
|
url,
|
2020-11-24 01:31:48 +00:00
|
|
|
sentence: {text: query, offset: 0},
|
2021-01-12 23:04:26 +00:00
|
|
|
documentTitle
|
2020-11-24 01:31:48 +00:00
|
|
|
},
|
|
|
|
content: {
|
|
|
|
definitions: null,
|
|
|
|
animate
|
|
|
|
}
|
|
|
|
};
|
|
|
|
this.setContent(details);
|
2020-11-08 17:34:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_updateSearchHeight() {
|
|
|
|
const node = this._queryInput;
|
|
|
|
const {scrollHeight} = node;
|
|
|
|
const currentHeight = node.getBoundingClientRect().height;
|
|
|
|
if (scrollHeight >= currentHeight - 1) {
|
|
|
|
node.style.height = `${scrollHeight}px`;
|
|
|
|
}
|
|
|
|
}
|
2017-08-15 02:55:04 +00:00
|
|
|
}
|