2017-03-05 01:30:10 +00:00
|
|
|
/*
|
2021-01-01 19:50:41 +00:00
|
|
|
* Copyright (C) 2017-2021 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
|
2020-09-10 22:03:46 +00:00
|
|
|
* AnkiNoteBuilder
|
2021-01-18 05:16:40 +00:00
|
|
|
* DisplayAudio
|
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
|
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
|
2020-04-11 18:23:49 +00:00
|
|
|
* MediaLoader
|
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
|
2020-11-24 01:31:48 +00:00
|
|
|
* TextScanner
|
2020-03-11 02:30:36 +00:00
|
|
|
* WindowScroll
|
2020-05-24 17:30:40 +00:00
|
|
|
* api
|
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 {
|
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;
|
2020-11-08 17:34:23 +00:00
|
|
|
this._container = document.querySelector('#definitions');
|
2020-07-03 16:02:21 +00:00
|
|
|
this._definitions = [];
|
2021-01-18 05:16:40 +00:00
|
|
|
this._definitionNodes = [];
|
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;
|
|
|
|
this._mediaLoader = new MediaLoader();
|
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,
|
2021-01-18 20:23:49 +00:00
|
|
|
mediaLoader: this._mediaLoader,
|
|
|
|
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 = '';
|
|
|
|
this._rawQuery = '';
|
2020-07-26 23:29:12 +00:00
|
|
|
this._fullQuery = '';
|
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),
|
2020-08-09 17:27:21 +00:00
|
|
|
documentUtil: this._documentUtil
|
2020-07-26 23:29:12 +00:00
|
|
|
});
|
2021-01-11 23:37:07 +00:00
|
|
|
this._ankiFieldTemplates = null;
|
|
|
|
this._ankiFieldTemplatesDefault = null;
|
2021-01-11 00:28:50 +00:00
|
|
|
this._ankiNoteBuilder = new AnkiNoteBuilder(true);
|
2020-11-08 21:48:15 +00:00
|
|
|
this._updateAdderButtonsPromise = Promise.resolve();
|
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');
|
|
|
|
this._windowScroll = new WindowScroll(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;
|
2020-11-24 01:31:48 +00:00
|
|
|
this._definitionTextScanner = null;
|
2020-12-09 01:31:02 +00:00
|
|
|
this._frameResizeToken = null;
|
|
|
|
this._frameResizeHandle = document.querySelector('#frame-resizer-handle');
|
|
|
|
this._frameResizeStartSize = null;
|
|
|
|
this._frameResizeStartOffset = null;
|
|
|
|
this._frameResizeEventListeners = new EventListenerCollection();
|
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-01-18 05:16:40 +00:00
|
|
|
this._displayAudio = new DisplayAudio(this);
|
2021-01-30 17:33:29 +00:00
|
|
|
this._ankiNoteNotification = null;
|
|
|
|
this._ankiNoteNotificationEventListeners = null;
|
2021-02-12 03:57:38 +00:00
|
|
|
this._queryPostProcessor = null;
|
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(); }],
|
2020-11-22 20:29:51 +00:00
|
|
|
['nextEntry', () => { this._focusEntry(this._index + 1, true); }],
|
|
|
|
['nextEntry3', () => { this._focusEntry(this._index + 3, true); }],
|
|
|
|
['previousEntry', () => { this._focusEntry(this._index - 1, true); }],
|
|
|
|
['previousEntry3', () => { this._focusEntry(this._index - 3, true); }],
|
|
|
|
['lastEntry', () => { this._focusEntry(this._definitions.length - 1, true); }],
|
|
|
|
['firstEntry', () => { this._focusEntry(0, true); }],
|
|
|
|
['historyBackward', () => { this._sourceTermView(); }],
|
|
|
|
['historyForward', () => { this._nextTermView(); }],
|
2021-01-11 23:37:07 +00:00
|
|
|
['addNoteKanji', () => { this._tryAddAnkiNoteForSelectedDefinition('kanji'); }],
|
|
|
|
['addNoteTermKanji', () => { this._tryAddAnkiNoteForSelectedDefinition('term-kanji'); }],
|
|
|
|
['addNoteTermKana', () => { this._tryAddAnkiNoteForSelectedDefinition('term-kana'); }],
|
|
|
|
['viewNote', () => { this._tryViewAnkiNoteForSelectedDefinition(); }],
|
2020-11-22 20:29:51 +00:00
|
|
|
['playAudio', () => { this._playAudioCurrent(); }],
|
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([
|
2020-07-19 03:47:02 +00:00
|
|
|
['setOptionsContext', {async: false, handler: this._onMessageSetOptionsContext.bind(this)}],
|
|
|
|
['setContent', {async: false, handler: this._onMessageSetContent.bind(this)}],
|
|
|
|
['clearAutoPlayTimer', {async: false, handler: this._onMessageClearAutoPlayTimer.bind(this)}],
|
2020-11-22 16:19:21 +00:00
|
|
|
['setCustomCss', {async: false, handler: this._onMessageSetCustomCss.bind(this)}],
|
|
|
|
['setContentScale', {async: false, handler: this._onMessageSetContentScale.bind(this)}],
|
|
|
|
['configure', {async: true, handler: this._onMessageConfigure.bind(this)}]
|
2020-07-19 03:47:02 +00:00
|
|
|
]);
|
2020-11-22 20:29:51 +00:00
|
|
|
this.registerWindowMessageHandlers([
|
|
|
|
['extensionUnloaded', {async: false, handler: this._onMessageExtensionUnloaded.bind(this)}]
|
|
|
|
]);
|
2020-07-19 03:47:02 +00:00
|
|
|
}
|
|
|
|
|
2020-12-30 04:38:44 +00:00
|
|
|
get displayGenerator() {
|
|
|
|
return this._displayGenerator;
|
|
|
|
}
|
|
|
|
|
2020-07-19 03:47:02 +00:00
|
|
|
get autoPlayAudioDelay() {
|
2021-01-18 05:16:40 +00:00
|
|
|
return this._displayAudio.autoPlayAudioDelay;
|
2020-07-19 03:47:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
set autoPlayAudioDelay(value) {
|
2021-01-18 05:16:40 +00:00
|
|
|
this._displayAudio.autoPlayAudioDelay = value;
|
2017-03-05 01:30:10 +00:00
|
|
|
}
|
|
|
|
|
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-01-18 05:16:40 +00:00
|
|
|
get definitions() {
|
|
|
|
return this._definitions;
|
|
|
|
}
|
|
|
|
|
|
|
|
get definitionNodes() {
|
|
|
|
return this._definitionNodes;
|
|
|
|
}
|
|
|
|
|
|
|
|
get progressIndicatorVisible() {
|
|
|
|
return this._progressIndicatorVisible;
|
|
|
|
}
|
|
|
|
|
2021-02-10 03:56:04 +00:00
|
|
|
get tabId() {
|
|
|
|
return this._tabId;
|
|
|
|
}
|
|
|
|
|
|
|
|
get frameId() {
|
|
|
|
return this._frameId;
|
|
|
|
}
|
|
|
|
|
2020-03-13 21:23:08 +00:00
|
|
|
async prepare() {
|
2020-11-22 20:29:51 +00:00
|
|
|
// State setup
|
|
|
|
const {documentElement} = document;
|
|
|
|
const {browser} = await api.getEnvironmentInfo();
|
|
|
|
this._browser = browser;
|
|
|
|
|
|
|
|
// 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();
|
2021-01-18 05:16:40 +00:00
|
|
|
this._displayAudio.prepare();
|
2020-11-08 17:35:32 +00:00
|
|
|
this._queryParser.prepare();
|
2020-07-26 20:51:54 +00:00
|
|
|
this._history.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));
|
2020-07-19 03:47:02 +00:00
|
|
|
api.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-12-09 01:31:02 +00:00
|
|
|
if (this._frameResizeHandle !== null) {
|
|
|
|
this._frameResizeHandle.addEventListener('mousedown', this._onFrameResizerMouseDown.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; }
|
|
|
|
yomichan.logError(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() {
|
2020-08-09 17:19:42 +00:00
|
|
|
const options = await api.optionsGet(this.getOptionsContext());
|
2021-01-11 23:37:07 +00:00
|
|
|
const templates = await this._getAnkiFieldTemplates(options);
|
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-11 23:37:07 +00:00
|
|
|
this._ankiFieldTemplates = templates;
|
2020-08-09 17:19:42 +00:00
|
|
|
|
2021-01-15 01:56:18 +00:00
|
|
|
this._updateHotkeys(options);
|
2020-08-09 17:19:42 +00:00
|
|
|
this._updateDocumentOptions(options);
|
|
|
|
this._updateTheme(options.general.popupTheme);
|
|
|
|
this.setCustomCss(options.general.customPopupCss);
|
2021-01-18 05:16:40 +00:00
|
|
|
this._displayAudio.updateOptions(options);
|
2021-01-18 20:23:49 +00:00
|
|
|
this._hotkeyHelpController.setOptions(options);
|
|
|
|
this._displayGenerator.updateHotkeys();
|
|
|
|
this._hotkeyHelpController.setupNode(document.documentElement);
|
2020-08-09 17:19:42 +00:00
|
|
|
|
|
|
|
this._queryParser.setOptions({
|
|
|
|
selectedParser: options.parsing.selectedParser,
|
|
|
|
termSpacing: options.parsing.termSpacing,
|
|
|
|
scanning: {
|
2021-01-10 04:10:55 +00:00
|
|
|
inputs: scanningOptions.inputs,
|
|
|
|
deepContentScan: scanningOptions.deepDomScan,
|
|
|
|
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,
|
|
|
|
sentenceParsingOptions
|
2020-08-09 17:19:42 +00:00
|
|
|
}
|
|
|
|
});
|
2020-11-22 16:19:21 +00:00
|
|
|
|
|
|
|
this._updateNestedFrontend(options);
|
2020-11-24 01:31:48 +00:00
|
|
|
this._updateDefinitionTextScanner(options);
|
2021-01-26 03:18:31 +00:00
|
|
|
|
|
|
|
this.trigger('optionsUpdated', {options});
|
2020-07-03 19:58:29 +00:00
|
|
|
}
|
|
|
|
|
2020-07-19 03:47:02 +00:00
|
|
|
clearAutoPlayTimer() {
|
2021-01-18 05:16:40 +00:00
|
|
|
this._displayAudio.clearAutoPlayTimer();
|
2020-07-03 19:58:29 +00:00
|
|
|
}
|
|
|
|
|
2020-07-26 20:51:54 +00:00
|
|
|
setContent(details) {
|
|
|
|
const {focus, history, params, state, content} = details;
|
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
|
|
|
|
2020-07-26 20:51:54 +00:00
|
|
|
if (history && this._historyHasChanged) {
|
2020-11-24 01:31:48 +00:00
|
|
|
this._updateHistoryState();
|
2020-07-26 20:51:54 +00:00
|
|
|
this._history.pushState(state, content, url);
|
|
|
|
} else {
|
|
|
|
this._history.clear();
|
|
|
|
this._history.replaceState(state, content, url);
|
|
|
|
}
|
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-07-19 03:47:02 +00:00
|
|
|
authenticateMessageData(data) {
|
2020-11-22 20:29:51 +00:00
|
|
|
if (this._frameEndpoint === null) {
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
if (!this._frameEndpoint.authenticate(data)) {
|
|
|
|
throw new Error('Invalid authentication');
|
|
|
|
}
|
|
|
|
return data.data;
|
2020-07-19 03:47:02 +00:00
|
|
|
}
|
|
|
|
|
2021-02-12 03:57:38 +00:00
|
|
|
setQueryPostProcessor(func) {
|
|
|
|
this._queryPostProcessor = func;
|
2020-07-26 23:29:12 +00:00
|
|
|
}
|
|
|
|
|
2020-11-15 19:12:48 +00:00
|
|
|
close() {
|
2021-01-16 01:19:56 +00:00
|
|
|
switch (this._pageType) {
|
|
|
|
case 'popup':
|
2021-02-10 03:56:04 +00:00
|
|
|
this._invokeContentOrigin('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
|
|
|
}
|
|
|
|
|
2020-12-22 00:19:59 +00:00
|
|
|
searchLast() {
|
|
|
|
const type = this._contentType;
|
|
|
|
if (type === 'clear') { return; }
|
|
|
|
const query = this._rawQuery;
|
|
|
|
const state = (
|
|
|
|
this._historyHasState() ?
|
|
|
|
clone(this._history.state) :
|
|
|
|
{
|
|
|
|
focusEntry: 0,
|
2021-01-12 23:04:26 +00:00
|
|
|
optionsContext: this._optionsContext,
|
|
|
|
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
|
|
|
}
|
|
|
|
);
|
|
|
|
const details = {
|
|
|
|
focus: false,
|
|
|
|
history: false,
|
|
|
|
params: this._createSearchParams(type, query, false),
|
|
|
|
state,
|
|
|
|
content: {
|
2021-02-10 03:56:04 +00:00
|
|
|
definitions: null,
|
|
|
|
contentOrigin: this.getContentOrigin()
|
2020-12-22 00:19:59 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
this.setContent(details);
|
|
|
|
}
|
|
|
|
|
2020-07-19 03:47:02 +00:00
|
|
|
// Message handlers
|
|
|
|
|
2020-08-09 17:11:41 +00:00
|
|
|
_onDirectMessage(data) {
|
2020-07-19 03:47:02 +00:00
|
|
|
data = this.authenticateMessageData(data);
|
|
|
|
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 {
|
|
|
|
data = this.authenticateMessageData(data);
|
|
|
|
} catch (e) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const {action, params} = data;
|
|
|
|
const messageHandler = this._windowMessageHandlers.get(action);
|
|
|
|
if (typeof messageHandler === 'undefined') { return; }
|
|
|
|
|
|
|
|
const callback = () => {}; // NOP
|
|
|
|
yomichan.invokeMessageHandler(messageHandler, params, callback);
|
|
|
|
}
|
|
|
|
|
2020-07-19 03:47:02 +00:00
|
|
|
_onMessageSetOptionsContext({optionsContext}) {
|
|
|
|
this.setOptionsContext(optionsContext);
|
2020-12-30 04:38:44 +00:00
|
|
|
this.searchLast();
|
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
|
|
|
}
|
|
|
|
|
|
|
|
_onMessageClearAutoPlayTimer() {
|
|
|
|
this.clearAutoPlayTimer();
|
|
|
|
}
|
|
|
|
|
|
|
|
_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);
|
|
|
|
}
|
|
|
|
|
2020-11-22 20:29:51 +00:00
|
|
|
_onMessageExtensionUnloaded() {
|
|
|
|
if (yomichan.isExtensionUnloaded) { return; }
|
|
|
|
yomichan.triggerExtensionUnloaded();
|
|
|
|
}
|
|
|
|
|
2020-07-03 19:58:29 +00:00
|
|
|
// Private
|
|
|
|
|
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();
|
|
|
|
this._mediaLoader.unloadAll();
|
2021-01-18 05:16:40 +00:00
|
|
|
this._displayAudio.cleanupEntries();
|
2020-12-18 14:43:54 +00:00
|
|
|
this._hideTagNotification(false);
|
2021-01-30 17:33:29 +00:00
|
|
|
this._hideAnkiNoteErrors(false);
|
2021-01-18 05:16:40 +00:00
|
|
|
this._definitions = [];
|
|
|
|
this._definitionNodes = [];
|
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');
|
|
|
|
if (type === null) { type = 'terms'; }
|
|
|
|
|
2020-08-01 20:22:00 +00:00
|
|
|
const fullVisible = urlSearchParams.get('full-visible');
|
|
|
|
this._queryParserVisibleOverride = (fullVisible === null ? null : (fullVisible !== 'false'));
|
2020-11-19 01:15:30 +00:00
|
|
|
this._updateQueryParser();
|
2020-08-01 20:22:00 +00:00
|
|
|
|
2020-11-24 16:54:08 +00:00
|
|
|
let clear = true;
|
2020-07-26 20:51:54 +00:00
|
|
|
this._historyHasChanged = true;
|
2020-08-03 01:51:51 +00:00
|
|
|
this._contentType = type;
|
2020-12-22 00:19:59 +00:00
|
|
|
this._query = '';
|
|
|
|
this._rawQuery = '';
|
2020-11-24 16:54:08 +00:00
|
|
|
const eventArgs = {type, urlSearchParams, token};
|
|
|
|
|
|
|
|
// Set content
|
2020-07-26 20:51:54 +00:00
|
|
|
switch (type) {
|
|
|
|
case 'terms':
|
|
|
|
case 'kanji':
|
|
|
|
{
|
2020-11-24 16:54:08 +00:00
|
|
|
let query = urlSearchParams.get('query');
|
|
|
|
if (!query) { break; }
|
|
|
|
|
2020-12-22 00:19:59 +00:00
|
|
|
this._query = query;
|
2020-11-24 16:54:08 +00:00
|
|
|
clear = false;
|
2020-07-26 20:51:54 +00:00
|
|
|
const isTerms = (type === 'terms');
|
2021-02-12 03:57:38 +00:00
|
|
|
query = this._postProcessQuery(query);
|
2020-12-22 00:19:59 +00:00
|
|
|
this._rawQuery = query;
|
2020-11-24 16:54:08 +00:00
|
|
|
let queryFull = urlSearchParams.get('full');
|
2021-02-12 03:57:38 +00:00
|
|
|
queryFull = (queryFull !== null ? this._postProcessQuery(queryFull) : query);
|
2020-11-24 16:54:08 +00:00
|
|
|
const wildcardsEnabled = (urlSearchParams.get('wildcards') !== 'off');
|
2021-01-26 03:05:06 +00:00
|
|
|
const lookup = (urlSearchParams.get('lookup') !== 'false');
|
|
|
|
await this._setContentTermsOrKanji(token, isTerms, query, queryFull, lookup, wildcardsEnabled, eventArgs);
|
2020-07-26 20:51:54 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'unloaded':
|
|
|
|
{
|
2020-11-24 16:54:08 +00:00
|
|
|
clear = false;
|
2020-07-26 20:51:54 +00:00
|
|
|
const {content} = this._history;
|
|
|
|
eventArgs.content = content;
|
|
|
|
this.trigger('contentUpdating', eventArgs);
|
|
|
|
this._setContentExtensionUnloaded();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-11-24 16:54:08 +00:00
|
|
|
// Clear
|
|
|
|
if (clear) {
|
|
|
|
type = 'clear';
|
|
|
|
this._contentType = type;
|
|
|
|
const {content} = this._history;
|
|
|
|
eventArgs.type = type;
|
|
|
|
eventArgs.content = content;
|
|
|
|
this.trigger('contentUpdating', eventArgs);
|
|
|
|
this._clearContent();
|
2020-07-26 20:51:54 +00:00
|
|
|
}
|
|
|
|
|
2020-11-24 16:54:08 +00:00
|
|
|
const stale = (this._setContentToken !== token);
|
2020-08-01 20:22:00 +00:00
|
|
|
eventArgs.stale = stale;
|
2020-07-26 20:51:54 +00:00
|
|
|
this.trigger('contentUpdated', eventArgs);
|
|
|
|
} catch (e) {
|
|
|
|
this.onError(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-17 16:43:05 +00:00
|
|
|
_onQueryParserSearch({type, definitions, sentence, inputInfo: {eventType}, textSource, optionsContext}) {
|
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;
|
|
|
|
const history = (
|
2021-01-17 16:43:05 +00:00
|
|
|
eventType === 'click' ||
|
2020-12-22 00:19:59 +00:00
|
|
|
!isObject(historyState) ||
|
|
|
|
historyState.cause !== 'queryParser'
|
|
|
|
);
|
2020-07-26 23:29:12 +00:00
|
|
|
const details = {
|
|
|
|
focus: false,
|
2020-09-12 02:46:41 +00:00
|
|
|
history,
|
2020-07-26 23:29:12 +00:00
|
|
|
params: this._createSearchParams(type, query, false),
|
|
|
|
state: {
|
|
|
|
sentence,
|
2020-12-22 00:19:59 +00:00
|
|
|
optionsContext,
|
|
|
|
cause: 'queryParser'
|
2020-07-26 23:29:12 +00:00
|
|
|
},
|
|
|
|
content: {
|
2021-02-10 03:56:04 +00:00
|
|
|
definitions,
|
|
|
|
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,
|
|
|
|
history: false,
|
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;
|
|
|
|
const definitions = await api.kanjiFind(query, optionsContext);
|
2020-07-26 23:29:12 +00:00
|
|
|
const details = {
|
2020-07-25 13:58:06 +00:00
|
|
|
focus: false,
|
|
|
|
history: true,
|
2020-07-26 23:29:12 +00:00
|
|
|
params: this._createSearchParams('kanji', query, false),
|
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-02-10 03:56:04 +00:00
|
|
|
definitions,
|
|
|
|
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
|
|
|
_onNoteAdd(e) {
|
2017-03-19 22:45:30 +00:00
|
|
|
e.preventDefault();
|
2019-09-15 18:06:27 +00:00
|
|
|
const link = e.currentTarget;
|
2020-11-22 02:17:39 +00:00
|
|
|
const index = this._getClosestDefinitionIndex(link);
|
2021-01-11 23:37:07 +00:00
|
|
|
this._addAnkiNote(index, link.dataset.mode);
|
2017-03-05 01:30:10 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
_onNoteView(e) {
|
2017-07-02 01:27:49 +00:00
|
|
|
e.preventDefault();
|
2019-09-15 18:06:27 +00:00
|
|
|
const link = e.currentTarget;
|
2020-05-24 17:30:40 +00:00
|
|
|
api.noteView(link.dataset.noteId);
|
2017-07-02 01:27:49 +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) {
|
2020-07-26 22:49:38 +00:00
|
|
|
this._focusEntry(this._index + (e.deltaY > 0 ? 1 : -1), 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;
|
2020-11-22 02:17:39 +00:00
|
|
|
const index = this._getClosestDefinitionIndex(link);
|
2020-11-14 00:48:22 +00:00
|
|
|
if (index < 0 || index >= this._definitions.length) { return; }
|
|
|
|
const definition = this._definitions[index];
|
|
|
|
console.log(definition);
|
|
|
|
}
|
|
|
|
|
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-01-09 21:02:03 +00:00
|
|
|
_showTagNotification(tagNode) {
|
2020-12-18 14:43:54 +00:00
|
|
|
if (this._tagNotification === null) {
|
|
|
|
const node = this._displayGenerator.createEmptyFooterNotification();
|
2020-12-31 19:21:50 +00:00
|
|
|
node.classList.add('click-scannable');
|
2021-01-30 17:33:29 +00:00
|
|
|
this._tagNotification = new DisplayNotification(this._footerNotificationContainer, node);
|
2020-12-18 14:43:54 +00:00
|
|
|
}
|
|
|
|
|
2021-01-09 21:02:03 +00:00
|
|
|
const content = this._displayGenerator.createTagFooterNotificationDetails(tagNode);
|
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}`;
|
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}`;
|
2020-01-26 19:00:19 +00:00
|
|
|
data.enableSearchTags = `${options.scanning.enableSearchTags}`;
|
2020-03-01 19:38:16 +00:00
|
|
|
data.showPitchAccentDownstepNotation = `${options.general.showPitchAccentDownstepNotation}`;
|
|
|
|
data.showPitchAccentPositionNotation = `${options.general.showPitchAccentPositionNotation}`;
|
|
|
|
data.showPitchAccentGraph = `${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
|
|
|
}
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
_updateTheme(themeName) {
|
2020-11-26 04:22:05 +00:00
|
|
|
document.documentElement.dataset.theme = themeName;
|
2019-10-12 18:20:54 +00:00
|
|
|
}
|
|
|
|
|
2020-11-24 16:54:08 +00:00
|
|
|
async _findDefinitions(isTerms, source, wildcardsEnabled, optionsContext) {
|
2020-07-26 20:51:54 +00:00
|
|
|
if (isTerms) {
|
|
|
|
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]) {
|
|
|
|
findDetails.wildcard = 'prefix';
|
|
|
|
} else if (match[3]) {
|
|
|
|
findDetails.wildcard = 'suffix';
|
|
|
|
}
|
|
|
|
source = match[2];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const {definitions} = await api.termsFind(source, findDetails, optionsContext);
|
|
|
|
return definitions;
|
|
|
|
} else {
|
|
|
|
const definitions = await api.kanjiFind(source, optionsContext);
|
|
|
|
return definitions;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-26 03:05:06 +00:00
|
|
|
async _setContentTermsOrKanji(token, isTerms, query, queryFull, lookup, wildcardsEnabled, eventArgs) {
|
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-01-12 23:04:26 +00:00
|
|
|
let {
|
|
|
|
focusEntry=null,
|
|
|
|
scrollX=null,
|
|
|
|
scrollY=null,
|
2021-01-13 03:47:07 +00:00
|
|
|
optionsContext=null
|
2021-01-12 23:04:26 +00:00
|
|
|
} = 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;
|
|
|
|
}
|
|
|
|
|
2020-11-24 16:54:08 +00:00
|
|
|
this._setFullQuery(queryFull);
|
|
|
|
this._setTitleText(query);
|
2020-08-01 20:22:00 +00:00
|
|
|
|
|
|
|
let {definitions} = content;
|
|
|
|
if (!Array.isArray(definitions)) {
|
2021-01-26 03:05:06 +00:00
|
|
|
definitions = lookup ? await this._findDefinitions(isTerms, query, wildcardsEnabled, optionsContext) : [];
|
2020-11-24 16:54:08 +00:00
|
|
|
if (this._setContentToken !== token) { return; }
|
2020-08-01 20:22:00 +00:00
|
|
|
content.definitions = definitions;
|
|
|
|
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;
|
|
|
|
if (this._pageType === 'popup') {
|
|
|
|
this._hotkeyHandler.forwardFrameId = (tabId === this._tabId ? frameId : null);
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-11-24 16:54:08 +00:00
|
|
|
eventArgs.source = query;
|
2020-08-01 20:22:00 +00:00
|
|
|
eventArgs.content = content;
|
|
|
|
this.trigger('contentUpdating', eventArgs);
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
this._definitions = definitions;
|
2017-08-13 23:11:51 +00:00
|
|
|
|
2020-07-26 20:51:54 +00:00
|
|
|
this._updateNavigation(this._history.hasPrevious(), this._history.hasNext());
|
2021-01-26 03:05:06 +00:00
|
|
|
this._setNoContentVisible(definitions.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
|
|
|
|
2019-12-27 23:15:16 +00:00
|
|
|
for (let i = 0, ii = definitions.length; i < ii; ++i) {
|
|
|
|
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
|
|
|
|
2020-11-23 22:43:17 +00:00
|
|
|
const definition = definitions[i];
|
2020-07-25 01:12:13 +00:00
|
|
|
const entry = (
|
|
|
|
isTerms ?
|
2020-11-23 22:43:17 +00:00
|
|
|
this._displayGenerator.createTermEntry(definition) :
|
|
|
|
this._displayGenerator.createKanjiEntry(definition)
|
2020-07-25 01:12:13 +00:00
|
|
|
);
|
2020-11-22 02:17:39 +00:00
|
|
|
entry.dataset.index = `${i}`;
|
2021-01-18 05:16:40 +00:00
|
|
|
this._definitionNodes.push(entry);
|
2020-11-23 22:43:17 +00:00
|
|
|
this._addEntryEventListeners(entry);
|
2021-01-18 05:16:40 +00:00
|
|
|
this._displayAudio.setupEntry(entry, i);
|
2019-12-27 23:15:16 +00:00
|
|
|
container.appendChild(entry);
|
2020-12-19 21:00:47 +00:00
|
|
|
if (focusEntry === i) {
|
|
|
|
this._focusEntry(i, false);
|
|
|
|
}
|
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-01-18 05:16:40 +00:00
|
|
|
this._displayAudio.setupEntriesComplete();
|
2017-08-13 23:11:51 +00:00
|
|
|
|
2020-11-08 21:48:15 +00:00
|
|
|
this._updateAdderButtons(token, isTerms, definitions);
|
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);
|
2020-08-01 20:22:00 +00:00
|
|
|
this._setTitleText('');
|
2020-11-24 16:54:08 +00:00
|
|
|
this._setFullQuery('');
|
2019-12-25 02:45:57 +00:00
|
|
|
}
|
|
|
|
|
2020-07-26 20:51:54 +00:00
|
|
|
_clearContent() {
|
|
|
|
this._container.textContent = '';
|
2020-08-01 20:22:00 +00:00
|
|
|
this._setTitleText('');
|
2020-11-24 16:54:08 +00:00
|
|
|
this._setFullQuery('');
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-19 01:15:30 +00:00
|
|
|
_setFullQuery(text) {
|
2020-08-01 20:22:00 +00:00
|
|
|
this._fullQuery = text;
|
2020-11-19 01:15:30 +00:00
|
|
|
this._updateQueryParser();
|
|
|
|
}
|
|
|
|
|
|
|
|
_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
|
|
|
}
|
|
|
|
|
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-11-08 21:48:15 +00:00
|
|
|
async _updateAdderButtons(token, isTerms, definitions) {
|
|
|
|
await this._updateAdderButtonsPromise;
|
|
|
|
if (this._setContentToken !== token) { return; }
|
|
|
|
|
|
|
|
const {promise, resolve} = deferPromise();
|
|
|
|
try {
|
|
|
|
this._updateAdderButtonsPromise = promise;
|
|
|
|
|
|
|
|
const modes = isTerms ? ['term-kanji', 'term-kana'] : ['kanji'];
|
|
|
|
let states;
|
|
|
|
try {
|
|
|
|
if (this._options.anki.checkForDuplicates) {
|
2021-01-12 23:04:26 +00:00
|
|
|
const noteContext = this._getNoteContext();
|
2020-11-08 21:48:15 +00:00
|
|
|
states = await this._areDefinitionsAddable(definitions, modes, noteContext);
|
|
|
|
} else {
|
|
|
|
if (!await api.isAnkiConnected()) {
|
|
|
|
throw new Error('Anki not connected');
|
|
|
|
}
|
|
|
|
states = this._areDefinitionsAddableForcedValue(definitions, modes, true);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._setContentToken !== token) { return; }
|
|
|
|
|
|
|
|
this._updateAdderButtons2(states, modes);
|
|
|
|
} finally {
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_updateAdderButtons2(states, modes) {
|
2020-09-10 20:05:17 +00:00
|
|
|
for (let i = 0, ii = states.length; i < ii; ++i) {
|
|
|
|
const infos = states[i];
|
2019-12-27 23:04:39 +00:00
|
|
|
let noteId = null;
|
2020-09-10 20:05:17 +00:00
|
|
|
for (let j = 0, jj = infos.length; j < jj; ++j) {
|
|
|
|
const {canAdd, noteIds} = infos[j];
|
|
|
|
const mode = modes[j];
|
2020-07-03 16:02:21 +00:00
|
|
|
const button = this._adderButtonFind(i, mode);
|
2019-12-27 23:04:39 +00:00
|
|
|
if (button === null) {
|
|
|
|
continue;
|
2017-08-13 23:11:51 +00:00
|
|
|
}
|
2019-12-27 23:04:39 +00:00
|
|
|
|
2020-09-10 20:05:17 +00:00
|
|
|
if (Array.isArray(noteIds) && noteIds.length > 0) {
|
|
|
|
noteId = noteIds[0];
|
2019-10-10 00:31:09 +00:00
|
|
|
}
|
2020-11-25 17:39:09 +00:00
|
|
|
button.disabled = !canAdd;
|
|
|
|
button.hidden = false;
|
2019-12-27 23:04:39 +00:00
|
|
|
}
|
|
|
|
if (noteId !== null) {
|
2020-07-03 16:02:21 +00:00
|
|
|
this._viewerButtonShow(i, noteId);
|
2017-08-13 23:11:51 +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
|
|
|
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
2020-07-26 22:49:38 +00:00
|
|
|
_focusEntry(index, smooth) {
|
2020-11-14 23:12:06 +00:00
|
|
|
index = Math.max(Math.min(index, this._definitions.length - 1), 0);
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
const entry = this._entrySetCurrent(index);
|
2020-07-26 22:49:38 +00:00
|
|
|
let target = index === 0 || entry === null ? 0 : this._getElementTop(entry);
|
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) {
|
|
|
|
const offsetSign = Math.sign(offset);
|
|
|
|
if (offsetSign === 0) { return false; }
|
|
|
|
|
|
|
|
let index = this._index;
|
|
|
|
const definitionCount = this._definitions.length;
|
|
|
|
if (index < 0 || index >= definitionCount) { return false; }
|
|
|
|
|
|
|
|
const {dictionary} = this._definitions[index];
|
|
|
|
for (let indexNext = index + offsetSign; indexNext >= 0 && indexNext < definitionCount; indexNext += offsetSign) {
|
|
|
|
const {dictionaryNames} = this._definitions[indexNext];
|
|
|
|
if (dictionaryNames.length > 1 || !dictionaryNames.includes(dictionary)) {
|
|
|
|
offset -= offsetSign;
|
|
|
|
if (Math.sign(offsetSign) !== offset) {
|
|
|
|
index = indexNext;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (index === this._index) { return false; }
|
|
|
|
|
|
|
|
this._focusEntry(index, smooth);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-01-11 23:37:07 +00:00
|
|
|
_tryAddAnkiNoteForSelectedDefinition(mode) {
|
|
|
|
this._addAnkiNote(this._index, mode);
|
2019-09-20 02:03:26 +00:00
|
|
|
}
|
|
|
|
|
2021-01-11 23:37:07 +00:00
|
|
|
_tryViewAnkiNoteForSelectedDefinition() {
|
2020-07-03 16:02:21 +00:00
|
|
|
const button = this._viewerButtonFind(this._index);
|
2020-11-25 17:39:09 +00:00
|
|
|
if (button !== null && !button.disabled) {
|
2020-05-24 17:30:40 +00:00
|
|
|
api.noteView(button.dataset.noteId);
|
2019-09-20 02:03:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-11 23:37:07 +00:00
|
|
|
async _addAnkiNote(definitionIndex, mode) {
|
2021-01-30 17:33:29 +00:00
|
|
|
if (definitionIndex < 0 || definitionIndex >= this._definitions.length) { return; }
|
2021-01-11 23:37:07 +00:00
|
|
|
const definition = this._definitions[definitionIndex];
|
|
|
|
|
|
|
|
const button = this._adderButtonFind(definitionIndex, mode);
|
2021-01-30 17:33:29 +00:00
|
|
|
if (button === null || button.disabled) { return; }
|
2021-01-11 23:37:07 +00:00
|
|
|
|
2021-01-30 17:33:29 +00:00
|
|
|
this._hideAnkiNoteErrors(true);
|
|
|
|
|
|
|
|
const errors = [];
|
2020-11-18 00:40:19 +00:00
|
|
|
const overrideToken = this._progressIndicatorVisible.setOverride(true);
|
2017-08-13 23:11:51 +00:00
|
|
|
try {
|
2021-01-15 03:42:11 +00:00
|
|
|
const {anki: {suspendNewCards}} = this._options;
|
2021-01-12 23:04:26 +00:00
|
|
|
const noteContext = this._getNoteContext();
|
2021-01-30 17:33:29 +00:00
|
|
|
const note = await this._createNote(definition, mode, noteContext, true, errors);
|
|
|
|
|
|
|
|
let noteId = null;
|
|
|
|
let addNoteOkay = false;
|
|
|
|
try {
|
|
|
|
noteId = await api.addAnkiNote(note);
|
|
|
|
addNoteOkay = true;
|
|
|
|
} catch (e) {
|
|
|
|
errors.length = 0;
|
|
|
|
errors.push(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (addNoteOkay) {
|
|
|
|
if (noteId === null) {
|
|
|
|
errors.push(new Error('Note could not be added'));
|
|
|
|
} else {
|
|
|
|
if (suspendNewCards) {
|
|
|
|
try {
|
|
|
|
await api.suspendAnkiCardsForNote(noteId);
|
|
|
|
} catch (e) {
|
|
|
|
errors.push(e);
|
|
|
|
}
|
2021-01-15 03:42:11 +00:00
|
|
|
}
|
2021-01-30 17:33:29 +00:00
|
|
|
button.disabled = true;
|
|
|
|
this._viewerButtonShow(definitionIndex, noteId);
|
2021-01-15 03:42:11 +00:00
|
|
|
}
|
2017-03-19 22:45:30 +00:00
|
|
|
}
|
2017-08-13 23:11:51 +00:00
|
|
|
} catch (e) {
|
2021-01-30 17:33:29 +00:00
|
|
|
errors.push(e);
|
2017-08-13 23:11:51 +00:00
|
|
|
} finally {
|
2020-11-18 00:40:19 +00:00
|
|
|
this._progressIndicatorVisible.clearOverride(overrideToken);
|
2017-08-13 23:11:51 +00:00
|
|
|
}
|
2021-01-30 17:33:29 +00:00
|
|
|
|
|
|
|
if (errors.length > 0) {
|
|
|
|
this._showAnkiNoteErrors(errors);
|
|
|
|
} else {
|
|
|
|
this._hideAnkiNoteErrors(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_showAnkiNoteErrors(errors) {
|
|
|
|
if (this._ankiNoteNotificationEventListeners !== null) {
|
|
|
|
this._ankiNoteNotificationEventListeners.removeAllEventListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._ankiNoteNotification === null) {
|
|
|
|
const node = this._displayGenerator.createEmptyFooterNotification();
|
|
|
|
this._ankiNoteNotification = new DisplayNotification(this._footerNotificationContainer, node);
|
|
|
|
this._ankiNoteNotificationEventListeners = new EventListenerCollection();
|
|
|
|
}
|
|
|
|
|
|
|
|
const content = this._displayGenerator.createAnkiNoteErrorsNotificationContent(errors);
|
|
|
|
for (const node of content.querySelectorAll('.anki-note-error-log-link')) {
|
|
|
|
this._ankiNoteNotificationEventListeners.addEventListener(node, 'click', () => {
|
|
|
|
console.log({ankiNoteErrors: errors});
|
|
|
|
}, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
this._ankiNoteNotification.setContent(content);
|
|
|
|
this._ankiNoteNotification.open();
|
|
|
|
}
|
|
|
|
|
|
|
|
_hideAnkiNoteErrors(animate) {
|
|
|
|
if (this._ankiNoteNotification === null) { return; }
|
|
|
|
this._ankiNoteNotification.close(animate);
|
|
|
|
this._ankiNoteNotificationEventListeners.removeAllEventListeners();
|
2017-03-19 22:45:30 +00:00
|
|
|
}
|
|
|
|
|
2020-11-22 02:17:39 +00:00
|
|
|
async _playAudioCurrent() {
|
2021-01-18 05:16:40 +00:00
|
|
|
return await this._displayAudio.playAudio(this._index, 0);
|
2020-04-17 22:00:28 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
_getEntry(index) {
|
2021-01-18 05:16:40 +00:00
|
|
|
const entries = this._definitionNodes;
|
2019-09-15 22:29:11 +00:00
|
|
|
return index >= 0 && index < entries.length ? entries[index] : null;
|
|
|
|
}
|
|
|
|
|
2020-07-26 20:51:54 +00:00
|
|
|
_getValidSentenceData(sentence) {
|
|
|
|
let {text, offset} = (isObject(sentence) ? sentence : {});
|
|
|
|
if (typeof text !== 'string') { text = ''; }
|
|
|
|
if (typeof offset !== 'number') { offset = 0; }
|
|
|
|
return {text, offset};
|
|
|
|
}
|
|
|
|
|
2020-11-22 02:17:39 +00:00
|
|
|
_getClosestDefinitionIndex(element) {
|
|
|
|
return this._getClosestIndex(element, '.entry');
|
|
|
|
}
|
|
|
|
|
|
|
|
_getClosestIndex(element, selector) {
|
|
|
|
const node = element.closest(selector);
|
|
|
|
if (node === null) { return -1; }
|
|
|
|
const index = parseInt(node.dataset.index, 10);
|
|
|
|
return Number.isFinite(index) ? index : -1;
|
2017-03-23 05:28:22 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
_adderButtonFind(index, mode) {
|
|
|
|
const entry = this._getEntry(index);
|
2019-09-15 22:29:11 +00:00
|
|
|
return entry !== null ? entry.querySelector(`.action-add-note[data-mode="${mode}"]`) : null;
|
2017-03-23 05:28:22 +00:00
|
|
|
}
|
2017-07-02 01:27:49 +00:00
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
_viewerButtonFind(index) {
|
|
|
|
const entry = this._getEntry(index);
|
2019-09-15 22:29:11 +00:00
|
|
|
return entry !== null ? entry.querySelector('.action-view-note') : null;
|
2017-07-02 01:27:49 +00:00
|
|
|
}
|
2019-08-15 23:39:58 +00:00
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
_viewerButtonShow(index, noteId) {
|
|
|
|
const viewerButton = this._viewerButtonFind(index);
|
2019-10-10 00:31:09 +00:00
|
|
|
if (viewerButton === null) {
|
|
|
|
return;
|
|
|
|
}
|
2020-11-25 17:39:09 +00:00
|
|
|
viewerButton.disabled = false;
|
|
|
|
viewerButton.hidden = false;
|
2019-10-10 00:31:09 +00:00
|
|
|
viewerButton.dataset.noteId = noteId;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2021-01-12 23:04:26 +00:00
|
|
|
_getNoteContext() {
|
|
|
|
const {state} = this._history;
|
2021-01-13 03:47:07 +00:00
|
|
|
let {documentTitle, url, sentence} = (isObject(state) ? state : {});
|
2021-01-12 23:04:26 +00:00
|
|
|
if (typeof documentTitle !== 'string') {
|
|
|
|
documentTitle = '';
|
|
|
|
}
|
2021-01-13 03:47:07 +00:00
|
|
|
if (typeof url !== 'string') {
|
|
|
|
url = window.location.href;
|
|
|
|
}
|
|
|
|
sentence = this._getValidSentenceData(sentence);
|
2020-03-15 21:32:31 +00:00
|
|
|
return {
|
2021-01-13 03:47:07 +00:00
|
|
|
url,
|
|
|
|
sentence,
|
|
|
|
documentTitle
|
2020-03-15 21:32:31 +00:00
|
|
|
};
|
2020-03-15 21:13:00 +00:00
|
|
|
}
|
2020-07-09 00:02:20 +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
|
|
|
|
|
|
|
_createSearchParams(type, query, wildcards) {
|
|
|
|
const params = {};
|
|
|
|
if (query.length < this._fullQuery.length) {
|
|
|
|
params.full = this._fullQuery;
|
|
|
|
}
|
|
|
|
params.query = query;
|
|
|
|
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
|
|
|
|
2021-01-11 23:37:07 +00:00
|
|
|
async _getAnkiFieldTemplates(options) {
|
2020-09-10 22:03:46 +00:00
|
|
|
let templates = options.anki.fieldTemplates;
|
|
|
|
if (typeof templates === 'string') { return templates; }
|
|
|
|
|
2021-01-11 23:37:07 +00:00
|
|
|
templates = this._ankiFieldTemplatesDefault;
|
2020-09-10 22:03:46 +00:00
|
|
|
if (typeof templates === 'string') { return templates; }
|
|
|
|
|
2021-01-11 23:37:07 +00:00
|
|
|
templates = await api.getDefaultAnkiFieldTemplates();
|
|
|
|
this._ankiFieldTemplatesDefault = templates;
|
|
|
|
return templates;
|
2020-09-10 22:03:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async _areDefinitionsAddable(definitions, modes, context) {
|
|
|
|
const modeCount = modes.length;
|
|
|
|
const notePromises = [];
|
|
|
|
for (const definition of definitions) {
|
|
|
|
for (const mode of modes) {
|
2021-01-30 17:33:29 +00:00
|
|
|
const notePromise = this._createNote(definition, mode, context, false, null);
|
2020-09-10 22:03:46 +00:00
|
|
|
notePromises.push(notePromise);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const notes = await Promise.all(notePromises);
|
|
|
|
|
2021-01-11 23:37:07 +00:00
|
|
|
const infos = await api.getAnkiNoteInfo(notes);
|
2020-09-10 22:03:46 +00:00
|
|
|
const results = [];
|
|
|
|
for (let i = 0, ii = infos.length; i < ii; i += modeCount) {
|
|
|
|
results.push(infos.slice(i, i + modeCount));
|
|
|
|
}
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
2020-11-08 21:25:07 +00:00
|
|
|
_areDefinitionsAddableForcedValue(definitions, modes, canAdd) {
|
|
|
|
const results = [];
|
|
|
|
const definitionCount = definitions.length;
|
|
|
|
const modeCount = modes.length;
|
|
|
|
for (let i = 0; i < definitionCount; ++i) {
|
|
|
|
const modeArray = [];
|
|
|
|
for (let j = 0; j < modeCount; ++j) {
|
|
|
|
modeArray.push({canAdd, noteIds: null});
|
|
|
|
}
|
|
|
|
results.push(modeArray);
|
|
|
|
}
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
2021-01-30 17:33:29 +00:00
|
|
|
async _createNote(definition, mode, context, injectMedia, errors) {
|
2021-01-11 23:37:07 +00:00
|
|
|
const options = this._options;
|
|
|
|
const templates = this._ankiFieldTemplates;
|
2020-09-10 22:03:46 +00:00
|
|
|
const {
|
2020-11-26 04:22:05 +00:00
|
|
|
general: {resultOutputMode, glossaryLayoutMode, compactTags},
|
2021-01-04 00:40:12 +00:00
|
|
|
anki: ankiOptions
|
2020-09-10 22:03:46 +00:00
|
|
|
} = options;
|
2021-01-04 00:40:12 +00:00
|
|
|
const {tags, checkForDuplicates, duplicateScope} = ankiOptions;
|
|
|
|
const modeOptions = (mode === 'kanji') ? ankiOptions.kanji : ankiOptions.terms;
|
|
|
|
const {deck: deckName, model: modelName} = modeOptions;
|
|
|
|
const fields = Object.entries(modeOptions.fields);
|
2020-09-10 22:03:46 +00:00
|
|
|
|
2021-01-30 17:33:29 +00:00
|
|
|
let injectedMedia = null;
|
|
|
|
if (injectMedia) {
|
|
|
|
let errors2;
|
|
|
|
({result: injectedMedia, errors: errors2} = await this._injectAnkiNoteMedia(definition, mode, options, fields));
|
|
|
|
if (Array.isArray(errors)) {
|
|
|
|
for (const error of errors2) {
|
|
|
|
errors.push(deserializeError(error));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-10 22:03:46 +00:00
|
|
|
|
|
|
|
return await this._ankiNoteBuilder.createNote({
|
|
|
|
definition,
|
|
|
|
mode,
|
|
|
|
context,
|
|
|
|
templates,
|
2021-01-04 00:40:12 +00:00
|
|
|
deckName,
|
|
|
|
modelName,
|
|
|
|
fields,
|
2020-09-10 22:03:46 +00:00
|
|
|
tags,
|
2020-11-08 21:25:07 +00:00
|
|
|
checkForDuplicates,
|
2020-09-10 22:03:46 +00:00
|
|
|
duplicateScope,
|
|
|
|
resultOutputMode,
|
2020-11-26 04:22:05 +00:00
|
|
|
glossaryLayoutMode,
|
2021-01-13 03:47:07 +00:00
|
|
|
compactTags,
|
2021-01-30 17:33:29 +00:00
|
|
|
injectedMedia,
|
|
|
|
errors
|
2020-09-10 22:03:46 +00:00
|
|
|
});
|
|
|
|
}
|
2020-09-26 17:41:26 +00:00
|
|
|
|
2021-01-04 00:40:12 +00:00
|
|
|
async _injectAnkiNoteMedia(definition, mode, options, fields) {
|
|
|
|
const {
|
|
|
|
anki: {screenshot: {format, quality}},
|
2021-01-24 03:46:00 +00:00
|
|
|
audio: {sources, customSourceUrl, customSourceType}
|
2021-01-04 00:40:12 +00:00
|
|
|
} = options;
|
|
|
|
|
|
|
|
const timestamp = Date.now();
|
|
|
|
const definitionDetails = this._getDefinitionDetailsForNote(definition);
|
2021-01-24 03:46:00 +00:00
|
|
|
const audioDetails = (mode !== 'kanji' && this._ankiNoteBuilder.containsMarker(fields, 'audio') ? {sources, customSourceUrl, customSourceType} : null);
|
2021-02-10 04:14:29 +00:00
|
|
|
const screenshotDetails = (this._ankiNoteBuilder.containsMarker(fields, 'screenshot') ? {tabId: this._contentOriginTabId, frameId: this._contentOriginFrameId, format, quality} : null);
|
2021-01-04 00:40:12 +00:00
|
|
|
const clipboardDetails = {
|
|
|
|
image: this._ankiNoteBuilder.containsMarker(fields, 'clipboard-image'),
|
|
|
|
text: this._ankiNoteBuilder.containsMarker(fields, 'clipboard-text')
|
|
|
|
};
|
2021-01-13 03:47:07 +00:00
|
|
|
return await api.injectAnkiNoteMedia(
|
2021-01-04 00:40:12 +00:00
|
|
|
timestamp,
|
|
|
|
definitionDetails,
|
|
|
|
audioDetails,
|
|
|
|
screenshotDetails,
|
|
|
|
clipboardDetails
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-11-27 03:53:58 +00:00
|
|
|
_getDefinitionDetailsForNote(definition) {
|
|
|
|
const {type} = definition;
|
|
|
|
if (type === 'kanji') {
|
|
|
|
const {character} = definition;
|
|
|
|
return {type, character};
|
|
|
|
}
|
2020-10-05 02:04:44 +00:00
|
|
|
|
|
|
|
const termDetailsList = definition.expressions;
|
|
|
|
let bestIndex = -1;
|
|
|
|
for (let i = 0, ii = termDetailsList.length; i < ii; ++i) {
|
|
|
|
const {sourceTerm, expression, reading} = termDetailsList[i];
|
|
|
|
if (expression === sourceTerm) {
|
|
|
|
bestIndex = i;
|
|
|
|
break;
|
|
|
|
} else if (reading === sourceTerm && bestIndex < 0) {
|
|
|
|
bestIndex = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const {expression, reading} = termDetailsList[Math.max(0, bestIndex)];
|
2020-11-27 03:53:58 +00:00
|
|
|
return {type, expression, reading};
|
|
|
|
}
|
|
|
|
|
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');
|
|
|
|
const isEnabled = this._childrenSupported && (
|
|
|
|
(isSearchPage) ?
|
|
|
|
(options.scanning.enableOnSearchPage) :
|
|
|
|
(this._depth < options.scanning.popupNestingMaxDepth)
|
|
|
|
);
|
|
|
|
|
|
|
|
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) {
|
|
|
|
yomichan.logError(e);
|
|
|
|
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
|
|
|
|
2021-02-10 03:56:04 +00:00
|
|
|
async _invokeContentOrigin(action, params={}) {
|
|
|
|
if (this._contentOriginTabId === this._tabId && this._contentOriginFrameId === this._frameId) {
|
|
|
|
throw new Error('Content origin is same page');
|
2020-11-22 20:29:51 +00:00
|
|
|
}
|
2021-02-10 03:56:04 +00:00
|
|
|
return await api.crossFrame.invokeTab(this._contentOriginTabId, this._contentOriginFrameId, action, params);
|
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; }
|
2020-11-22 20:29:51 +00:00
|
|
|
this._copyHostSelectionInner();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
async _copyHostSelectionInner() {
|
|
|
|
switch (this._browser) {
|
|
|
|
case 'firefox':
|
|
|
|
case 'firefox-mobile':
|
|
|
|
{
|
|
|
|
let text;
|
|
|
|
try {
|
2021-02-10 03:56:04 +00:00
|
|
|
text = await this._invokeContentOrigin('getSelectionText');
|
2020-11-22 20:29:51 +00:00
|
|
|
} catch (e) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
this._copyText(text);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2021-02-10 03:56:04 +00:00
|
|
|
await this._invokeContentOrigin('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
|
|
|
_addMultipleEventListeners(container, selector, ...args) {
|
|
|
|
for (const node of container.querySelectorAll(selector)) {
|
|
|
|
this._eventListeners.addEventListener(node, ...args);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_addEntryEventListeners(entry) {
|
|
|
|
this._eventListeners.addEventListener(entry, 'click', this._onEntryClick.bind(this));
|
|
|
|
this._addMultipleEventListeners(entry, '.action-add-note', 'click', this._onNoteAdd.bind(this));
|
|
|
|
this._addMultipleEventListeners(entry, '.action-view-note', 'click', this._onNoteView.bind(this));
|
|
|
|
this._addMultipleEventListeners(entry, '.kanji-link', 'click', this._onKanjiLookup.bind(this));
|
|
|
|
this._addMultipleEventListeners(entry, '.debug-log-link', 'click', this._onDebugLogClick.bind(this));
|
2020-12-18 14:43:54 +00:00
|
|
|
this._addMultipleEventListeners(entry, '.tag', 'click', this._onTagClick.bind(this));
|
2020-11-24 01:31:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_updateDefinitionTextScanner(options) {
|
|
|
|
if (!options.scanning.enablePopupSearch) {
|
|
|
|
if (this._definitionTextScanner !== null) {
|
|
|
|
this._definitionTextScanner.setEnabled(false);
|
|
|
|
}
|
|
|
|
return;
|
2020-11-23 22:43:17 +00:00
|
|
|
}
|
2020-11-24 01:31:48 +00:00
|
|
|
|
|
|
|
if (this._definitionTextScanner === null) {
|
|
|
|
this._definitionTextScanner = new TextScanner({
|
|
|
|
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
|
|
|
|
});
|
2020-12-31 19:21:50 +00:00
|
|
|
this._definitionTextScanner.includeSelector = '.click-scannable,.click-scannable *';
|
|
|
|
this._definitionTextScanner.excludeSelector = '.scan-disable,.scan-disable *';
|
2020-11-24 01:31:48 +00:00
|
|
|
this._definitionTextScanner.prepare();
|
|
|
|
this._definitionTextScanner.on('searched', this._onDefinitionTextScannerSearched.bind(this));
|
|
|
|
}
|
|
|
|
|
2021-01-10 04:10:55 +00:00
|
|
|
const {scanning: scanningOptions, sentenceParsing: sentenceParsingOptions} = options;
|
2020-11-24 01:31:48 +00:00
|
|
|
this._definitionTextScanner.setOptions({
|
|
|
|
inputs: [{
|
|
|
|
include: 'mouse0',
|
|
|
|
exclude: '',
|
|
|
|
types: {mouse: true, pen: false, touch: false},
|
|
|
|
options: {
|
|
|
|
searchTerms: true,
|
|
|
|
searchKanji: true,
|
|
|
|
scanOnTouchMove: false,
|
|
|
|
scanOnPenHover: false,
|
|
|
|
scanOnPenPress: false,
|
|
|
|
scanOnPenRelease: false,
|
|
|
|
preventTouchScrolling: false
|
|
|
|
}
|
|
|
|
}],
|
|
|
|
deepContentScan: scanningOptions.deepDomScan,
|
|
|
|
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
|
|
|
});
|
|
|
|
|
|
|
|
this._definitionTextScanner.setEnabled(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
_onDefinitionTextScannerSearched({type, definitions, sentence, textSource, optionsContext, error}) {
|
|
|
|
if (error !== null && !yomichan.isExtensionUnloaded) {
|
|
|
|
yomichan.logError(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
history: true,
|
|
|
|
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-02-10 03:56:04 +00:00
|
|
|
definitions,
|
|
|
|
contentOrigin: this.getContentOrigin()
|
2020-11-24 01:31:48 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
this._definitionTextScanner.clearSelection(true);
|
|
|
|
this.setContent(details);
|
2020-11-23 22:43:17 +00:00
|
|
|
}
|
2020-12-09 01:31:02 +00:00
|
|
|
|
|
|
|
_onFrameResizerMouseDown(e) {
|
|
|
|
if (e.button !== 0) { return; }
|
|
|
|
// Don't do e.preventDefault() here; this allows mousemove events to be processed
|
|
|
|
// if the pointer moves out of the frame.
|
|
|
|
this._startFrameResize(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
_onFrameResizerMouseUp() {
|
|
|
|
this._stopFrameResize();
|
|
|
|
}
|
|
|
|
|
|
|
|
_onFrameResizerWindowBlur() {
|
|
|
|
this._stopFrameResize();
|
|
|
|
}
|
|
|
|
|
|
|
|
_onFrameResizerMouseMove(e) {
|
|
|
|
if ((e.buttons & 0x1) === 0x0) {
|
|
|
|
this._stopFrameResize();
|
|
|
|
} else {
|
|
|
|
if (this._frameResizeStartSize === null) { return; }
|
|
|
|
const {clientX: x, clientY: y} = e;
|
|
|
|
this._updateFrameSize(x, y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-12 04:13:35 +00:00
|
|
|
_getSearchContext() {
|
|
|
|
return {optionsContext: this.getOptionsContext()};
|
|
|
|
}
|
|
|
|
|
2020-12-09 01:31:02 +00:00
|
|
|
_startFrameResize(e) {
|
|
|
|
if (this._frameResizeToken !== null) { return; }
|
|
|
|
|
|
|
|
const {clientX: x, clientY: y} = e;
|
|
|
|
const token = {};
|
|
|
|
this._frameResizeToken = token;
|
|
|
|
this._frameResizeStartOffset = {x, y};
|
|
|
|
this._frameResizeEventListeners.addEventListener(window, 'mouseup', this._onFrameResizerMouseUp.bind(this), false);
|
|
|
|
this._frameResizeEventListeners.addEventListener(window, 'blur', this._onFrameResizerWindowBlur.bind(this), false);
|
|
|
|
this._frameResizeEventListeners.addEventListener(window, 'mousemove', this._onFrameResizerMouseMove.bind(this), false);
|
|
|
|
|
2020-12-18 17:06:39 +00:00
|
|
|
const {documentElement} = document;
|
|
|
|
if (documentElement !== null) {
|
|
|
|
documentElement.dataset.isResizing = 'true';
|
|
|
|
}
|
|
|
|
|
2020-12-09 01:31:02 +00:00
|
|
|
this._initializeFrameResize(token);
|
|
|
|
}
|
|
|
|
|
|
|
|
async _initializeFrameResize(token) {
|
2021-02-10 03:56:04 +00:00
|
|
|
const size = await this._invokeContentOrigin('getFrameSize');
|
2020-12-09 01:31:02 +00:00
|
|
|
if (this._frameResizeToken !== token) { return; }
|
|
|
|
this._frameResizeStartSize = size;
|
|
|
|
}
|
|
|
|
|
|
|
|
_stopFrameResize() {
|
|
|
|
if (this._frameResizeToken === null) { return; }
|
|
|
|
|
|
|
|
this._frameResizeEventListeners.removeAllEventListeners();
|
|
|
|
this._frameResizeStartSize = null;
|
|
|
|
this._frameResizeStartOffset = null;
|
|
|
|
this._frameResizeToken = null;
|
2020-12-18 17:06:39 +00:00
|
|
|
|
|
|
|
const {documentElement} = document;
|
|
|
|
if (documentElement !== null) {
|
|
|
|
delete documentElement.dataset.isResizing;
|
|
|
|
}
|
2020-12-09 01:31:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async _updateFrameSize(x, y) {
|
|
|
|
const handleSize = this._frameResizeHandle.getBoundingClientRect();
|
|
|
|
let {width, height} = this._frameResizeStartSize;
|
|
|
|
width += x - this._frameResizeStartOffset.x;
|
|
|
|
height += y - this._frameResizeStartOffset.y;
|
|
|
|
width = Math.max(Math.max(0, handleSize.width), width);
|
|
|
|
height = Math.max(Math.max(0, handleSize.height), height);
|
2021-02-10 03:56:04 +00:00
|
|
|
await this._invokeContentOrigin('setFrameSize', {width, height});
|
2020-12-09 01:31:02 +00:00
|
|
|
}
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
_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
|
|
|
|
|
|
|
_postProcessQuery(query) {
|
|
|
|
const queryPostProcessor = this._queryPostProcessor;
|
|
|
|
return typeof queryPostProcessor === 'function' ? queryPostProcessor(query) : query;
|
|
|
|
}
|
2017-03-05 01:30:10 +00:00
|
|
|
}
|