2020-06-28 15:26:43 +00:00
|
|
|
/*
|
2022-02-03 01:43:10 +00:00
|
|
|
* Copyright (C) 2020-2022 Yomichan Authors
|
2020-06-28 15:26:43 +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
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2021-02-14 20:53:35 +00:00
|
|
|
/* global
|
|
|
|
* API
|
|
|
|
* CrossFrameAPI
|
|
|
|
*/
|
|
|
|
|
2020-06-28 16:40:02 +00:00
|
|
|
// Set up chrome alias if it's not available (Edge Legacy)
|
|
|
|
if ((() => {
|
|
|
|
let hasChrome = false;
|
|
|
|
let hasBrowser = false;
|
|
|
|
try {
|
|
|
|
hasChrome = (typeof chrome === 'object' && chrome !== null && typeof chrome.runtime !== 'undefined');
|
|
|
|
} catch (e) {
|
|
|
|
// NOP
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
hasBrowser = (typeof browser === 'object' && browser !== null && typeof browser.runtime !== 'undefined');
|
|
|
|
} catch (e) {
|
|
|
|
// NOP
|
|
|
|
}
|
|
|
|
return (hasBrowser && !hasChrome);
|
|
|
|
})()) {
|
|
|
|
chrome = browser;
|
|
|
|
}
|
|
|
|
|
2021-11-01 01:45:57 +00:00
|
|
|
/**
|
|
|
|
* The Yomichan class is a core component through which various APIs are handled and invoked.
|
|
|
|
*/
|
2021-02-14 20:53:35 +00:00
|
|
|
class Yomichan extends EventDispatcher {
|
2021-11-01 01:45:57 +00:00
|
|
|
/**
|
|
|
|
* Creates a new instance. The instance should not be used until it has been fully prepare()'d.
|
|
|
|
*/
|
2021-02-14 20:53:35 +00:00
|
|
|
constructor() {
|
|
|
|
super();
|
2020-07-02 01:06:25 +00:00
|
|
|
|
2021-02-14 20:53:35 +00:00
|
|
|
try {
|
|
|
|
const manifest = chrome.runtime.getManifest();
|
|
|
|
this._extensionName = `${manifest.name} v${manifest.version}`;
|
|
|
|
} catch (e) {
|
2021-11-21 17:27:29 +00:00
|
|
|
this._extensionName = 'Yomichan';
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
this._extensionUrlBase = chrome.runtime.getURL('/');
|
|
|
|
} catch (e) {
|
|
|
|
this._extensionUrlBase = null;
|
2020-06-28 15:26:43 +00:00
|
|
|
}
|
|
|
|
|
2021-02-14 20:53:35 +00:00
|
|
|
this._isBackground = null;
|
|
|
|
this._api = null;
|
|
|
|
this._crossFrame = null;
|
|
|
|
this._isExtensionUnloaded = false;
|
|
|
|
this._isTriggeringExtensionUnloaded = false;
|
|
|
|
this._isReady = false;
|
|
|
|
|
|
|
|
const {promise, resolve} = deferPromise();
|
|
|
|
this._isBackendReadyPromise = promise;
|
|
|
|
this._isBackendReadyPromiseResolve = resolve;
|
|
|
|
|
|
|
|
this._messageHandlers = new Map([
|
2021-11-21 20:54:58 +00:00
|
|
|
['Yomichan.isReady', {async: false, handler: this._onMessageIsReady.bind(this)}],
|
|
|
|
['Yomichan.backendReady', {async: false, handler: this._onMessageBackendReady.bind(this)}],
|
|
|
|
['Yomichan.getUrl', {async: false, handler: this._onMessageGetUrl.bind(this)}],
|
|
|
|
['Yomichan.optionsUpdated', {async: false, handler: this._onMessageOptionsUpdated.bind(this)}],
|
|
|
|
['Yomichan.databaseUpdated', {async: false, handler: this._onMessageDatabaseUpdated.bind(this)}],
|
|
|
|
['Yomichan.zoomChanged', {async: false, handler: this._onMessageZoomChanged.bind(this)}]
|
2021-02-14 20:53:35 +00:00
|
|
|
]);
|
|
|
|
}
|
2020-06-28 15:26:43 +00:00
|
|
|
|
2021-11-01 01:45:57 +00:00
|
|
|
/**
|
|
|
|
* Whether the current frame is the background page/service worker or not.
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
2021-02-14 20:53:35 +00:00
|
|
|
get isBackground() {
|
|
|
|
return this._isBackground;
|
|
|
|
}
|
2020-06-28 15:26:43 +00:00
|
|
|
|
2021-11-01 01:45:57 +00:00
|
|
|
/**
|
|
|
|
* Whether or not the extension is unloaded.
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
2021-02-14 20:53:35 +00:00
|
|
|
get isExtensionUnloaded() {
|
|
|
|
return this._isExtensionUnloaded;
|
|
|
|
}
|
2020-07-18 21:11:38 +00:00
|
|
|
|
2021-11-01 01:45:57 +00:00
|
|
|
/**
|
|
|
|
* Gets the API instance for communicating with the backend.
|
|
|
|
* This value will be null on the background page/service worker.
|
|
|
|
* @type {API}
|
|
|
|
*/
|
2021-02-14 20:53:35 +00:00
|
|
|
get api() {
|
|
|
|
return this._api;
|
|
|
|
}
|
2020-06-28 15:26:43 +00:00
|
|
|
|
2021-11-01 01:45:57 +00:00
|
|
|
/**
|
|
|
|
* Gets the CrossFrameAPI instance for communicating with different frames.
|
|
|
|
* This value will be null on the background page/service worker.
|
|
|
|
* @type {CrossFrameAPI}
|
|
|
|
*/
|
2021-02-14 20:53:35 +00:00
|
|
|
get crossFrame() {
|
|
|
|
return this._crossFrame;
|
|
|
|
}
|
2020-06-28 15:26:43 +00:00
|
|
|
|
2021-11-01 01:45:57 +00:00
|
|
|
/**
|
|
|
|
* Prepares the instance for use.
|
|
|
|
* @param {boolean} [isBackground=false] Assigns whether this instance is being used from the background page/service worker.
|
|
|
|
*/
|
2021-02-14 20:53:35 +00:00
|
|
|
async prepare(isBackground=false) {
|
|
|
|
this._isBackground = isBackground;
|
|
|
|
chrome.runtime.onMessage.addListener(this._onMessage.bind(this));
|
2020-06-28 15:26:43 +00:00
|
|
|
|
2021-02-14 20:53:35 +00:00
|
|
|
if (!isBackground) {
|
|
|
|
this._api = new API(this);
|
2020-06-28 15:26:43 +00:00
|
|
|
|
2021-02-14 20:53:35 +00:00
|
|
|
this._crossFrame = new CrossFrameAPI();
|
|
|
|
this._crossFrame.prepare();
|
2020-06-28 15:26:43 +00:00
|
|
|
|
2021-02-14 20:53:35 +00:00
|
|
|
this.sendMessage({action: 'requestBackendReadySignal'});
|
|
|
|
await this._isBackendReadyPromise;
|
2020-06-28 15:26:43 +00:00
|
|
|
|
2021-02-14 22:52:01 +00:00
|
|
|
log.on('log', this._onForwardLog.bind(this));
|
2020-06-28 15:26:43 +00:00
|
|
|
}
|
2021-02-14 20:53:35 +00:00
|
|
|
}
|
2020-06-28 15:26:43 +00:00
|
|
|
|
2021-11-01 01:45:57 +00:00
|
|
|
/**
|
|
|
|
* Sends a message to the backend indicating that the frame is ready and all script
|
|
|
|
* setup has completed.
|
|
|
|
*/
|
2021-02-14 20:53:35 +00:00
|
|
|
ready() {
|
|
|
|
this._isReady = true;
|
|
|
|
this.sendMessage({action: 'yomichanReady'});
|
|
|
|
}
|
2020-06-28 15:26:43 +00:00
|
|
|
|
2021-11-01 01:45:57 +00:00
|
|
|
/**
|
|
|
|
* Checks whether or not a URL is an extension URL.
|
|
|
|
* @param {string} url The URL to check.
|
|
|
|
* @returns true if the URL is an extension URL, false otherwise.
|
|
|
|
*/
|
2021-02-14 20:53:35 +00:00
|
|
|
isExtensionUrl(url) {
|
2021-11-21 17:27:29 +00:00
|
|
|
return this._extensionUrlBase !== null && url.startsWith(this._extensionUrlBase);
|
2021-02-14 20:53:35 +00:00
|
|
|
}
|
2020-06-28 15:26:43 +00:00
|
|
|
|
2021-11-01 01:45:57 +00:00
|
|
|
/**
|
|
|
|
* Runs chrome.runtime.sendMessage() with additional exception handling events.
|
|
|
|
* @param {...*} args The arguments to be passed to chrome.runtime.sendMessage().
|
|
|
|
* @returns {void} The result of the chrome.runtime.sendMessage() call.
|
|
|
|
*/
|
2021-02-14 20:53:35 +00:00
|
|
|
sendMessage(...args) {
|
|
|
|
try {
|
|
|
|
return chrome.runtime.sendMessage(...args);
|
|
|
|
} catch (e) {
|
|
|
|
this.triggerExtensionUnloaded();
|
|
|
|
throw e;
|
2020-06-28 15:26:43 +00:00
|
|
|
}
|
2021-02-14 20:53:35 +00:00
|
|
|
}
|
2020-06-28 15:26:43 +00:00
|
|
|
|
2021-11-01 01:45:57 +00:00
|
|
|
/**
|
|
|
|
* Runs chrome.runtime.connect() with additional exception handling events.
|
|
|
|
* @param {...*} args The arguments to be passed to chrome.runtime.connect().
|
|
|
|
* @returns {Port} The resulting port.
|
|
|
|
*/
|
2021-02-14 20:53:35 +00:00
|
|
|
connect(...args) {
|
|
|
|
try {
|
|
|
|
return chrome.runtime.connect(...args);
|
|
|
|
} catch (e) {
|
|
|
|
this.triggerExtensionUnloaded();
|
|
|
|
throw e;
|
2020-06-28 15:26:43 +00:00
|
|
|
}
|
2021-02-14 20:53:35 +00:00
|
|
|
}
|
2020-06-28 15:26:43 +00:00
|
|
|
|
2021-11-01 01:45:57 +00:00
|
|
|
/**
|
|
|
|
* Runs chrome.runtime.connect() with additional exception handling events.
|
|
|
|
*/
|
2021-02-14 20:53:35 +00:00
|
|
|
triggerExtensionUnloaded() {
|
|
|
|
this._isExtensionUnloaded = true;
|
|
|
|
if (this._isTriggeringExtensionUnloaded) { return; }
|
|
|
|
try {
|
|
|
|
this._isTriggeringExtensionUnloaded = true;
|
|
|
|
this.trigger('extensionUnloaded');
|
|
|
|
} finally {
|
|
|
|
this._isTriggeringExtensionUnloaded = false;
|
2020-06-28 15:26:43 +00:00
|
|
|
}
|
2021-02-14 20:53:35 +00:00
|
|
|
}
|
2020-06-28 15:26:43 +00:00
|
|
|
|
2021-02-14 20:53:35 +00:00
|
|
|
// Private
|
|
|
|
|
|
|
|
_getUrl() {
|
|
|
|
return location.href;
|
|
|
|
}
|
|
|
|
|
|
|
|
_getLogContext() {
|
|
|
|
return {url: this._getUrl()};
|
|
|
|
}
|
|
|
|
|
|
|
|
_onMessage({action, params}, sender, callback) {
|
|
|
|
const messageHandler = this._messageHandlers.get(action);
|
|
|
|
if (typeof messageHandler === 'undefined') { return false; }
|
2021-02-14 23:18:02 +00:00
|
|
|
return invokeMessageHandler(messageHandler, params, callback, sender);
|
2021-02-14 20:53:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_onMessageIsReady() {
|
|
|
|
return this._isReady;
|
|
|
|
}
|
|
|
|
|
|
|
|
_onMessageBackendReady() {
|
|
|
|
if (this._isBackendReadyPromiseResolve === null) { return; }
|
|
|
|
this._isBackendReadyPromiseResolve();
|
|
|
|
this._isBackendReadyPromiseResolve = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
_onMessageGetUrl() {
|
|
|
|
return {url: this._getUrl()};
|
|
|
|
}
|
|
|
|
|
|
|
|
_onMessageOptionsUpdated({source}) {
|
|
|
|
this.trigger('optionsUpdated', {source});
|
|
|
|
}
|
2020-09-13 22:43:44 +00:00
|
|
|
|
2021-02-14 20:53:35 +00:00
|
|
|
_onMessageDatabaseUpdated({type, cause}) {
|
|
|
|
this.trigger('databaseUpdated', {type, cause});
|
|
|
|
}
|
|
|
|
|
|
|
|
_onMessageZoomChanged({oldZoomFactor, newZoomFactor}) {
|
|
|
|
this.trigger('zoomChanged', {oldZoomFactor, newZoomFactor});
|
|
|
|
}
|
|
|
|
|
|
|
|
async _onForwardLog({error, level, context}) {
|
|
|
|
try {
|
|
|
|
await this._api.log(serializeError(error), level, context);
|
|
|
|
} catch (e) {
|
|
|
|
// NOP
|
2020-06-28 15:26:43 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-14 20:53:35 +00:00
|
|
|
}
|
2020-06-28 15:26:43 +00:00
|
|
|
|
2021-11-01 01:45:57 +00:00
|
|
|
/**
|
|
|
|
* The default Yomichan class instance.
|
|
|
|
*/
|
2021-02-14 20:53:35 +00:00
|
|
|
const yomichan = new Yomichan();
|