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-07-08 23:58:06 +00:00
|
|
|
* FrameEndpoint
|
2020-06-21 20:14:05 +00:00
|
|
|
* Frontend
|
|
|
|
* PopupFactory
|
2020-05-24 17:30:40 +00:00
|
|
|
* api
|
2020-06-21 20:14:05 +00:00
|
|
|
* dynamicLoader
|
2020-03-11 02:30:36 +00:00
|
|
|
*/
|
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'));
|
2020-07-03 19:58:29 +00:00
|
|
|
this._autoPlayAudioTimer = null;
|
2020-06-21 20:14:05 +00:00
|
|
|
this._nestedPopupsPrepared = false;
|
2020-07-08 23:50:13 +00:00
|
|
|
this._ownerFrameId = null;
|
2020-07-08 23:58:06 +00:00
|
|
|
this._frameEndpoint = new FrameEndpoint();
|
2020-07-03 19:58:29 +00:00
|
|
|
this._windowMessageHandlers = new Map([
|
2020-07-11 02:13:59 +00:00
|
|
|
['configure', {async: true, handler: this._onMessageConfigure.bind(this)}],
|
|
|
|
['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)}],
|
|
|
|
['setContentScale', {async: false, handler: this._onMessageSetContentScale.bind(this)}]
|
2020-07-03 19:58:29 +00:00
|
|
|
]);
|
2019-12-20 18:44:33 +00:00
|
|
|
|
2020-07-09 00:02:20 +00:00
|
|
|
this.registerActions([
|
|
|
|
['copy-host-selection', () => this._copySelection()]
|
|
|
|
]);
|
|
|
|
this.registerHotkeys([
|
|
|
|
{key: 'C', modifiers: ['ctrl'], action: 'copy-host-selection'}
|
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-07-11 02:13:59 +00:00
|
|
|
api.crossFrame.registerHandlers([
|
|
|
|
['popupMessage', {async: 'dynamic', handler: this._onMessage.bind(this)}]
|
|
|
|
]);
|
2020-02-13 11:18:54 +00:00
|
|
|
|
2020-07-08 23:58:06 +00:00
|
|
|
this._frameEndpoint.signal();
|
2020-02-12 23:59:26 +00:00
|
|
|
}
|
|
|
|
|
2020-04-27 22:10:37 +00:00
|
|
|
onEscape() {
|
2020-07-08 23:50:13 +00:00
|
|
|
this._invoke('closePopup');
|
2017-04-01 04:48:10 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 19:58:29 +00:00
|
|
|
async setOptionsContext(optionsContext) {
|
|
|
|
super.setOptionsContext(optionsContext);
|
|
|
|
await this.updateOptions();
|
2017-03-05 01:30:10 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 19:58:29 +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
|
|
|
|
);
|
|
|
|
api.broadcastTab('requestDocumentInformationBroadcast', {uniqueId});
|
|
|
|
|
|
|
|
const {title} = await promise;
|
|
|
|
return title;
|
|
|
|
} catch (e) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
autoPlayAudio() {
|
|
|
|
this._clearAutoPlayTimer();
|
|
|
|
this._autoPlayAudioTimer = window.setTimeout(() => super.autoPlayAudio(), 400);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Message handling
|
|
|
|
|
2020-07-11 02:13:59 +00:00
|
|
|
_onMessage(data) {
|
|
|
|
if (!this._frameEndpoint.authenticate(data)) {
|
|
|
|
throw new Error('Invalid authentication');
|
2020-02-17 16:02:21 +00:00
|
|
|
}
|
|
|
|
|
2020-07-11 02:13:59 +00:00
|
|
|
const {action, params} = data.data;
|
2020-05-06 23:27:21 +00:00
|
|
|
const handlerInfo = this._windowMessageHandlers.get(action);
|
|
|
|
if (typeof handlerInfo === 'undefined') {
|
2020-07-11 02:13:59 +00:00
|
|
|
throw new Error(`Invalid action: ${action}`);
|
2020-02-17 16:02:21 +00:00
|
|
|
}
|
|
|
|
|
2020-07-11 02:13:59 +00:00
|
|
|
const {async, handler} = handlerInfo;
|
|
|
|
const result = handler(params);
|
|
|
|
return {async, result};
|
2020-02-17 16:02:21 +00:00
|
|
|
}
|
|
|
|
|
2020-07-11 02:13:59 +00:00
|
|
|
async _onMessageConfigure({frameId, ownerFrameId, popupId, optionsContext, childrenSupported, scale}) {
|
2020-07-08 23:50:13 +00:00
|
|
|
this._ownerFrameId = ownerFrameId;
|
2020-07-03 19:58:29 +00:00
|
|
|
this.setOptionsContext(optionsContext);
|
|
|
|
|
|
|
|
await this.updateOptions();
|
|
|
|
|
|
|
|
if (childrenSupported && !this._nestedPopupsPrepared) {
|
|
|
|
const {depth, url} = optionsContext;
|
|
|
|
this._prepareNestedPopups(popupId, depth, frameId, url);
|
|
|
|
this._nestedPopupsPrepared = true;
|
2017-12-16 18:56:53 +00:00
|
|
|
}
|
2020-07-03 19:58:29 +00:00
|
|
|
|
|
|
|
this._setContentScale(scale);
|
2017-12-16 18:56:53 +00:00
|
|
|
}
|
2019-07-09 21:52:44 +00:00
|
|
|
|
2020-07-03 19:58:29 +00:00
|
|
|
_onMessageSetOptionsContext({optionsContext}) {
|
|
|
|
this.setOptionsContext(optionsContext);
|
2020-04-26 19:33:50 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 19:58:29 +00:00
|
|
|
_onMessageSetContent({type, details}) {
|
|
|
|
this.setContent(type, details);
|
2019-12-23 22:12:09 +00:00
|
|
|
}
|
2020-04-06 22:55:36 +00:00
|
|
|
|
2020-07-03 19:58:29 +00:00
|
|
|
_onMessageClearAutoPlayTimer() {
|
|
|
|
this._clearAutoPlayTimer.bind(this);
|
|
|
|
}
|
2020-04-06 22:55:36 +00:00
|
|
|
|
2020-07-03 19:58:29 +00:00
|
|
|
_onMessageSetCustomCss({css}) {
|
|
|
|
this.setCustomCss(css);
|
|
|
|
}
|
2020-04-06 22:55:36 +00:00
|
|
|
|
2020-07-03 19:58:29 +00:00
|
|
|
_onMessageSetContentScale({scale}) {
|
|
|
|
this._setContentScale(scale);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Private
|
|
|
|
|
|
|
|
_copySelection() {
|
2020-07-09 00:02:20 +00:00
|
|
|
if (window.getSelection().toString()) { return false; }
|
2020-07-08 23:50:13 +00:00
|
|
|
this._invoke('copySelection');
|
2020-07-09 00:02:20 +00:00
|
|
|
return true;
|
2020-07-03 19:58:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_clearAutoPlayTimer() {
|
|
|
|
if (this._autoPlayAudioTimer) {
|
|
|
|
window.clearTimeout(this._autoPlayAudioTimer);
|
|
|
|
this._autoPlayAudioTimer = null;
|
2020-04-06 22:55:36 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-06 23:27:21 +00:00
|
|
|
|
2020-07-03 19:58:29 +00:00
|
|
|
_setContentScale(scale) {
|
|
|
|
const body = document.body;
|
|
|
|
if (body === null) { return; }
|
|
|
|
body.style.fontSize = `${scale}em`;
|
|
|
|
}
|
|
|
|
|
2020-06-21 20:14:05 +00:00
|
|
|
async _prepareNestedPopups(id, depth, parentFrameId, url) {
|
|
|
|
let complete = false;
|
|
|
|
|
|
|
|
const onOptionsUpdated = async () => {
|
2020-07-03 16:02:21 +00:00
|
|
|
const optionsContext = this.getOptionsContext();
|
2020-06-21 20:14:05 +00:00
|
|
|
const options = await api.optionsGet(optionsContext);
|
|
|
|
const maxPopupDepthExceeded = !(typeof depth === 'number' && depth < options.scanning.popupNestingMaxDepth);
|
|
|
|
if (maxPopupDepthExceeded || complete) { return; }
|
|
|
|
|
|
|
|
complete = true;
|
|
|
|
yomichan.off('optionsUpdated', onOptionsUpdated);
|
|
|
|
|
|
|
|
try {
|
|
|
|
await this._setupNestedPopups(id, depth, parentFrameId, url);
|
|
|
|
} catch (e) {
|
|
|
|
yomichan.logError(e);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
yomichan.on('optionsUpdated', onOptionsUpdated);
|
|
|
|
|
|
|
|
await onOptionsUpdated();
|
|
|
|
}
|
|
|
|
|
|
|
|
async _setupNestedPopups(id, depth, parentFrameId, url) {
|
|
|
|
await dynamicLoader.loadScripts([
|
|
|
|
'/mixed/js/text-scanner.js',
|
2020-07-08 23:58:06 +00:00
|
|
|
'/mixed/js/frame-client.js',
|
2020-06-21 20:14:05 +00:00
|
|
|
'/fg/js/popup.js',
|
|
|
|
'/fg/js/popup-proxy.js',
|
|
|
|
'/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);
|
2020-06-25 22:32:29 +00:00
|
|
|
popupFactory.prepare();
|
2020-06-21 20:14:05 +00:00
|
|
|
|
|
|
|
const frontend = new Frontend(
|
|
|
|
frameId,
|
|
|
|
popupFactory,
|
|
|
|
{
|
|
|
|
id,
|
|
|
|
depth,
|
|
|
|
parentFrameId,
|
|
|
|
url,
|
|
|
|
proxy: true
|
|
|
|
}
|
|
|
|
);
|
|
|
|
await frontend.prepare();
|
|
|
|
}
|
2020-07-08 23:50:13 +00:00
|
|
|
|
|
|
|
_invoke(action, params={}) {
|
|
|
|
return api.crossFrame.invoke(this._ownerFrameId, action, params);
|
|
|
|
}
|
2017-08-15 02:55:04 +00:00
|
|
|
}
|