2021-03-06 03:35:23 +01:00
|
|
|
import Debug from './conf/Debug.js';
|
|
|
|
import BrowserDetect from './conf/BrowserDetect';
|
|
|
|
import CommsServer from './lib/comms/CommsServer';
|
|
|
|
import Settings from './lib/Settings';
|
|
|
|
import Logger, { baseLoggingOptions } from './lib/Logger';
|
|
|
|
|
|
|
|
import { sleep } from '../common/js/utils';
|
|
|
|
|
|
|
|
import { browser } from 'webextension-polyfill-ts';
|
2022-07-31 00:15:28 +02:00
|
|
|
import EventBus, { EventBusCommand } from './lib/EventBus';
|
2021-03-06 03:35:23 +01:00
|
|
|
|
|
|
|
export default class UWServer {
|
|
|
|
settings: Settings;
|
|
|
|
logger: Logger;
|
|
|
|
comms: CommsServer;
|
2022-07-31 00:15:28 +02:00
|
|
|
eventBus: EventBus;
|
2021-03-06 03:35:23 +01:00
|
|
|
|
|
|
|
ports: any[] = [];
|
|
|
|
hasVideos: boolean;
|
|
|
|
currentSite: string = '';
|
2021-03-29 21:07:54 +02:00
|
|
|
videoTabs: any = {};
|
2021-03-06 03:35:23 +01:00
|
|
|
currentTabId: number = 0;
|
|
|
|
|
|
|
|
selectedSubitem: any = {
|
|
|
|
'siteSettings': undefined,
|
|
|
|
'videoSettings': undefined,
|
|
|
|
}
|
|
|
|
|
2022-07-31 00:15:28 +02:00
|
|
|
eventBusCommands = {
|
|
|
|
'popup-set-selected-tab': [{
|
|
|
|
function: (message) => this.setSelectedTab(message.selectedMenu, message.selectedSubitem)
|
|
|
|
}],
|
|
|
|
'has-video': [{
|
|
|
|
function: (message, context) => this.registerVideo(context.comms.sender)
|
|
|
|
}],
|
|
|
|
'noVideo' : [{
|
|
|
|
function: (message, context) => this.unregisterVideo(context.comms.sender)
|
|
|
|
}],
|
|
|
|
'inject-css': [{
|
|
|
|
function: (message, context) => this.injectCss(message.cssString, context.comms.sender)
|
|
|
|
}],
|
|
|
|
'eject-css': [{
|
|
|
|
function: (message, context) => this.removeCss(message.cssString, context.comms.sender)
|
|
|
|
}],
|
|
|
|
'replace-css': [{
|
|
|
|
function: (message, context) => this.replaceCss(message.oldCssString, message.newCssString, context.comms.sender)
|
2022-08-21 22:46:06 +02:00
|
|
|
}],
|
|
|
|
'get-current-site': [{
|
|
|
|
function: (message, context) => this.getCurrentSite()
|
2022-07-31 00:15:28 +02:00
|
|
|
}]
|
|
|
|
};
|
2021-03-06 03:35:23 +01:00
|
|
|
|
|
|
|
private gcTimeout: any;
|
|
|
|
uiLoggerInitialized: boolean = false;
|
|
|
|
|
2022-08-21 22:46:06 +02:00
|
|
|
|
|
|
|
//#region getters
|
|
|
|
get activeTab() {
|
|
|
|
return browser.tabs.query({currentWindow: true, active: true});
|
|
|
|
}
|
|
|
|
//#endregion
|
|
|
|
|
2021-03-06 03:35:23 +01:00
|
|
|
constructor() {
|
|
|
|
this.setup();
|
|
|
|
}
|
|
|
|
|
|
|
|
async setup() {
|
2021-04-12 20:54:26 +02:00
|
|
|
try {
|
|
|
|
// logger is the first thing that goes up
|
|
|
|
const loggingOptions = {
|
|
|
|
isBackgroundScript: true,
|
|
|
|
allowLogging: false,
|
|
|
|
useConfFromStorage: true,
|
|
|
|
logAll: true,
|
|
|
|
fileOptions: {
|
|
|
|
enabled: false,
|
|
|
|
},
|
|
|
|
consoleOptions: {
|
|
|
|
enabled: false
|
|
|
|
}
|
|
|
|
};
|
|
|
|
this.logger = new Logger();
|
|
|
|
await this.logger.init(loggingOptions);
|
|
|
|
|
|
|
|
this.settings = new Settings({logger: this.logger});
|
|
|
|
await this.settings.init();
|
2022-09-20 01:34:59 +02:00
|
|
|
|
2023-07-10 22:00:53 +02:00
|
|
|
this.eventBus = new EventBus({isUWServer: true});
|
2022-07-31 00:15:28 +02:00
|
|
|
|
2022-07-31 01:12:54 +02:00
|
|
|
for (const action in this.eventBusCommands) {
|
|
|
|
for (const command of this.eventBusCommands[action]) {
|
|
|
|
this.eventBus.subscribe(action, command);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.comms = new CommsServer(this);
|
2022-09-20 01:34:59 +02:00
|
|
|
this.eventBus.setComms(this.comms);
|
2021-04-12 20:54:26 +02:00
|
|
|
|
2022-07-31 00:15:28 +02:00
|
|
|
browser.tabs.onActivated.addListener((m) => {this.onTabSwitched(m)});
|
2021-04-12 20:54:26 +02:00
|
|
|
} catch (e) {
|
|
|
|
console.error(`Ultrawidify [server]: failed to start. Reason:`, e);
|
|
|
|
}
|
2021-03-06 03:35:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async _promisifyTabsGet(browserObj, tabId){
|
|
|
|
return new Promise( (resolve, reject) => {
|
|
|
|
browserObj.tabs.get(tabId, (tab) => resolve(tab));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async injectCss(css, sender) {
|
2023-07-10 22:00:53 +02:00
|
|
|
if (!css) {
|
|
|
|
return;
|
|
|
|
}
|
2021-03-06 03:35:23 +01:00
|
|
|
try {
|
2023-07-10 22:00:53 +02:00
|
|
|
if (BrowserDetect.firefox) {
|
|
|
|
browser.scripting.insertCSS({
|
|
|
|
target: {
|
|
|
|
tabId: sender.tab.id,
|
|
|
|
frameIds: [
|
|
|
|
sender.frameId
|
|
|
|
]
|
|
|
|
},
|
|
|
|
css,
|
|
|
|
origin: "USER"
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
await chrome.scripting.insertCSS({
|
|
|
|
target: {
|
|
|
|
tabId: sender.tab.id,
|
|
|
|
frameIds: [
|
|
|
|
sender.frameId
|
|
|
|
]
|
|
|
|
},
|
|
|
|
css,
|
|
|
|
origin: "USER"
|
|
|
|
});
|
2021-03-06 03:35:23 +01:00
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
this.logger.log('error','debug', '[UwServer::injectCss] Error while injecting css:', {error: e, css, sender});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
async removeCss(css, sender) {
|
|
|
|
try {
|
2023-07-10 22:00:53 +02:00
|
|
|
if (BrowserDetect.firefox) {
|
|
|
|
browser.scripting.removeCSS({
|
|
|
|
target: {
|
|
|
|
tabId: sender.tab.id,
|
|
|
|
frameIds: [
|
|
|
|
sender.frameId
|
|
|
|
]
|
|
|
|
},
|
|
|
|
css,
|
|
|
|
origin: "USER"
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
|
|
|
|
await chrome.scripting.removeCSS({
|
|
|
|
target: {
|
|
|
|
tabId: sender.tab.id,
|
|
|
|
frameIds: [
|
|
|
|
sender.frameId
|
|
|
|
]
|
|
|
|
},
|
|
|
|
css,
|
|
|
|
origin: "USER"
|
|
|
|
});
|
|
|
|
}
|
2022-07-31 00:15:28 +02:00
|
|
|
} catch (e) {
|
2021-03-06 03:35:23 +01:00
|
|
|
this.logger.log('error','debug', '[UwServer::injectCss] Error while removing css:', {error: e, css, sender});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async replaceCss(oldCss, newCss, sender) {
|
|
|
|
if (oldCss !== newCss) {
|
|
|
|
this.removeCss(oldCss, sender);
|
2023-07-10 22:00:53 +02:00
|
|
|
this.injectCss(newCss, sender);
|
2021-03-06 03:35:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extractHostname(url){
|
|
|
|
var hostname;
|
2022-07-31 00:15:28 +02:00
|
|
|
|
2021-03-06 03:35:23 +01:00
|
|
|
if (!url) {
|
|
|
|
return "<no url>";
|
|
|
|
}
|
|
|
|
|
2022-07-31 00:15:28 +02:00
|
|
|
// extract hostname
|
2021-03-06 03:35:23 +01:00
|
|
|
if (url.indexOf("://") > -1) { //find & remove protocol (http, ftp, etc.) and get hostname
|
|
|
|
hostname = url.split('/')[2];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
hostname = url.split('/')[0];
|
|
|
|
}
|
2022-07-31 00:15:28 +02:00
|
|
|
|
2021-03-06 03:35:23 +01:00
|
|
|
hostname = hostname.split(':')[0]; //find & remove port number
|
|
|
|
hostname = hostname.split('?')[0]; //find & remove "?"
|
2022-07-31 00:15:28 +02:00
|
|
|
|
2021-03-06 03:35:23 +01:00
|
|
|
return hostname;
|
|
|
|
}
|
|
|
|
|
|
|
|
async onTabSwitched(activeInfo){
|
|
|
|
this.hasVideos = false;
|
|
|
|
|
|
|
|
try {
|
|
|
|
this.currentTabId = activeInfo.tabId; // just for readability
|
|
|
|
|
|
|
|
let tab;
|
|
|
|
if (BrowserDetect.firefox) {
|
|
|
|
tab = await browser.tabs.get(this.currentTabId);
|
|
|
|
} else if (BrowserDetect.anyChromium) {
|
|
|
|
tab = await this._promisifyTabsGet(chrome, this.currentTabId);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.currentSite = this.extractHostname(tab.url);
|
|
|
|
this.logger.log('info', 'debug', '[UwServer::onTabSwitched] user switched tab. New site:', this.currentSite);
|
|
|
|
} catch(e) {
|
|
|
|
this.logger.log('error', 'debug', '[UwServer::onTabSwitched] there was a problem getting currnet site:', e)
|
|
|
|
}
|
|
|
|
|
|
|
|
this.selectedSubitem = {
|
|
|
|
'siteSettings': undefined,
|
|
|
|
'videoSettings': undefined,
|
|
|
|
}
|
|
|
|
//TODO: change extension icon based on whether there's any videos on current page
|
|
|
|
}
|
|
|
|
|
|
|
|
registerVideo(sender) {
|
|
|
|
this.logger.log('info', 'comms', '[UWServer::registerVideo] Registering video.\nsender:', sender);
|
|
|
|
|
|
|
|
const tabHostname = this.extractHostname(sender.tab.url);
|
|
|
|
const frameHostname = this.extractHostname(sender.url);
|
|
|
|
|
|
|
|
// check for orphaned/outdated values and remove them if neccessary
|
|
|
|
if (this.videoTabs[sender.tab.id]?.host != tabHostname) {
|
|
|
|
delete this.videoTabs[sender.tab.id]
|
|
|
|
} else if(this.videoTabs[sender.tab.id]?.frames[sender.frameId]?.host != frameHostname) {
|
|
|
|
delete this.videoTabs[sender.tab.id].frames[sender.frameId];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.videoTabs[sender.tab.id]) {
|
|
|
|
this.videoTabs[sender.tab.id].frames[sender.frameId] = {
|
|
|
|
id: sender.frameId,
|
|
|
|
host: frameHostname,
|
|
|
|
url: sender.url,
|
|
|
|
registerTime: Date.now(),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.videoTabs[sender.tab.id] = {
|
|
|
|
id: sender.tab.id,
|
|
|
|
host: tabHostname,
|
|
|
|
url: sender.tab.url,
|
|
|
|
frames: {}
|
|
|
|
};
|
|
|
|
this.videoTabs[sender.tab.id].frames[sender.frameId] = {
|
|
|
|
id: sender.frameId,
|
|
|
|
host: frameHostname,
|
|
|
|
url: sender.url,
|
|
|
|
registerTime: Date.now(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.logger.log('info', 'comms', '[UWServer::registerVideo] Video registered. current videoTabs:', this.videoTabs);
|
|
|
|
}
|
|
|
|
|
|
|
|
unregisterVideo(sender) {
|
|
|
|
this.logger.log('info', 'comms', '[UwServer::unregisterVideo] Unregistering video.\nsender:', sender);
|
|
|
|
if (this.videoTabs[sender.tab.id]) {
|
|
|
|
if ( Object.keys(this.videoTabs[sender.tab.id].frames).length <= 1) {
|
|
|
|
delete this.videoTabs[sender.tab.id]
|
|
|
|
} else {
|
|
|
|
if(this.videoTabs[sender.tab.id].frames[sender.frameId]) {
|
|
|
|
delete this.videoTabs[sender.tab.id].frames[sender.frameId];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.logger.log('info', 'comms', '[UwServer::unregisterVideo] Video has been unregistered. Current videoTabs:', this.videoTabs);
|
|
|
|
}
|
|
|
|
|
|
|
|
setSelectedTab(menu, subitem) {
|
|
|
|
this.logger.log('info', 'comms', '[UwServer::setSelectedTab] saving selected tab for', menu, ':', subitem);
|
|
|
|
this.selectedSubitem[menu] = subitem;
|
|
|
|
}
|
|
|
|
|
2022-08-21 22:46:06 +02:00
|
|
|
async getCurrentSite() {
|
|
|
|
this.eventBus.send(
|
|
|
|
'set-current-site',
|
|
|
|
{
|
|
|
|
site: await this.getVideoTab(),
|
|
|
|
tabHostname: await this.getCurrentTabHostname(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
comms: {
|
|
|
|
forwardTo: 'popup'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-03-06 03:35:23 +01:00
|
|
|
async getCurrentTab() {
|
|
|
|
return (await browser.tabs.query({active: true, currentWindow: true}))[0];
|
|
|
|
}
|
|
|
|
|
2022-08-21 22:46:06 +02:00
|
|
|
|
2021-03-06 03:35:23 +01:00
|
|
|
async getVideoTab() {
|
2022-07-31 00:15:28 +02:00
|
|
|
// friendly reminder: if current tab doesn't have a video,
|
2021-03-06 03:35:23 +01:00
|
|
|
// there won't be anything in this.videoTabs[this.currentTabId]
|
|
|
|
|
|
|
|
const ctab = await this.getCurrentTab();
|
|
|
|
|
|
|
|
if (!ctab || !ctab.id) {
|
|
|
|
return {
|
|
|
|
host: 'INVALID SITE',
|
|
|
|
frames: [],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.videoTabs[ctab.id]) {
|
|
|
|
// if video is older than PageInfo's video rescan period (+ 4000ms of grace),
|
|
|
|
// we clean it up from videoTabs[tabId].frames array.
|
|
|
|
const ageLimit = Date.now() - this.settings.active.pageInfo.timeouts.rescan - 4000;
|
|
|
|
try {
|
|
|
|
for (const key in this.videoTabs[ctab.id].frames) {
|
|
|
|
if (this.videoTabs[ctab.id].frames[key].registerTime < ageLimit) {
|
|
|
|
delete this.videoTabs[ctab.id].frames[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
// something went wrong. There's prolly no frames.
|
|
|
|
return {
|
|
|
|
host: this.extractHostname(ctab.url),
|
|
|
|
frames: [],
|
|
|
|
selected: this.selectedSubitem
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
...this.videoTabs[ctab.id],
|
|
|
|
host: this.extractHostname(ctab.url),
|
2022-07-31 00:15:28 +02:00
|
|
|
selected: this.selectedSubitem
|
2021-03-06 03:35:23 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-07-31 00:15:28 +02:00
|
|
|
// return something more or less empty if this tab doesn't have
|
2021-03-06 03:35:23 +01:00
|
|
|
// a video registered for it
|
|
|
|
return {
|
|
|
|
host: this.extractHostname(ctab.url),
|
|
|
|
frames: [],
|
|
|
|
selected: this.selectedSubitem
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-21 22:46:06 +02:00
|
|
|
async getCurrentTabHostname() {
|
|
|
|
const activeTab = await this.activeTab;
|
|
|
|
|
|
|
|
if (!activeTab || activeTab.length < 1) {
|
|
|
|
this.logger.log('warn', 'comms', 'There is no active tab for some reason. activeTab:', activeTab);
|
2022-11-21 01:01:28 +01:00
|
|
|
return null;
|
2022-08-21 22:46:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const url = activeTab[0].url;
|
|
|
|
|
|
|
|
var hostname;
|
|
|
|
|
|
|
|
if (url.indexOf("://") > -1) { //find & remove protocol (http, ftp, etc.) and get hostname
|
|
|
|
hostname = url.split('/')[2];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
hostname = url.split('/')[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
hostname = hostname.split(':')[0]; //find & remove port number
|
|
|
|
hostname = hostname.split('?')[0]; //find & remove "?"
|
|
|
|
|
|
|
|
return hostname;
|
2021-03-06 03:35:23 +01:00
|
|
|
}
|
|
|
|
}
|