2021-03-06 03:35:23 +01:00
|
|
|
import Debug from './conf/Debug';
|
|
|
|
import ExtensionMode from '../common/enums/ExtensionMode.enum';
|
|
|
|
import Settings from './lib/Settings';
|
|
|
|
import Comms from './lib/comms/Comms';
|
|
|
|
import CommsClient from './lib/comms/CommsClient';
|
|
|
|
import PageInfo from './lib/video-data/PageInfo';
|
|
|
|
import Logger, { baseLoggingOptions } from './lib/Logger';
|
2021-08-26 01:34:59 +02:00
|
|
|
import UWGlobals from './lib/UWGlobals';
|
2022-07-31 00:15:28 +02:00
|
|
|
import EventBus from './lib/EventBus';
|
2022-09-28 01:18:58 +02:00
|
|
|
import KeyboardHandler from './lib/kbm/KeyboardHandler';
|
2023-01-07 18:57:47 +01:00
|
|
|
import { SiteSettings } from './lib/settings/SiteSettings';
|
2021-03-06 03:35:23 +01:00
|
|
|
|
|
|
|
export default class UWContent {
|
|
|
|
pageInfo: PageInfo;
|
|
|
|
comms: CommsClient;
|
|
|
|
settings: Settings;
|
2023-01-07 18:57:47 +01:00
|
|
|
siteSettings: SiteSettings;
|
2022-09-28 00:38:36 +02:00
|
|
|
keyboardHandler: KeyboardHandler;
|
2021-03-06 03:35:23 +01:00
|
|
|
logger: Logger;
|
2022-07-31 00:15:28 +02:00
|
|
|
eventBus: EventBus;
|
2023-03-29 22:07:50 +02:00
|
|
|
isIframe: boolean = false;
|
2021-03-06 03:35:23 +01:00
|
|
|
|
|
|
|
commsHandlers: {
|
|
|
|
[x: string]: ((a: any, b?: any) => void | Promise<void>)[]
|
|
|
|
} = {
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(){
|
2023-03-29 22:07:50 +02:00
|
|
|
this.isIframe = window.self !== window.top
|
2021-03-06 03:35:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
reloadSettings() {
|
2021-04-12 20:54:26 +02:00
|
|
|
try {
|
|
|
|
this.logger.log('info', 'debug', 'Things happened in the popup. Will reload extension settings.');
|
|
|
|
this.init();
|
|
|
|
} catch (e) {
|
|
|
|
console.warn('Ultrawidify: settings reload failed. This probably shouldn\'t outright kill the extension, but page reload is recommended.');
|
|
|
|
}
|
2021-03-06 03:35:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async init(){
|
|
|
|
try {
|
2021-04-12 20:54:26 +02:00
|
|
|
if (Debug.debug) {
|
|
|
|
console.log("[uw::main] loading configuration ...");
|
|
|
|
}
|
2021-10-19 22:49:28 +02:00
|
|
|
|
2021-04-12 20:54:26 +02:00
|
|
|
// logger init is the first thing that needs to run
|
|
|
|
try {
|
|
|
|
if (!this.logger) {
|
|
|
|
this.logger = new Logger();
|
|
|
|
await this.logger.init(baseLoggingOptions);
|
2021-03-06 03:35:23 +01:00
|
|
|
}
|
2021-04-12 20:54:26 +02:00
|
|
|
} catch (e) {
|
|
|
|
console.error("logger init failed!", e)
|
2021-03-06 03:35:23 +01:00
|
|
|
}
|
|
|
|
|
2021-04-12 20:54:26 +02:00
|
|
|
// init() is re-run any time settings change
|
|
|
|
if (this.comms) {
|
|
|
|
this.comms.destroy();
|
|
|
|
}
|
2022-07-31 00:15:28 +02:00
|
|
|
if (this.eventBus) {
|
|
|
|
this.eventBus.destroy();
|
|
|
|
}
|
2021-04-12 20:54:26 +02:00
|
|
|
if (!this.settings) {
|
|
|
|
this.settings = new Settings({
|
|
|
|
onSettingsChanged: () => this.reloadSettings(),
|
|
|
|
logger: this.logger
|
|
|
|
});
|
|
|
|
await this.settings.init();
|
2023-01-07 18:57:47 +01:00
|
|
|
this.siteSettings = this.settings.getSiteSettings();
|
2021-04-12 20:54:26 +02:00
|
|
|
}
|
2021-10-19 22:49:28 +02:00
|
|
|
|
2022-07-31 00:15:28 +02:00
|
|
|
this.eventBus = new EventBus();
|
|
|
|
this.eventBus.subscribe(
|
|
|
|
'uw-restart',
|
|
|
|
{
|
|
|
|
function: () => this.initPhase2()
|
|
|
|
}
|
|
|
|
);
|
2023-03-29 22:07:50 +02:00
|
|
|
this.eventBus.subscribe(
|
|
|
|
'uw-show-ui',
|
|
|
|
{
|
|
|
|
function: () => {}
|
|
|
|
}
|
|
|
|
);
|
2022-07-31 00:15:28 +02:00
|
|
|
this.comms = new CommsClient('content-main-port', this.logger, this.eventBus);
|
2022-07-31 01:12:54 +02:00
|
|
|
this.eventBus.setComms(this.comms);
|
|
|
|
|
2021-03-06 03:35:23 +01:00
|
|
|
|
2022-07-31 00:15:28 +02:00
|
|
|
this.initPhase2();
|
|
|
|
} catch (e) {
|
|
|
|
console.error('Ultrawidify initalization failed for some reason:', e);
|
|
|
|
}
|
|
|
|
}
|
2021-03-06 03:35:23 +01:00
|
|
|
|
2023-01-07 18:57:47 +01:00
|
|
|
// we always initialize extension, even if it's disabled.
|
2022-07-31 00:15:28 +02:00
|
|
|
initPhase2() {
|
|
|
|
try {
|
|
|
|
if (this.pageInfo) {
|
|
|
|
this.logger.log('info', 'debug', '[uw.js::setup] An instance of pageInfo already exists and will be destroyed.');
|
|
|
|
this.pageInfo.destroy();
|
|
|
|
}
|
2023-01-07 18:57:47 +01:00
|
|
|
this.pageInfo = new PageInfo(this.eventBus, this.siteSettings, this.settings, this.logger);
|
2022-07-31 00:15:28 +02:00
|
|
|
this.logger.log('info', 'debug', "[uw.js::setup] pageInfo initialized.");
|
2021-03-06 03:35:23 +01:00
|
|
|
|
2022-09-28 00:38:36 +02:00
|
|
|
this.logger.log('info', 'debug', "[uw.js::setup] will try to initate KeyboardHandler.");
|
2021-10-19 22:49:28 +02:00
|
|
|
|
2023-01-07 18:57:47 +01:00
|
|
|
if (this.keyboardHandler) {
|
|
|
|
this.keyboardHandler.destroy();
|
2021-04-12 20:54:26 +02:00
|
|
|
}
|
2023-01-07 18:57:47 +01:00
|
|
|
this.keyboardHandler = new KeyboardHandler(this.eventBus, this.siteSettings, this.settings, this.logger);
|
|
|
|
this.keyboardHandler.init();
|
|
|
|
|
|
|
|
this.logger.log('info', 'debug', "[uw.js::setup] KeyboardHandler initiated.");
|
2022-07-31 00:15:28 +02:00
|
|
|
|
2021-03-06 03:35:23 +01:00
|
|
|
} catch (e) {
|
2022-07-31 00:15:28 +02:00
|
|
|
console.error('Ultrawidify: failed to start extension. Error:', e)
|
|
|
|
this.logger.log('error', 'debug', "[uw::init] FAILED TO START EXTENSION. Error:", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
destroy() {
|
|
|
|
if (this.pageInfo) {
|
|
|
|
this.pageInfo.destroy();
|
|
|
|
}
|
2022-09-28 00:38:36 +02:00
|
|
|
if (this.keyboardHandler) {
|
|
|
|
this.keyboardHandler.destroy();
|
2021-03-06 03:35:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|