2017-03-05 01:30:10 +00:00
|
|
|
/*
|
2022-02-03 01:43:10 +00:00
|
|
|
* Copyright (C) 2017-2022 Yomichan Authors
|
2017-03-05 01:30: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:30:10 +00:00
|
|
|
*/
|
|
|
|
|
2020-03-11 02:30:36 +00:00
|
|
|
/* global
|
2022-03-15 01:42:33 +00:00
|
|
|
* DisplayContentManager
|
2020-03-11 02:30:36 +00:00
|
|
|
* DisplayGenerator
|
2020-07-26 20:51:54 +00:00
|
|
|
* DisplayHistory
|
2020-12-18 14:43:54 +00:00
|
|
|
* DisplayNotification
|
2020-08-09 17:27:21 +00:00
|
|
|
* DocumentUtil
|
2021-03-31 22:17:28 +00:00
|
|
|
* ElementOverflowController
|
2020-11-22 20:29:51 +00:00
|
|
|
* FrameEndpoint
|
2020-07-19 03:47:02 +00:00
|
|
|
* Frontend
|
2021-01-18 20:23:49 +00:00
|
|
|
* HotkeyHelpController
|
2021-03-28 18:04:20 +00:00
|
|
|
* OptionToggleHotkeyHandler
|
2020-07-19 03:47:02 +00:00
|
|
|
* PopupFactory
|
2021-01-24 02:13:01 +00:00
|
|
|
* PopupMenu
|
2020-07-26 23:29:12 +00:00
|
|
|
* QueryParser
|
2021-02-14 16:36:44 +00:00
|
|
|
* ScrollElement
|
2020-11-24 01:31:48 +00:00
|
|
|
* TextScanner
|
2022-04-03 20:20:55 +00:00
|
|
|
* ThemeController
|
2020-07-19 03:47:02 +00:00
|
|
|
* dynamicLoader
|
2020-03-11 02:30:36 +00:00
|
|
|
*/
|
2017-03-05 01:30:10 +00:00
|
|
|
|
2020-07-26 20:51:54 +00:00
|
|
|
class Display extends EventDispatcher {
|
2022-05-17 01:45:22 +00:00
|
|
|
/**
|
|
|
|
* Information about how popup content should be shown, specifically related to the inner popup content.
|
|
|
|
* @typedef {object} ContentDetails
|
2022-05-20 14:28:38 +00:00
|
|
|
* @property {boolean} focus Whether or not the frame should be `focus()`'d.
|
|
|
|
* @property {HistoryParams} params An object containing key-value pairs representing the URL search params.
|
|
|
|
* @property {?HistoryState} state The semi-persistent state assigned to the navigation entry.
|
|
|
|
* @property {?HistoryContent} content The non-persistent content assigned to the navigation entry.
|
|
|
|
* @property {'clear'|'overwrite'|'new'} historyMode How the navigation history should be modified.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An object containing key-value pairs representing the URL search params.
|
|
|
|
* @typedef {object} HistoryParams
|
|
|
|
* @property {'terms'|'kanji'|'unloaded'|'clear'} [type] The type of content that is being shown.
|
|
|
|
* @property {string} [query] The search query.
|
|
|
|
* @property {'on'|'off'} [wildcards] Whether or not wildcards can be used for the search query.
|
|
|
|
* @property {string} [offset] The start position of the `query` string as an index into the `full` query string.
|
|
|
|
* @property {string} [full] The full search text. If absent, `query` is the full search text.
|
|
|
|
* @property {'true'|'false'} [full-visible] Whether or not the full search query should be forced to be visible.
|
|
|
|
* @property {'true'|'false'} [lookup] Whether or not the query should be looked up. If it is not looked up,
|
|
|
|
* the content should be provided.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The semi-persistent state assigned to the navigation entry.
|
|
|
|
* @typedef {object} HistoryState
|
|
|
|
* @property {'queryParser'} [cause] What was the cause of the navigation.
|
|
|
|
* @property {object} [sentence] The sentence context.
|
|
|
|
* @property {string} sentence.text The full string.
|
|
|
|
* @property {number} sentence.offset The offset from the start of `text` to the full search query.
|
|
|
|
* @property {number} [focusEntry] The index of the dictionary entry to focus.
|
|
|
|
* @property {number} [scrollX] The horizontal scroll position.
|
|
|
|
* @property {number} [scrollY] The vertical scroll position.
|
|
|
|
* @property {object} [optionsContext] The options context which should be used for lookups.
|
|
|
|
* @property {string} [url] The originating URL of the content.
|
|
|
|
* @property {string} [documentTitle] The originating document title of the content.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The non-persistent content assigned to the navigation entry.
|
|
|
|
* @typedef {object} HistoryContent
|
|
|
|
* @property {boolean} [animate] Whether or not any CSS animations should occur.
|
|
|
|
* @property {object[]} [dictionaryEntries] An array of dictionary entries to display as content.
|
|
|
|
* @property {object} [contentOrigin] The identifying information for the frame the content originated from.
|
|
|
|
* @property {number} [contentOrigin.tabId] The tab id.
|
|
|
|
* @property {number} [contentOrigin.frameId] The frame id within the tab.
|
2022-05-17 01:45:22 +00:00
|
|
|
*/
|
|
|
|
|
2021-02-10 03:56:04 +00:00
|
|
|
constructor(tabId, frameId, pageType, japaneseUtil, documentFocusController, hotkeyHandler) {
|
2020-07-26 20:51:54 +00:00
|
|
|
super();
|
2021-02-10 03:56:04 +00:00
|
|
|
this._tabId = tabId;
|
|
|
|
this._frameId = frameId;
|
2020-11-22 16:19:21 +00:00
|
|
|
this._pageType = pageType;
|
2020-11-29 18:09:02 +00:00
|
|
|
this._japaneseUtil = japaneseUtil;
|
2020-12-28 22:41:59 +00:00
|
|
|
this._documentFocusController = documentFocusController;
|
2021-01-17 21:55:45 +00:00
|
|
|
this._hotkeyHandler = hotkeyHandler;
|
2021-04-08 23:59:55 +00:00
|
|
|
this._container = document.querySelector('#dictionary-entries');
|
2021-04-04 20:22:35 +00:00
|
|
|
this._dictionaryEntries = [];
|
|
|
|
this._dictionaryEntryNodes = [];
|
2020-07-19 03:47:02 +00:00
|
|
|
this._optionsContext = {depth: 0, url: window.location.href};
|
2020-07-03 16:02:21 +00:00
|
|
|
this._options = null;
|
|
|
|
this._index = 0;
|
|
|
|
this._styleNode = null;
|
|
|
|
this._eventListeners = new EventListenerCollection();
|
|
|
|
this._setContentToken = null;
|
2022-03-17 23:01:59 +00:00
|
|
|
this._contentManager = new DisplayContentManager(this);
|
2021-01-18 20:23:49 +00:00
|
|
|
this._hotkeyHelpController = new HotkeyHelpController();
|
2020-11-29 18:09:02 +00:00
|
|
|
this._displayGenerator = new DisplayGenerator({
|
|
|
|
japaneseUtil,
|
2022-03-15 01:42:33 +00:00
|
|
|
contentManager: this._contentManager,
|
2021-01-18 20:23:49 +00:00
|
|
|
hotkeyHelpController: this._hotkeyHelpController
|
2020-11-29 18:09:02 +00:00
|
|
|
});
|
2020-07-19 03:47:02 +00:00
|
|
|
this._messageHandlers = new Map();
|
2020-08-09 17:11:41 +00:00
|
|
|
this._directMessageHandlers = new Map();
|
2020-11-22 20:29:51 +00:00
|
|
|
this._windowMessageHandlers = new Map();
|
2020-07-26 20:51:54 +00:00
|
|
|
this._history = new DisplayHistory({clearable: true, useBrowserHistory: false});
|
|
|
|
this._historyChangeIgnore = false;
|
|
|
|
this._historyHasChanged = false;
|
2020-07-26 22:49:38 +00:00
|
|
|
this._navigationHeader = document.querySelector('#navigation-header');
|
2020-08-03 01:51:51 +00:00
|
|
|
this._contentType = 'clear';
|
2020-11-23 22:43:17 +00:00
|
|
|
this._defaultTitle = document.title;
|
|
|
|
this._titleMaxLength = 1000;
|
2020-12-22 00:19:59 +00:00
|
|
|
this._query = '';
|
2020-07-26 23:29:12 +00:00
|
|
|
this._fullQuery = '';
|
2021-09-27 23:07:28 +00:00
|
|
|
this._queryOffset = 0;
|
2020-08-09 17:27:21 +00:00
|
|
|
this._documentUtil = new DocumentUtil();
|
2020-11-19 01:06:02 +00:00
|
|
|
this._progressIndicator = document.querySelector('#progress-indicator');
|
|
|
|
this._progressIndicatorTimer = null;
|
2020-11-18 00:40:19 +00:00
|
|
|
this._progressIndicatorVisible = new DynamicProperty(false);
|
2020-08-01 20:22:00 +00:00
|
|
|
this._queryParserVisible = false;
|
|
|
|
this._queryParserVisibleOverride = null;
|
|
|
|
this._queryParserContainer = document.querySelector('#query-parser-container');
|
2020-07-26 23:29:12 +00:00
|
|
|
this._queryParser = new QueryParser({
|
2021-01-12 04:13:35 +00:00
|
|
|
getSearchContext: this._getSearchContext.bind(this),
|
2021-07-09 20:05:57 +00:00
|
|
|
documentUtil: this._documentUtil,
|
|
|
|
japaneseUtil
|
2020-07-26 23:29:12 +00:00
|
|
|
});
|
2020-11-21 03:42:49 +00:00
|
|
|
this._contentScrollElement = document.querySelector('#content-scroll');
|
2020-11-14 23:12:06 +00:00
|
|
|
this._contentScrollBodyElement = document.querySelector('#content-body');
|
2021-02-14 16:36:44 +00:00
|
|
|
this._windowScroll = new ScrollElement(this._contentScrollElement);
|
2020-11-15 19:12:48 +00:00
|
|
|
this._closeButton = document.querySelector('#close-button');
|
|
|
|
this._navigationPreviousButton = document.querySelector('#navigate-previous-button');
|
|
|
|
this._navigationNextButton = document.querySelector('#navigate-next-button');
|
2020-11-22 16:19:21 +00:00
|
|
|
this._frontend = null;
|
|
|
|
this._frontendSetupPromise = null;
|
|
|
|
this._depth = 0;
|
|
|
|
this._parentPopupId = null;
|
|
|
|
this._parentFrameId = null;
|
2021-02-10 03:56:04 +00:00
|
|
|
this._contentOriginTabId = tabId;
|
|
|
|
this._contentOriginFrameId = frameId;
|
2020-11-22 16:19:21 +00:00
|
|
|
this._childrenSupported = true;
|
2020-11-22 20:29:51 +00:00
|
|
|
this._frameEndpoint = (pageType === 'popup' ? new FrameEndpoint() : null);
|
|
|
|
this._browser = null;
|
|
|
|
this._copyTextarea = null;
|
2021-04-04 20:22:35 +00:00
|
|
|
this._contentTextScanner = null;
|
2020-12-18 14:43:54 +00:00
|
|
|
this._tagNotification = null;
|
2021-01-30 17:33:29 +00:00
|
|
|
this._footerNotificationContainer = document.querySelector('#content-footer');
|
2021-03-28 18:04:20 +00:00
|
|
|
this._optionToggleHotkeyHandler = new OptionToggleHotkeyHandler(this);
|
2021-03-31 22:17:28 +00:00
|
|
|
this._elementOverflowController = new ElementOverflowController();
|
2021-08-10 23:23:05 +00:00
|
|
|
this._frameVisible = (pageType === 'search');
|
2021-10-17 13:55:13 +00:00
|
|
|
this._menuContainer = document.querySelector('#popup-menus');
|
2021-10-17 04:15:43 +00:00
|
|
|
this._onEntryClickBind = this._onEntryClick.bind(this);
|
|
|
|
this._onKanjiLookupBind = this._onKanjiLookup.bind(this);
|
|
|
|
this._onDebugLogClickBind = this._onDebugLogClick.bind(this);
|
|
|
|
this._onTagClickBind = this._onTagClick.bind(this);
|
2021-10-17 13:55:13 +00:00
|
|
|
this._onMenuButtonClickBind = this._onMenuButtonClick.bind(this);
|
|
|
|
this._onMenuButtonMenuCloseBind = this._onMenuButtonMenuClose.bind(this);
|
2022-04-03 20:20:55 +00:00
|
|
|
this._themeController = new ThemeController(document.documentElement);
|
2020-07-09 00:02:20 +00:00
|
|
|
|
2021-01-16 19:54:35 +00:00
|
|
|
this._hotkeyHandler.registerActions([
|
2021-01-24 02:13:01 +00:00
|
|
|
['close', () => { this._onHotkeyClose(); }],
|
2021-03-28 02:30:45 +00:00
|
|
|
['nextEntry', this._onHotkeyActionMoveRelative.bind(this, 1)],
|
|
|
|
['previousEntry', this._onHotkeyActionMoveRelative.bind(this, -1)],
|
2021-09-30 23:31:45 +00:00
|
|
|
['lastEntry', () => { this._focusEntry(this._dictionaryEntries.length - 1, 0, true); }],
|
|
|
|
['firstEntry', () => { this._focusEntry(0, 0, true); }],
|
2020-11-22 20:29:51 +00:00
|
|
|
['historyBackward', () => { this._sourceTermView(); }],
|
|
|
|
['historyForward', () => { this._nextTermView(); }],
|
2021-01-16 02:11:09 +00:00
|
|
|
['copyHostSelection', () => this._copyHostSelection()],
|
|
|
|
['nextEntryDifferentDictionary', () => { this._focusEntryWithDifferentDictionary(1, true); }],
|
|
|
|
['previousEntryDifferentDictionary', () => { this._focusEntryWithDifferentDictionary(-1, true); }]
|
2020-07-09 00:02:20 +00:00
|
|
|
]);
|
2020-08-09 17:11:41 +00:00
|
|
|
this.registerDirectMessageHandlers([
|
2022-08-20 15:31:50 +00:00
|
|
|
['Display.setOptionsContext', {async: true, handler: this._onMessageSetOptionsContext.bind(this)}],
|
2021-11-21 20:54:58 +00:00
|
|
|
['Display.setContent', {async: false, handler: this._onMessageSetContent.bind(this)}],
|
|
|
|
['Display.setCustomCss', {async: false, handler: this._onMessageSetCustomCss.bind(this)}],
|
|
|
|
['Display.setContentScale', {async: false, handler: this._onMessageSetContentScale.bind(this)}],
|
|
|
|
['Display.configure', {async: true, handler: this._onMessageConfigure.bind(this)}],
|
|
|
|
['Display.visibilityChanged', {async: false, handler: this._onMessageVisibilityChanged.bind(this)}]
|
2020-07-19 03:47:02 +00:00
|
|
|
]);
|
2020-11-22 20:29:51 +00:00
|
|
|
this.registerWindowMessageHandlers([
|
2021-11-21 20:54:58 +00:00
|
|
|
['Display.extensionUnloaded', {async: false, handler: this._onMessageExtensionUnloaded.bind(this)}]
|
2020-11-22 20:29:51 +00:00
|
|
|
]);
|
2020-07-19 03:47:02 +00:00
|
|
|
}
|
|
|
|
|
2020-12-30 04:38:44 +00:00
|
|
|
get displayGenerator() {
|
|
|
|
return this._displayGenerator;
|
|
|
|
}
|
|
|
|
|
2020-08-01 20:22:00 +00:00
|
|
|
get queryParserVisible() {
|
|
|
|
return this._queryParserVisible;
|
|
|
|
}
|
|
|
|
|
|
|
|
set queryParserVisible(value) {
|
|
|
|
this._queryParserVisible = value;
|
2020-11-19 01:15:30 +00:00
|
|
|
this._updateQueryParser();
|
2020-08-01 20:22:00 +00:00
|
|
|
}
|
|
|
|
|
2020-11-29 18:09:02 +00:00
|
|
|
get japaneseUtil() {
|
|
|
|
return this._japaneseUtil;
|
|
|
|
}
|
|
|
|
|
2021-01-12 23:04:26 +00:00
|
|
|
get depth() {
|
|
|
|
return this._depth;
|
|
|
|
}
|
|
|
|
|
2021-01-16 19:54:35 +00:00
|
|
|
get hotkeyHandler() {
|
|
|
|
return this._hotkeyHandler;
|
|
|
|
}
|
|
|
|
|
2021-04-04 20:22:35 +00:00
|
|
|
get dictionaryEntries() {
|
|
|
|
return this._dictionaryEntries;
|
2021-01-18 05:16:40 +00:00
|
|
|
}
|
|
|
|
|
2021-04-04 20:22:35 +00:00
|
|
|
get dictionaryEntryNodes() {
|
|
|
|
return this._dictionaryEntryNodes;
|
2021-01-18 05:16:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get progressIndicatorVisible() {
|
|
|
|
return this._progressIndicatorVisible;
|
|
|
|
}
|
|
|
|
|
2021-03-06 19:31:23 +00:00
|
|
|
get parentPopupId() {
|
|
|
|
return this._parentPopupId;
|
|
|
|
}
|
|
|
|
|
2021-07-03 23:32:11 +00:00
|
|
|
get selectedIndex() {
|
|
|
|
return this._index;
|
|
|
|
}
|
|
|
|
|
|
|
|
get history() {
|
|
|
|
return this._history;
|
|
|
|
}
|
|
|
|
|
|
|
|
get query() {
|
|
|
|
return this._query;
|
|
|
|
}
|
|
|
|
|
|
|
|
get fullQuery() {
|
|
|
|
return this._fullQuery;
|
|
|
|
}
|
|
|
|
|
2021-09-27 23:07:28 +00:00
|
|
|
get queryOffset() {
|
|
|
|
return this._queryOffset;
|
|
|
|
}
|
|
|
|
|
2021-08-10 23:23:05 +00:00
|
|
|
get frameVisible() {
|
|
|
|
return this._frameVisible;
|
2021-08-02 23:25:10 +00:00
|
|
|
}
|
|
|
|
|
2020-03-13 21:23:08 +00:00
|
|
|
async prepare() {
|
2022-04-03 20:20:55 +00:00
|
|
|
// Theme
|
|
|
|
this._themeController.siteTheme = 'light';
|
|
|
|
this._themeController.prepare();
|
|
|
|
|
2020-11-22 20:29:51 +00:00
|
|
|
// State setup
|
|
|
|
const {documentElement} = document;
|
2021-02-14 20:53:35 +00:00
|
|
|
const {browser} = await yomichan.api.getEnvironmentInfo();
|
2020-11-22 20:29:51 +00:00
|
|
|
this._browser = browser;
|
|
|
|
|
2021-06-06 18:47:48 +00:00
|
|
|
if (documentElement !== null) {
|
|
|
|
documentElement.dataset.browser = browser;
|
|
|
|
}
|
|
|
|
|
2020-11-22 20:29:51 +00:00
|
|
|
// Prepare
|
2021-01-18 20:23:49 +00:00
|
|
|
await this._hotkeyHelpController.prepare();
|
2020-07-03 16:02:21 +00:00
|
|
|
await this._displayGenerator.prepare();
|
2020-11-08 17:35:32 +00:00
|
|
|
this._queryParser.prepare();
|
2020-07-26 20:51:54 +00:00
|
|
|
this._history.prepare();
|
2021-03-28 18:04:20 +00:00
|
|
|
this._optionToggleHotkeyHandler.prepare();
|
2020-11-22 20:29:51 +00:00
|
|
|
|
|
|
|
// Event setup
|
2020-07-26 20:51:54 +00:00
|
|
|
this._history.on('stateChanged', this._onStateChanged.bind(this));
|
2020-07-26 23:29:12 +00:00
|
|
|
this._queryParser.on('searched', this._onQueryParserSearch.bind(this));
|
2020-11-22 20:29:51 +00:00
|
|
|
this._progressIndicatorVisible.on('change', this._onProgressIndicatorVisibleChanged.bind(this));
|
2020-07-18 18:15:36 +00:00
|
|
|
yomichan.on('extensionUnloaded', this._onExtensionUnloaded.bind(this));
|
2021-02-14 20:53:35 +00:00
|
|
|
yomichan.crossFrame.registerHandlers([
|
2020-08-09 17:11:41 +00:00
|
|
|
['popupMessage', {async: 'dynamic', handler: this._onDirectMessage.bind(this)}]
|
2020-07-19 03:47:02 +00:00
|
|
|
]);
|
2020-11-22 20:29:51 +00:00
|
|
|
window.addEventListener('message', this._onWindowMessage.bind(this), false);
|
|
|
|
|
|
|
|
if (this._pageType === 'popup' && documentElement !== null) {
|
|
|
|
documentElement.addEventListener('mouseup', this._onDocumentElementMouseUp.bind(this), false);
|
|
|
|
documentElement.addEventListener('click', this._onDocumentElementClick.bind(this), false);
|
|
|
|
documentElement.addEventListener('auxclick', this._onDocumentElementClick.bind(this), false);
|
|
|
|
}
|
|
|
|
|
|
|
|
document.addEventListener('wheel', this._onWheel.bind(this), {passive: false});
|
|
|
|
if (this._closeButton !== null) {
|
|
|
|
this._closeButton.addEventListener('click', this._onCloseButtonClick.bind(this), false);
|
|
|
|
}
|
|
|
|
if (this._navigationPreviousButton !== null) {
|
|
|
|
this._navigationPreviousButton.addEventListener('click', this._onSourceTermView.bind(this), false);
|
|
|
|
}
|
|
|
|
if (this._navigationNextButton !== null) {
|
|
|
|
this._navigationNextButton.addEventListener('click', this._onNextTermView.bind(this), false);
|
|
|
|
}
|
2020-07-03 16:02:21 +00:00
|
|
|
}
|
|
|
|
|
2021-02-10 03:56:04 +00:00
|
|
|
getContentOrigin() {
|
|
|
|
return {
|
|
|
|
tabId: this._contentOriginTabId,
|
|
|
|
frameId: this._contentOriginFrameId
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-07-26 20:51:54 +00:00
|
|
|
initializeState() {
|
|
|
|
this._onStateChanged();
|
2020-11-22 20:29:51 +00:00
|
|
|
if (this._frameEndpoint !== null) {
|
|
|
|
this._frameEndpoint.signal();
|
|
|
|
}
|
2020-07-26 20:51:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
setHistorySettings({clearable, useBrowserHistory}) {
|
|
|
|
if (typeof clearable !== 'undefined') {
|
|
|
|
this._history.clearable = clearable;
|
|
|
|
}
|
|
|
|
if (typeof useBrowserHistory !== 'undefined') {
|
|
|
|
this._history.useBrowserHistory = useBrowserHistory;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
onError(error) {
|
2020-07-18 18:15:36 +00:00
|
|
|
if (yomichan.isExtensionUnloaded) { return; }
|
2021-02-14 22:52:01 +00:00
|
|
|
log.error(error);
|
2017-03-05 01:30:10 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 19:58:29 +00:00
|
|
|
getOptions() {
|
|
|
|
return this._options;
|
|
|
|
}
|
|
|
|
|
|
|
|
getOptionsContext() {
|
|
|
|
return this._optionsContext;
|
|
|
|
}
|
|
|
|
|
2020-11-13 01:32:46 +00:00
|
|
|
async setOptionsContext(optionsContext) {
|
2020-07-03 19:58:29 +00:00
|
|
|
this._optionsContext = optionsContext;
|
2020-11-13 01:32:46 +00:00
|
|
|
await this.updateOptions();
|
2020-07-03 19:58:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async updateOptions() {
|
2021-02-14 20:53:35 +00:00
|
|
|
const options = await yomichan.api.optionsGet(this.getOptionsContext());
|
2021-01-10 04:10:55 +00:00
|
|
|
const {scanning: scanningOptions, sentenceParsing: sentenceParsingOptions} = options;
|
2020-08-09 17:19:42 +00:00
|
|
|
this._options = options;
|
|
|
|
|
2021-01-15 01:56:18 +00:00
|
|
|
this._updateHotkeys(options);
|
2020-08-09 17:19:42 +00:00
|
|
|
this._updateDocumentOptions(options);
|
2022-04-03 20:20:55 +00:00
|
|
|
this._setTheme(options);
|
2021-01-18 20:23:49 +00:00
|
|
|
this._hotkeyHelpController.setOptions(options);
|
|
|
|
this._displayGenerator.updateHotkeys();
|
|
|
|
this._hotkeyHelpController.setupNode(document.documentElement);
|
2021-03-31 22:17:28 +00:00
|
|
|
this._elementOverflowController.setOptions(options);
|
2020-08-09 17:19:42 +00:00
|
|
|
|
|
|
|
this._queryParser.setOptions({
|
|
|
|
selectedParser: options.parsing.selectedParser,
|
|
|
|
termSpacing: options.parsing.termSpacing,
|
2021-07-09 20:05:57 +00:00
|
|
|
readingMode: options.parsing.readingMode,
|
|
|
|
useInternalParser: options.parsing.enableScanningParser,
|
|
|
|
useMecabParser: options.parsing.enableMecabParser,
|
2020-08-09 17:19:42 +00:00
|
|
|
scanning: {
|
2021-01-10 04:10:55 +00:00
|
|
|
inputs: scanningOptions.inputs,
|
|
|
|
deepContentScan: scanningOptions.deepDomScan,
|
2022-09-21 01:06:39 +00:00
|
|
|
normalizeCssZoom: scanningOptions.normalizeCssZoom,
|
2021-01-10 04:10:55 +00:00
|
|
|
selectText: scanningOptions.selectText,
|
|
|
|
delay: scanningOptions.delay,
|
|
|
|
touchInputEnabled: scanningOptions.touchInputEnabled,
|
|
|
|
pointerEventsEnabled: scanningOptions.pointerEventsEnabled,
|
|
|
|
scanLength: scanningOptions.length,
|
|
|
|
layoutAwareScan: scanningOptions.layoutAwareScan,
|
2021-01-10 19:43:06 +00:00
|
|
|
preventMiddleMouse: scanningOptions.preventMiddleMouse.onSearchQuery,
|
2021-12-18 17:38:39 +00:00
|
|
|
matchTypePrefix: false,
|
2021-01-10 19:43:06 +00:00
|
|
|
sentenceParsingOptions
|
2020-08-09 17:19:42 +00:00
|
|
|
}
|
|
|
|
});
|
2020-11-22 16:19:21 +00:00
|
|
|
|
|
|
|
this._updateNestedFrontend(options);
|
2021-04-04 20:22:35 +00:00
|
|
|
this._updateContentTextScanner(options);
|
2021-01-26 03:18:31 +00:00
|
|
|
|
|
|
|
this.trigger('optionsUpdated', {options});
|
2020-07-03 19:58:29 +00:00
|
|
|
}
|
|
|
|
|
2022-05-17 01:45:22 +00:00
|
|
|
/**
|
|
|
|
* Updates the content of the display.
|
2022-05-20 14:28:38 +00:00
|
|
|
* @param {ContentDetails} details Information about the content to show.
|
2022-05-17 01:45:22 +00:00
|
|
|
*/
|
2020-07-26 20:51:54 +00:00
|
|
|
setContent(details) {
|
2021-08-31 01:55:08 +00:00
|
|
|
const {focus, params, state, content} = details;
|
|
|
|
const historyMode = this._historyHasChanged ? details.historyMode : 'clear';
|
2020-07-25 01:12:13 +00:00
|
|
|
|
2020-07-26 20:51:54 +00:00
|
|
|
if (focus) {
|
|
|
|
window.focus();
|
|
|
|
}
|
2020-07-25 01:12:13 +00:00
|
|
|
|
2020-07-26 20:51:54 +00:00
|
|
|
const urlSearchParams = new URLSearchParams();
|
|
|
|
for (const [key, value] of Object.entries(params)) {
|
|
|
|
urlSearchParams.append(key, value);
|
2020-07-03 19:58:29 +00:00
|
|
|
}
|
2020-07-26 20:51:54 +00:00
|
|
|
const url = `${location.protocol}//${location.host}${location.pathname}?${urlSearchParams.toString()}`;
|
2020-07-03 19:58:29 +00:00
|
|
|
|
2021-08-31 01:55:08 +00:00
|
|
|
switch (historyMode) {
|
|
|
|
case 'clear':
|
|
|
|
this._history.clear();
|
|
|
|
this._history.replaceState(state, content, url);
|
|
|
|
break;
|
|
|
|
case 'overwrite':
|
|
|
|
this._history.replaceState(state, content, url);
|
|
|
|
break;
|
|
|
|
default: // 'new'
|
|
|
|
this._updateHistoryState();
|
|
|
|
this._history.pushState(state, content, url);
|
|
|
|
break;
|
2020-07-26 20:51:54 +00:00
|
|
|
}
|
2020-07-03 19:58:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
setCustomCss(css) {
|
|
|
|
if (this._styleNode === null) {
|
|
|
|
if (css.length === 0) { return; }
|
|
|
|
this._styleNode = document.createElement('style');
|
|
|
|
}
|
|
|
|
|
|
|
|
this._styleNode.textContent = css;
|
|
|
|
|
|
|
|
const parent = document.head;
|
|
|
|
if (this._styleNode.parentNode !== parent) {
|
|
|
|
parent.appendChild(this._styleNode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-09 17:11:41 +00:00
|
|
|
registerDirectMessageHandlers(handlers) {
|
|
|
|
for (const [name, handlerInfo] of handlers) {
|
|
|
|
this._directMessageHandlers.set(name, handlerInfo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-22 20:29:51 +00:00
|
|
|
registerWindowMessageHandlers(handlers) {
|
|
|
|
for (const [name, handlerInfo] of handlers) {
|
|
|
|
this._windowMessageHandlers.set(name, handlerInfo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-15 19:12:48 +00:00
|
|
|
close() {
|
2021-01-16 01:19:56 +00:00
|
|
|
switch (this._pageType) {
|
|
|
|
case 'popup':
|
2021-11-21 20:54:58 +00:00
|
|
|
this.invokeContentOrigin('Frontend.closePopup');
|
2021-01-16 01:19:56 +00:00
|
|
|
break;
|
|
|
|
case 'search':
|
|
|
|
this._closeTab();
|
|
|
|
break;
|
2020-11-22 20:29:51 +00:00
|
|
|
}
|
2020-11-15 19:12:48 +00:00
|
|
|
}
|
|
|
|
|
2020-11-22 02:17:39 +00:00
|
|
|
blurElement(element) {
|
2020-12-28 22:41:59 +00:00
|
|
|
this._documentFocusController.blurElement(element);
|
2020-11-22 02:17:39 +00:00
|
|
|
}
|
|
|
|
|
2022-08-20 15:31:50 +00:00
|
|
|
searchLast(updateOptionsContext) {
|
2020-12-22 00:19:59 +00:00
|
|
|
const type = this._contentType;
|
|
|
|
if (type === 'clear') { return; }
|
2021-09-27 02:14:52 +00:00
|
|
|
const query = this._query;
|
2022-08-20 15:31:50 +00:00
|
|
|
const hasState = this._historyHasState();
|
2020-12-22 00:19:59 +00:00
|
|
|
const state = (
|
2022-08-20 15:31:50 +00:00
|
|
|
hasState ?
|
2020-12-22 00:19:59 +00:00
|
|
|
clone(this._history.state) :
|
|
|
|
{
|
|
|
|
focusEntry: 0,
|
2022-08-20 15:31:50 +00:00
|
|
|
optionsContext: null,
|
2021-01-12 23:04:26 +00:00
|
|
|
url: window.location.href,
|
2020-12-22 00:19:59 +00:00
|
|
|
sentence: {text: query, offset: 0},
|
2021-01-12 23:04:26 +00:00
|
|
|
documentTitle: document.title
|
2020-12-22 00:19:59 +00:00
|
|
|
}
|
|
|
|
);
|
2022-08-20 15:31:50 +00:00
|
|
|
if (!hasState || updateOptionsContext) {
|
|
|
|
state.optionsContext = clone(this._optionsContext);
|
|
|
|
}
|
2020-12-22 00:19:59 +00:00
|
|
|
const details = {
|
|
|
|
focus: false,
|
2021-08-31 01:55:08 +00:00
|
|
|
historyMode: 'clear',
|
2021-09-27 23:07:28 +00:00
|
|
|
params: this._createSearchParams(type, query, false, this._queryOffset),
|
2020-12-22 00:19:59 +00:00
|
|
|
state,
|
|
|
|
content: {
|
2021-04-04 20:22:35 +00:00
|
|
|
dictionaryEntries: null,
|
2021-02-10 03:56:04 +00:00
|
|
|
contentOrigin: this.getContentOrigin()
|
2020-12-22 00:19:59 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
this.setContent(details);
|
|
|
|
}
|
|
|
|
|
2021-03-06 19:31:23 +00:00
|
|
|
async invokeContentOrigin(action, params={}) {
|
|
|
|
if (this._contentOriginTabId === this._tabId && this._contentOriginFrameId === this._frameId) {
|
|
|
|
throw new Error('Content origin is same page');
|
|
|
|
}
|
|
|
|
return await yomichan.crossFrame.invokeTab(this._contentOriginTabId, this._contentOriginFrameId, action, params);
|
|
|
|
}
|
|
|
|
|
|
|
|
async invokeParentFrame(action, params={}) {
|
|
|
|
if (this._parentFrameId === null || this._parentFrameId === this._frameId) {
|
|
|
|
throw new Error('Invalid parent frame');
|
|
|
|
}
|
|
|
|
return await yomichan.crossFrame.invoke(this._parentFrameId, action, params);
|
|
|
|
}
|
|
|
|
|
2021-07-03 23:32:11 +00:00
|
|
|
getElementDictionaryEntryIndex(element) {
|
|
|
|
const node = element.closest('.entry');
|
|
|
|
if (node === null) { return -1; }
|
|
|
|
const index = parseInt(node.dataset.index, 10);
|
|
|
|
return Number.isFinite(index) ? index : -1;
|
|
|
|
}
|
|
|
|
|
2022-05-20 22:08:20 +00:00
|
|
|
/**
|
|
|
|
* Creates a new notification.
|
|
|
|
* @param {boolean} scannable Whether or not the notification should permit its content to be scanned.
|
|
|
|
* @returns {DisplayNotification} A new notification instance.
|
|
|
|
*/
|
|
|
|
createNotification(scannable) {
|
|
|
|
const node = this._displayGenerator.createEmptyFooterNotification();
|
|
|
|
if (scannable) {
|
|
|
|
node.classList.add('click-scannable');
|
|
|
|
}
|
|
|
|
return new DisplayNotification(this._footerNotificationContainer, node);
|
|
|
|
}
|
|
|
|
|
2020-07-19 03:47:02 +00:00
|
|
|
// Message handlers
|
|
|
|
|
2020-08-09 17:11:41 +00:00
|
|
|
_onDirectMessage(data) {
|
2021-06-30 02:17:14 +00:00
|
|
|
data = this._authenticateMessageData(data);
|
2020-07-19 03:47:02 +00:00
|
|
|
const {action, params} = data;
|
2020-08-09 17:11:41 +00:00
|
|
|
const handlerInfo = this._directMessageHandlers.get(action);
|
2020-07-19 03:47:02 +00:00
|
|
|
if (typeof handlerInfo === 'undefined') {
|
|
|
|
throw new Error(`Invalid action: ${action}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
const {async, handler} = handlerInfo;
|
|
|
|
const result = handler(params);
|
|
|
|
return {async, result};
|
|
|
|
}
|
|
|
|
|
2020-11-22 20:29:51 +00:00
|
|
|
_onWindowMessage({data}) {
|
|
|
|
try {
|
2021-06-30 02:17:14 +00:00
|
|
|
data = this._authenticateMessageData(data);
|
2020-11-22 20:29:51 +00:00
|
|
|
} catch (e) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const {action, params} = data;
|
|
|
|
const messageHandler = this._windowMessageHandlers.get(action);
|
|
|
|
if (typeof messageHandler === 'undefined') { return; }
|
|
|
|
|
|
|
|
const callback = () => {}; // NOP
|
2021-02-14 23:18:02 +00:00
|
|
|
invokeMessageHandler(messageHandler, params, callback);
|
2020-11-22 20:29:51 +00:00
|
|
|
}
|
|
|
|
|
2022-08-20 15:31:50 +00:00
|
|
|
async _onMessageSetOptionsContext({optionsContext}) {
|
|
|
|
await this.setOptionsContext(optionsContext);
|
|
|
|
this.searchLast(true);
|
2020-07-19 03:47:02 +00:00
|
|
|
}
|
|
|
|
|
2020-07-25 13:58:06 +00:00
|
|
|
_onMessageSetContent({details}) {
|
|
|
|
this.setContent(details);
|
2020-07-19 03:47:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_onMessageSetCustomCss({css}) {
|
|
|
|
this.setCustomCss(css);
|
|
|
|
}
|
|
|
|
|
2020-11-22 16:19:21 +00:00
|
|
|
_onMessageSetContentScale({scale}) {
|
|
|
|
this._setContentScale(scale);
|
|
|
|
}
|
|
|
|
|
2021-02-10 03:56:04 +00:00
|
|
|
async _onMessageConfigure({depth, parentPopupId, parentFrameId, childrenSupported, scale, optionsContext}) {
|
2020-11-22 16:19:21 +00:00
|
|
|
this._depth = depth;
|
|
|
|
this._parentPopupId = parentPopupId;
|
|
|
|
this._parentFrameId = parentFrameId;
|
|
|
|
this._childrenSupported = childrenSupported;
|
|
|
|
this._setContentScale(scale);
|
|
|
|
await this.setOptionsContext(optionsContext);
|
|
|
|
}
|
|
|
|
|
2021-08-02 23:25:10 +00:00
|
|
|
_onMessageVisibilityChanged({value}) {
|
2021-08-10 23:23:05 +00:00
|
|
|
this._frameVisible = value;
|
2021-10-03 20:46:15 +00:00
|
|
|
this.trigger('frameVisibilityChange', {value});
|
2021-08-02 23:25:10 +00:00
|
|
|
}
|
|
|
|
|
2020-11-22 20:29:51 +00:00
|
|
|
_onMessageExtensionUnloaded() {
|
|
|
|
if (yomichan.isExtensionUnloaded) { return; }
|
|
|
|
yomichan.triggerExtensionUnloaded();
|
|
|
|
}
|
|
|
|
|
2020-07-03 19:58:29 +00:00
|
|
|
// Private
|
|
|
|
|
2021-06-30 02:17:14 +00:00
|
|
|
_authenticateMessageData(data) {
|
|
|
|
if (this._frameEndpoint === null) {
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
if (!this._frameEndpoint.authenticate(data)) {
|
|
|
|
throw new Error('Invalid authentication');
|
|
|
|
}
|
|
|
|
return data.data;
|
|
|
|
}
|
|
|
|
|
2020-07-26 20:51:54 +00:00
|
|
|
async _onStateChanged() {
|
|
|
|
if (this._historyChangeIgnore) { return; }
|
|
|
|
|
|
|
|
const token = {}; // Unique identifier token
|
|
|
|
this._setContentToken = token;
|
|
|
|
try {
|
2020-11-24 16:54:08 +00:00
|
|
|
// Clear
|
|
|
|
this._closePopups();
|
2021-01-24 02:13:01 +00:00
|
|
|
this._closeAllPopupMenus();
|
2020-11-24 16:54:08 +00:00
|
|
|
this._eventListeners.removeAllEventListeners();
|
2022-03-15 01:42:33 +00:00
|
|
|
this._contentManager.unloadAll();
|
2020-12-18 14:43:54 +00:00
|
|
|
this._hideTagNotification(false);
|
2021-10-03 20:46:15 +00:00
|
|
|
this._triggerContentClear();
|
2021-04-04 20:22:35 +00:00
|
|
|
this._dictionaryEntries = [];
|
|
|
|
this._dictionaryEntryNodes = [];
|
2021-03-31 22:17:28 +00:00
|
|
|
this._elementOverflowController.clearElements();
|
2020-11-24 16:54:08 +00:00
|
|
|
|
|
|
|
// Prepare
|
2020-07-26 20:51:54 +00:00
|
|
|
const urlSearchParams = new URLSearchParams(location.search);
|
|
|
|
let type = urlSearchParams.get('type');
|
2021-11-13 14:29:58 +00:00
|
|
|
if (type === null && urlSearchParams.get('query') !== null) { type = 'terms'; }
|
2020-07-26 20:51:54 +00:00
|
|
|
|
2020-08-01 20:22:00 +00:00
|
|
|
const fullVisible = urlSearchParams.get('full-visible');
|
|
|
|
this._queryParserVisibleOverride = (fullVisible === null ? null : (fullVisible !== 'false'));
|
|
|
|
|
2020-07-26 20:51:54 +00:00
|
|
|
this._historyHasChanged = true;
|
2020-08-03 01:51:51 +00:00
|
|
|
this._contentType = type;
|
2020-11-24 16:54:08 +00:00
|
|
|
|
|
|
|
// Set content
|
2020-07-26 20:51:54 +00:00
|
|
|
switch (type) {
|
|
|
|
case 'terms':
|
|
|
|
case 'kanji':
|
2021-10-03 20:46:15 +00:00
|
|
|
await this._setContentTermsOrKanji(type, urlSearchParams, token);
|
2020-07-26 20:51:54 +00:00
|
|
|
break;
|
|
|
|
case 'unloaded':
|
2021-10-03 20:46:15 +00:00
|
|
|
this._setContentExtensionUnloaded();
|
|
|
|
break;
|
|
|
|
default:
|
2022-05-21 21:54:57 +00:00
|
|
|
this._contentType = 'clear';
|
2021-10-03 20:46:15 +00:00
|
|
|
this._clearContent();
|
2020-07-26 20:51:54 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
this.onError(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-27 23:07:28 +00:00
|
|
|
_onQueryParserSearch({type, dictionaryEntries, sentence, inputInfo: {eventType}, textSource, optionsContext, sentenceOffset}) {
|
2020-07-26 23:29:12 +00:00
|
|
|
const query = textSource.text();
|
2020-12-22 00:19:59 +00:00
|
|
|
const historyState = this._history.state;
|
2021-08-31 01:55:08 +00:00
|
|
|
const historyMode = (
|
2021-01-17 16:43:05 +00:00
|
|
|
eventType === 'click' ||
|
2020-12-22 00:19:59 +00:00
|
|
|
!isObject(historyState) ||
|
|
|
|
historyState.cause !== 'queryParser'
|
2021-08-31 01:55:08 +00:00
|
|
|
) ? 'new' : 'overwrite';
|
2020-07-26 23:29:12 +00:00
|
|
|
const details = {
|
|
|
|
focus: false,
|
2021-08-31 01:55:08 +00:00
|
|
|
historyMode,
|
2021-09-27 23:07:28 +00:00
|
|
|
params: this._createSearchParams(type, query, false, sentenceOffset),
|
2020-07-26 23:29:12 +00:00
|
|
|
state: {
|
|
|
|
sentence,
|
2020-12-22 00:19:59 +00:00
|
|
|
optionsContext,
|
|
|
|
cause: 'queryParser'
|
2020-07-26 23:29:12 +00:00
|
|
|
},
|
|
|
|
content: {
|
2021-04-04 20:22:35 +00:00
|
|
|
dictionaryEntries,
|
2021-02-10 03:56:04 +00:00
|
|
|
contentOrigin: this.getContentOrigin()
|
2020-07-26 23:29:12 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
this.setContent(details);
|
|
|
|
}
|
|
|
|
|
2020-07-18 18:15:36 +00:00
|
|
|
_onExtensionUnloaded() {
|
2020-08-03 01:51:51 +00:00
|
|
|
const type = 'unloaded';
|
|
|
|
if (this._contentType === type) { return; }
|
2020-07-26 23:29:12 +00:00
|
|
|
const details = {
|
2020-07-26 20:51:54 +00:00
|
|
|
focus: false,
|
2021-08-31 01:55:08 +00:00
|
|
|
historyMode: 'clear',
|
2020-08-03 01:51:51 +00:00
|
|
|
params: {type},
|
2020-07-26 20:51:54 +00:00
|
|
|
state: {},
|
2021-02-10 03:56:04 +00:00
|
|
|
content: {
|
|
|
|
contentOrigin: {
|
|
|
|
tabId: this._tabId,
|
|
|
|
frameId: this._frameId
|
|
|
|
}
|
|
|
|
}
|
2020-07-26 23:29:12 +00:00
|
|
|
};
|
|
|
|
this.setContent(details);
|
2020-07-18 18:15:36 +00:00
|
|
|
}
|
|
|
|
|
2020-11-15 19:12:48 +00:00
|
|
|
_onCloseButtonClick(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
this.close();
|
|
|
|
}
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
_onSourceTermView(e) {
|
2017-03-05 01:30:10 +00:00
|
|
|
e.preventDefault();
|
2020-07-03 16:02:21 +00:00
|
|
|
this._sourceTermView();
|
2017-03-19 22:45:30 +00:00
|
|
|
}
|
2017-03-05 01:30:10 +00:00
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
_onNextTermView(e) {
|
2019-11-28 13:20:18 +00:00
|
|
|
e.preventDefault();
|
2020-07-03 16:02:21 +00:00
|
|
|
this._nextTermView();
|
2019-11-28 13:20:18 +00:00
|
|
|
}
|
|
|
|
|
2020-11-18 00:40:19 +00:00
|
|
|
_onProgressIndicatorVisibleChanged({value}) {
|
2020-11-19 01:06:02 +00:00
|
|
|
if (this._progressIndicatorTimer !== null) {
|
|
|
|
clearTimeout(this._progressIndicatorTimer);
|
|
|
|
this._progressIndicatorTimer = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value) {
|
|
|
|
this._progressIndicator.hidden = false;
|
|
|
|
getComputedStyle(this._progressIndicator).getPropertyValue('display'); // Force update of CSS display property, allowing animation
|
|
|
|
this._progressIndicator.dataset.active = 'true';
|
|
|
|
} else {
|
|
|
|
this._progressIndicator.dataset.active = 'false';
|
|
|
|
this._progressIndicatorTimer = setTimeout(() => {
|
|
|
|
this._progressIndicator.hidden = true;
|
|
|
|
this._progressIndicatorTimer = null;
|
|
|
|
}, 250);
|
|
|
|
}
|
2020-11-18 00:40:19 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
async _onKanjiLookup(e) {
|
2017-08-13 23:11:51 +00:00
|
|
|
try {
|
|
|
|
e.preventDefault();
|
2020-07-26 20:51:54 +00:00
|
|
|
if (!this._historyHasState()) { return; }
|
2017-03-19 22:45:30 +00:00
|
|
|
|
2021-01-12 23:04:26 +00:00
|
|
|
let {state: {sentence, url, documentTitle}} = this._history;
|
|
|
|
if (typeof url !== 'string') { url = window.location.href; }
|
|
|
|
if (typeof documentTitle !== 'string') { documentTitle = document.title; }
|
2020-11-24 01:31:48 +00:00
|
|
|
const optionsContext = this.getOptionsContext();
|
|
|
|
const query = e.currentTarget.textContent;
|
2021-04-04 20:22:35 +00:00
|
|
|
const dictionaryEntries = await yomichan.api.kanjiFind(query, optionsContext);
|
2020-07-26 23:29:12 +00:00
|
|
|
const details = {
|
2020-07-25 13:58:06 +00:00
|
|
|
focus: false,
|
2021-08-31 01:55:08 +00:00
|
|
|
historyMode: 'new',
|
2021-09-27 23:07:28 +00:00
|
|
|
params: this._createSearchParams('kanji', query, false, null),
|
2020-07-26 20:51:54 +00:00
|
|
|
state: {
|
2020-07-26 22:49:38 +00:00
|
|
|
focusEntry: 0,
|
2021-01-12 23:04:26 +00:00
|
|
|
optionsContext,
|
|
|
|
url,
|
2020-11-24 01:31:48 +00:00
|
|
|
sentence,
|
2021-01-12 23:04:26 +00:00
|
|
|
documentTitle
|
2020-07-26 20:51:54 +00:00
|
|
|
},
|
|
|
|
content: {
|
2021-04-04 20:22:35 +00:00
|
|
|
dictionaryEntries,
|
2021-02-10 03:56:04 +00:00
|
|
|
contentOrigin: this.getContentOrigin()
|
2020-07-26 20:51:54 +00:00
|
|
|
}
|
2020-07-26 23:29:12 +00:00
|
|
|
};
|
|
|
|
this.setContent(details);
|
2019-11-10 18:55:37 +00:00
|
|
|
} catch (error) {
|
|
|
|
this.onError(error);
|
2017-03-19 22:45:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
_onWheel(e) {
|
2019-10-03 00:57:48 +00:00
|
|
|
if (e.altKey) {
|
2019-11-28 13:20:18 +00:00
|
|
|
if (e.deltaY !== 0) {
|
2021-09-30 23:31:45 +00:00
|
|
|
this._focusEntry(this._index + (e.deltaY > 0 ? 1 : -1), 0, true);
|
2019-10-03 00:57:48 +00:00
|
|
|
e.preventDefault();
|
2017-09-25 20:47:53 +00:00
|
|
|
}
|
2019-11-28 13:24:00 +00:00
|
|
|
} else if (e.shiftKey) {
|
2020-07-03 16:02:21 +00:00
|
|
|
this._onHistoryWheel(e);
|
2019-12-07 15:41:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
_onHistoryWheel(e) {
|
2020-02-05 12:19:48 +00:00
|
|
|
if (e.altKey) { return; }
|
2019-12-07 15:41:14 +00:00
|
|
|
const delta = -e.deltaX || e.deltaY;
|
|
|
|
if (delta > 0) {
|
2020-07-03 16:02:21 +00:00
|
|
|
this._sourceTermView();
|
2019-12-07 15:41:14 +00:00
|
|
|
e.preventDefault();
|
2020-02-05 12:19:48 +00:00
|
|
|
e.stopPropagation();
|
2019-12-07 15:41:14 +00:00
|
|
|
} else if (delta < 0) {
|
2020-07-03 16:02:21 +00:00
|
|
|
this._nextTermView();
|
2019-12-07 15:41:14 +00:00
|
|
|
e.preventDefault();
|
2020-02-05 12:19:48 +00:00
|
|
|
e.stopPropagation();
|
2019-11-28 13:20:18 +00:00
|
|
|
}
|
2017-09-25 20:47:53 +00:00
|
|
|
}
|
|
|
|
|
2020-11-14 00:48:22 +00:00
|
|
|
_onDebugLogClick(e) {
|
|
|
|
const link = e.currentTarget;
|
2021-07-03 23:32:11 +00:00
|
|
|
const index = this.getElementDictionaryEntryIndex(link);
|
2021-04-04 20:22:35 +00:00
|
|
|
this._logDictionaryEntryData(index);
|
2020-11-14 00:48:22 +00:00
|
|
|
}
|
|
|
|
|
2020-11-22 20:29:51 +00:00
|
|
|
_onDocumentElementMouseUp(e) {
|
|
|
|
switch (e.button) {
|
|
|
|
case 3: // Back
|
|
|
|
if (this._history.hasPrevious()) {
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 4: // Forward
|
|
|
|
if (this._history.hasNext()) {
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_onDocumentElementClick(e) {
|
|
|
|
switch (e.button) {
|
|
|
|
case 3: // Back
|
|
|
|
if (this._history.hasPrevious()) {
|
|
|
|
e.preventDefault();
|
|
|
|
this._history.back();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 4: // Forward
|
|
|
|
if (this._history.hasNext()) {
|
|
|
|
e.preventDefault();
|
|
|
|
this._history.forward();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-23 22:43:17 +00:00
|
|
|
_onEntryClick(e) {
|
|
|
|
if (e.button !== 0) { return; }
|
|
|
|
const node = e.currentTarget;
|
|
|
|
const index = parseInt(node.dataset.index, 10);
|
|
|
|
if (!Number.isFinite(index)) { return; }
|
|
|
|
this._entrySetCurrent(index);
|
|
|
|
}
|
|
|
|
|
2020-12-18 14:43:54 +00:00
|
|
|
_onTagClick(e) {
|
2021-01-09 21:02:03 +00:00
|
|
|
this._showTagNotification(e.currentTarget);
|
2020-12-18 14:43:54 +00:00
|
|
|
}
|
|
|
|
|
2021-10-17 13:55:13 +00:00
|
|
|
_onMenuButtonClick(e) {
|
|
|
|
const node = e.currentTarget;
|
|
|
|
|
|
|
|
const menuContainerNode = this._displayGenerator.instantiateTemplate('dictionary-entry-popup-menu');
|
|
|
|
const menuBodyNode = menuContainerNode.querySelector('.popup-menu-body');
|
|
|
|
|
|
|
|
const addItem = (menuAction, label) => {
|
|
|
|
const item = this._displayGenerator.instantiateTemplate('dictionary-entry-popup-menu-item');
|
|
|
|
item.querySelector('.popup-menu-item-label').textContent = label;
|
|
|
|
item.dataset.menuAction = menuAction;
|
|
|
|
menuBodyNode.appendChild(item);
|
|
|
|
};
|
|
|
|
|
|
|
|
addItem('log-debug-info', 'Log debug info');
|
|
|
|
|
|
|
|
this._menuContainer.appendChild(menuContainerNode);
|
|
|
|
const popupMenu = new PopupMenu(node, menuContainerNode);
|
|
|
|
popupMenu.prepare();
|
|
|
|
}
|
|
|
|
|
|
|
|
_onMenuButtonMenuClose(e) {
|
|
|
|
const {currentTarget: node, detail: {action}} = e;
|
|
|
|
switch (action) {
|
|
|
|
case 'log-debug-info':
|
|
|
|
this._logDictionaryEntryData(this.getElementDictionaryEntryIndex(node));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-09 21:02:03 +00:00
|
|
|
_showTagNotification(tagNode) {
|
2021-02-21 18:57:28 +00:00
|
|
|
tagNode = tagNode.parentNode;
|
|
|
|
if (tagNode === null) { return; }
|
|
|
|
|
2020-12-18 14:43:54 +00:00
|
|
|
if (this._tagNotification === null) {
|
2022-05-20 22:08:20 +00:00
|
|
|
this._tagNotification = this.createNotification(true);
|
2020-12-18 14:43:54 +00:00
|
|
|
}
|
|
|
|
|
2021-07-03 23:32:11 +00:00
|
|
|
const index = this.getElementDictionaryEntryIndex(tagNode);
|
2021-04-04 20:22:35 +00:00
|
|
|
const dictionaryEntry = (index >= 0 && index < this._dictionaryEntries.length ? this._dictionaryEntries[index] : null);
|
2021-03-26 00:54:39 +00:00
|
|
|
|
|
|
|
const content = this._displayGenerator.createTagFooterNotificationDetails(tagNode, dictionaryEntry);
|
2020-12-18 14:43:54 +00:00
|
|
|
this._tagNotification.setContent(content);
|
|
|
|
this._tagNotification.open();
|
|
|
|
}
|
|
|
|
|
|
|
|
_hideTagNotification(animate) {
|
|
|
|
if (this._tagNotification === null) { return; }
|
|
|
|
this._tagNotification.close(animate);
|
|
|
|
}
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
_updateDocumentOptions(options) {
|
2019-12-25 02:45:57 +00:00
|
|
|
const data = document.documentElement.dataset;
|
|
|
|
data.ankiEnabled = `${options.anki.enable}`;
|
2021-03-16 22:48:24 +00:00
|
|
|
data.resultOutputMode = `${options.general.resultOutputMode}`;
|
2020-11-26 04:22:05 +00:00
|
|
|
data.glossaryLayoutMode = `${options.general.glossaryLayoutMode}`;
|
2020-11-13 01:34:11 +00:00
|
|
|
data.compactTags = `${options.general.compactTags}`;
|
2021-02-25 22:48:39 +00:00
|
|
|
data.frequencyDisplayMode = `${options.general.frequencyDisplayMode}`;
|
2021-02-27 19:04:52 +00:00
|
|
|
data.termDisplayMode = `${options.general.termDisplayMode}`;
|
2020-01-26 19:00:19 +00:00
|
|
|
data.enableSearchTags = `${options.scanning.enableSearchTags}`;
|
2021-07-25 22:16:07 +00:00
|
|
|
data.showPronunciationText = `${options.general.showPitchAccentDownstepNotation}`;
|
|
|
|
data.showPronunciationDownstepPosition = `${options.general.showPitchAccentPositionNotation}`;
|
|
|
|
data.showPronunciationGraph = `${options.general.showPitchAccentGraph}`;
|
2019-12-25 02:45:57 +00:00
|
|
|
data.debug = `${options.general.debugInfo}`;
|
2020-12-09 01:31:02 +00:00
|
|
|
data.popupDisplayMode = `${options.general.popupDisplayMode}`;
|
2020-12-19 20:42:44 +00:00
|
|
|
data.popupCurrentIndicatorMode = `${options.general.popupCurrentIndicatorMode}`;
|
2020-12-20 01:07:55 +00:00
|
|
|
data.popupActionBarVisibility = `${options.general.popupActionBarVisibility}`;
|
|
|
|
data.popupActionBarLocation = `${options.general.popupActionBarLocation}`;
|
2019-12-25 02:45:57 +00:00
|
|
|
}
|
|
|
|
|
2022-04-03 20:20:55 +00:00
|
|
|
_setTheme(options) {
|
|
|
|
const {general} = options;
|
|
|
|
const {popupTheme} = general;
|
|
|
|
this._themeController.theme = popupTheme;
|
|
|
|
this._themeController.outerTheme = general.popupOuterTheme;
|
|
|
|
this._themeController.updateTheme();
|
|
|
|
this.setCustomCss(general.customPopupCss);
|
2019-10-12 18:20:54 +00:00
|
|
|
}
|
|
|
|
|
2021-10-03 20:46:15 +00:00
|
|
|
async _findDictionaryEntries(isKanji, source, wildcardsEnabled, optionsContext) {
|
|
|
|
if (isKanji) {
|
|
|
|
const dictionaryEntries = await yomichan.api.kanjiFind(source, optionsContext);
|
|
|
|
return dictionaryEntries;
|
|
|
|
} else {
|
2020-07-26 20:51:54 +00:00
|
|
|
const findDetails = {};
|
2020-11-24 16:54:08 +00:00
|
|
|
if (wildcardsEnabled) {
|
2020-07-26 20:51:54 +00:00
|
|
|
const match = /^([*\uff0a]*)([\w\W]*?)([*\uff0a]*)$/.exec(source);
|
|
|
|
if (match !== null) {
|
|
|
|
if (match[1]) {
|
2021-12-17 21:11:19 +00:00
|
|
|
findDetails.matchType = 'suffix';
|
2021-12-17 22:02:13 +00:00
|
|
|
findDetails.deinflect = false;
|
2020-07-26 20:51:54 +00:00
|
|
|
} else if (match[3]) {
|
2021-12-17 21:11:19 +00:00
|
|
|
findDetails.matchType = 'prefix';
|
2021-12-17 22:02:13 +00:00
|
|
|
findDetails.deinflect = false;
|
2020-07-26 20:51:54 +00:00
|
|
|
}
|
|
|
|
source = match[2];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-04 20:22:35 +00:00
|
|
|
const {dictionaryEntries} = await yomichan.api.termsFind(source, findDetails, optionsContext);
|
|
|
|
return dictionaryEntries;
|
2020-07-26 20:51:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-03 20:46:15 +00:00
|
|
|
async _setContentTermsOrKanji(type, urlSearchParams, token) {
|
|
|
|
const lookup = (urlSearchParams.get('lookup') !== 'false');
|
|
|
|
const wildcardsEnabled = (urlSearchParams.get('wildcards') !== 'off');
|
|
|
|
|
|
|
|
// Set query
|
2021-10-15 01:16:40 +00:00
|
|
|
let query = urlSearchParams.get('query');
|
|
|
|
if (query === null) { query = ''; }
|
2021-10-03 20:46:15 +00:00
|
|
|
let queryFull = urlSearchParams.get('full');
|
|
|
|
queryFull = (queryFull !== null ? queryFull : query);
|
|
|
|
let queryOffset = urlSearchParams.get('offset');
|
|
|
|
if (queryOffset !== null) {
|
|
|
|
queryOffset = Number.parseInt(queryOffset, 10);
|
|
|
|
queryOffset = Number.isFinite(queryOffset) ? Math.max(0, Math.min(queryFull.length - query.length, queryOffset)) : null;
|
|
|
|
}
|
|
|
|
this._setQuery(query, queryFull, queryOffset);
|
|
|
|
|
2020-08-01 20:22:00 +00:00
|
|
|
let {state, content} = this._history;
|
|
|
|
let changeHistory = false;
|
|
|
|
if (!isObject(content)) {
|
|
|
|
content = {};
|
|
|
|
changeHistory = true;
|
|
|
|
}
|
|
|
|
if (!isObject(state)) {
|
|
|
|
state = {};
|
|
|
|
changeHistory = true;
|
|
|
|
}
|
|
|
|
|
2021-10-03 20:46:15 +00:00
|
|
|
let {focusEntry, scrollX, scrollY, optionsContext} = state;
|
2020-12-19 21:00:47 +00:00
|
|
|
if (typeof focusEntry !== 'number') { focusEntry = 0; }
|
2020-11-13 01:32:46 +00:00
|
|
|
if (!(typeof optionsContext === 'object' && optionsContext !== null)) {
|
|
|
|
optionsContext = this.getOptionsContext();
|
|
|
|
state.optionsContext = optionsContext;
|
|
|
|
changeHistory = true;
|
|
|
|
}
|
|
|
|
|
2021-04-04 20:22:35 +00:00
|
|
|
let {dictionaryEntries} = content;
|
|
|
|
if (!Array.isArray(dictionaryEntries)) {
|
2021-10-03 20:46:15 +00:00
|
|
|
dictionaryEntries = lookup && query.length > 0 ? await this._findDictionaryEntries(type === 'kanji', query, wildcardsEnabled, optionsContext) : [];
|
2020-11-24 16:54:08 +00:00
|
|
|
if (this._setContentToken !== token) { return; }
|
2021-04-04 20:22:35 +00:00
|
|
|
content.dictionaryEntries = dictionaryEntries;
|
2020-08-01 20:22:00 +00:00
|
|
|
changeHistory = true;
|
|
|
|
}
|
|
|
|
|
2021-02-10 03:56:04 +00:00
|
|
|
let contentOriginValid = false;
|
|
|
|
const {contentOrigin} = content;
|
|
|
|
if (typeof contentOrigin === 'object' && contentOrigin !== null) {
|
|
|
|
const {tabId, frameId} = contentOrigin;
|
|
|
|
if (typeof tabId === 'number' && typeof frameId === 'number') {
|
|
|
|
this._contentOriginTabId = tabId;
|
|
|
|
this._contentOriginFrameId = frameId;
|
|
|
|
contentOriginValid = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!contentOriginValid) {
|
|
|
|
content.contentOrigin = this.getContentOrigin();
|
|
|
|
changeHistory = true;
|
|
|
|
}
|
|
|
|
|
2020-11-13 01:32:46 +00:00
|
|
|
await this._setOptionsContextIfDifferent(optionsContext);
|
2020-11-24 16:54:08 +00:00
|
|
|
if (this._setContentToken !== token) { return; }
|
|
|
|
|
|
|
|
if (this._options === null) {
|
|
|
|
await this.updateOptions();
|
|
|
|
if (this._setContentToken !== token) { return; }
|
|
|
|
}
|
2020-11-13 01:32:46 +00:00
|
|
|
|
2020-08-01 20:22:00 +00:00
|
|
|
if (changeHistory) {
|
2020-11-24 01:31:48 +00:00
|
|
|
this._replaceHistoryStateNoNavigate(state, content);
|
2020-08-01 20:22:00 +00:00
|
|
|
}
|
|
|
|
|
2021-04-04 20:22:35 +00:00
|
|
|
this._dictionaryEntries = dictionaryEntries;
|
2017-08-13 23:11:51 +00:00
|
|
|
|
2022-03-17 23:01:59 +00:00
|
|
|
this._updateNavigationAuto();
|
2021-04-04 20:22:35 +00:00
|
|
|
this._setNoContentVisible(dictionaryEntries.length === 0 && lookup);
|
2017-08-13 23:11:51 +00:00
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
const container = this._container;
|
2019-12-27 23:15:16 +00:00
|
|
|
container.textContent = '';
|
2019-12-25 02:45:57 +00:00
|
|
|
|
2021-10-03 20:46:15 +00:00
|
|
|
this._triggerContentUpdateStart();
|
2021-07-04 00:09:22 +00:00
|
|
|
|
2021-04-04 20:22:35 +00:00
|
|
|
for (let i = 0, ii = dictionaryEntries.length; i < ii; ++i) {
|
2019-12-27 23:15:16 +00:00
|
|
|
if (i > 0) {
|
|
|
|
await promiseTimeout(1);
|
2020-11-24 16:54:08 +00:00
|
|
|
if (this._setContentToken !== token) { return; }
|
2019-12-27 23:15:16 +00:00
|
|
|
}
|
2019-12-25 02:45:57 +00:00
|
|
|
|
2021-04-04 20:22:35 +00:00
|
|
|
const dictionaryEntry = dictionaryEntries[i];
|
2020-07-25 01:12:13 +00:00
|
|
|
const entry = (
|
2021-10-03 20:46:15 +00:00
|
|
|
dictionaryEntry.type === 'term' ?
|
2021-04-04 20:22:35 +00:00
|
|
|
this._displayGenerator.createTermEntry(dictionaryEntry) :
|
|
|
|
this._displayGenerator.createKanjiEntry(dictionaryEntry)
|
2020-07-25 01:12:13 +00:00
|
|
|
);
|
2020-11-22 02:17:39 +00:00
|
|
|
entry.dataset.index = `${i}`;
|
2021-04-04 20:22:35 +00:00
|
|
|
this._dictionaryEntryNodes.push(entry);
|
2020-11-23 22:43:17 +00:00
|
|
|
this._addEntryEventListeners(entry);
|
2021-10-03 20:46:15 +00:00
|
|
|
this._triggerContentUpdateEntry(dictionaryEntry, entry, i);
|
2019-12-27 23:15:16 +00:00
|
|
|
container.appendChild(entry);
|
2020-12-19 21:00:47 +00:00
|
|
|
if (focusEntry === i) {
|
2021-09-30 23:31:45 +00:00
|
|
|
this._focusEntry(i, 0, false);
|
2020-12-19 21:00:47 +00:00
|
|
|
}
|
2021-03-31 22:17:28 +00:00
|
|
|
|
|
|
|
this._elementOverflowController.addElements(entry);
|
2019-12-27 23:15:16 +00:00
|
|
|
}
|
2019-12-25 02:45:57 +00:00
|
|
|
|
2020-07-26 22:49:38 +00:00
|
|
|
if (typeof scrollX === 'number' || typeof scrollY === 'number') {
|
|
|
|
let {x, y} = this._windowScroll;
|
|
|
|
if (typeof scrollX === 'number') { x = scrollX; }
|
|
|
|
if (typeof scrollY === 'number') { y = scrollY; }
|
|
|
|
this._windowScroll.stop();
|
|
|
|
this._windowScroll.to(x, y);
|
|
|
|
}
|
2019-12-25 02:45:57 +00:00
|
|
|
|
2021-10-03 20:46:15 +00:00
|
|
|
this._triggerContentUpdateComplete();
|
2020-08-01 20:22:00 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 16:20:22 +00:00
|
|
|
_setContentExtensionUnloaded() {
|
|
|
|
const errorExtensionUnloaded = document.querySelector('#error-extension-unloaded');
|
2019-10-17 01:36:10 +00:00
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
if (this._container !== null) {
|
|
|
|
this._container.hidden = true;
|
2019-10-17 01:36:10 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 16:20:22 +00:00
|
|
|
if (errorExtensionUnloaded !== null) {
|
|
|
|
errorExtensionUnloaded.hidden = false;
|
2019-10-17 01:36:10 +00:00
|
|
|
}
|
2019-12-25 02:45:57 +00:00
|
|
|
|
2020-11-15 19:12:48 +00:00
|
|
|
this._updateNavigation(false, false);
|
2020-07-03 16:02:21 +00:00
|
|
|
this._setNoContentVisible(false);
|
2021-10-03 20:46:15 +00:00
|
|
|
this._setQuery('', '', 0);
|
|
|
|
|
|
|
|
this._triggerContentUpdateStart();
|
|
|
|
this._triggerContentUpdateComplete();
|
2019-12-25 02:45:57 +00:00
|
|
|
}
|
|
|
|
|
2020-07-26 20:51:54 +00:00
|
|
|
_clearContent() {
|
|
|
|
this._container.textContent = '';
|
2022-03-17 23:01:59 +00:00
|
|
|
this._updateNavigationAuto();
|
2021-10-03 20:46:15 +00:00
|
|
|
this._setQuery('', '', 0);
|
|
|
|
|
|
|
|
this._triggerContentUpdateStart();
|
|
|
|
this._triggerContentUpdateComplete();
|
2020-07-26 20:51:54 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
_setNoContentVisible(visible) {
|
2019-12-25 02:45:57 +00:00
|
|
|
const noResults = document.querySelector('#no-results');
|
|
|
|
|
|
|
|
if (noResults !== null) {
|
|
|
|
noResults.hidden = !visible;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-03 20:46:15 +00:00
|
|
|
_setQuery(query, fullQuery, queryOffset) {
|
|
|
|
this._query = query;
|
|
|
|
this._fullQuery = fullQuery;
|
|
|
|
this._queryOffset = queryOffset;
|
2020-11-19 01:15:30 +00:00
|
|
|
this._updateQueryParser();
|
2021-10-03 20:46:15 +00:00
|
|
|
this._setTitleText(query);
|
2020-11-19 01:15:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_updateQueryParser() {
|
|
|
|
const text = this._fullQuery;
|
|
|
|
const visible = this._isQueryParserVisible();
|
|
|
|
this._queryParserContainer.hidden = !visible || text.length === 0;
|
|
|
|
if (visible && this._queryParser.text !== text) {
|
|
|
|
this._setQueryParserText(text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async _setQueryParserText(text) {
|
|
|
|
const overrideToken = this._progressIndicatorVisible.setOverride(true);
|
|
|
|
try {
|
|
|
|
await this._queryParser.setText(text);
|
|
|
|
} finally {
|
|
|
|
this._progressIndicatorVisible.clearOverride(overrideToken);
|
|
|
|
}
|
2020-08-01 20:22:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_setTitleText(text) {
|
2020-11-24 01:31:48 +00:00
|
|
|
let title = this._defaultTitle;
|
2020-11-23 22:43:17 +00:00
|
|
|
if (text.length > 0) {
|
|
|
|
// Chrome limits title to 1024 characters
|
|
|
|
const ellipsis = '...';
|
|
|
|
const separator = ' - ';
|
2020-11-24 01:31:48 +00:00
|
|
|
const maxLength = this._titleMaxLength - title.length - separator.length;
|
2020-11-23 22:43:17 +00:00
|
|
|
if (text.length > maxLength) {
|
|
|
|
text = `${text.substring(0, Math.max(0, maxLength - ellipsis.length))}${ellipsis}`;
|
|
|
|
}
|
2020-08-01 20:22:00 +00:00
|
|
|
|
2020-11-24 01:31:48 +00:00
|
|
|
title = `${text}${separator}${title}`;
|
2020-11-23 22:43:17 +00:00
|
|
|
}
|
|
|
|
document.title = title;
|
2020-08-01 20:22:00 +00:00
|
|
|
}
|
|
|
|
|
2022-03-17 23:01:59 +00:00
|
|
|
_updateNavigationAuto() {
|
|
|
|
this._updateNavigation(this._history.hasPrevious(), this._history.hasNext());
|
|
|
|
}
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
_updateNavigation(previous, next) {
|
2021-01-10 21:48:31 +00:00
|
|
|
const {documentElement} = document;
|
|
|
|
if (documentElement !== null) {
|
|
|
|
documentElement.dataset.hasNavigationPrevious = `${previous}`;
|
|
|
|
documentElement.dataset.hasNavigationNext = `${next}`;
|
2020-11-15 19:12:48 +00:00
|
|
|
}
|
|
|
|
if (this._navigationPreviousButton !== null) {
|
|
|
|
this._navigationPreviousButton.disabled = !previous;
|
|
|
|
}
|
|
|
|
if (this._navigationNextButton !== null) {
|
|
|
|
this._navigationNextButton.disabled = !next;
|
|
|
|
}
|
2019-10-17 01:36:10 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
_entrySetCurrent(index) {
|
|
|
|
const entryPre = this._getEntry(this._index);
|
2019-09-15 22:44:49 +00:00
|
|
|
if (entryPre !== null) {
|
|
|
|
entryPre.classList.remove('entry-current');
|
|
|
|
}
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
const entry = this._getEntry(index);
|
2019-09-15 22:44:49 +00:00
|
|
|
if (entry !== null) {
|
|
|
|
entry.classList.add('entry-current');
|
|
|
|
}
|
2017-08-13 23:11:51 +00:00
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
this._index = index;
|
2019-11-28 11:22:47 +00:00
|
|
|
}
|
|
|
|
|
2021-09-30 23:31:45 +00:00
|
|
|
_focusEntry(index, definitionIndex, smooth) {
|
2021-04-04 20:22:35 +00:00
|
|
|
index = Math.max(Math.min(index, this._dictionaryEntries.length - 1), 0);
|
2020-11-14 23:12:06 +00:00
|
|
|
|
2021-09-30 23:31:45 +00:00
|
|
|
this._entrySetCurrent(index);
|
|
|
|
|
|
|
|
let node = (index >= 0 && index < this._dictionaryEntryNodes.length ? this._dictionaryEntryNodes[index] : null);
|
|
|
|
if (definitionIndex > 0) {
|
|
|
|
const definitionNodes = this._getDictionaryEntryDefinitionNodes(index);
|
|
|
|
if (definitionIndex < definitionNodes.length) {
|
|
|
|
node = definitionNodes[definitionIndex];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let target = (index === 0 && definitionIndex <= 0) || node === null ? 0 : this._getElementTop(node);
|
2019-12-29 04:03:18 +00:00
|
|
|
|
2020-07-26 22:49:38 +00:00
|
|
|
if (this._navigationHeader !== null) {
|
|
|
|
target -= this._navigationHeader.getBoundingClientRect().height;
|
2019-08-03 16:46:54 +00:00
|
|
|
}
|
2017-08-13 23:11:51 +00:00
|
|
|
|
2020-07-26 22:49:38 +00:00
|
|
|
this._windowScroll.stop();
|
2017-08-13 23:11:51 +00:00
|
|
|
if (smooth) {
|
2020-07-03 16:02:21 +00:00
|
|
|
this._windowScroll.animate(this._windowScroll.x, target, 200);
|
2017-08-13 23:11:51 +00:00
|
|
|
} else {
|
2020-07-03 16:02:21 +00:00
|
|
|
this._windowScroll.toY(target);
|
2017-08-13 23:11:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-16 02:11:09 +00:00
|
|
|
_focusEntryWithDifferentDictionary(offset, smooth) {
|
2021-09-30 23:31:45 +00:00
|
|
|
const sign = Math.sign(offset);
|
|
|
|
if (sign === 0) { return false; }
|
2021-01-16 02:11:09 +00:00
|
|
|
|
|
|
|
let index = this._index;
|
2021-09-30 23:31:45 +00:00
|
|
|
const count = Math.min(this._dictionaryEntries.length, this._dictionaryEntryNodes.length);
|
|
|
|
if (index < 0 || index >= count) { return false; }
|
|
|
|
|
|
|
|
const dictionaryEntry = this._dictionaryEntries[index];
|
|
|
|
const visibleDefinitionIndex = this._getDictionaryEntryVisibleDefinitionIndex(index, sign);
|
|
|
|
if (visibleDefinitionIndex === null) { return false; }
|
|
|
|
|
|
|
|
const {dictionary} = dictionaryEntry.definitions[visibleDefinitionIndex];
|
|
|
|
let focusDefinitionIndex = null;
|
|
|
|
for (let i = index; i >= 0 && i < count; i += sign) {
|
|
|
|
const {definitions} = this._dictionaryEntries[i];
|
|
|
|
const jj = definitions.length;
|
|
|
|
let j = (i === index ? visibleDefinitionIndex + sign : (sign > 0 ? 0 : jj - 1));
|
|
|
|
for (; j >= 0 && j < jj; j += sign) {
|
|
|
|
if (definitions[j].dictionary !== dictionary) {
|
|
|
|
focusDefinitionIndex = j;
|
|
|
|
index = i;
|
|
|
|
i = -2; // Terminate outer loop
|
2021-01-16 02:11:09 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-30 23:31:45 +00:00
|
|
|
if (focusDefinitionIndex === null) { return false; }
|
2021-01-16 02:11:09 +00:00
|
|
|
|
2021-09-30 23:31:45 +00:00
|
|
|
this._focusEntry(index, focusDefinitionIndex, smooth);
|
2021-01-16 02:11:09 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-09-30 23:31:45 +00:00
|
|
|
_getDictionaryEntryVisibleDefinitionIndex(index, sign) {
|
|
|
|
const {top: scrollTop, bottom: scrollBottom} = this._windowScroll.getRect();
|
|
|
|
|
|
|
|
const {definitions} = this._dictionaryEntries[index];
|
|
|
|
const nodes = this._getDictionaryEntryDefinitionNodes(index);
|
|
|
|
const definitionCount = Math.min(definitions.length, nodes.length);
|
|
|
|
if (definitionCount <= 0) { return null; }
|
|
|
|
|
|
|
|
let visibleIndex = null;
|
|
|
|
let visibleCoverage = 0;
|
|
|
|
for (let i = (sign > 0 ? 0 : definitionCount - 1); i >= 0 && i < definitionCount; i += sign) {
|
|
|
|
const {top, bottom} = nodes[i].getBoundingClientRect();
|
|
|
|
if (bottom <= scrollTop || top >= scrollBottom) { continue; }
|
|
|
|
const top2 = Math.max(scrollTop, Math.min(scrollBottom, top));
|
|
|
|
const bottom2 = Math.max(scrollTop, Math.min(scrollBottom, bottom));
|
|
|
|
const coverage = (bottom2 - top2) / (bottom - top);
|
|
|
|
if (coverage >= visibleCoverage) {
|
|
|
|
visibleCoverage = coverage;
|
|
|
|
visibleIndex = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return visibleIndex !== null ? visibleIndex : (sign > 0 ? definitionCount - 1 : 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
_getDictionaryEntryDefinitionNodes(index) {
|
|
|
|
return this._dictionaryEntryNodes[index].querySelectorAll('.definition-item');
|
|
|
|
}
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
_sourceTermView() {
|
2020-07-25 13:58:06 +00:00
|
|
|
this._relativeTermView(false);
|
2017-03-26 01:08:42 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
_nextTermView() {
|
2020-07-25 13:58:06 +00:00
|
|
|
this._relativeTermView(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
_relativeTermView(next) {
|
2020-07-26 20:51:54 +00:00
|
|
|
if (next) {
|
|
|
|
return this._history.hasNext() && this._history.forward();
|
|
|
|
} else {
|
|
|
|
return this._history.hasPrevious() && this._history.back();
|
|
|
|
}
|
2019-11-28 13:20:18 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
_getEntry(index) {
|
2021-04-04 20:22:35 +00:00
|
|
|
const entries = this._dictionaryEntryNodes;
|
2019-09-15 22:29:11 +00:00
|
|
|
return index >= 0 && index < entries.length ? entries[index] : null;
|
|
|
|
}
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
_getElementTop(element) {
|
2019-09-15 22:44:49 +00:00
|
|
|
const elementRect = element.getBoundingClientRect();
|
2020-11-14 23:12:06 +00:00
|
|
|
const documentRect = this._contentScrollBodyElement.getBoundingClientRect();
|
2019-09-15 22:44:49 +00:00
|
|
|
return elementRect.top - documentRect.top;
|
|
|
|
}
|
2019-09-20 02:03:26 +00:00
|
|
|
|
2020-07-26 20:51:54 +00:00
|
|
|
_historyHasState() {
|
|
|
|
return isObject(this._history.state);
|
|
|
|
}
|
|
|
|
|
2020-11-24 01:31:48 +00:00
|
|
|
_updateHistoryState() {
|
|
|
|
const {state, content} = this._history;
|
|
|
|
if (!isObject(state)) { return; }
|
|
|
|
|
|
|
|
state.focusEntry = this._index;
|
|
|
|
state.scrollX = this._windowScroll.x;
|
|
|
|
state.scrollY = this._windowScroll.y;
|
|
|
|
this._replaceHistoryStateNoNavigate(state, content);
|
|
|
|
}
|
|
|
|
|
|
|
|
_replaceHistoryStateNoNavigate(state, content) {
|
2020-07-26 20:51:54 +00:00
|
|
|
const historyChangeIgnorePre = this._historyChangeIgnore;
|
|
|
|
try {
|
|
|
|
this._historyChangeIgnore = true;
|
|
|
|
this._history.replaceState(state, content);
|
|
|
|
} finally {
|
|
|
|
this._historyChangeIgnore = historyChangeIgnorePre;
|
|
|
|
}
|
|
|
|
}
|
2020-07-26 23:29:12 +00:00
|
|
|
|
2021-09-27 23:07:28 +00:00
|
|
|
_createSearchParams(type, query, wildcards, sentenceOffset) {
|
2020-07-26 23:29:12 +00:00
|
|
|
const params = {};
|
2021-09-27 23:07:28 +00:00
|
|
|
const fullQuery = this._fullQuery;
|
|
|
|
const includeFull = (query.length < fullQuery.length);
|
|
|
|
if (includeFull) {
|
|
|
|
params.full = fullQuery;
|
2020-07-26 23:29:12 +00:00
|
|
|
}
|
|
|
|
params.query = query;
|
2021-09-27 23:07:28 +00:00
|
|
|
if (includeFull && sentenceOffset !== null) {
|
|
|
|
params.offset = `${sentenceOffset}`;
|
|
|
|
}
|
2020-07-26 23:29:12 +00:00
|
|
|
if (typeof type === 'string') {
|
|
|
|
params.type = type;
|
|
|
|
}
|
|
|
|
if (!wildcards) {
|
|
|
|
params.wildcards = 'off';
|
|
|
|
}
|
2020-08-01 20:22:00 +00:00
|
|
|
if (this._queryParserVisibleOverride !== null) {
|
|
|
|
params['full-visible'] = `${this._queryParserVisibleOverride}`;
|
|
|
|
}
|
2020-07-26 23:29:12 +00:00
|
|
|
return params;
|
|
|
|
}
|
2020-08-01 20:22:00 +00:00
|
|
|
|
|
|
|
_isQueryParserVisible() {
|
|
|
|
return (
|
|
|
|
this._queryParserVisibleOverride !== null ?
|
|
|
|
this._queryParserVisibleOverride :
|
|
|
|
this._queryParserVisible
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
_closePopups() {
|
|
|
|
yomichan.trigger('closePopups');
|
|
|
|
}
|
2020-08-09 17:11:41 +00:00
|
|
|
|
2020-11-13 01:32:46 +00:00
|
|
|
async _setOptionsContextIfDifferent(optionsContext) {
|
|
|
|
if (deepEqual(this._optionsContext, optionsContext)) { return; }
|
|
|
|
await this.setOptionsContext(optionsContext);
|
|
|
|
}
|
2020-11-21 03:42:49 +00:00
|
|
|
|
2020-11-22 16:19:21 +00:00
|
|
|
_setContentScale(scale) {
|
|
|
|
const body = document.body;
|
|
|
|
if (body === null) { return; }
|
|
|
|
body.style.fontSize = `${scale}em`;
|
|
|
|
}
|
|
|
|
|
|
|
|
async _updateNestedFrontend(options) {
|
|
|
|
const isSearchPage = (this._pageType === 'search');
|
2021-05-01 21:30:17 +00:00
|
|
|
const isEnabled = (
|
|
|
|
this._childrenSupported &&
|
|
|
|
typeof this._tabId === 'number' &&
|
|
|
|
(
|
|
|
|
(isSearchPage) ?
|
|
|
|
(options.scanning.enableOnSearchPage) :
|
|
|
|
(this._depth < options.scanning.popupNestingMaxDepth)
|
|
|
|
)
|
2020-11-22 16:19:21 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
if (this._frontend === null) {
|
|
|
|
if (!isEnabled) { return; }
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (this._frontendSetupPromise === null) {
|
2020-11-24 16:54:08 +00:00
|
|
|
this._frontendSetupPromise = this._setupNestedFrontend();
|
2020-11-22 16:19:21 +00:00
|
|
|
}
|
|
|
|
await this._frontendSetupPromise;
|
|
|
|
} catch (e) {
|
2021-02-14 22:52:01 +00:00
|
|
|
log.error(e);
|
2020-11-22 16:19:21 +00:00
|
|
|
return;
|
|
|
|
} finally {
|
|
|
|
this._frontendSetupPromise = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this._frontend.setDisabledOverride(!isEnabled);
|
|
|
|
}
|
|
|
|
|
2020-11-24 16:54:08 +00:00
|
|
|
async _setupNestedFrontend() {
|
|
|
|
const setupNestedPopupsOptions = {
|
|
|
|
useProxyPopup: this._parentFrameId !== null,
|
|
|
|
parentPopupId: this._parentPopupId,
|
|
|
|
parentFrameId: this._parentFrameId
|
|
|
|
};
|
2020-11-22 16:19:21 +00:00
|
|
|
|
|
|
|
await dynamicLoader.loadScripts([
|
2021-02-14 03:52:28 +00:00
|
|
|
'/js/language/text-scanner.js',
|
|
|
|
'/js/comm/frame-client.js',
|
2021-02-14 04:13:53 +00:00
|
|
|
'/js/app/popup.js',
|
|
|
|
'/js/app/popup-proxy.js',
|
|
|
|
'/js/app/popup-window.js',
|
|
|
|
'/js/app/popup-factory.js',
|
|
|
|
'/js/comm/frame-ancestry-handler.js',
|
|
|
|
'/js/comm/frame-offset-forwarder.js',
|
|
|
|
'/js/app/frontend.js'
|
2020-11-22 16:19:21 +00:00
|
|
|
]);
|
|
|
|
|
2021-02-10 03:56:04 +00:00
|
|
|
const popupFactory = new PopupFactory(this._frameId);
|
2020-11-22 16:19:21 +00:00
|
|
|
popupFactory.prepare();
|
|
|
|
|
|
|
|
Object.assign(setupNestedPopupsOptions, {
|
|
|
|
depth: this._depth + 1,
|
2021-02-10 03:56:04 +00:00
|
|
|
tabId: this._tabId,
|
|
|
|
frameId: this._frameId,
|
2020-11-22 16:19:21 +00:00
|
|
|
popupFactory,
|
|
|
|
pageType: this._pageType,
|
2020-11-23 20:23:47 +00:00
|
|
|
allowRootFramePopupProxy: true,
|
2021-01-18 00:28:42 +00:00
|
|
|
childrenSupported: this._childrenSupported,
|
|
|
|
hotkeyHandler: this._hotkeyHandler
|
2020-11-22 16:19:21 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const frontend = new Frontend(setupNestedPopupsOptions);
|
|
|
|
this._frontend = frontend;
|
|
|
|
await frontend.prepare();
|
|
|
|
}
|
2020-11-22 20:29:51 +00:00
|
|
|
|
|
|
|
_copyHostSelection() {
|
2021-02-10 03:56:04 +00:00
|
|
|
if (this._contentOriginFrameId === null || window.getSelection().toString()) { return false; }
|
2021-02-28 21:51:48 +00:00
|
|
|
this._copyHostSelectionSafe();
|
2020-11-22 20:29:51 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-02-28 21:51:48 +00:00
|
|
|
async _copyHostSelectionSafe() {
|
|
|
|
try {
|
|
|
|
await this._copyHostSelectionInner();
|
|
|
|
} catch (e) {
|
|
|
|
// NOP
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-22 20:29:51 +00:00
|
|
|
async _copyHostSelectionInner() {
|
|
|
|
switch (this._browser) {
|
|
|
|
case 'firefox':
|
|
|
|
case 'firefox-mobile':
|
|
|
|
{
|
|
|
|
let text;
|
|
|
|
try {
|
2021-11-21 20:54:58 +00:00
|
|
|
text = await this.invokeContentOrigin('Frontend.getSelectionText');
|
2020-11-22 20:29:51 +00:00
|
|
|
} catch (e) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
this._copyText(text);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2021-11-21 20:54:58 +00:00
|
|
|
await this.invokeContentOrigin('Frontend.copySelection');
|
2020-11-22 20:29:51 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_copyText(text) {
|
|
|
|
const parent = document.body;
|
|
|
|
if (parent === null) { return; }
|
|
|
|
|
|
|
|
let textarea = this._copyTextarea;
|
|
|
|
if (textarea === null) {
|
|
|
|
textarea = document.createElement('textarea');
|
|
|
|
this._copyTextarea = textarea;
|
|
|
|
}
|
|
|
|
|
|
|
|
textarea.value = text;
|
|
|
|
parent.appendChild(textarea);
|
|
|
|
textarea.select();
|
|
|
|
document.execCommand('copy');
|
|
|
|
parent.removeChild(textarea);
|
|
|
|
}
|
|
|
|
|
2020-11-23 22:43:17 +00:00
|
|
|
_addEntryEventListeners(entry) {
|
2021-10-17 04:15:43 +00:00
|
|
|
const eventListeners = this._eventListeners;
|
|
|
|
eventListeners.addEventListener(entry, 'click', this._onEntryClickBind);
|
|
|
|
for (const node of entry.querySelectorAll('.headword-kanji-link')) {
|
|
|
|
eventListeners.addEventListener(node, 'click', this._onKanjiLookupBind);
|
|
|
|
}
|
|
|
|
for (const node of entry.querySelectorAll('.tag-label')) {
|
|
|
|
eventListeners.addEventListener(node, 'click', this._onTagClickBind);
|
|
|
|
}
|
2021-10-17 13:55:13 +00:00
|
|
|
for (const node of entry.querySelectorAll('.action-button[data-action=menu]')) {
|
|
|
|
eventListeners.addEventListener(node, 'click', this._onMenuButtonClickBind);
|
|
|
|
eventListeners.addEventListener(node, 'menuClose', this._onMenuButtonMenuCloseBind);
|
|
|
|
}
|
2020-11-24 01:31:48 +00:00
|
|
|
}
|
|
|
|
|
2021-04-04 20:22:35 +00:00
|
|
|
_updateContentTextScanner(options) {
|
2020-11-24 01:31:48 +00:00
|
|
|
if (!options.scanning.enablePopupSearch) {
|
2021-04-04 20:22:35 +00:00
|
|
|
if (this._contentTextScanner !== null) {
|
|
|
|
this._contentTextScanner.setEnabled(false);
|
2021-05-20 00:09:37 +00:00
|
|
|
this._contentTextScanner.clearSelection();
|
2020-11-24 01:31:48 +00:00
|
|
|
}
|
|
|
|
return;
|
2020-11-23 22:43:17 +00:00
|
|
|
}
|
2020-11-24 01:31:48 +00:00
|
|
|
|
2021-04-04 20:22:35 +00:00
|
|
|
if (this._contentTextScanner === null) {
|
|
|
|
this._contentTextScanner = new TextScanner({
|
2020-11-24 01:31:48 +00:00
|
|
|
node: window,
|
2021-01-12 04:13:35 +00:00
|
|
|
getSearchContext: this._getSearchContext.bind(this),
|
2020-11-24 01:31:48 +00:00
|
|
|
documentUtil: this._documentUtil,
|
|
|
|
searchTerms: true,
|
|
|
|
searchKanji: false,
|
|
|
|
searchOnClick: true,
|
|
|
|
searchOnClickOnly: true
|
|
|
|
});
|
2021-04-04 20:22:35 +00:00
|
|
|
this._contentTextScanner.includeSelector = '.click-scannable,.click-scannable *';
|
|
|
|
this._contentTextScanner.excludeSelector = '.scan-disable,.scan-disable *';
|
|
|
|
this._contentTextScanner.prepare();
|
2021-05-20 00:09:37 +00:00
|
|
|
this._contentTextScanner.on('clear', this._onContentTextScannerClear.bind(this));
|
2021-04-04 20:22:35 +00:00
|
|
|
this._contentTextScanner.on('searched', this._onContentTextScannerSearched.bind(this));
|
2020-11-24 01:31:48 +00:00
|
|
|
}
|
|
|
|
|
2021-01-10 04:10:55 +00:00
|
|
|
const {scanning: scanningOptions, sentenceParsing: sentenceParsingOptions} = options;
|
2021-04-04 20:22:35 +00:00
|
|
|
this._contentTextScanner.setOptions({
|
2020-11-24 01:31:48 +00:00
|
|
|
inputs: [{
|
|
|
|
include: 'mouse0',
|
|
|
|
exclude: '',
|
|
|
|
types: {mouse: true, pen: false, touch: false},
|
|
|
|
options: {
|
|
|
|
searchTerms: true,
|
|
|
|
searchKanji: true,
|
|
|
|
scanOnTouchMove: false,
|
2022-06-03 21:11:32 +00:00
|
|
|
scanOnTouchPress: false,
|
|
|
|
scanOnTouchRelease: false,
|
|
|
|
scanOnPenMove: false,
|
2020-11-24 01:31:48 +00:00
|
|
|
scanOnPenHover: false,
|
2022-06-03 21:11:32 +00:00
|
|
|
scanOnPenReleaseHover: false,
|
2020-11-24 01:31:48 +00:00
|
|
|
scanOnPenPress: false,
|
|
|
|
scanOnPenRelease: false,
|
2022-06-03 21:11:32 +00:00
|
|
|
preventTouchScrolling: false,
|
|
|
|
preventPenScrolling: false
|
2020-11-24 01:31:48 +00:00
|
|
|
}
|
|
|
|
}],
|
|
|
|
deepContentScan: scanningOptions.deepDomScan,
|
2022-09-21 01:06:39 +00:00
|
|
|
normalizeCssZoom: scanningOptions.normalizeCssZoom,
|
2020-11-24 01:31:48 +00:00
|
|
|
selectText: false,
|
|
|
|
delay: scanningOptions.delay,
|
|
|
|
touchInputEnabled: false,
|
|
|
|
pointerEventsEnabled: false,
|
|
|
|
scanLength: scanningOptions.length,
|
|
|
|
layoutAwareScan: scanningOptions.layoutAwareScan,
|
2021-01-10 19:43:06 +00:00
|
|
|
preventMiddleMouse: false,
|
|
|
|
sentenceParsingOptions
|
2020-11-24 01:31:48 +00:00
|
|
|
});
|
|
|
|
|
2021-04-04 20:22:35 +00:00
|
|
|
this._contentTextScanner.setEnabled(true);
|
2020-11-24 01:31:48 +00:00
|
|
|
}
|
|
|
|
|
2021-05-20 00:09:37 +00:00
|
|
|
_onContentTextScannerClear() {
|
|
|
|
this._contentTextScanner.clearSelection();
|
|
|
|
}
|
|
|
|
|
2021-04-04 20:22:35 +00:00
|
|
|
_onContentTextScannerSearched({type, dictionaryEntries, sentence, textSource, optionsContext, error}) {
|
2020-11-24 01:31:48 +00:00
|
|
|
if (error !== null && !yomichan.isExtensionUnloaded) {
|
2021-02-14 22:52:01 +00:00
|
|
|
log.error(error);
|
2020-11-24 01:31:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (type === null) { return; }
|
|
|
|
|
|
|
|
const query = textSource.text();
|
2021-01-12 23:04:26 +00:00
|
|
|
const url = window.location.href;
|
|
|
|
const documentTitle = document.title;
|
2020-11-24 01:31:48 +00:00
|
|
|
const details = {
|
|
|
|
focus: false,
|
2021-08-31 01:55:08 +00:00
|
|
|
historyMode: 'new',
|
2020-11-24 01:31:48 +00:00
|
|
|
params: {
|
|
|
|
type,
|
|
|
|
query,
|
|
|
|
wildcards: 'off'
|
|
|
|
},
|
|
|
|
state: {
|
|
|
|
focusEntry: 0,
|
2021-01-12 23:04:26 +00:00
|
|
|
optionsContext,
|
|
|
|
url,
|
2020-11-24 01:31:48 +00:00
|
|
|
sentence,
|
2021-01-12 23:04:26 +00:00
|
|
|
documentTitle
|
2020-11-24 01:31:48 +00:00
|
|
|
},
|
|
|
|
content: {
|
2021-04-04 20:22:35 +00:00
|
|
|
dictionaryEntries,
|
2021-02-10 03:56:04 +00:00
|
|
|
contentOrigin: this.getContentOrigin()
|
2020-11-24 01:31:48 +00:00
|
|
|
}
|
|
|
|
};
|
2021-05-20 00:09:37 +00:00
|
|
|
this._contentTextScanner.clearSelection();
|
2020-11-24 01:31:48 +00:00
|
|
|
this.setContent(details);
|
2020-11-23 22:43:17 +00:00
|
|
|
}
|
2020-12-09 01:31:02 +00:00
|
|
|
|
2021-01-12 04:13:35 +00:00
|
|
|
_getSearchContext() {
|
|
|
|
return {optionsContext: this.getOptionsContext()};
|
|
|
|
}
|
|
|
|
|
2021-01-15 01:56:18 +00:00
|
|
|
_updateHotkeys(options) {
|
2021-01-17 22:05:06 +00:00
|
|
|
this._hotkeyHandler.setHotkeys(this._pageType, options.inputs.hotkeys);
|
2021-01-15 01:56:18 +00:00
|
|
|
}
|
2021-01-16 01:19:56 +00:00
|
|
|
|
|
|
|
async _closeTab() {
|
|
|
|
const tab = await new Promise((resolve, reject) => {
|
|
|
|
chrome.tabs.getCurrent((result) => {
|
|
|
|
const e = chrome.runtime.lastError;
|
|
|
|
if (e) {
|
|
|
|
reject(new Error(e.message));
|
|
|
|
} else {
|
|
|
|
resolve(result);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
const tabId = tab.id;
|
|
|
|
await new Promise((resolve, reject) => {
|
|
|
|
chrome.tabs.remove(tabId, () => {
|
|
|
|
const e = chrome.runtime.lastError;
|
|
|
|
if (e) {
|
|
|
|
reject(new Error(e.message));
|
|
|
|
} else {
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2021-01-24 02:13:01 +00:00
|
|
|
|
|
|
|
_onHotkeyClose() {
|
|
|
|
if (this._closeSinglePopupMenu()) { return; }
|
|
|
|
this.close();
|
|
|
|
}
|
|
|
|
|
2021-03-28 02:30:45 +00:00
|
|
|
_onHotkeyActionMoveRelative(sign, argument) {
|
|
|
|
let count = Number.parseInt(argument, 10);
|
|
|
|
if (!Number.isFinite(count)) { count = 1; }
|
|
|
|
count = Math.max(0, Math.floor(count));
|
2021-09-30 23:31:45 +00:00
|
|
|
this._focusEntry(this._index + count * sign, 0, true);
|
2021-03-28 02:30:45 +00:00
|
|
|
}
|
|
|
|
|
2021-01-24 02:13:01 +00:00
|
|
|
_closeAllPopupMenus() {
|
|
|
|
for (const popupMenu of PopupMenu.openMenus) {
|
|
|
|
popupMenu.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_closeSinglePopupMenu() {
|
|
|
|
for (const popupMenu of PopupMenu.openMenus) {
|
|
|
|
popupMenu.close();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2021-02-12 03:57:38 +00:00
|
|
|
|
2021-04-04 20:22:35 +00:00
|
|
|
async _logDictionaryEntryData(index) {
|
|
|
|
if (index < 0 || index >= this._dictionaryEntries.length) { return; }
|
|
|
|
const dictionaryEntry = this._dictionaryEntries[index];
|
2021-06-26 18:10:18 +00:00
|
|
|
const result = {dictionaryEntry};
|
|
|
|
|
2021-10-03 20:46:15 +00:00
|
|
|
const promises = [];
|
|
|
|
this.trigger('logDictionaryEntryData', {dictionaryEntry, promises});
|
|
|
|
if (promises.length > 0) {
|
|
|
|
for (const result2 of await Promise.all(promises)) {
|
|
|
|
Object.assign(result, result2);
|
|
|
|
}
|
|
|
|
}
|
2021-06-26 18:10:18 +00:00
|
|
|
|
2021-04-01 00:07:11 +00:00
|
|
|
console.log(result);
|
|
|
|
}
|
2021-10-03 20:46:15 +00:00
|
|
|
|
|
|
|
_triggerContentClear() {
|
|
|
|
this.trigger('contentClear', {});
|
|
|
|
}
|
|
|
|
|
|
|
|
_triggerContentUpdateStart() {
|
2021-10-09 21:06:51 +00:00
|
|
|
this.trigger('contentUpdateStart', {type: this._contentType, query: this._query});
|
2021-10-03 20:46:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_triggerContentUpdateEntry(dictionaryEntry, element, index) {
|
|
|
|
this.trigger('contentUpdateEntry', {dictionaryEntry, element, index});
|
|
|
|
}
|
|
|
|
|
|
|
|
_triggerContentUpdateComplete() {
|
|
|
|
this.trigger('contentUpdateComplete', {type: this._contentType});
|
|
|
|
}
|
2017-03-05 01:30:10 +00:00
|
|
|
}
|