2017-03-05 01:56:10 +00:00
|
|
|
/*
|
2020-04-10 18:06:55 +00:00
|
|
|
* Copyright (C) 2016-2020 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-08-10 01:07:11 +00:00
|
|
|
* DocumentUtil
|
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 {
|
2017-03-05 01:56:10 +00:00
|
|
|
constructor() {
|
2019-09-15 18:16:23 +00:00
|
|
|
super(document.querySelector('#spinner'), document.querySelector('#content'));
|
2020-08-10 01:03:11 +00:00
|
|
|
this._searchButton = document.querySelector('#search');
|
|
|
|
this._queryInput = document.querySelector('#query');
|
|
|
|
this._introElement = document.querySelector('#intro');
|
|
|
|
this._clipboardMonitorEnableCheckbox = document.querySelector('#clipboard-monitor-enable');
|
|
|
|
this._wanakanaEnableCheckbox = document.querySelector('#wanakana-enable');
|
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({
|
|
|
|
getClipboard: api.clipboardGet.bind(api)
|
2020-07-03 16:02:21 +00:00
|
|
|
});
|
2020-02-27 00:34:25 +00:00
|
|
|
this._onKeyDownIgnoreKeys = new Map([
|
|
|
|
['ANY_MOD', new Set([
|
|
|
|
'Tab', 'ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight', 'PageDown', 'PageUp', 'Home', 'End',
|
|
|
|
'F1', 'F2', 'F3', 'F4', 'F5', 'F6', 'F7', 'F8', 'F9', 'F10',
|
|
|
|
'F11', 'F12', 'F13', 'F14', 'F15', 'F16', 'F17', 'F18', 'F19', 'F20',
|
|
|
|
'F21', 'F22', 'F23', 'F24'
|
|
|
|
])],
|
|
|
|
['Control', new Set(['C', 'A', 'Z', 'Y', 'X', 'F', 'G'])],
|
|
|
|
['Meta', new Set(['C', 'A', 'Z', 'Y', 'X', 'F', 'G'])],
|
2020-02-29 15:00:28 +00:00
|
|
|
['OS', new Set()],
|
|
|
|
['Alt', new Set()],
|
|
|
|
['AltGraph', new Set()],
|
|
|
|
['Shift', new Set()]
|
2020-02-27 00:34:25 +00:00
|
|
|
]);
|
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();
|
|
|
|
yomichan.on('optionsUpdated', () => this.updateOptions());
|
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-07-03 16:02:21 +00:00
|
|
|
const options = this.getOptions();
|
|
|
|
if (options.general.enableWanakana === true) {
|
2020-08-10 01:03:11 +00:00
|
|
|
this._wanakanaEnableCheckbox.checked = true;
|
|
|
|
wanakana.bind(this._queryInput);
|
2020-06-21 20:14:05 +00:00
|
|
|
} else {
|
2020-08-10 01:03:11 +00:00
|
|
|
this._wanakanaEnableCheckbox.checked = false;
|
2020-06-21 20:14:05 +00:00
|
|
|
}
|
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._queryInput.addEventListener('input', this._onSearchInput.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));
|
2020-02-09 23:16:06 +00:00
|
|
|
|
2020-07-03 19:58:29 +00:00
|
|
|
this._updateSearchButton();
|
2020-08-09 17:11:41 +00:00
|
|
|
this._onModeChange();
|
2019-10-26 13:39:43 +00:00
|
|
|
|
2020-06-21 20:14:05 +00:00
|
|
|
await this._prepareNestedPopups();
|
2020-03-14 00:51:39 +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-04-27 22:10:37 +00:00
|
|
|
onEscape() {
|
2020-08-10 01:03:11 +00:00
|
|
|
if (this._queryInput === null) {
|
2019-09-15 16:12:30 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-08-10 01:03:11 +00:00
|
|
|
this._queryInput.focus();
|
|
|
|
this._queryInput.select();
|
2017-08-06 02:11:06 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 19:58:29 +00:00
|
|
|
onKeyDown(e) {
|
2020-08-10 01:07:11 +00:00
|
|
|
const key = DocumentUtil.getKeyFromEvent(e);
|
2020-07-03 19:58:29 +00:00
|
|
|
const ignoreKeys = this._onKeyDownIgnoreKeys;
|
2019-10-26 11:30:36 +00:00
|
|
|
|
2020-07-03 19:58:29 +00:00
|
|
|
const activeModifierMap = new Map([
|
|
|
|
['Control', e.ctrlKey],
|
|
|
|
['Meta', e.metaKey],
|
|
|
|
['Shift', e.shiftKey],
|
|
|
|
['Alt', e.altKey],
|
|
|
|
['ANY_MOD', true]
|
|
|
|
]);
|
|
|
|
|
|
|
|
let preventFocus = false;
|
|
|
|
for (const [modifier, keys] of ignoreKeys.entries()) {
|
|
|
|
const modifierActive = activeModifierMap.get(modifier);
|
|
|
|
if (key === modifier || (modifierActive && keys.has(key))) {
|
|
|
|
preventFocus = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-10 01:03:11 +00:00
|
|
|
if (!super.onKeyDown(e) && !preventFocus && document.activeElement !== this._queryInput) {
|
|
|
|
this._queryInput.focus({preventScroll: true});
|
2020-07-03 19:58:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async updateOptions() {
|
|
|
|
await super.updateOptions();
|
|
|
|
if (!this._isPrepared) { return; }
|
2020-08-10 01:03:11 +00:00
|
|
|
const query = this._queryInput.value;
|
2020-07-03 19:58:29 +00:00
|
|
|
if (query) {
|
|
|
|
this._onSearchQueryUpdated(query, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-01 20:22:00 +00:00
|
|
|
postProcessQuery(query) {
|
|
|
|
if (this._isWanakanaEnabled()) {
|
|
|
|
try {
|
|
|
|
query = wanakana.toKana(query);
|
|
|
|
} catch (e) {
|
|
|
|
// NOP
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return query;
|
|
|
|
}
|
|
|
|
|
2020-07-26 20:51:54 +00:00
|
|
|
// Private
|
2020-07-24 18:54:54 +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':
|
|
|
|
animate = content.animate;
|
|
|
|
valid = content.definitions.length > 0;
|
2020-08-10 01:03:11 +00:00
|
|
|
this._queryInput.blur();
|
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-07-26 20:51:54 +00:00
|
|
|
this._setIntroVisible(!valid, animate);
|
|
|
|
this._updateSearchButton();
|
2020-07-03 19:58:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_onSearchInput() {
|
|
|
|
this._updateSearchButton();
|
|
|
|
|
2020-08-10 01:03:11 +00:00
|
|
|
const queryElementRect = this._queryInput.getBoundingClientRect();
|
2019-10-26 11:30:36 +00:00
|
|
|
if (queryElementRect.top < 0 || queryElementRect.bottom > window.innerHeight) {
|
2020-08-10 01:03:11 +00:00
|
|
|
this._queryInput.scrollIntoView();
|
2019-10-26 11:30:36 +00:00
|
|
|
}
|
2017-03-25 22:59:33 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 19:58:29 +00:00
|
|
|
_onSearch(e) {
|
2020-08-10 01:03:11 +00:00
|
|
|
if (this._queryInput === null) {
|
2019-09-15 16:12:30 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-10-26 22:26:17 +00:00
|
|
|
e.preventDefault();
|
2019-10-08 23:49:08 +00:00
|
|
|
|
2020-08-10 01:03:11 +00:00
|
|
|
const query = this._queryInput.value;
|
2020-07-03 19:58:29 +00:00
|
|
|
this._onSearchQueryUpdated(query, 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-07-26 20:51:54 +00:00
|
|
|
this._onSearchQueryUpdated(text, animate);
|
2020-01-26 01:02:33 +00:00
|
|
|
}
|
|
|
|
|
2020-07-26 20:51:54 +00:00
|
|
|
_onSearchQueryUpdated(query, animate) {
|
2020-07-26 23:29:12 +00:00
|
|
|
const details = {
|
2020-07-26 20:51:54 +00:00
|
|
|
focus: false,
|
|
|
|
history: false,
|
|
|
|
params: {
|
|
|
|
query
|
|
|
|
},
|
|
|
|
state: {
|
2020-07-26 22:49:38 +00:00
|
|
|
focusEntry: 0,
|
2020-07-26 20:51:54 +00:00
|
|
|
sentence: {text: query, offset: 0},
|
|
|
|
url: window.location.href
|
|
|
|
},
|
|
|
|
content: {
|
|
|
|
definitions: null,
|
|
|
|
animate
|
2019-10-08 23:49:08 +00:00
|
|
|
}
|
2020-07-26 23:29:12 +00:00
|
|
|
};
|
|
|
|
this.setContent(details);
|
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;
|
|
|
|
if (value) {
|
2020-08-10 01:03:11 +00:00
|
|
|
wanakana.bind(this._queryInput);
|
2020-02-27 00:04:21 +00:00
|
|
|
} else {
|
2020-08-10 01:03:11 +00:00
|
|
|
wanakana.unbind(this._queryInput);
|
2020-02-27 00:04:21 +00:00
|
|
|
}
|
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-07-03 19:58:29 +00:00
|
|
|
_isWanakanaEnabled() {
|
2020-08-10 01:03:11 +00:00
|
|
|
return this._wanakanaEnableCheckbox !== null && this._wanakanaEnableCheckbox.checked;
|
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-07-03 19:58:29 +00:00
|
|
|
_updateSearchButton() {
|
2020-08-10 01:03:11 +00:00
|
|
|
this._searchButton.disabled = this._introVisible && (this._queryInput === null || this._queryInput.value.length === 0);
|
2019-10-08 23:49:08 +00:00
|
|
|
}
|
|
|
|
|
2020-06-21 20:14:05 +00:00
|
|
|
async _prepareNestedPopups() {
|
|
|
|
let complete = false;
|
|
|
|
|
|
|
|
const onOptionsUpdated = async () => {
|
|
|
|
const optionsContext = this.getOptionsContext();
|
|
|
|
const options = await api.optionsGet(optionsContext);
|
|
|
|
if (!options.scanning.enableOnSearchPage || complete) { return; }
|
|
|
|
|
|
|
|
complete = true;
|
|
|
|
yomichan.off('optionsUpdated', onOptionsUpdated);
|
|
|
|
|
|
|
|
try {
|
2020-07-19 03:47:02 +00:00
|
|
|
await this.setupNestedPopups({
|
|
|
|
depth: 1,
|
|
|
|
proxy: false,
|
|
|
|
isSearchPage: true
|
|
|
|
});
|
2020-06-21 20:14:05 +00:00
|
|
|
} catch (e) {
|
|
|
|
yomichan.logError(e);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
yomichan.on('optionsUpdated', onOptionsUpdated);
|
|
|
|
|
|
|
|
await onOptionsUpdated();
|
|
|
|
}
|
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);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
2017-08-15 02:55:04 +00:00
|
|
|
}
|