2017-03-05 01:30:10 +00:00
|
|
|
/*
|
2020-04-10 18:06:55 +00:00
|
|
|
* Copyright (C) 2016-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
|
|
|
|
* Display
|
2020-05-24 17:30:40 +00:00
|
|
|
* api
|
2020-03-11 02:30:36 +00:00
|
|
|
* popupNestedInitialize
|
|
|
|
*/
|
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;
|
|
|
|
|
2020-05-06 23:27:21 +00:00
|
|
|
this._secret = yomichan.generateId(16);
|
|
|
|
this._token = null;
|
2020-03-13 21:23:08 +00:00
|
|
|
|
2019-12-20 18:44:33 +00:00
|
|
|
this._orphaned = false;
|
2020-05-06 23:27:21 +00:00
|
|
|
this._initializedNestedPopups = false;
|
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;
|
2020-03-01 17:02:43 +00:00
|
|
|
}],
|
|
|
|
...this._onKeyDownHandlers
|
2020-02-27 01:02:50 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
this._windowMessageHandlers = new Map([
|
2020-05-06 23:27:21 +00:00
|
|
|
['initialize', {handler: this._initialize.bind(this), authenticate: false}],
|
|
|
|
['configure', {handler: this._configure.bind(this)}],
|
|
|
|
['setOptionsContext', {handler: ({optionsContext}) => this.setOptionsContext(optionsContext)}],
|
|
|
|
['setContent', {handler: ({type, details}) => this.setContent(type, details)}],
|
|
|
|
['clearAutoPlayTimer', {handler: () => this.clearAutoPlayTimer()}],
|
|
|
|
['setCustomCss', {handler: ({css}) => this.setCustomCss(css)}],
|
|
|
|
['setContentScale', {handler: ({scale}) => this.setContentScale(scale)}]
|
2020-02-27 01:02:50 +00:00
|
|
|
]);
|
2017-03-05 01:30:10 +00:00
|
|
|
}
|
|
|
|
|
2020-05-06 23:27:21 +00:00
|
|
|
async prepare() {
|
2020-03-13 21:23:08 +00:00
|
|
|
await super.prepare();
|
2020-02-12 23:59:26 +00:00
|
|
|
|
2020-05-06 23:27:21 +00:00
|
|
|
yomichan.on('orphaned', this.onOrphaned.bind(this));
|
|
|
|
window.addEventListener('message', this.onMessage.bind(this), false);
|
2020-02-13 11:18:54 +00:00
|
|
|
|
2020-05-24 17:30:40 +00:00
|
|
|
api.broadcastTab('popupPrepared', {secret: this._secret});
|
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 {
|
2020-04-26 20:55:25 +00:00
|
|
|
yomichan.logError(error);
|
2017-03-05 01:30:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-20 18:44:33 +00:00
|
|
|
onOrphaned() {
|
|
|
|
this._orphaned = true;
|
|
|
|
}
|
|
|
|
|
2020-04-27 22:10:37 +00:00
|
|
|
onEscape() {
|
2017-08-13 23:11:51 +00:00
|
|
|
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;
|
2020-05-06 23:27:21 +00:00
|
|
|
if (typeof data !== 'object' || data === null) {
|
|
|
|
this._logMessageError(e, 'Invalid data');
|
|
|
|
return;
|
2020-02-17 16:02:21 +00:00
|
|
|
}
|
2017-04-01 04:48:10 +00:00
|
|
|
|
2020-05-06 23:27:21 +00:00
|
|
|
const action = data.action;
|
|
|
|
if (typeof action !== 'string') {
|
|
|
|
this._logMessageError(e, 'Invalid data');
|
|
|
|
return;
|
2020-02-17 16:02:21 +00:00
|
|
|
}
|
|
|
|
|
2020-05-06 23:27:21 +00:00
|
|
|
const handlerInfo = this._windowMessageHandlers.get(action);
|
|
|
|
if (typeof handlerInfo === 'undefined') {
|
|
|
|
this._logMessageError(e, `Invalid action: ${JSON.stringify(action)}`);
|
2020-02-17 16:02:21 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-06 23:27:21 +00:00
|
|
|
if (handlerInfo.authenticate !== false && !this._isMessageAuthenticated(data)) {
|
|
|
|
this._logMessageError(e, 'Invalid authentication');
|
|
|
|
return;
|
|
|
|
}
|
2020-02-17 16:02:21 +00:00
|
|
|
|
2020-05-06 23:27:21 +00:00
|
|
|
const handler = handlerInfo.handler;
|
|
|
|
handler(data.params);
|
2020-02-17 16:02:21 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2020-04-26 19:33:50 +00:00
|
|
|
async setOptionsContext(optionsContext) {
|
|
|
|
this.optionsContext = optionsContext;
|
|
|
|
await this.updateOptions();
|
|
|
|
}
|
|
|
|
|
2019-12-23 22:12:09 +00:00
|
|
|
setContentScale(scale) {
|
2020-05-02 17:00:46 +00:00
|
|
|
const body = document.body;
|
|
|
|
if (body === null) { return; }
|
|
|
|
body.style.fontSize = `${scale}em`;
|
2019-12-23 22:12:09 +00:00
|
|
|
}
|
2020-04-06 22:55:36 +00:00
|
|
|
|
|
|
|
async getDocumentTitle() {
|
|
|
|
try {
|
|
|
|
const uniqueId = yomichan.generateId(16);
|
|
|
|
|
|
|
|
const promise = yomichan.getTemporaryListenerResult(
|
|
|
|
chrome.runtime.onMessage,
|
|
|
|
({action, params}, {resolve}) => {
|
|
|
|
if (
|
|
|
|
action === 'documentInformationBroadcast' &&
|
|
|
|
isObject(params) &&
|
|
|
|
params.uniqueId === uniqueId &&
|
|
|
|
params.frameId === 0
|
|
|
|
) {
|
|
|
|
resolve(params);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
2000
|
|
|
|
);
|
2020-05-24 17:30:40 +00:00
|
|
|
api.broadcastTab('requestDocumentInformationBroadcast', {uniqueId});
|
2020-04-06 22:55:36 +00:00
|
|
|
|
|
|
|
const {title} = await promise;
|
|
|
|
return title;
|
|
|
|
} catch (e) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
2020-05-06 23:27:21 +00:00
|
|
|
|
|
|
|
_logMessageError(event, type) {
|
|
|
|
yomichan.logWarning(new Error(`Popup received invalid message from origin ${JSON.stringify(event.origin)}: ${type}`));
|
|
|
|
}
|
|
|
|
|
|
|
|
_initialize(params) {
|
|
|
|
if (this._token !== null) { return; } // Already initialized
|
|
|
|
if (!isObject(params)) { return; } // Invalid data
|
|
|
|
|
|
|
|
const secret = params.secret;
|
|
|
|
if (secret !== this._secret) { return; } // Invalid authentication
|
|
|
|
|
|
|
|
const {token, frameId} = params;
|
|
|
|
this._token = token;
|
|
|
|
|
2020-05-24 17:30:40 +00:00
|
|
|
api.sendMessageToFrame(frameId, 'popupInitialized', {secret, token});
|
2020-05-06 23:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async _configure({messageId, frameId, popupId, optionsContext, childrenSupported, scale}) {
|
|
|
|
this.optionsContext = optionsContext;
|
|
|
|
|
|
|
|
await this.updateOptions();
|
|
|
|
|
|
|
|
if (childrenSupported && !this._initializedNestedPopups) {
|
|
|
|
const {depth, url} = optionsContext;
|
|
|
|
popupNestedInitialize(popupId, depth, frameId, url);
|
|
|
|
this._initializedNestedPopups = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setContentScale(scale);
|
|
|
|
|
2020-05-24 17:30:40 +00:00
|
|
|
api.sendMessageToFrame(frameId, 'popupConfigured', {messageId});
|
2020-05-06 23:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_isMessageAuthenticated(message) {
|
|
|
|
return (
|
|
|
|
this._token !== null &&
|
|
|
|
this._token === message.token &&
|
|
|
|
this._secret === message.secret
|
|
|
|
);
|
|
|
|
}
|
2017-08-15 02:55:04 +00:00
|
|
|
}
|