ultrawidify/src/ext/UWContent.ts

134 lines
4.0 KiB
TypeScript
Raw Normal View History

import Debug from './conf/Debug';
2025-07-15 19:41:00 +02:00
import Settings from './lib/settings/Settings';
import CommsClient from './lib/comms/CommsClient';
import PageInfo from './lib/video-data/PageInfo';
import EventBus from './lib/EventBus';
2022-09-28 01:18:58 +02:00
import KeyboardHandler from './lib/kbm/KeyboardHandler';
import { SiteSettings } from './lib/settings/SiteSettings';
import UI from './lib/uwui/UI';
2025-05-04 02:29:33 +02:00
import { BLANK_LOGGER_CONFIG, LogAggregator } from './lib/logging/LogAggregator';
2025-05-01 00:55:22 +02:00
import { ComponentLogger } from './lib/logging/ComponentLogger';
export default class UWContent {
pageInfo: PageInfo;
comms: CommsClient;
settings: Settings;
siteSettings: SiteSettings;
keyboardHandler: KeyboardHandler;
2025-05-01 00:55:22 +02:00
logAggregator: LogAggregator;
logger: ComponentLogger;
eventBus: EventBus;
2023-03-29 22:07:50 +02:00
isIframe: boolean = false;
globalUi: any;
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
}
reloadSettings() {
try {
2025-05-01 00:55:22 +02:00
this.logger.debug('reloadSettings', '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.');
}
}
async init(){
try {
if (Debug.debug) {
console.log("[uw::main] loading configuration ...");
}
2021-10-19 22:49:28 +02:00
// logger init is the first thing that needs to run
try {
if (!this.logger) {
2025-05-01 00:55:22 +02:00
this.logAggregator = new LogAggregator('◈');
this.logger = new ComponentLogger(this.logAggregator, 'UWContent');
await this.logAggregator.init(BLANK_LOGGER_CONFIG);
}
} catch (e) {
console.error("logger init failed!", e)
}
// init() is re-run any time settings change
if (this.comms) {
this.comms.destroy();
}
if (this.eventBus) {
this.eventBus.destroy();
}
if (!this.settings) {
this.settings = new Settings({
onSettingsChanged: () => this.reloadSettings(),
2025-05-01 00:55:22 +02:00
logAggregator: this.logAggregator
});
await this.settings.init();
this.siteSettings = this.settings.getSiteSettings();
}
2021-10-19 22:49:28 +02:00
this.eventBus = new EventBus();
this.eventBus.subscribe(
'uw-restart',
{
source: this,
function: () => this.initPhase2()
}
);
2025-05-01 00:55:22 +02:00
this.comms = new CommsClient('content-main-port', this.logAggregator, this.eventBus);
2022-07-31 01:12:54 +02:00
this.eventBus.setComms(this.comms);
this.initPhase2();
} catch (e) {
2025-05-01 00:55:22 +02:00
console.error('Ultrawidify initialization failed for some reason:', e);
}
}
// we always initialize extension, even if it's disabled.
initPhase2() {
try {
if (this.pageInfo) {
2025-05-01 00:55:22 +02:00
this.logger.info('setup', 'An instance of pageInfo already exists and will be destroyed.');
this.pageInfo.destroy();
}
2025-05-01 00:55:22 +02:00
this.pageInfo = new PageInfo(this.eventBus, this.siteSettings, this.settings, this.logAggregator);
this.logger.debug('setup', "pageInfo initialized.");
2025-05-01 00:55:22 +02:00
this.logger.debug('setup', "will try to initate KeyboardHandler.");
2021-10-19 22:49:28 +02:00
if (this.keyboardHandler) {
this.keyboardHandler.destroy();
}
2025-05-01 00:55:22 +02:00
this.keyboardHandler = new KeyboardHandler(this.eventBus, this.siteSettings, this.settings, this.logAggregator);
this.keyboardHandler.init();
2025-05-01 00:55:22 +02:00
this.logger.debug('setup', "KeyboardHandler initiated.");
2024-06-05 01:08:50 +02:00
this.globalUi = new UI('ultrawidify-global-ui', {eventBus: this.eventBus, isGlobal: true});
this.globalUi.enable();
this.globalUi.setUiVisibility(false);
} catch (e) {
console.error('Ultrawidify: failed to start extension. Error:', e)
2025-05-01 00:55:22 +02:00
this.logger.error('setup', "FAILED TO START EXTENSION. Error:", e);
}
}
destroy() {
this.eventBus.unsubscribeAll(this);
if (this.pageInfo) {
this.pageInfo.destroy();
}
if (this.keyboardHandler) {
this.keyboardHandler.destroy();
}
}
}