2021-09-22 14:59:27 +02:00
|
|
|
import {DidFailLoadEvent, ipcRenderer, PageFaviconUpdatedEvent, UpdateTargetUrlEvent,} from "electron";
|
2020-09-29 20:43:41 +02:00
|
|
|
import Service from "../../src/Service";
|
|
|
|
import {IconProperties, IconSet, SpecialPages} from "../../src/Meta";
|
|
|
|
import Config from "../../src/Config";
|
2020-05-25 06:16:28 +02:00
|
|
|
|
2020-09-29 20:43:41 +02:00
|
|
|
const appInfo: {
|
|
|
|
title?: string;
|
|
|
|
} = {};
|
|
|
|
let icons: IconProperties[] = [];
|
|
|
|
|
|
|
|
let services: (FrontService | undefined)[] = [];
|
|
|
|
let selectedServiceId: number | null = null;
|
|
|
|
let securityButton: HTMLElement | null = null,
|
|
|
|
homeButton: HTMLElement | null = null,
|
|
|
|
forwardButton: HTMLElement | null = null,
|
|
|
|
backButton: HTMLElement | null = null,
|
|
|
|
refreshButton: HTMLElement | null = null;
|
2020-05-19 10:54:00 +02:00
|
|
|
let addButton, settingsButton;
|
2020-09-29 20:43:41 +02:00
|
|
|
let specialPages: SpecialPages | null = null;
|
|
|
|
let urlPreview: HTMLElement | null = null;
|
|
|
|
let serviceSelector: HTMLElement | null = null;
|
2020-01-10 15:14:41 +01:00
|
|
|
|
2020-05-19 10:54:00 +02:00
|
|
|
// Service reordering
|
2020-09-29 20:43:41 +02:00
|
|
|
let lastDragPosition: HTMLElement | null = null;
|
|
|
|
let oldActiveService: number | null = null;
|
2020-01-10 15:14:41 +01:00
|
|
|
|
|
|
|
|
2020-09-29 20:43:41 +02:00
|
|
|
ipcRenderer.on('data', (
|
|
|
|
event,
|
|
|
|
appTitle: string,
|
|
|
|
iconSets: IconSet[],
|
|
|
|
activeServiceId: number,
|
|
|
|
_specialPages: SpecialPages,
|
|
|
|
config: Config,
|
|
|
|
) => {
|
2020-01-12 13:29:24 +01:00
|
|
|
// App info
|
2020-09-29 20:43:41 +02:00
|
|
|
appInfo.title = appTitle;
|
2020-01-12 13:29:24 +01:00
|
|
|
|
|
|
|
// Icons
|
2020-05-25 06:16:28 +02:00
|
|
|
icons = [];
|
|
|
|
for (const set of iconSets) {
|
2020-09-29 20:43:41 +02:00
|
|
|
icons.push(...set);
|
2020-01-10 15:14:41 +01:00
|
|
|
}
|
|
|
|
|
2020-09-29 20:43:41 +02:00
|
|
|
// Special pages
|
|
|
|
specialPages = _specialPages;
|
|
|
|
|
2020-01-10 15:14:41 +01:00
|
|
|
console.log('Updating services ...');
|
2020-05-20 17:13:33 +02:00
|
|
|
services = config.services;
|
2020-01-10 15:14:41 +01:00
|
|
|
|
|
|
|
const nav = document.querySelector('#service-selector');
|
2020-05-25 06:16:28 +02:00
|
|
|
if (nav) {
|
|
|
|
while (nav.children.length > 0) {
|
|
|
|
nav.removeChild(nav.children[0]);
|
|
|
|
}
|
2020-01-10 15:14:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const serviceContainer = document.querySelector('#services');
|
2020-05-25 06:16:28 +02:00
|
|
|
if (serviceContainer) {
|
|
|
|
serviceContainer.querySelectorAll(":scope > webview").forEach(w => serviceContainer.removeChild(w));
|
|
|
|
}
|
2020-01-10 15:14:41 +01:00
|
|
|
|
|
|
|
for (let i = 0; i < services.length; i++) {
|
2020-09-29 20:43:41 +02:00
|
|
|
createServiceNavigationElement(i);
|
2020-01-10 15:14:41 +01:00
|
|
|
}
|
|
|
|
|
2020-01-21 13:03:32 +01:00
|
|
|
// Init drag last position
|
2020-05-19 05:28:52 +02:00
|
|
|
lastDragPosition = document.getElementById('service-last-drag-position');
|
2020-05-25 06:16:28 +02:00
|
|
|
if (lastDragPosition) {
|
|
|
|
lastDragPosition.addEventListener('dragover', () => {
|
|
|
|
const index = services.length;
|
|
|
|
if (draggedId !== index && draggedId !== index - 1) {
|
|
|
|
resetDrag();
|
2020-09-29 20:43:41 +02:00
|
|
|
lastDragTarget = index;
|
2020-05-25 06:16:28 +02:00
|
|
|
lastDragPosition?.classList.remove('hidden');
|
|
|
|
lastDragPosition?.classList.add('drag-target');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2020-01-21 13:03:32 +01:00
|
|
|
|
|
|
|
// Set active service
|
2020-09-29 20:43:41 +02:00
|
|
|
if (activeServiceId < 0 || activeServiceId >= services.length) {
|
|
|
|
activeServiceId = 0;
|
2020-01-10 15:14:41 +01:00
|
|
|
}
|
2020-09-29 20:43:41 +02:00
|
|
|
setActiveService(activeServiceId);
|
2020-03-06 15:19:06 +01:00
|
|
|
|
|
|
|
// Url preview element
|
|
|
|
urlPreview = document.getElementById("url-preview");
|
2020-05-25 06:16:28 +02:00
|
|
|
if (urlPreview) {
|
2020-09-29 20:43:41 +02:00
|
|
|
const _urlPreview = urlPreview;
|
2020-05-25 06:16:28 +02:00
|
|
|
urlPreview.addEventListener('mouseover', () => {
|
2020-09-29 20:43:41 +02:00
|
|
|
if (_urlPreview.classList.contains('right')) {
|
|
|
|
_urlPreview.classList.remove('right');
|
2020-05-25 06:16:28 +02:00
|
|
|
} else {
|
2020-09-29 20:43:41 +02:00
|
|
|
_urlPreview.classList.add('right');
|
2020-05-25 06:16:28 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2020-05-20 17:13:33 +02:00
|
|
|
|
|
|
|
// History nav buttons
|
2020-05-25 06:16:28 +02:00
|
|
|
const buttons: { [k: string]: HTMLElement | null } = {
|
2020-05-20 17:13:33 +02:00
|
|
|
securityButton: securityButton,
|
|
|
|
homeButton: homeButton,
|
|
|
|
backButton: backButton,
|
|
|
|
forwardButton: forwardButton,
|
|
|
|
refreshButton: refreshButton,
|
|
|
|
};
|
|
|
|
for (const k in buttons) {
|
2020-05-25 06:16:28 +02:00
|
|
|
if (config[k]) buttons[k]?.classList.remove('hidden');
|
|
|
|
else buttons[k]?.classList.add('hidden');
|
2020-05-20 17:13:33 +02:00
|
|
|
}
|
2020-01-10 15:14:41 +01:00
|
|
|
|
2020-05-25 06:16:28 +02:00
|
|
|
// Other elements
|
|
|
|
serviceSelector = document.getElementById('service-selector');
|
2020-06-15 15:45:19 +02:00
|
|
|
|
|
|
|
// Navbar size
|
|
|
|
document.documentElement.style.setProperty('--nav-width', config.bigNavBar ? '64px' : '48px');
|
2020-05-25 06:16:28 +02:00
|
|
|
});
|
2020-03-06 14:32:14 +01:00
|
|
|
|
2021-03-06 18:43:45 +01:00
|
|
|
ipcRenderer.on('load-service-home', (event, serviceId: number) => {
|
|
|
|
const service = services[serviceId];
|
|
|
|
if (!service) throw new Error('Service doesn\'t exist.');
|
|
|
|
|
|
|
|
service.view?.loadURL(service.url)
|
|
|
|
.catch(console.error);
|
|
|
|
});
|
|
|
|
|
|
|
|
ipcRenderer.on('reload-service', (event, serviceId: number) => {
|
|
|
|
reloadService(serviceId);
|
|
|
|
});
|
|
|
|
|
|
|
|
ipcRenderer.on('unload-service', (event, serviceId: number) => {
|
|
|
|
unloadService(serviceId);
|
|
|
|
});
|
|
|
|
|
|
|
|
ipcRenderer.on('reset-service-zoom-level', (event, serviceId: number) => {
|
|
|
|
const service = services[serviceId];
|
|
|
|
if (service?.view) {
|
|
|
|
service.view.setZoomFactor(1);
|
|
|
|
service.view.setZoomLevel(0);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
ipcRenderer.on('zoom-in-service', (event, serviceId: number) => {
|
|
|
|
const service = services[serviceId];
|
|
|
|
if (service?.view) {
|
|
|
|
service.view.setZoomLevel(service.view.getZoomLevel() + 1);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
ipcRenderer.on('zoom-out-service', (event, serviceId: number) => {
|
|
|
|
const service = services[serviceId];
|
|
|
|
if (service?.view) {
|
|
|
|
service.view.setZoomLevel(service.view.getZoomLevel() - 1);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-07-14 10:01:34 +02:00
|
|
|
function removeServiceFeatures(id: number): Element | null {
|
2020-03-06 14:32:14 +01:00
|
|
|
// Remove nav
|
2020-05-25 06:16:28 +02:00
|
|
|
const nav = document.querySelector('#service-selector');
|
|
|
|
let oldNavButton: HTMLElement | null = null;
|
2020-07-14 10:01:34 +02:00
|
|
|
let nextSibling: Element | null = null;
|
2020-05-25 06:16:28 +02:00
|
|
|
if (nav) {
|
|
|
|
oldNavButton = nav.querySelector('li:nth-of-type(' + (id + 1) + ')');
|
|
|
|
if (oldNavButton) {
|
2020-07-14 10:01:34 +02:00
|
|
|
nextSibling = oldNavButton.nextElementSibling;
|
2020-05-25 06:16:28 +02:00
|
|
|
nav.removeChild(oldNavButton);
|
|
|
|
}
|
2020-03-06 14:32:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove webview
|
2020-09-29 20:43:41 +02:00
|
|
|
const service = services[id];
|
|
|
|
if (service) {
|
|
|
|
const view = service.view;
|
|
|
|
if (view) document.querySelector('#services')?.removeChild(view);
|
2020-03-06 14:32:14 +01:00
|
|
|
}
|
|
|
|
|
2020-07-14 10:01:34 +02:00
|
|
|
return nextSibling;
|
2020-03-06 14:32:14 +01:00
|
|
|
}
|
|
|
|
|
2020-09-29 20:43:41 +02:00
|
|
|
ipcRenderer.on('updateService', (e, id: number | null, data: Service) => {
|
2020-01-10 15:14:41 +01:00
|
|
|
if (id === null) {
|
2020-07-14 10:01:34 +02:00
|
|
|
console.log('Adding new service');
|
2020-01-10 15:14:41 +01:00
|
|
|
services.push(data);
|
2020-09-29 20:43:41 +02:00
|
|
|
createServiceNavigationElement(services.length - 1);
|
2020-01-10 15:14:41 +01:00
|
|
|
} else {
|
2020-07-14 10:01:34 +02:00
|
|
|
console.log('Updating existing service', id);
|
|
|
|
const nextSibling = removeServiceFeatures(id);
|
2020-01-10 15:14:41 +01:00
|
|
|
|
|
|
|
// Create new service
|
|
|
|
services[id] = data;
|
2020-09-29 20:43:41 +02:00
|
|
|
createServiceNavigationElement(id, nextSibling);
|
|
|
|
if (selectedServiceId === id) {
|
2020-01-10 15:14:41 +01:00
|
|
|
setActiveService(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-09-29 20:43:41 +02:00
|
|
|
ipcRenderer.on('reorderService', (e, serviceId: number, targetId: number) => {
|
2020-01-21 13:03:32 +01:00
|
|
|
const oldServices = services;
|
|
|
|
services = [];
|
|
|
|
|
2020-05-19 05:34:00 +02:00
|
|
|
let newId = targetId;
|
|
|
|
|
2020-01-21 13:03:32 +01:00
|
|
|
for (let i = 0; i < targetId; i++) {
|
|
|
|
if (i !== serviceId) {
|
|
|
|
services.push(oldServices[i]);
|
2020-05-19 05:49:50 +02:00
|
|
|
if (i === oldActiveService) newId = services.length - 1;
|
2020-01-21 13:03:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
services.push(oldServices[serviceId]);
|
|
|
|
for (let i = targetId; i < oldServices.length; i++) {
|
|
|
|
if (i !== serviceId) {
|
|
|
|
services.push(oldServices[i]);
|
2020-05-19 05:49:50 +02:00
|
|
|
if (i === oldActiveService) newId = services.length - 1;
|
2020-01-21 13:03:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-25 06:16:28 +02:00
|
|
|
if (serviceSelector) {
|
|
|
|
serviceSelector.innerHTML = '';
|
|
|
|
}
|
2020-01-21 13:03:32 +01:00
|
|
|
for (let i = 0; i < services.length; i++) {
|
2020-09-29 20:43:41 +02:00
|
|
|
const service = services[i];
|
|
|
|
if (service) service.li = undefined;
|
|
|
|
createServiceNavigationElement(i);
|
2020-01-21 13:03:32 +01:00
|
|
|
}
|
|
|
|
setActiveService(newId);
|
|
|
|
});
|
|
|
|
|
2020-09-29 20:43:41 +02:00
|
|
|
ipcRenderer.on('deleteService', (e, id: number) => {
|
2020-03-06 14:32:14 +01:00
|
|
|
removeServiceFeatures(id);
|
2020-01-10 15:14:41 +01:00
|
|
|
|
2020-09-29 20:43:41 +02:00
|
|
|
if (selectedServiceId === id) {
|
2020-01-10 15:14:41 +01:00
|
|
|
setActiveService(0);
|
|
|
|
}
|
|
|
|
|
2020-09-29 20:43:41 +02:00
|
|
|
services.splice(id, 1);
|
2020-01-10 15:14:41 +01:00
|
|
|
});
|
|
|
|
|
2020-09-29 20:43:41 +02:00
|
|
|
function createServiceNavigationElement(index: number, nextNavButton?: Element | null) {
|
|
|
|
const service = services[index];
|
|
|
|
if (!service) throw new Error('Service doesn\'t exist.');
|
|
|
|
|
|
|
|
const li = document.createElement('li') as NavigationElement;
|
2020-01-10 15:14:41 +01:00
|
|
|
service.li = li;
|
|
|
|
|
2020-09-29 20:43:41 +02:00
|
|
|
const button = document.createElement('button');
|
2020-01-10 15:14:41 +01:00
|
|
|
button.dataset.serviceId = '' + index;
|
|
|
|
button.dataset.tooltip = service.name;
|
|
|
|
button.addEventListener('click', () => {
|
2020-09-29 20:43:41 +02:00
|
|
|
const rawId = button.dataset.serviceId;
|
|
|
|
if (rawId) {
|
|
|
|
const id = parseInt(rawId);
|
|
|
|
setActiveService(id);
|
|
|
|
ipcRenderer.send('setActiveService', id);
|
2020-05-25 06:16:28 +02:00
|
|
|
}
|
2020-01-10 15:14:41 +01:00
|
|
|
});
|
2021-03-06 18:43:45 +01:00
|
|
|
button.addEventListener('contextmenu', e => {
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
const service = services[index];
|
|
|
|
if (!service) throw new Error('Service doesn\'t exist.');
|
|
|
|
|
|
|
|
ipcRenderer.send(
|
|
|
|
'open-service-navigation-context-menu',
|
|
|
|
index,
|
|
|
|
!!service.view && !!service.viewReady,
|
|
|
|
!service.view && !service.viewReady,
|
|
|
|
service.view?.getZoomFactor() !== 1 && service.view?.getZoomLevel() !== 0,
|
|
|
|
);
|
|
|
|
});
|
2020-01-10 15:14:41 +01:00
|
|
|
|
2020-09-29 20:43:41 +02:00
|
|
|
let icon: HTMLImageElement | HTMLElement;
|
2020-01-10 15:14:41 +01:00
|
|
|
if (service.useFavicon && service.favicon != null) {
|
|
|
|
icon = document.createElement('img');
|
2020-09-29 20:43:41 +02:00
|
|
|
if (icon instanceof HTMLImageElement) {
|
|
|
|
icon.src = service.favicon;
|
|
|
|
icon.alt = service.name;
|
|
|
|
}
|
|
|
|
} else if (service.isImage && service.icon) {
|
2020-01-10 15:14:41 +01:00
|
|
|
icon = document.createElement('img');
|
2020-09-29 20:43:41 +02:00
|
|
|
if (icon instanceof HTMLImageElement) {
|
|
|
|
icon.src = service.icon;
|
|
|
|
icon.alt = service.name;
|
|
|
|
}
|
2020-01-10 15:14:41 +01:00
|
|
|
} else {
|
|
|
|
icon = document.createElement('i');
|
2020-05-25 06:16:28 +02:00
|
|
|
let iconProperties = icons.find(i => `${i.set}/${i.name}` === service.icon);
|
|
|
|
|
|
|
|
// Compatibility with old services
|
|
|
|
if (!iconProperties) iconProperties = icons.find(i => i.name === service.icon);
|
|
|
|
|
2020-01-10 15:14:41 +01:00
|
|
|
if (iconProperties) {
|
2020-05-25 06:16:28 +02:00
|
|
|
iconProperties.faIcon.split(' ').forEach((cl: string) => {
|
2020-01-10 15:14:41 +01:00
|
|
|
icon.classList.add(cl);
|
|
|
|
});
|
2020-07-14 11:08:24 +02:00
|
|
|
} else {
|
|
|
|
icon.classList.add('fas', 'fa-circle');
|
2020-01-10 15:14:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
button.appendChild(icon);
|
|
|
|
li.appendChild(button);
|
|
|
|
li.button = button;
|
|
|
|
|
|
|
|
const nav = document.querySelector('#service-selector');
|
2020-05-25 06:16:28 +02:00
|
|
|
if (nav) {
|
|
|
|
if (nextNavButton === nav || nextNavButton === undefined) {
|
|
|
|
nav.appendChild(li);
|
|
|
|
} else {
|
|
|
|
nav.insertBefore(li, nextNavButton);
|
|
|
|
}
|
2020-01-10 15:14:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (service.autoLoad) {
|
|
|
|
loadService(index, service);
|
|
|
|
}
|
2020-01-21 13:03:32 +01:00
|
|
|
|
|
|
|
initDrag(index, li);
|
|
|
|
}
|
|
|
|
|
2020-05-25 06:16:28 +02:00
|
|
|
let draggedId: number;
|
2020-01-21 13:03:32 +01:00
|
|
|
let lastDragTarget = -1;
|
|
|
|
|
2020-09-29 20:43:41 +02:00
|
|
|
function initDrag(index: number, li: NavigationElement) {
|
2020-01-21 13:03:32 +01:00
|
|
|
li.serviceId = index;
|
|
|
|
li.draggable = true;
|
2020-05-25 06:16:28 +02:00
|
|
|
li.addEventListener('dragstart', (event: DragEvent) => {
|
2020-01-21 13:03:32 +01:00
|
|
|
draggedId = index;
|
2020-05-25 06:16:28 +02:00
|
|
|
if (event.dataTransfer) {
|
|
|
|
event.dataTransfer.dropEffect = 'move';
|
|
|
|
}
|
|
|
|
lastDragPosition?.classList.remove('hidden');
|
2020-01-21 13:03:32 +01:00
|
|
|
});
|
2020-05-25 06:16:28 +02:00
|
|
|
li.addEventListener('dragover', (e: DragEvent) => {
|
2020-05-19 05:28:52 +02:00
|
|
|
let realIndex = index;
|
2020-09-29 20:43:41 +02:00
|
|
|
const rect = li.getBoundingClientRect();
|
2020-05-19 05:28:52 +02:00
|
|
|
|
|
|
|
if ((e.clientY - rect.y) / rect.height >= 0.5) {
|
|
|
|
realIndex++;
|
2020-01-21 13:03:32 +01:00
|
|
|
}
|
2020-05-19 05:28:52 +02:00
|
|
|
|
|
|
|
if (draggedId === realIndex - 1) {
|
|
|
|
realIndex--;
|
|
|
|
}
|
|
|
|
|
|
|
|
resetDrag();
|
2020-09-29 20:43:41 +02:00
|
|
|
const el = realIndex === services.length ?
|
|
|
|
lastDragPosition :
|
|
|
|
services[realIndex]?.li;
|
|
|
|
lastDragTarget = realIndex;
|
2020-05-25 06:16:28 +02:00
|
|
|
lastDragPosition?.classList.remove('hidden');
|
2020-05-19 05:28:52 +02:00
|
|
|
|
2020-09-29 20:43:41 +02:00
|
|
|
if (el) {
|
|
|
|
el.classList.add('drag-target');
|
|
|
|
|
|
|
|
if (draggedId === realIndex || draggedId === realIndex - 1)
|
|
|
|
el.classList.add('drag-target-self');
|
|
|
|
}
|
2020-01-21 13:03:32 +01:00
|
|
|
});
|
|
|
|
li.addEventListener('dragend', () => {
|
|
|
|
reorderService(draggedId, lastDragTarget);
|
|
|
|
resetDrag();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function resetDrag() {
|
|
|
|
lastDragTarget = -1;
|
2020-05-25 06:16:28 +02:00
|
|
|
serviceSelector?.querySelectorAll('li').forEach(li => {
|
2020-01-21 13:03:32 +01:00
|
|
|
li.classList.remove('drag-target');
|
2020-05-19 05:28:52 +02:00
|
|
|
li.classList.remove('drag-target-self');
|
2020-01-21 13:03:32 +01:00
|
|
|
});
|
|
|
|
const lastDragPosition = document.getElementById('service-last-drag-position');
|
2020-05-25 06:16:28 +02:00
|
|
|
lastDragPosition?.classList.remove('drag-target');
|
|
|
|
lastDragPosition?.classList.add('hidden');
|
2020-01-21 13:03:32 +01:00
|
|
|
}
|
|
|
|
|
2020-05-25 06:16:28 +02:00
|
|
|
function reorderService(serviceId: number, targetId: number) {
|
2020-01-21 13:03:32 +01:00
|
|
|
console.log('Reordering service', serviceId, targetId);
|
|
|
|
if (targetId >= 0) {
|
2020-09-29 20:43:41 +02:00
|
|
|
oldActiveService = selectedServiceId;
|
2020-05-25 06:16:28 +02:00
|
|
|
setActiveService(-1);
|
2020-01-21 13:03:32 +01:00
|
|
|
ipcRenderer.send('reorderService', serviceId, targetId);
|
|
|
|
}
|
2020-01-10 15:14:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
document.addEventListener('DOMContentLoaded', () => {
|
2020-05-20 17:13:33 +02:00
|
|
|
securityButton = document.getElementById('status');
|
2020-05-19 07:00:14 +02:00
|
|
|
|
2020-05-19 05:56:28 +02:00
|
|
|
homeButton = document.getElementById('home');
|
2020-05-25 06:16:28 +02:00
|
|
|
homeButton?.addEventListener('click', () => goHome());
|
2020-05-19 05:56:28 +02:00
|
|
|
|
|
|
|
forwardButton = document.getElementById('forward');
|
2020-05-25 06:16:28 +02:00
|
|
|
forwardButton?.addEventListener('click', () => goForward());
|
2020-01-10 15:14:41 +01:00
|
|
|
|
2020-05-19 05:56:28 +02:00
|
|
|
backButton = document.getElementById('back');
|
2020-05-25 06:16:28 +02:00
|
|
|
backButton?.addEventListener('click', () => goBack());
|
2020-01-10 15:14:41 +01:00
|
|
|
|
2020-05-20 17:13:33 +02:00
|
|
|
refreshButton = document.getElementById('reload');
|
2020-05-25 06:16:28 +02:00
|
|
|
refreshButton?.addEventListener('click', () => reload());
|
2020-05-19 06:00:24 +02:00
|
|
|
|
2020-05-19 05:56:28 +02:00
|
|
|
addButton = document.getElementById('add-button');
|
2021-03-06 18:43:45 +01:00
|
|
|
addButton?.addEventListener('click', () => ipcRenderer.send('create-new-service', null));
|
2020-05-19 10:54:00 +02:00
|
|
|
|
|
|
|
settingsButton = document.getElementById('settings-button');
|
2020-05-25 06:16:28 +02:00
|
|
|
settingsButton?.addEventListener('click', () => ipcRenderer.send('openSettings', null));
|
2020-01-10 15:14:41 +01:00
|
|
|
});
|
|
|
|
|
2020-05-25 06:16:28 +02:00
|
|
|
function setActiveService(serviceId: number) {
|
2020-01-10 15:14:41 +01:00
|
|
|
const currentService = services[serviceId];
|
|
|
|
process.nextTick(() => {
|
2020-01-21 13:03:32 +01:00
|
|
|
if (currentService) {
|
|
|
|
loadService(serviceId, currentService);
|
|
|
|
}
|
2020-01-10 15:14:41 +01:00
|
|
|
|
|
|
|
// Hide previous service
|
2020-09-29 20:43:41 +02:00
|
|
|
if (typeof selectedServiceId === 'number') {
|
|
|
|
const selectedService = services[selectedServiceId];
|
|
|
|
if (selectedService && selectedService.view) {
|
|
|
|
selectedService.view.classList.remove('active');
|
|
|
|
}
|
2020-01-10 15:14:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Show service
|
2020-09-29 20:43:41 +02:00
|
|
|
currentService?.view?.classList.add('active');
|
2020-01-10 15:14:41 +01:00
|
|
|
|
|
|
|
// Save active service ID
|
2020-09-29 20:43:41 +02:00
|
|
|
selectedServiceId = serviceId;
|
2020-01-10 15:14:41 +01:00
|
|
|
|
|
|
|
// Refresh navigation
|
|
|
|
updateNavigation();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-09-29 20:43:41 +02:00
|
|
|
function loadService(serviceId: number, service: FrontService) {
|
2020-01-10 15:14:41 +01:00
|
|
|
// Load service if not loaded yet
|
|
|
|
if (!service.view && !service.viewReady) {
|
2020-01-21 13:25:36 +01:00
|
|
|
console.log('Loading service', serviceId);
|
|
|
|
|
2020-05-25 06:16:28 +02:00
|
|
|
document.querySelector('#services > .loader')?.classList.remove('hidden');
|
2020-09-29 20:43:41 +02:00
|
|
|
const view = service.view = document.createElement('webview');
|
2020-07-14 11:08:09 +02:00
|
|
|
updateNavigation(); // Start loading animation
|
2020-09-29 20:43:41 +02:00
|
|
|
view.setAttribute('enableRemoteModule', 'false');
|
|
|
|
view.setAttribute('partition', 'persist:service_' + service.partition);
|
|
|
|
view.setAttribute('autosize', 'true');
|
2021-09-25 11:22:14 +02:00
|
|
|
view.setAttribute('allowpopups', 'true');
|
2020-09-29 20:43:41 +02:00
|
|
|
if (specialPages) view.setAttribute('src', specialPages.empty);
|
2020-01-10 15:14:41 +01:00
|
|
|
|
2020-07-14 10:31:09 +02:00
|
|
|
// Error handling
|
2020-09-29 20:43:41 +02:00
|
|
|
view.addEventListener('did-fail-load', (e: DidFailLoadEvent) => {
|
2020-07-16 18:10:33 +02:00
|
|
|
if (e.errorCode <= -100 && e.errorCode > -200) {
|
2020-09-29 20:43:41 +02:00
|
|
|
if (specialPages) view.setAttribute('src', specialPages.connectionError);
|
2020-07-16 18:10:33 +02:00
|
|
|
} else if (e.errorCode === -6) {
|
2020-09-29 20:43:41 +02:00
|
|
|
if (specialPages) view.setAttribute('src', specialPages.fileNotFound);
|
2020-07-16 18:10:33 +02:00
|
|
|
} else if (e.errorCode !== -3) {
|
|
|
|
console.error('Unhandled error:', e);
|
|
|
|
}
|
2020-07-14 10:31:09 +02:00
|
|
|
});
|
|
|
|
|
2020-01-10 15:14:41 +01:00
|
|
|
// Append element to DOM
|
2020-09-29 20:43:41 +02:00
|
|
|
document.querySelector('#services')?.appendChild(view);
|
2020-01-10 15:14:41 +01:00
|
|
|
|
2020-02-20 12:46:53 +01:00
|
|
|
// Load chain
|
2020-09-29 20:43:41 +02:00
|
|
|
let listener: () => void;
|
|
|
|
view.addEventListener('dom-ready', listener = () => {
|
|
|
|
view.removeEventListener('dom-ready', listener);
|
2020-01-10 15:14:41 +01:00
|
|
|
|
2020-09-29 20:43:41 +02:00
|
|
|
view.addEventListener('dom-ready', listener = () => {
|
2020-02-20 12:46:53 +01:00
|
|
|
if (service.customCSS) {
|
2020-09-29 20:43:41 +02:00
|
|
|
view.insertCSS(service.customCSS)
|
|
|
|
.catch(console.error);
|
2020-02-20 12:46:53 +01:00
|
|
|
}
|
2020-01-10 15:14:41 +01:00
|
|
|
|
2020-05-25 06:16:28 +02:00
|
|
|
document.querySelector('#services > .loader')?.classList.add('hidden');
|
2020-09-29 20:43:41 +02:00
|
|
|
service.li?.classList.add('loaded');
|
2020-02-20 12:46:53 +01:00
|
|
|
service.viewReady = true;
|
2020-01-21 21:27:48 +01:00
|
|
|
|
2020-02-20 12:46:53 +01:00
|
|
|
updateNavigation();
|
|
|
|
|
2020-09-29 20:43:41 +02:00
|
|
|
if (selectedServiceId === null) {
|
2020-02-20 12:46:53 +01:00
|
|
|
setActiveService(serviceId);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-04-01 09:53:00 +02:00
|
|
|
|
2020-04-01 09:24:10 +02:00
|
|
|
// Set custom user agent
|
2020-02-20 12:46:53 +01:00
|
|
|
if (typeof service.customUserAgent === 'string') {
|
2021-03-06 18:43:45 +01:00
|
|
|
ipcRenderer.send('set-web-contents-user-agent', view.getWebContentsId(), service.customUserAgent);
|
2020-01-10 15:14:41 +01:00
|
|
|
}
|
2020-04-01 09:24:10 +02:00
|
|
|
|
2020-04-01 09:53:00 +02:00
|
|
|
// Set context menu
|
2021-03-06 18:43:45 +01:00
|
|
|
ipcRenderer.send('open-service-content-context-menu', view.getWebContentsId());
|
2020-04-01 09:53:00 +02:00
|
|
|
|
2020-04-01 09:24:10 +02:00
|
|
|
// Set permission request handler
|
2021-03-06 18:43:45 +01:00
|
|
|
ipcRenderer.send('set-partition-permissions', serviceId, view.partition);
|
2020-04-01 09:24:10 +02:00
|
|
|
|
2020-09-29 20:43:41 +02:00
|
|
|
view.setAttribute('src', service.url);
|
2020-01-10 15:14:41 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
// Load favicon
|
2020-09-29 20:43:41 +02:00
|
|
|
view.addEventListener('page-favicon-updated', (event: PageFaviconUpdatedEvent) => {
|
2020-01-10 15:14:41 +01:00
|
|
|
console.debug('Loaded favicons for', service.name, event.favicons);
|
2020-05-19 07:32:38 +02:00
|
|
|
if (event.favicons.length > 0 && service.favicon !== event.favicons[0]) {
|
2020-01-10 15:14:41 +01:00
|
|
|
ipcRenderer.send('setServiceFavicon', serviceId, event.favicons[0]);
|
|
|
|
if (service.useFavicon) {
|
|
|
|
const img = document.createElement('img');
|
|
|
|
img.src = event.favicons[0];
|
|
|
|
img.alt = service.name;
|
|
|
|
img.onload = () => {
|
2020-09-29 20:43:41 +02:00
|
|
|
if (service.li) {
|
|
|
|
service.li.button.innerHTML = '';
|
|
|
|
service.li.button.appendChild(img);
|
|
|
|
}
|
2020-01-10 15:14:41 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2020-03-06 15:19:06 +01:00
|
|
|
|
|
|
|
// Display target urls
|
2020-09-29 20:43:41 +02:00
|
|
|
view.addEventListener('update-target-url', (event: UpdateTargetUrlEvent) => {
|
2020-03-06 15:19:06 +01:00
|
|
|
if (event.url.length === 0) {
|
2020-07-10 14:08:31 +02:00
|
|
|
urlPreview?.classList.add('invisible');
|
2020-03-06 15:19:06 +01:00
|
|
|
} else {
|
2020-07-10 14:08:31 +02:00
|
|
|
urlPreview?.classList.remove('invisible');
|
2020-05-25 06:16:28 +02:00
|
|
|
if (urlPreview) {
|
|
|
|
urlPreview.innerHTML = event.url;
|
|
|
|
}
|
2020-03-06 15:19:06 +01:00
|
|
|
}
|
|
|
|
});
|
2020-01-10 15:14:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-25 06:16:28 +02:00
|
|
|
function unloadService(serviceId: number) {
|
2020-01-10 15:14:41 +01:00
|
|
|
const service = services[serviceId];
|
2020-09-29 20:43:41 +02:00
|
|
|
if (!service) throw new Error('Service doesn\'t exist.');
|
|
|
|
|
2020-01-10 15:14:41 +01:00
|
|
|
if (service.view && service.viewReady) {
|
|
|
|
service.view.remove();
|
2020-09-29 20:43:41 +02:00
|
|
|
service.view = undefined;
|
|
|
|
service.li?.classList.remove('loaded');
|
2020-01-10 15:14:41 +01:00
|
|
|
service.viewReady = false;
|
|
|
|
|
2020-09-29 20:43:41 +02:00
|
|
|
if (selectedServiceId === serviceId) {
|
|
|
|
selectedServiceId = null;
|
|
|
|
|
2020-01-10 15:14:41 +01:00
|
|
|
for (let i = 0; i < services.length; i++) {
|
2020-09-29 20:43:41 +02:00
|
|
|
const otherService = services[i];
|
|
|
|
if (otherService && otherService.view && otherService.viewReady) {
|
2020-01-10 15:14:41 +01:00
|
|
|
setActiveService(i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-09-29 20:43:41 +02:00
|
|
|
|
|
|
|
// false positive:
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
|
|
if (selectedServiceId === null) {
|
2020-01-10 15:14:41 +01:00
|
|
|
updateNavigation();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-25 06:16:28 +02:00
|
|
|
function reloadService(serviceId: number) {
|
2020-01-10 15:14:41 +01:00
|
|
|
const service = services[serviceId];
|
2020-09-29 20:43:41 +02:00
|
|
|
if (!service) throw new Error('Service doesn\'t exist.');
|
|
|
|
|
2020-01-10 15:14:41 +01:00
|
|
|
if (service.view && service.viewReady) {
|
2020-01-21 13:25:36 +01:00
|
|
|
console.log('Reloading service', serviceId);
|
2020-05-25 06:16:28 +02:00
|
|
|
document.querySelector('#services > .loader')?.classList.remove('hidden');
|
2020-01-10 15:14:41 +01:00
|
|
|
service.view.reload();
|
|
|
|
} else if (!service.view && !service.viewReady) {
|
|
|
|
loadService(serviceId, service);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateNavigation() {
|
|
|
|
console.debug('Updating navigation');
|
|
|
|
// Update active list element
|
|
|
|
for (let i = 0; i < services.length; i++) {
|
|
|
|
const service = services[i];
|
2020-09-29 20:43:41 +02:00
|
|
|
if (!service) continue;
|
2020-05-19 04:46:34 +02:00
|
|
|
|
2020-07-14 11:08:09 +02:00
|
|
|
if (!service.li) continue;
|
|
|
|
|
2020-05-19 04:46:34 +02:00
|
|
|
// Active?
|
2020-09-29 20:43:41 +02:00
|
|
|
if (selectedServiceId === i) service.li.classList.add('active');
|
2020-05-19 04:46:34 +02:00
|
|
|
else service.li.classList.remove('active');
|
|
|
|
|
2020-07-14 11:08:09 +02:00
|
|
|
// Loading?
|
|
|
|
if (service.view && !service.viewReady) service.li.classList.add('loading');
|
|
|
|
else service.li.classList.remove('loading');
|
|
|
|
|
2020-05-19 04:46:34 +02:00
|
|
|
// Loaded?
|
|
|
|
if (service.viewReady) service.li.classList.add('loaded');
|
|
|
|
else service.li.classList.remove('loaded');
|
2020-01-10 15:14:41 +01:00
|
|
|
}
|
|
|
|
|
2020-09-29 20:43:41 +02:00
|
|
|
if (selectedServiceId !== null && services[selectedServiceId]?.viewReady) {
|
2020-01-10 15:14:41 +01:00
|
|
|
console.debug('Updating navigation buttons because view is ready');
|
|
|
|
// Update history navigation
|
2020-09-29 20:43:41 +02:00
|
|
|
const view = services[selectedServiceId]?.view;
|
2020-01-10 15:14:41 +01:00
|
|
|
|
2020-05-25 06:16:28 +02:00
|
|
|
homeButton?.classList.remove('disabled');
|
2020-05-19 07:00:14 +02:00
|
|
|
|
2020-05-25 06:16:28 +02:00
|
|
|
if (view && view.canGoForward()) forwardButton?.classList.remove('disabled');
|
|
|
|
else forwardButton?.classList.add('disabled');
|
2020-01-10 15:14:41 +01:00
|
|
|
|
2020-05-25 06:16:28 +02:00
|
|
|
if (view && view.canGoBack()) backButton?.classList.remove('disabled');
|
|
|
|
else backButton?.classList.add('disabled');
|
2020-05-19 07:00:14 +02:00
|
|
|
|
2020-05-25 06:16:28 +02:00
|
|
|
refreshButton?.classList.remove('disabled');
|
2020-05-19 07:00:14 +02:00
|
|
|
|
|
|
|
updateStatusButton();
|
2020-01-10 15:14:41 +01:00
|
|
|
}
|
2020-01-12 13:29:24 +01:00
|
|
|
|
|
|
|
updateWindowTitle();
|
|
|
|
}
|
|
|
|
|
2020-05-19 07:00:14 +02:00
|
|
|
function updateStatusButton() {
|
2020-11-27 14:47:11 +01:00
|
|
|
if (typeof selectedServiceId !== 'number') return;
|
|
|
|
|
2020-09-29 20:43:41 +02:00
|
|
|
const protocol = services[selectedServiceId]?.view?.getURL().split('://')[0] || 'unknown';
|
|
|
|
securityButton?.childNodes.forEach(el => {
|
|
|
|
if (el instanceof HTMLElement) {
|
|
|
|
if (el.classList.contains(protocol)) el.classList.add('active');
|
|
|
|
else el.classList.remove('active');
|
|
|
|
}
|
|
|
|
});
|
2020-05-19 07:00:14 +02:00
|
|
|
}
|
|
|
|
|
2020-01-12 13:29:24 +01:00
|
|
|
function updateWindowTitle() {
|
2020-09-29 20:43:41 +02:00
|
|
|
if (selectedServiceId === null) {
|
2021-03-06 18:43:45 +01:00
|
|
|
ipcRenderer.send('update-window-title', null);
|
2020-09-29 20:43:41 +02:00
|
|
|
} else {
|
|
|
|
const service = services[selectedServiceId];
|
|
|
|
if (service?.viewReady && service.view) {
|
2021-03-06 18:43:45 +01:00
|
|
|
ipcRenderer.send('update-window-title', selectedServiceId, service.view.getWebContentsId());
|
2020-09-29 20:43:41 +02:00
|
|
|
}
|
2020-01-12 13:29:24 +01:00
|
|
|
}
|
2020-01-10 15:14:41 +01:00
|
|
|
}
|
|
|
|
|
2020-05-19 05:56:28 +02:00
|
|
|
function goHome() {
|
2020-09-29 20:43:41 +02:00
|
|
|
if (selectedServiceId === null) return;
|
|
|
|
|
|
|
|
const service = services[selectedServiceId];
|
|
|
|
if (!service) throw new Error('Service doesn\'t exist.');
|
|
|
|
|
|
|
|
service.view?.loadURL(service.url)
|
2020-05-19 05:56:28 +02:00
|
|
|
.catch(console.error);
|
|
|
|
}
|
|
|
|
|
2020-01-10 15:14:41 +01:00
|
|
|
function goForward() {
|
2020-09-29 20:43:41 +02:00
|
|
|
if (selectedServiceId === null) return;
|
|
|
|
|
|
|
|
const view = services[selectedServiceId]?.view;
|
2021-03-06 18:43:45 +01:00
|
|
|
if (view) ipcRenderer.send('go-forward', view.getWebContentsId());
|
2020-01-10 15:14:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function goBack() {
|
2020-09-29 20:43:41 +02:00
|
|
|
if (selectedServiceId === null) return;
|
|
|
|
|
|
|
|
const view = services[selectedServiceId]?.view;
|
2021-03-06 18:43:45 +01:00
|
|
|
if (view) ipcRenderer.send('go-back', view.getWebContentsId());
|
2020-02-20 11:51:13 +01:00
|
|
|
}
|
2020-04-01 09:53:00 +02:00
|
|
|
|
2020-05-19 06:00:24 +02:00
|
|
|
function reload() {
|
2020-09-29 20:43:41 +02:00
|
|
|
if (selectedServiceId === null) return;
|
|
|
|
|
|
|
|
reloadService(selectedServiceId);
|
2020-05-19 06:00:24 +02:00
|
|
|
}
|
|
|
|
|
2020-09-29 20:43:41 +02:00
|
|
|
ipcRenderer.on('fullscreenchange', (e, fullscreen: boolean) => {
|
2020-06-24 18:51:24 +02:00
|
|
|
if (fullscreen) document.body.classList.add('fullscreen');
|
|
|
|
else document.body.classList.remove('fullscreen');
|
|
|
|
});
|
2020-09-29 20:43:41 +02:00
|
|
|
|
|
|
|
type FrontService = Service & {
|
2021-09-22 14:59:27 +02:00
|
|
|
view?: Electron.WebviewTag;
|
2020-09-29 20:43:41 +02:00
|
|
|
viewReady?: boolean;
|
|
|
|
li?: NavigationElement;
|
|
|
|
};
|
|
|
|
|
|
|
|
type NavigationElement = HTMLLIElement & {
|
|
|
|
serviceId: number;
|
|
|
|
button: HTMLButtonElement;
|
|
|
|
};
|