2017-03-05 01:30:10 +00:00
|
|
|
/*
|
2020-01-01 17:00:00 +00:00
|
|
|
* Copyright (C) 2016-2020 Alex Yatskov <alex@foosoft.net>
|
2017-03-05 01:30:10 +00:00
|
|
|
* Author: Alex Yatskov <alex@foosoft.net>
|
|
|
|
*
|
|
|
|
* 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-02-17 16:02:21 +00:00
|
|
|
/*global popupNestedInitialize, apiForward, apiGetMessageToken, Display*/
|
2017-03-05 01:30:10 +00:00
|
|
|
|
2017-08-15 02:55:04 +00:00
|
|
|
class DisplayFloat extends Display {
|
2017-03-05 01:30:10 +00:00
|
|
|
constructor() {
|
2019-09-15 18:16:23 +00:00
|
|
|
super(document.querySelector('#spinner'), document.querySelector('#definitions'));
|
2017-12-16 18:56:53 +00:00
|
|
|
this.autoPlayAudioTimer = null;
|
|
|
|
|
2019-09-07 17:58:19 +00:00
|
|
|
this.optionsContext = {
|
2019-09-10 23:55:14 +00:00
|
|
|
depth: 0,
|
|
|
|
url: window.location.href
|
2019-09-07 17:58:19 +00:00
|
|
|
};
|
|
|
|
|
2019-12-20 18:44:33 +00:00
|
|
|
this._orphaned = false;
|
2020-02-16 17:23:40 +00:00
|
|
|
this._prepareInvoked = false;
|
2020-02-17 16:02:21 +00:00
|
|
|
this._messageToken = null;
|
|
|
|
this._messageTokenPromise = null;
|
2019-12-20 18:44:33 +00:00
|
|
|
|
2020-02-27 01:02:50 +00:00
|
|
|
this._onKeyDownHandlers = new Map([
|
|
|
|
['C', (e) => {
|
|
|
|
if (e.ctrlKey && !window.getSelection().toString()) {
|
|
|
|
this.onSelectionCopy();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}]
|
|
|
|
]);
|
|
|
|
|
|
|
|
this._windowMessageHandlers = new Map([
|
|
|
|
['setContent', ({type, details}) => this.setContent(type, details)],
|
|
|
|
['clearAutoPlayTimer', () => this.clearAutoPlayTimer()],
|
|
|
|
['setCustomCss', ({css}) => this.setCustomCss(css)],
|
|
|
|
['prepare', ({options, popupInfo, url, childrenSupported, scale, uniqueId}) => this.prepare(options, popupInfo, url, childrenSupported, scale, uniqueId)],
|
|
|
|
['setContentScale', ({scale}) => this.setContentScale(scale)]
|
|
|
|
]);
|
|
|
|
|
2020-02-27 02:01:40 +00:00
|
|
|
yomichan.on('orphaned', this.onOrphaned.bind(this));
|
|
|
|
window.addEventListener('message', this.onMessage.bind(this), false);
|
2017-03-05 01:30:10 +00:00
|
|
|
}
|
|
|
|
|
2020-02-16 17:23:20 +00:00
|
|
|
async prepare(options, popupInfo, url, childrenSupported, scale, uniqueId) {
|
2020-02-16 17:23:40 +00:00
|
|
|
if (this._prepareInvoked) { return; }
|
|
|
|
this._prepareInvoked = true;
|
|
|
|
|
2020-02-12 23:59:26 +00:00
|
|
|
await super.prepare(options);
|
|
|
|
|
|
|
|
const {id, depth, parentFrameId} = popupInfo;
|
|
|
|
this.optionsContext.depth = depth;
|
|
|
|
this.optionsContext.url = url;
|
|
|
|
|
|
|
|
if (childrenSupported) {
|
|
|
|
popupNestedInitialize(id, depth, parentFrameId, url);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setContentScale(scale);
|
2020-02-13 11:18:54 +00:00
|
|
|
|
2020-02-16 17:23:20 +00:00
|
|
|
apiForward('popupPrepareCompleted', {uniqueId});
|
2020-02-12 23:59:26 +00:00
|
|
|
}
|
|
|
|
|
2017-08-13 23:11:51 +00:00
|
|
|
onError(error) {
|
2019-12-20 18:44:33 +00:00
|
|
|
if (this._orphaned) {
|
2019-12-27 22:42:36 +00:00
|
|
|
this.setContent('orphaned');
|
2017-03-05 01:30:10 +00:00
|
|
|
} else {
|
2019-10-09 01:33:10 +00:00
|
|
|
logError(error, true);
|
2017-03-05 01:30:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-20 18:44:33 +00:00
|
|
|
onOrphaned() {
|
|
|
|
this._orphaned = true;
|
|
|
|
}
|
|
|
|
|
2017-08-13 23:11:51 +00:00
|
|
|
onSearchClear() {
|
|
|
|
window.parent.postMessage('popupClose', '*');
|
2017-04-01 04:48:10 +00:00
|
|
|
}
|
|
|
|
|
2017-08-13 23:11:51 +00:00
|
|
|
onSelectionCopy() {
|
|
|
|
window.parent.postMessage('selectionCopy', '*');
|
2017-03-05 01:30:10 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 02:24:57 +00:00
|
|
|
onMessage(e) {
|
2020-02-17 16:02:21 +00:00
|
|
|
const data = e.data;
|
|
|
|
if (typeof data !== 'object' || data === null) { return; } // Invalid data
|
|
|
|
|
|
|
|
const token = data.token;
|
|
|
|
if (typeof token !== 'string') { return; } // Invalid data
|
|
|
|
|
|
|
|
if (this._messageToken === null) {
|
|
|
|
// Async
|
|
|
|
this.getMessageToken()
|
|
|
|
.then(
|
|
|
|
() => { this.handleAction(token, data); },
|
|
|
|
() => {}
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
// Sync
|
|
|
|
this.handleAction(token, data);
|
|
|
|
}
|
2017-03-05 01:30:10 +00:00
|
|
|
}
|
2017-04-01 04:48:10 +00:00
|
|
|
|
|
|
|
onKeyDown(e) {
|
2019-09-20 02:03:26 +00:00
|
|
|
const key = Display.getKeyFromEvent(e);
|
2020-02-27 01:02:50 +00:00
|
|
|
const handler = this._onKeyDownHandlers.get(key);
|
2019-12-12 02:53:27 +00:00
|
|
|
if (typeof handler === 'function') {
|
2020-02-27 01:02:50 +00:00
|
|
|
if (handler(e)) {
|
2019-09-20 02:03:26 +00:00
|
|
|
e.preventDefault();
|
2019-12-12 02:53:27 +00:00
|
|
|
return true;
|
2017-04-01 04:48:10 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-12 02:53:27 +00:00
|
|
|
return super.onKeyDown(e);
|
2017-04-01 04:48:10 +00:00
|
|
|
}
|
2017-12-16 18:56:53 +00:00
|
|
|
|
2020-02-17 16:02:21 +00:00
|
|
|
async getMessageToken() {
|
|
|
|
// this._messageTokenPromise is used to ensure that only one call to apiGetMessageToken is made.
|
|
|
|
if (this._messageTokenPromise === null) {
|
|
|
|
this._messageTokenPromise = apiGetMessageToken();
|
|
|
|
}
|
|
|
|
const messageToken = await this._messageTokenPromise;
|
|
|
|
if (this._messageToken === null) {
|
|
|
|
this._messageToken = messageToken;
|
|
|
|
}
|
|
|
|
this._messageTokenPromise = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
handleAction(token, {action, params}) {
|
|
|
|
if (token !== this._messageToken) {
|
|
|
|
// Invalid token
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-27 01:02:50 +00:00
|
|
|
const handler = this._windowMessageHandlers.get(action);
|
2020-02-17 16:02:21 +00:00
|
|
|
if (typeof handler !== 'function') { return; }
|
|
|
|
|
2020-02-27 01:02:50 +00:00
|
|
|
handler(params);
|
2020-02-17 16:02:21 +00:00
|
|
|
}
|
|
|
|
|
2019-10-12 18:20:54 +00:00
|
|
|
getOptionsContext() {
|
|
|
|
return this.optionsContext;
|
|
|
|
}
|
|
|
|
|
2017-12-16 18:56:53 +00:00
|
|
|
autoPlayAudio() {
|
|
|
|
this.clearAutoPlayTimer();
|
|
|
|
this.autoPlayAudioTimer = window.setTimeout(() => super.autoPlayAudio(), 400);
|
|
|
|
}
|
|
|
|
|
|
|
|
clearAutoPlayTimer() {
|
|
|
|
if (this.autoPlayAudioTimer) {
|
|
|
|
window.clearTimeout(this.autoPlayAudioTimer);
|
|
|
|
this.autoPlayAudioTimer = null;
|
|
|
|
}
|
|
|
|
}
|
2019-07-09 21:52:44 +00:00
|
|
|
|
2019-12-23 22:12:09 +00:00
|
|
|
setContentScale(scale) {
|
|
|
|
document.body.style.fontSize = `${scale}em`;
|
|
|
|
}
|
2017-08-15 02:55:04 +00:00
|
|
|
}
|
|
|
|
|
2019-12-21 04:20:42 +00:00
|
|
|
DisplayFloat.instance = new DisplayFloat();
|