2017-03-05 01:30:10 +00:00
|
|
|
/*
|
2020-04-10 18:06:55 +00:00
|
|
|
* Copyright (C) 2017-2020 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
|
2020-03-11 02:30:36 +00:00
|
|
|
* AudioSystem
|
|
|
|
* DisplayGenerator
|
2020-07-26 20:51:54 +00:00
|
|
|
* DisplayHistory
|
2020-08-09 17:27:21 +00:00
|
|
|
* DocumentUtil
|
2020-07-19 03:47:02 +00:00
|
|
|
* Frontend
|
2020-04-11 18:23:49 +00:00
|
|
|
* MediaLoader
|
2020-07-19 03:47:02 +00:00
|
|
|
* PopupFactory
|
2020-07-26 23:29:12 +00:00
|
|
|
* QueryParser
|
2020-09-19 01:16:39 +00:00
|
|
|
* TemplateRendererProxy
|
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 {
|
2020-11-08 17:34:23 +00:00
|
|
|
constructor() {
|
2020-07-26 20:51:54 +00:00
|
|
|
super();
|
2020-11-08 17:34:23 +00:00
|
|
|
this._spinner = document.querySelector('#spinner');
|
|
|
|
this._container = document.querySelector('#definitions');
|
2020-07-03 16:02:21 +00:00
|
|
|
this._definitions = [];
|
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._audioPlaying = null;
|
|
|
|
this._audioFallback = null;
|
|
|
|
this._audioSystem = new AudioSystem({
|
2020-09-26 17:41:26 +00:00
|
|
|
getAudioInfo: this._getAudioInfo.bind(this)
|
2020-04-10 17:44:31 +00:00
|
|
|
});
|
2020-07-03 16:02:21 +00:00
|
|
|
this._styleNode = null;
|
|
|
|
this._eventListeners = new EventListenerCollection();
|
|
|
|
this._persistentEventListeners = new EventListenerCollection();
|
|
|
|
this._interactive = false;
|
|
|
|
this._eventListenersActive = false;
|
|
|
|
this._clickScanPrevent = false;
|
|
|
|
this._setContentToken = null;
|
2020-07-19 03:47:02 +00:00
|
|
|
this._autoPlayAudioTimer = null;
|
|
|
|
this._autoPlayAudioDelay = 0;
|
2020-07-03 16:02:21 +00:00
|
|
|
this._mediaLoader = new MediaLoader();
|
|
|
|
this._displayGenerator = new DisplayGenerator({mediaLoader: this._mediaLoader});
|
|
|
|
this._windowScroll = new WindowScroll();
|
2020-07-09 00:02:20 +00:00
|
|
|
this._hotkeys = new Map();
|
|
|
|
this._actions = new Map();
|
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-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-08-01 20:22:00 +00:00
|
|
|
this._defaultTitle = 'Yomichan Search';
|
|
|
|
this._defaultTitleMaxLength = 1000;
|
2020-07-26 23:29:12 +00:00
|
|
|
this._fullQuery = '';
|
2020-08-09 17:27:21 +00:00
|
|
|
this._documentUtil = new DocumentUtil();
|
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({
|
|
|
|
getOptionsContext: this.getOptionsContext.bind(this),
|
2020-08-09 17:27:21 +00:00
|
|
|
setSpinnerVisible: this.setSpinnerVisible.bind(this),
|
|
|
|
documentUtil: this._documentUtil
|
2020-07-26 23:29:12 +00:00
|
|
|
});
|
2020-08-09 17:11:41 +00:00
|
|
|
this._mode = null;
|
2020-08-23 15:15:23 +00:00
|
|
|
this._ownerFrameId = null;
|
2020-09-10 22:03:46 +00:00
|
|
|
this._defaultAnkiFieldTemplates = null;
|
|
|
|
this._defaultAnkiFieldTemplatesPromise = null;
|
2020-09-19 01:16:39 +00:00
|
|
|
this._templateRenderer = new TemplateRendererProxy();
|
2020-09-10 22:03:46 +00:00
|
|
|
this._ankiNoteBuilder = new AnkiNoteBuilder({
|
|
|
|
renderTemplate: this._renderTemplate.bind(this)
|
|
|
|
});
|
2020-11-08 21:48:15 +00:00
|
|
|
this._updateAdderButtonsPromise = Promise.resolve();
|
2020-07-09 00:02:20 +00:00
|
|
|
|
|
|
|
this.registerActions([
|
2020-07-27 00:25:15 +00:00
|
|
|
['close', () => { this.onEscape(); }],
|
|
|
|
['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(); }],
|
|
|
|
['addNoteKanji', () => { this._noteTryAdd('kanji'); }],
|
|
|
|
['addNoteTermKanji', () => { this._noteTryAdd('term-kanji'); }],
|
|
|
|
['addNoteTermKana', () => { this._noteTryAdd('term-kana'); }],
|
|
|
|
['viewNote', () => { this._noteTryView(); }],
|
|
|
|
['playAudio', () => { this._playAudioCurrent(); }]
|
2020-07-09 00:02:20 +00:00
|
|
|
]);
|
|
|
|
this.registerHotkeys([
|
|
|
|
{key: 'Escape', modifiers: [], action: 'close'},
|
2020-07-27 00:25:15 +00:00
|
|
|
{key: 'PageUp', modifiers: ['alt'], action: 'previousEntry3'},
|
|
|
|
{key: 'PageDown', modifiers: ['alt'], action: 'nextEntry3'},
|
|
|
|
{key: 'End', modifiers: ['alt'], action: 'lastEntry'},
|
|
|
|
{key: 'Home', modifiers: ['alt'], action: 'firstEntry'},
|
|
|
|
{key: 'ArrowUp', modifiers: ['alt'], action: 'previousEntry'},
|
|
|
|
{key: 'ArrowDown', modifiers: ['alt'], action: 'nextEntry'},
|
|
|
|
{key: 'B', modifiers: ['alt'], action: 'historyBackward'},
|
|
|
|
{key: 'F', modifiers: ['alt'], action: 'historyForward'},
|
|
|
|
{key: 'K', modifiers: ['alt'], action: 'addNoteKanji'},
|
|
|
|
{key: 'E', modifiers: ['alt'], action: 'addNoteTermKanji'},
|
|
|
|
{key: 'R', modifiers: ['alt'], action: 'addNoteTermKana'},
|
|
|
|
{key: 'P', modifiers: ['alt'], action: 'playAudio'},
|
|
|
|
{key: 'V', modifiers: ['alt'], action: 'viewNote'}
|
2020-02-27 00:48:30 +00:00
|
|
|
]);
|
2020-07-19 03:47:02 +00:00
|
|
|
this.registerMessageHandlers([
|
2020-08-09 17:11:41 +00:00
|
|
|
['setMode', {async: false, handler: this._onMessageSetMode.bind(this)}]
|
|
|
|
]);
|
|
|
|
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)}],
|
|
|
|
['setCustomCss', {async: false, handler: this._onMessageSetCustomCss.bind(this)}]
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
get autoPlayAudioDelay() {
|
|
|
|
return this._autoPlayAudioDelay;
|
|
|
|
}
|
|
|
|
|
|
|
|
set autoPlayAudioDelay(value) {
|
|
|
|
this._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;
|
|
|
|
this._updateQueryParserVisibility();
|
|
|
|
}
|
|
|
|
|
2020-08-09 17:11:41 +00:00
|
|
|
get mode() {
|
|
|
|
return this._mode;
|
|
|
|
}
|
|
|
|
|
2020-08-23 15:15:23 +00:00
|
|
|
get ownerFrameId() {
|
|
|
|
return this._ownerFrameId;
|
|
|
|
}
|
|
|
|
|
|
|
|
set ownerFrameId(value) {
|
|
|
|
this._ownerFrameId = value;
|
|
|
|
}
|
|
|
|
|
2020-03-13 21:23:08 +00:00
|
|
|
async prepare() {
|
2020-09-26 17:41:26 +00:00
|
|
|
this._audioSystem.prepare();
|
2020-08-09 17:11:41 +00:00
|
|
|
this._updateMode();
|
2020-07-03 16:02:21 +00:00
|
|
|
this._setInteractive(true);
|
|
|
|
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();
|
|
|
|
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-07-18 18:15:36 +00:00
|
|
|
yomichan.on('extensionUnloaded', this._onExtensionUnloaded.bind(this));
|
2020-08-09 17:11:41 +00:00
|
|
|
chrome.runtime.onMessage.addListener(this._onMessage.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-07-03 16:02:21 +00:00
|
|
|
}
|
|
|
|
|
2020-07-26 20:51:54 +00:00
|
|
|
initializeState() {
|
|
|
|
this._onStateChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
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-04-27 22:10:37 +00:00
|
|
|
onEscape() {
|
2019-10-08 00:46:02 +00:00
|
|
|
throw new Error('Override me');
|
2017-03-25 22:59:33 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 19:58:29 +00:00
|
|
|
onKeyDown(e) {
|
2020-08-10 01:07:11 +00:00
|
|
|
const key = DocumentUtil.getKeyFromEvent(e);
|
2020-07-09 00:02:20 +00:00
|
|
|
const handlers = this._hotkeys.get(key);
|
|
|
|
if (typeof handlers === 'undefined') { return false; }
|
|
|
|
|
2020-08-10 01:07:11 +00:00
|
|
|
const eventModifiers = DocumentUtil.getActiveModifiers(e);
|
2020-07-09 00:02:20 +00:00
|
|
|
for (const {modifiers, action} of handlers) {
|
2020-11-09 03:19:54 +00:00
|
|
|
if (!this._areSame(modifiers, eventModifiers)) { continue; }
|
2020-07-09 00:02:20 +00:00
|
|
|
|
|
|
|
const actionHandler = this._actions.get(action);
|
|
|
|
if (typeof actionHandler === 'undefined') { continue; }
|
|
|
|
|
|
|
|
const result = actionHandler(e);
|
|
|
|
if (result !== false) {
|
2020-07-03 19:58:29 +00:00
|
|
|
e.preventDefault();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
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());
|
|
|
|
const scanning = options.scanning;
|
|
|
|
this._options = options;
|
|
|
|
|
|
|
|
this._updateDocumentOptions(options);
|
|
|
|
this._updateTheme(options.general.popupTheme);
|
|
|
|
this.setCustomCss(options.general.customPopupCss);
|
|
|
|
|
|
|
|
this._queryParser.setOptions({
|
|
|
|
selectedParser: options.parsing.selectedParser,
|
|
|
|
termSpacing: options.parsing.termSpacing,
|
|
|
|
scanning: {
|
2020-09-09 20:59:03 +00:00
|
|
|
inputs: scanning.inputs,
|
2020-08-09 17:19:42 +00:00
|
|
|
deepContentScan: scanning.deepDomScan,
|
|
|
|
selectText: scanning.selectText,
|
|
|
|
delay: scanning.delay,
|
2020-08-22 18:40:44 +00:00
|
|
|
touchInputEnabled: scanning.touchInputEnabled,
|
2020-09-13 15:33:10 +00:00
|
|
|
pointerEventsEnabled: scanning.pointerEventsEnabled,
|
2020-08-22 18:40:44 +00:00
|
|
|
scanLength: scanning.length,
|
|
|
|
sentenceExtent: options.anki.sentenceExt,
|
2020-09-26 23:24:21 +00:00
|
|
|
layoutAwareScan: scanning.layoutAwareScan,
|
|
|
|
preventMiddleMouse: scanning.preventMiddleMouse.onSearchQuery
|
2020-08-09 17:19:42 +00:00
|
|
|
}
|
|
|
|
});
|
2020-07-03 19:58:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
addMultipleEventListeners(selector, type, listener, options) {
|
|
|
|
for (const node of this._container.querySelectorAll(selector)) {
|
|
|
|
this._eventListeners.addEventListener(node, type, listener, options);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
autoPlayAudio() {
|
2020-07-19 03:47:02 +00:00
|
|
|
this.clearAutoPlayTimer();
|
|
|
|
|
2020-07-03 19:58:29 +00:00
|
|
|
if (this._definitions.length === 0) { return; }
|
|
|
|
|
2020-07-19 03:47:02 +00:00
|
|
|
const definition = this._definitions[0];
|
|
|
|
const expressionIndex = this._getFirstExpressionIndex();
|
|
|
|
const callback = () => {
|
|
|
|
this._audioPlay(definition, expressionIndex, 0);
|
|
|
|
};
|
|
|
|
|
|
|
|
if (this._autoPlayAudioDelay > 0) {
|
|
|
|
this._autoPlayAudioTimer = setTimeout(callback, this._autoPlayAudioDelay);
|
|
|
|
} else {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
clearAutoPlayTimer() {
|
|
|
|
if (this._autoPlayAudioTimer !== null) {
|
|
|
|
clearTimeout(this._autoPlayAudioTimer);
|
|
|
|
this._autoPlayAudioTimer = null;
|
|
|
|
}
|
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) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async getDocumentTitle() {
|
|
|
|
return document.title;
|
|
|
|
}
|
|
|
|
|
|
|
|
setSpinnerVisible(visible) {
|
|
|
|
if (this._spinner !== null) {
|
|
|
|
this._spinner.hidden = !visible;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-09 00:02:20 +00:00
|
|
|
registerActions(actions) {
|
|
|
|
for (const [name, handler] of actions) {
|
|
|
|
this._actions.set(name, handler);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
registerHotkeys(hotkeys) {
|
|
|
|
for (const {key, modifiers, action} of hotkeys) {
|
|
|
|
let handlers = this._hotkeys.get(key);
|
|
|
|
if (typeof handlers === 'undefined') {
|
|
|
|
handlers = [];
|
|
|
|
this._hotkeys.set(key, handlers);
|
|
|
|
}
|
|
|
|
handlers.push({modifiers: new Set(modifiers), action});
|
2020-07-03 19:58:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-19 03:47:02 +00:00
|
|
|
registerMessageHandlers(handlers) {
|
|
|
|
for (const [name, handlerInfo] of handlers) {
|
|
|
|
this._messageHandlers.set(name, handlerInfo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-09 17:11:41 +00:00
|
|
|
registerDirectMessageHandlers(handlers) {
|
|
|
|
for (const [name, handlerInfo] of handlers) {
|
|
|
|
this._directMessageHandlers.set(name, handlerInfo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-26 22:47:58 +00:00
|
|
|
async setupNestedPopups({depth, parentPopupId, parentFrameId, useProxyPopup, pageType}) {
|
2020-07-19 03:47:02 +00:00
|
|
|
await dynamicLoader.loadScripts([
|
|
|
|
'/mixed/js/text-scanner.js',
|
|
|
|
'/mixed/js/frame-client.js',
|
|
|
|
'/fg/js/popup.js',
|
|
|
|
'/fg/js/popup-proxy.js',
|
2020-09-06 02:03:35 +00:00
|
|
|
'/fg/js/popup-window.js',
|
2020-07-19 03:47:02 +00:00
|
|
|
'/fg/js/popup-factory.js',
|
|
|
|
'/fg/js/frame-offset-forwarder.js',
|
|
|
|
'/fg/js/frontend.js'
|
|
|
|
]);
|
|
|
|
|
|
|
|
const {frameId} = await api.frameInformationGet();
|
|
|
|
|
|
|
|
const popupFactory = new PopupFactory(frameId);
|
|
|
|
popupFactory.prepare();
|
|
|
|
|
2020-08-16 20:16:18 +00:00
|
|
|
const frontend = new Frontend({
|
|
|
|
frameId,
|
|
|
|
popupFactory,
|
|
|
|
depth,
|
|
|
|
parentPopupId,
|
|
|
|
parentFrameId,
|
|
|
|
useProxyPopup,
|
2020-09-26 22:47:58 +00:00
|
|
|
pageType,
|
2020-08-16 20:16:18 +00:00
|
|
|
allowRootFramePopupProxy: true
|
|
|
|
});
|
2020-07-19 03:47:02 +00:00
|
|
|
await frontend.prepare();
|
|
|
|
}
|
|
|
|
|
|
|
|
authenticateMessageData(data) {
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2020-08-01 20:22:00 +00:00
|
|
|
postProcessQuery(query) {
|
|
|
|
return query;
|
2020-07-26 23:29:12 +00:00
|
|
|
}
|
|
|
|
|
2020-07-19 03:47:02 +00:00
|
|
|
// Message handlers
|
|
|
|
|
2020-08-09 17:11:41 +00:00
|
|
|
_onMessage({action, params}, sender, callback) {
|
|
|
|
const messageHandler = this._messageHandlers.get(action);
|
|
|
|
if (typeof messageHandler === 'undefined') { return false; }
|
|
|
|
return yomichan.invokeMessageHandler(messageHandler, params, callback, sender);
|
|
|
|
}
|
|
|
|
|
|
|
|
_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-08-09 17:11:41 +00:00
|
|
|
_onMessageSetMode({mode}) {
|
|
|
|
this._setMode(mode, true);
|
|
|
|
}
|
|
|
|
|
2020-07-19 03:47:02 +00:00
|
|
|
_onMessageSetOptionsContext({optionsContext}) {
|
|
|
|
this.setOptionsContext(optionsContext);
|
|
|
|
}
|
|
|
|
|
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-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 {
|
|
|
|
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'));
|
|
|
|
this._updateQueryParserVisibility();
|
|
|
|
|
|
|
|
this._closePopups();
|
|
|
|
this._setEventListenersActive(false);
|
|
|
|
|
2020-07-26 20:51:54 +00:00
|
|
|
let asigned = false;
|
|
|
|
const eventArgs = {type, urlSearchParams, token};
|
|
|
|
this._historyHasChanged = true;
|
2020-08-03 01:51:51 +00:00
|
|
|
this._contentType = type;
|
2020-07-26 20:51:54 +00:00
|
|
|
this._mediaLoader.unloadAll();
|
|
|
|
switch (type) {
|
|
|
|
case 'terms':
|
|
|
|
case 'kanji':
|
|
|
|
{
|
|
|
|
const isTerms = (type === 'terms');
|
2020-08-01 20:22:00 +00:00
|
|
|
asigned = await this._setContentTermsOrKanji(token, isTerms, urlSearchParams, eventArgs);
|
2020-07-26 20:51:54 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'unloaded':
|
|
|
|
{
|
|
|
|
const {content} = this._history;
|
|
|
|
eventArgs.content = content;
|
|
|
|
this.trigger('contentUpdating', eventArgs);
|
|
|
|
this._setContentExtensionUnloaded();
|
2020-08-01 20:22:00 +00:00
|
|
|
asigned = true;
|
2020-07-26 20:51:54 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-08-01 20:22:00 +00:00
|
|
|
const stale = (this._setContentToken !== token);
|
|
|
|
if (!stale) {
|
|
|
|
if (!asigned) {
|
2020-08-03 01:51:51 +00:00
|
|
|
type = 'clear';
|
|
|
|
this._contentType = type;
|
2020-08-01 20:22:00 +00:00
|
|
|
const {content} = this._history;
|
2020-08-03 01:51:51 +00:00
|
|
|
eventArgs.type = type;
|
2020-08-01 20:22:00 +00:00
|
|
|
eventArgs.content = content;
|
|
|
|
this.trigger('contentUpdating', eventArgs);
|
|
|
|
this._clearContent();
|
|
|
|
}
|
|
|
|
|
|
|
|
this._setEventListenersActive(true);
|
2020-07-26 20:51:54 +00:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-13 01:32:46 +00:00
|
|
|
_onQueryParserSearch({type, definitions, sentence, inputInfo: {cause}, textSource, optionsContext}) {
|
2020-07-26 23:29:12 +00:00
|
|
|
const query = textSource.text();
|
2020-09-12 02:46:41 +00:00
|
|
|
const history = (cause === 'click');
|
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-11-13 01:32:46 +00:00
|
|
|
optionsContext
|
2020-07-26 23:29:12 +00:00
|
|
|
},
|
|
|
|
content: {
|
|
|
|
definitions
|
|
|
|
}
|
|
|
|
};
|
|
|
|
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: {},
|
|
|
|
content: {}
|
2020-07-26 23:29:12 +00:00
|
|
|
};
|
|
|
|
this.setContent(details);
|
2020-07-18 18:15:36 +00:00
|
|
|
}
|
|
|
|
|
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-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
|
|
|
|
2019-09-15 18:06:27 +00:00
|
|
|
const link = e.target;
|
2020-07-26 20:51:54 +00:00
|
|
|
const {state} = this._history;
|
|
|
|
|
2020-07-26 22:49:38 +00:00
|
|
|
state.focusEntry = this._entryIndexFind(link);
|
|
|
|
state.scrollX = this._windowScroll.x;
|
|
|
|
state.scrollY = this._windowScroll.y;
|
2020-07-26 20:51:54 +00:00
|
|
|
this._historyStateUpdate(state);
|
2017-08-13 23:11:51 +00:00
|
|
|
|
2020-07-26 20:51:54 +00:00
|
|
|
const query = link.textContent;
|
|
|
|
const definitions = await api.kanjiFind(query, this.getOptionsContext());
|
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,
|
2020-07-26 20:51:54 +00:00
|
|
|
sentence: state.sentence,
|
2020-11-13 01:32:46 +00:00
|
|
|
optionsContext: state.optionsContext
|
2020-07-26 20:51:54 +00:00
|
|
|
},
|
|
|
|
content: {
|
|
|
|
definitions
|
|
|
|
}
|
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
|
|
|
_onGlossaryMouseDown(e) {
|
2020-08-10 01:07:11 +00:00
|
|
|
if (DocumentUtil.isMouseButtonPressed(e, 'primary')) {
|
2020-07-03 16:02:21 +00:00
|
|
|
this._clickScanPrevent = false;
|
2019-11-03 16:56:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
_onGlossaryMouseMove() {
|
|
|
|
this._clickScanPrevent = true;
|
2019-11-03 16:56:22 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
_onGlossaryMouseUp(e) {
|
2020-08-10 01:07:11 +00:00
|
|
|
if (!this._clickScanPrevent && DocumentUtil.isMouseButtonPressed(e, 'primary')) {
|
2020-08-01 20:33:21 +00:00
|
|
|
try {
|
|
|
|
this._onTermLookup(e);
|
|
|
|
} catch (error) {
|
|
|
|
this.onError(error);
|
|
|
|
}
|
2019-11-03 16:56:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-25 01:12:13 +00:00
|
|
|
async _onTermLookup(e) {
|
2020-08-01 20:33:21 +00:00
|
|
|
if (!this._historyHasState()) { return; }
|
2019-12-01 03:38:23 +00:00
|
|
|
|
2020-08-01 20:33:21 +00:00
|
|
|
const termLookupResults = await this._termLookup(e);
|
|
|
|
if (!termLookupResults || !this._historyHasState()) { return; }
|
2020-07-26 20:51:54 +00:00
|
|
|
|
2020-08-01 20:33:21 +00:00
|
|
|
const {state} = this._history;
|
|
|
|
const {textSource, definitions} = termLookupResults;
|
2019-10-30 01:58:24 +00:00
|
|
|
|
2020-08-01 20:33:21 +00:00
|
|
|
const scannedElement = e.target;
|
|
|
|
const sentenceExtent = this._options.anki.sentenceExt;
|
|
|
|
const layoutAwareScan = this._options.scanning.layoutAwareScan;
|
2020-08-09 17:27:21 +00:00
|
|
|
const sentence = this._documentUtil.extractSentence(textSource, sentenceExtent, layoutAwareScan);
|
2019-10-30 01:58:24 +00:00
|
|
|
|
2020-08-01 20:33:21 +00:00
|
|
|
state.focusEntry = this._entryIndexFind(scannedElement);
|
|
|
|
state.scrollX = this._windowScroll.x;
|
|
|
|
state.scrollY = this._windowScroll.y;
|
|
|
|
this._historyStateUpdate(state);
|
2019-10-30 01:58:24 +00:00
|
|
|
|
2020-08-01 20:33:21 +00:00
|
|
|
const query = textSource.text();
|
|
|
|
const details = {
|
|
|
|
focus: false,
|
|
|
|
history: true,
|
|
|
|
params: this._createSearchParams('terms', query, false),
|
|
|
|
state: {
|
|
|
|
focusEntry: 0,
|
|
|
|
sentence,
|
2020-11-13 01:32:46 +00:00
|
|
|
optionsContext: state.optionsContext
|
2020-08-01 20:33:21 +00:00
|
|
|
},
|
|
|
|
content: {
|
|
|
|
definitions
|
|
|
|
}
|
|
|
|
};
|
|
|
|
this.setContent(details);
|
2019-10-30 01:58:24 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
async _termLookup(e) {
|
2020-08-01 20:33:21 +00:00
|
|
|
e.preventDefault();
|
2019-08-03 12:06:28 +00:00
|
|
|
|
2020-08-01 20:33:21 +00:00
|
|
|
const {length: scanLength, deepDomScan: deepScan, layoutAwareScan} = this._options.scanning;
|
2020-08-09 17:27:21 +00:00
|
|
|
const textSource = this._documentUtil.getRangeFromPoint(e.clientX, e.clientY, deepScan);
|
2020-08-01 20:33:21 +00:00
|
|
|
if (textSource === null) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-08-03 12:06:28 +00:00
|
|
|
|
2020-08-01 20:33:21 +00:00
|
|
|
let definitions, length;
|
|
|
|
try {
|
|
|
|
textSource.setEndOffset(scanLength, layoutAwareScan);
|
2019-08-03 12:06:28 +00:00
|
|
|
|
2020-08-01 20:33:21 +00:00
|
|
|
({definitions, length} = await api.termsFind(textSource.text(), {}, this.getOptionsContext()));
|
|
|
|
if (definitions.length === 0) {
|
|
|
|
return false;
|
2019-08-31 18:57:24 +00:00
|
|
|
}
|
2019-08-03 12:06:28 +00:00
|
|
|
|
2020-08-01 20:33:21 +00:00
|
|
|
textSource.setEndOffset(length, layoutAwareScan);
|
|
|
|
} finally {
|
|
|
|
textSource.cleanup();
|
2019-08-03 12:06:28 +00:00
|
|
|
}
|
2020-08-01 20:33:21 +00:00
|
|
|
|
|
|
|
return {textSource, definitions};
|
2019-08-03 12:06:28 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
_onAudioPlay(e) {
|
2017-03-19 22:45:30 +00:00
|
|
|
e.preventDefault();
|
2019-09-15 18:06:27 +00:00
|
|
|
const link = e.currentTarget;
|
|
|
|
const entry = link.closest('.entry');
|
2020-07-03 16:02:21 +00:00
|
|
|
const index = this._entryIndexFind(entry);
|
|
|
|
if (index < 0 || index >= this._definitions.length) { return; }
|
2020-02-14 01:42:25 +00:00
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
const expressionIndex = this._indexOf(entry.querySelectorAll('.term-expression .action-play-audio'), link);
|
|
|
|
this._audioPlay(
|
|
|
|
this._definitions[index],
|
2020-01-27 21:24:34 +00:00
|
|
|
// expressionIndex is used in audioPlay to detect result output mode
|
2020-07-03 16:02:21 +00:00
|
|
|
Math.max(expressionIndex, this._options.general.resultOutputMode === 'merge' ? 0 : -1),
|
2020-02-14 01:42:25 +00:00
|
|
|
index
|
2020-01-27 21:24:34 +00:00
|
|
|
);
|
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-07-03 16:02:21 +00:00
|
|
|
const index = this._entryIndexFind(link);
|
|
|
|
if (index < 0 || index >= this._definitions.length) { return; }
|
2020-02-14 01:42:25 +00:00
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
this._noteAdd(this._definitions[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-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-07 17:03:10 +00:00
|
|
|
data.audioEnabled = `${options.audio.enabled && options.audio.sources.length > 0}`;
|
2019-12-25 02:45:57 +00:00
|
|
|
data.compactGlossaries = `${options.general.compactGlossaries}`;
|
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-07-03 16:02:21 +00:00
|
|
|
_updateTheme(themeName) {
|
2019-10-12 21:06:03 +00:00
|
|
|
document.documentElement.dataset.yomichanTheme = themeName;
|
2019-10-12 18:20:54 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
_setInteractive(interactive) {
|
2019-10-12 03:24:27 +00:00
|
|
|
interactive = !!interactive;
|
2020-07-03 16:02:21 +00:00
|
|
|
if (this._interactive === interactive) { return; }
|
|
|
|
this._interactive = interactive;
|
2019-10-12 03:24:27 +00:00
|
|
|
|
|
|
|
if (interactive) {
|
2020-02-16 21:33:48 +00:00
|
|
|
const actionPrevious = document.querySelector('.action-previous');
|
|
|
|
const actionNext = document.querySelector('.action-next');
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
this._persistentEventListeners.addEventListener(document, 'keydown', this.onKeyDown.bind(this), false);
|
|
|
|
this._persistentEventListeners.addEventListener(document, 'wheel', this._onWheel.bind(this), {passive: false});
|
2020-02-16 21:33:48 +00:00
|
|
|
if (actionPrevious !== null) {
|
2020-07-03 16:02:21 +00:00
|
|
|
this._persistentEventListeners.addEventListener(actionPrevious, 'click', this._onSourceTermView.bind(this));
|
2020-02-16 21:33:48 +00:00
|
|
|
}
|
|
|
|
if (actionNext !== null) {
|
2020-07-03 16:02:21 +00:00
|
|
|
this._persistentEventListeners.addEventListener(actionNext, 'click', this._onNextTermView.bind(this));
|
2020-02-16 21:33:48 +00:00
|
|
|
}
|
2019-10-12 03:24:27 +00:00
|
|
|
} else {
|
2020-07-03 16:02:21 +00:00
|
|
|
this._persistentEventListeners.removeAllEventListeners();
|
2019-10-12 03:24:27 +00:00
|
|
|
}
|
2020-07-03 16:02:21 +00:00
|
|
|
this._setEventListenersActive(this._eventListenersActive);
|
2019-10-12 03:24:27 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
_setEventListenersActive(active) {
|
|
|
|
active = !!active && this._interactive;
|
|
|
|
if (this._eventListenersActive === active) { return; }
|
|
|
|
this._eventListenersActive = active;
|
2019-10-12 03:24:27 +00:00
|
|
|
|
|
|
|
if (active) {
|
2020-07-03 16:02:21 +00:00
|
|
|
this.addMultipleEventListeners('.action-add-note', 'click', this._onNoteAdd.bind(this));
|
|
|
|
this.addMultipleEventListeners('.action-view-note', 'click', this._onNoteView.bind(this));
|
|
|
|
this.addMultipleEventListeners('.action-play-audio', 'click', this._onAudioPlay.bind(this));
|
|
|
|
this.addMultipleEventListeners('.kanji-link', 'click', this._onKanjiLookup.bind(this));
|
2020-07-26 20:51:54 +00:00
|
|
|
if (this._options !== null && this._options.scanning.enablePopupSearch) {
|
2020-07-03 16:02:21 +00:00
|
|
|
this.addMultipleEventListeners('.term-glossary-item, .tag', 'mouseup', this._onGlossaryMouseUp.bind(this));
|
|
|
|
this.addMultipleEventListeners('.term-glossary-item, .tag', 'mousedown', this._onGlossaryMouseDown.bind(this));
|
|
|
|
this.addMultipleEventListeners('.term-glossary-item, .tag', 'mousemove', this._onGlossaryMouseMove.bind(this));
|
2019-10-12 03:24:27 +00:00
|
|
|
}
|
|
|
|
} else {
|
2020-07-03 16:02:21 +00:00
|
|
|
this._eventListeners.removeAllEventListeners();
|
2019-10-12 03:24:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-13 01:32:46 +00:00
|
|
|
async _findDefinitions(isTerms, source, urlSearchParams, optionsContext) {
|
2020-07-26 20:51:54 +00:00
|
|
|
if (isTerms) {
|
|
|
|
const findDetails = {};
|
|
|
|
if (urlSearchParams.get('wildcards') !== 'off') {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-01 20:22:00 +00:00
|
|
|
async _setContentTermsOrKanji(token, isTerms, urlSearchParams, eventArgs) {
|
|
|
|
let source = urlSearchParams.get('query');
|
2020-11-08 17:34:23 +00:00
|
|
|
if (!source) {
|
|
|
|
this._setQueryParserText('');
|
|
|
|
return false;
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2020-11-13 01:32:46 +00:00
|
|
|
let {sentence=null, optionsContext=null, focusEntry=null, scrollX=null, scrollY=null} = state;
|
|
|
|
if (!(typeof optionsContext === 'object' && optionsContext !== null)) {
|
|
|
|
optionsContext = this.getOptionsContext();
|
|
|
|
state.optionsContext = optionsContext;
|
|
|
|
changeHistory = true;
|
|
|
|
}
|
|
|
|
let {url} = optionsContext;
|
|
|
|
if (typeof url !== 'string') { url = window.location.href; }
|
|
|
|
sentence = this._getValidSentenceData(sentence);
|
|
|
|
|
2020-08-01 20:22:00 +00:00
|
|
|
source = this.postProcessQuery(source);
|
|
|
|
let full = urlSearchParams.get('full');
|
|
|
|
full = (full === null ? source : this.postProcessQuery(full));
|
|
|
|
this._setQueryParserText(full);
|
|
|
|
this._setTitleText(source);
|
|
|
|
|
|
|
|
let {definitions} = content;
|
|
|
|
if (!Array.isArray(definitions)) {
|
2020-11-13 01:32:46 +00:00
|
|
|
definitions = await this._findDefinitions(isTerms, source, urlSearchParams, optionsContext);
|
2020-08-01 20:22:00 +00:00
|
|
|
if (this._setContentToken !== token) { return true; }
|
|
|
|
content.definitions = definitions;
|
|
|
|
changeHistory = true;
|
|
|
|
}
|
|
|
|
|
2020-11-13 01:32:46 +00:00
|
|
|
await this._setOptionsContextIfDifferent(optionsContext);
|
|
|
|
if (this._setContentToken !== token) { return true; }
|
|
|
|
|
2020-08-01 20:22:00 +00:00
|
|
|
if (changeHistory) {
|
|
|
|
this._historyStateUpdate(state, content);
|
|
|
|
}
|
|
|
|
|
|
|
|
eventArgs.source = source;
|
|
|
|
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
|
|
|
|
2019-12-27 23:04:39 +00:00
|
|
|
for (const definition of definitions) {
|
2020-07-25 01:12:13 +00:00
|
|
|
definition.cloze = this._clozeBuild(sentence, isTerms ? definition.source : definition.character);
|
|
|
|
definition.url = url;
|
2019-12-27 23:04:39 +00:00
|
|
|
}
|
2017-08-13 23:11:51 +00:00
|
|
|
|
2020-07-26 20:51:54 +00:00
|
|
|
this._updateNavigation(this._history.hasPrevious(), this._history.hasNext());
|
2020-07-03 16:02:21 +00:00
|
|
|
this._setNoContentVisible(definitions.length === 0);
|
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-08-01 20:22:00 +00:00
|
|
|
if (this._setContentToken !== token) { return true; }
|
2019-12-27 23:15:16 +00:00
|
|
|
}
|
2019-12-25 02:45:57 +00:00
|
|
|
|
2020-07-25 01:12:13 +00:00
|
|
|
const entry = (
|
|
|
|
isTerms ?
|
|
|
|
this._displayGenerator.createTermEntry(definitions[i]) :
|
|
|
|
this._displayGenerator.createKanjiEntry(definitions[i])
|
|
|
|
);
|
2019-12-27 23:15:16 +00:00
|
|
|
container.appendChild(entry);
|
|
|
|
}
|
2019-12-25 02:45:57 +00:00
|
|
|
|
2020-07-26 22:49:38 +00:00
|
|
|
if (typeof focusEntry === 'number') {
|
|
|
|
this._focusEntry(focusEntry, false);
|
|
|
|
}
|
|
|
|
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
|
|
|
|
2020-07-25 01:12:13 +00:00
|
|
|
if (
|
|
|
|
isTerms &&
|
|
|
|
this._options.audio.enabled &&
|
|
|
|
this._options.audio.autoPlay
|
|
|
|
) {
|
2019-12-27 23:04:39 +00:00
|
|
|
this.autoPlayAudio();
|
|
|
|
}
|
2017-08-13 23:11:51 +00:00
|
|
|
|
2020-11-08 21:48:15 +00:00
|
|
|
this._updateAdderButtons(token, isTerms, definitions);
|
2017-12-16 18:56:53 +00:00
|
|
|
|
2020-08-01 20:22:00 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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-07-03 16:02:21 +00:00
|
|
|
this._updateNavigation(null, null);
|
|
|
|
this._setNoContentVisible(false);
|
2020-08-01 20:22:00 +00:00
|
|
|
this._setTitleText('');
|
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-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-08-01 20:22:00 +00:00
|
|
|
_setQueryParserText(text) {
|
|
|
|
if (this._fullQuery === text) { return; }
|
|
|
|
this._fullQuery = text;
|
|
|
|
if (!this._isQueryParserVisible()) { return; }
|
|
|
|
this._queryParser.setText(text);
|
|
|
|
}
|
|
|
|
|
|
|
|
_setTitleText(text) {
|
|
|
|
// Chrome limits title to 1024 characters
|
|
|
|
const ellipsis = '...';
|
|
|
|
const maxLength = this._defaultTitleMaxLength - this._defaultTitle.length;
|
|
|
|
if (text.length > maxLength) {
|
|
|
|
text = `${text.substring(0, Math.max(0, maxLength - maxLength))}${ellipsis}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
document.title = (
|
|
|
|
text.length === 0 ?
|
|
|
|
this._defaultTitle :
|
|
|
|
`${text} - ${this._defaultTitle}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
_updateNavigation(previous, next) {
|
2020-07-26 22:49:38 +00:00
|
|
|
if (this._navigationHeader === null) { return; }
|
|
|
|
this._navigationHeader.hidden = !(previous || next);
|
|
|
|
this._navigationHeader.dataset.hasPrevious = `${!!previous}`;
|
|
|
|
this._navigationHeader.dataset.hasNext = `${!!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) {
|
|
|
|
const noteContext = await this._getNoteContext();
|
|
|
|
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-09-10 20:05:17 +00:00
|
|
|
button.classList.toggle('disabled', !canAdd);
|
2019-12-27 23:04:39 +00:00
|
|
|
button.classList.remove('pending');
|
|
|
|
}
|
|
|
|
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) {
|
|
|
|
index = Math.min(index, this._definitions.length - 1);
|
2017-08-13 23:11:51 +00:00
|
|
|
index = Math.max(index, 0);
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
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-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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
_noteTryAdd(mode) {
|
|
|
|
const index = this._index;
|
|
|
|
if (index < 0 || index >= this._definitions.length) { return; }
|
2020-02-14 01:42:25 +00:00
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
const button = this._adderButtonFind(index, mode);
|
2019-09-20 02:03:26 +00:00
|
|
|
if (button !== null && !button.classList.contains('disabled')) {
|
2020-07-03 16:02:21 +00:00
|
|
|
this._noteAdd(this._definitions[index], mode);
|
2019-09-20 02:03:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
_noteTryView() {
|
|
|
|
const button = this._viewerButtonFind(this._index);
|
2019-09-20 02:03:26 +00:00
|
|
|
if (button !== null && !button.classList.contains('disabled')) {
|
2020-05-24 17:30:40 +00:00
|
|
|
api.noteView(button.dataset.noteId);
|
2019-09-20 02:03:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
async _noteAdd(definition, mode) {
|
2017-08-13 23:11:51 +00:00
|
|
|
try {
|
2019-09-15 18:16:23 +00:00
|
|
|
this.setSpinnerVisible(true);
|
2017-08-13 23:11:51 +00:00
|
|
|
|
2020-07-25 01:12:13 +00:00
|
|
|
const noteContext = await this._getNoteContext();
|
2020-09-10 22:03:46 +00:00
|
|
|
const noteId = await this._addDefinition(definition, mode, noteContext);
|
2017-07-02 01:27:49 +00:00
|
|
|
if (noteId) {
|
2020-07-03 16:02:21 +00:00
|
|
|
const index = this._definitions.indexOf(definition);
|
|
|
|
const adderButton = this._adderButtonFind(index, mode);
|
2019-09-15 22:29:11 +00:00
|
|
|
if (adderButton !== null) {
|
|
|
|
adderButton.classList.add('disabled');
|
|
|
|
}
|
2020-07-03 16:02:21 +00:00
|
|
|
this._viewerButtonShow(index, noteId);
|
2017-03-25 17:46:59 +00:00
|
|
|
} else {
|
2019-10-08 00:46:02 +00:00
|
|
|
throw new Error('Note could not be added');
|
2017-03-19 22:45:30 +00:00
|
|
|
}
|
2017-08-13 23:11:51 +00:00
|
|
|
} catch (e) {
|
|
|
|
this.onError(e);
|
|
|
|
} finally {
|
2019-09-15 18:16:23 +00:00
|
|
|
this.setSpinnerVisible(false);
|
2017-08-13 23:11:51 +00:00
|
|
|
}
|
2017-03-19 22:45:30 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
async _audioPlay(definition, expressionIndex, entryIndex) {
|
2017-08-13 23:11:51 +00:00
|
|
|
try {
|
2019-09-15 18:16:23 +00:00
|
|
|
this.setSpinnerVisible(true);
|
2017-03-05 01:30:10 +00:00
|
|
|
|
2020-09-10 15:57:38 +00:00
|
|
|
const {expression, reading} = expressionIndex === -1 ? definition : definition.expressions[expressionIndex];
|
2017-03-05 01:30:10 +00:00
|
|
|
|
2020-04-17 22:00:28 +00:00
|
|
|
this._stopPlayingAudio();
|
2017-08-13 23:11:51 +00:00
|
|
|
|
2020-04-10 20:38:53 +00:00
|
|
|
let audio, info;
|
2020-03-07 18:18:48 +00:00
|
|
|
try {
|
2020-07-03 16:02:21 +00:00
|
|
|
const {sources, textToSpeechVoice, customSourceUrl} = this._options.audio;
|
2020-04-10 20:38:53 +00:00
|
|
|
let index;
|
2020-09-26 17:41:26 +00:00
|
|
|
({audio, index} = await this._audioSystem.createDefinitionAudio(sources, expression, reading, {textToSpeechVoice, customSourceUrl}));
|
2020-04-10 20:38:53 +00:00
|
|
|
info = `From source ${1 + index}: ${sources[index]}`;
|
2020-03-07 18:18:48 +00:00
|
|
|
} catch (e) {
|
2020-07-03 16:02:21 +00:00
|
|
|
if (this._audioFallback === null) {
|
|
|
|
this._audioFallback = new Audio('/mixed/mp3/button.mp3');
|
2019-10-10 23:58:06 +00:00
|
|
|
}
|
2020-07-03 16:02:21 +00:00
|
|
|
audio = this._audioFallback;
|
2019-10-11 00:13:36 +00:00
|
|
|
info = 'Could not find audio';
|
|
|
|
}
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
const button = this._audioButtonFindImage(entryIndex, expressionIndex);
|
2019-10-11 00:13:36 +00:00
|
|
|
if (button !== null) {
|
|
|
|
let titleDefault = button.dataset.titleDefault;
|
|
|
|
if (!titleDefault) {
|
2019-11-25 19:21:19 +00:00
|
|
|
titleDefault = button.title || '';
|
2019-10-11 00:13:36 +00:00
|
|
|
button.dataset.titleDefault = titleDefault;
|
|
|
|
}
|
|
|
|
button.title = `${titleDefault}\n${info}`;
|
2017-03-23 04:57:16 +00:00
|
|
|
}
|
2019-10-10 23:58:06 +00:00
|
|
|
|
2020-04-17 22:00:28 +00:00
|
|
|
this._stopPlayingAudio();
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
const volume = Math.max(0.0, Math.min(1.0, this._options.audio.volume / 100.0));
|
|
|
|
this._audioPlaying = audio;
|
2019-10-10 23:58:06 +00:00
|
|
|
audio.currentTime = 0;
|
2020-06-21 19:51:36 +00:00
|
|
|
audio.volume = Number.isFinite(volume) ? volume : 1.0;
|
2020-04-17 21:48:55 +00:00
|
|
|
const playPromise = audio.play();
|
|
|
|
if (typeof playPromise !== 'undefined') {
|
|
|
|
try {
|
|
|
|
await playPromise;
|
|
|
|
} catch (e2) {
|
|
|
|
// NOP
|
|
|
|
}
|
|
|
|
}
|
2017-08-13 23:11:51 +00:00
|
|
|
} catch (e) {
|
|
|
|
this.onError(e);
|
|
|
|
} finally {
|
2019-09-15 18:16:23 +00:00
|
|
|
this.setSpinnerVisible(false);
|
2017-08-13 23:11:51 +00:00
|
|
|
}
|
2017-03-18 16:48:53 +00:00
|
|
|
}
|
|
|
|
|
2020-04-17 22:00:28 +00:00
|
|
|
_stopPlayingAudio() {
|
2020-07-03 16:02:21 +00:00
|
|
|
if (this._audioPlaying !== null) {
|
|
|
|
this._audioPlaying.pause();
|
|
|
|
this._audioPlaying = null;
|
2020-04-17 22:00:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
_getFirstExpressionIndex() {
|
|
|
|
return this._options.general.resultOutputMode === 'merge' ? 0 : -1;
|
2017-12-16 18:56:53 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
_getEntry(index) {
|
|
|
|
const entries = this._container.querySelectorAll('.entry');
|
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-07-03 16:02:21 +00:00
|
|
|
_clozeBuild({text, offset}, source) {
|
2019-11-26 15:53:21 +00:00
|
|
|
return {
|
|
|
|
sentence: text.trim(),
|
|
|
|
prefix: text.substring(0, offset).trim(),
|
|
|
|
body: text.substring(offset, offset + source.length),
|
|
|
|
suffix: text.substring(offset + source.length).trim()
|
2017-07-16 19:48:27 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
_entryIndexFind(element) {
|
2019-09-15 18:06:27 +00:00
|
|
|
const entry = element.closest('.entry');
|
2020-07-03 16:02:21 +00:00
|
|
|
return entry !== null ? this._indexOf(this._container.querySelectorAll('.entry'), entry) : -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;
|
|
|
|
}
|
|
|
|
viewerButton.classList.remove('pending', 'disabled');
|
|
|
|
viewerButton.dataset.noteId = noteId;
|
|
|
|
}
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
_audioButtonFindImage(index, expressionIndex) {
|
|
|
|
const entry = this._getEntry(index);
|
2020-04-10 20:43:57 +00:00
|
|
|
if (entry === null) { return null; }
|
|
|
|
|
|
|
|
const container = (
|
|
|
|
expressionIndex >= 0 ?
|
|
|
|
entry.querySelector(`.term-expression:nth-of-type(${expressionIndex + 1})`) :
|
|
|
|
entry
|
|
|
|
);
|
|
|
|
return container !== null ? container.querySelector('.action-play-audio>img') : null;
|
2019-10-11 00:13:36 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
_indexOf(nodeList, node) {
|
2019-09-15 18:06:27 +00:00
|
|
|
for (let i = 0, ii = nodeList.length; i < ii; ++i) {
|
|
|
|
if (nodeList[i] === node) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
2019-09-15 18:53:30 +00:00
|
|
|
|
2020-07-03 16:02:21 +00:00
|
|
|
_getElementTop(element) {
|
2019-09-15 22:44:49 +00:00
|
|
|
const elementRect = element.getBoundingClientRect();
|
|
|
|
const documentRect = document.documentElement.getBoundingClientRect();
|
|
|
|
return elementRect.top - documentRect.top;
|
|
|
|
}
|
2019-09-20 02:03:26 +00:00
|
|
|
|
2020-03-28 02:11:24 +00:00
|
|
|
async _getNoteContext() {
|
|
|
|
const documentTitle = await this.getDocumentTitle();
|
2020-03-15 21:32:31 +00:00
|
|
|
return {
|
|
|
|
document: {
|
2020-03-28 02:11:24 +00:00
|
|
|
title: 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
|
|
|
|
|
|
|
_playAudioCurrent() {
|
|
|
|
const index = this._index;
|
|
|
|
if (index < 0 || index >= this._definitions.length) { return; }
|
|
|
|
|
|
|
|
const entry = this._getEntry(index);
|
|
|
|
if (entry !== null && entry.dataset.type === 'term') {
|
|
|
|
this._audioPlay(this._definitions[index], this._getFirstExpressionIndex(), index);
|
|
|
|
}
|
|
|
|
}
|
2020-07-26 20:51:54 +00:00
|
|
|
|
|
|
|
_historyHasState() {
|
|
|
|
return isObject(this._history.state);
|
|
|
|
}
|
|
|
|
|
|
|
|
_historyStateUpdate(state, content) {
|
|
|
|
const historyChangeIgnorePre = this._historyChangeIgnore;
|
|
|
|
try {
|
|
|
|
this._historyChangeIgnore = true;
|
|
|
|
if (typeof state === 'undefined') { state = this._history.state; }
|
|
|
|
if (typeof content === 'undefined') { content = this._history.content; }
|
|
|
|
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
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
_updateQueryParserVisibility() {
|
|
|
|
this._queryParserContainer.hidden = !this._isQueryParserVisible();
|
|
|
|
}
|
|
|
|
|
|
|
|
_closePopups() {
|
|
|
|
yomichan.trigger('closePopups');
|
|
|
|
}
|
2020-08-09 17:11:41 +00:00
|
|
|
|
|
|
|
_updateMode() {
|
2020-08-24 22:26:04 +00:00
|
|
|
let mode = null;
|
|
|
|
try {
|
|
|
|
mode = sessionStorage.getItem('mode');
|
|
|
|
} catch (e) {
|
|
|
|
// Browsers can throw a SecurityError when cookie blocking is enabled.
|
|
|
|
}
|
2020-08-09 17:11:41 +00:00
|
|
|
this._setMode(mode, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
_setMode(mode, save) {
|
|
|
|
if (mode === this._mode) { return; }
|
|
|
|
if (save) {
|
2020-08-24 22:26:04 +00:00
|
|
|
try {
|
|
|
|
if (mode === null) {
|
|
|
|
sessionStorage.removeItem('mode');
|
|
|
|
} else {
|
|
|
|
sessionStorage.setItem('mode', mode);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
// Browsers can throw a SecurityError when cookie blocking is enabled.
|
2020-08-09 17:11:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
this._mode = mode;
|
|
|
|
this.trigger('modeChange', {mode});
|
|
|
|
}
|
2020-09-10 22:03:46 +00:00
|
|
|
|
|
|
|
async _getTemplates(options) {
|
|
|
|
let templates = options.anki.fieldTemplates;
|
|
|
|
if (typeof templates === 'string') { return templates; }
|
|
|
|
|
|
|
|
templates = this._defaultAnkiFieldTemplates;
|
|
|
|
if (typeof templates === 'string') { return templates; }
|
|
|
|
|
|
|
|
return await this._getDefaultTemplatesPromise();
|
|
|
|
}
|
|
|
|
|
|
|
|
_getDefaultTemplatesPromise() {
|
|
|
|
if (this._defaultAnkiFieldTemplatesPromise === null) {
|
|
|
|
this._defaultAnkiFieldTemplatesPromise = this._getDefaultTemplates();
|
|
|
|
this._defaultAnkiFieldTemplatesPromise.then(
|
|
|
|
() => { this._defaultAnkiFieldTemplatesPromise = null; },
|
|
|
|
() => {} // NOP
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return this._defaultAnkiFieldTemplatesPromise;
|
|
|
|
}
|
|
|
|
|
|
|
|
async _getDefaultTemplates() {
|
|
|
|
const value = await api.getDefaultAnkiFieldTemplates();
|
|
|
|
this._defaultAnkiFieldTemplates = value;
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
async _renderTemplate(template, data, marker) {
|
|
|
|
return await this._templateRenderer.render(template, data, marker);
|
|
|
|
}
|
|
|
|
|
|
|
|
async _addDefinition(definition, mode, context) {
|
|
|
|
const options = this._options;
|
|
|
|
const templates = await this._getTemplates(options);
|
|
|
|
const note = await this._createNote(definition, mode, context, options, templates, true);
|
|
|
|
return await api.addAnkiNote(note);
|
|
|
|
}
|
|
|
|
|
|
|
|
async _areDefinitionsAddable(definitions, modes, context) {
|
|
|
|
const options = this._options;
|
|
|
|
const templates = await this._getTemplates(options);
|
|
|
|
|
|
|
|
const modeCount = modes.length;
|
|
|
|
const {duplicateScope} = options.anki;
|
|
|
|
const notePromises = [];
|
|
|
|
for (const definition of definitions) {
|
|
|
|
for (const mode of modes) {
|
|
|
|
const notePromise = this._createNote(definition, mode, context, options, templates, false);
|
|
|
|
notePromises.push(notePromise);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const notes = await Promise.all(notePromises);
|
|
|
|
|
|
|
|
const infos = await api.getAnkiNoteInfo(notes, duplicateScope);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-09-10 22:03:46 +00:00
|
|
|
async _createNote(definition, mode, context, options, templates, injectMedia) {
|
|
|
|
const {
|
2020-11-13 01:34:11 +00:00
|
|
|
general: {resultOutputMode, compactGlossaries, compactTags},
|
2020-11-08 21:25:07 +00:00
|
|
|
anki: {tags, checkForDuplicates, duplicateScope, kanji, terms, screenshot: {format, quality}},
|
2020-09-10 22:03:46 +00:00
|
|
|
audio: {sources, customSourceUrl}
|
|
|
|
} = options;
|
|
|
|
const modeOptions = (mode === 'kanji') ? kanji : terms;
|
|
|
|
|
|
|
|
if (injectMedia) {
|
|
|
|
const timestamp = Date.now();
|
|
|
|
const ownerFrameId = this._ownerFrameId;
|
|
|
|
const {fields} = modeOptions;
|
2020-10-05 02:04:44 +00:00
|
|
|
const {expression, reading} = this._getDefinitionPrimaryExpressionAndReading(definition);
|
2020-09-10 22:03:46 +00:00
|
|
|
const audioDetails = (mode !== 'kanji' && this._ankiNoteBuilder.containsMarker(fields, 'audio') ? {sources, customSourceUrl} : null);
|
|
|
|
const screenshotDetails = (this._ankiNoteBuilder.containsMarker(fields, 'screenshot') ? {ownerFrameId, format, quality} : null);
|
2020-09-26 17:45:48 +00:00
|
|
|
const clipboardDetails = {
|
|
|
|
image: this._ankiNoteBuilder.containsMarker(fields, 'clipboard-image'),
|
|
|
|
text: this._ankiNoteBuilder.containsMarker(fields, 'clipboard-text')
|
|
|
|
};
|
|
|
|
const {screenshotFileName, clipboardImageFileName, clipboardText, audioFileName} = await api.injectAnkiNoteMedia(
|
2020-09-10 22:03:46 +00:00
|
|
|
expression,
|
|
|
|
reading,
|
|
|
|
timestamp,
|
|
|
|
audioDetails,
|
|
|
|
screenshotDetails,
|
2020-09-26 17:45:48 +00:00
|
|
|
clipboardDetails
|
2020-09-10 22:03:46 +00:00
|
|
|
);
|
|
|
|
if (screenshotFileName !== null) { definition.screenshotFileName = screenshotFileName; }
|
|
|
|
if (clipboardImageFileName !== null) { definition.clipboardImageFileName = clipboardImageFileName; }
|
|
|
|
if (audioFileName !== null) { definition.audioFileName = audioFileName; }
|
2020-09-26 17:45:48 +00:00
|
|
|
if (clipboardText !== null) { definition.clipboardText = clipboardText; }
|
2020-09-10 22:03:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return await this._ankiNoteBuilder.createNote({
|
|
|
|
definition,
|
|
|
|
mode,
|
|
|
|
context,
|
|
|
|
templates,
|
|
|
|
tags,
|
2020-11-08 21:25:07 +00:00
|
|
|
checkForDuplicates,
|
2020-09-10 22:03:46 +00:00
|
|
|
duplicateScope,
|
|
|
|
resultOutputMode,
|
|
|
|
compactGlossaries,
|
2020-11-13 01:34:11 +00:00
|
|
|
compactTags,
|
2020-09-10 22:03:46 +00:00
|
|
|
modeOptions
|
|
|
|
});
|
|
|
|
}
|
2020-09-26 17:41:26 +00:00
|
|
|
|
|
|
|
async _getAudioInfo(source, expression, reading, details) {
|
|
|
|
return await api.getDefinitionAudioInfo(source, expression, reading, details);
|
|
|
|
}
|
2020-10-05 02:04:44 +00:00
|
|
|
|
|
|
|
_getDefinitionPrimaryExpressionAndReading(definition) {
|
|
|
|
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)];
|
|
|
|
return {expression, reading};
|
|
|
|
}
|
2020-11-09 03:19:54 +00:00
|
|
|
|
|
|
|
_areSame(set, array) {
|
|
|
|
if (set.size !== array.size) { return false; }
|
|
|
|
for (const value of array) {
|
|
|
|
if (!set.has(value)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2020-11-13 01:32:46 +00:00
|
|
|
|
|
|
|
async _setOptionsContextIfDifferent(optionsContext) {
|
|
|
|
if (deepEqual(this._optionsContext, optionsContext)) { return; }
|
|
|
|
await this.setOptionsContext(optionsContext);
|
|
|
|
}
|
2017-03-05 01:30:10 +00:00
|
|
|
}
|