2020-05-25 06:16:28 +02:00
|
|
|
import {
|
2020-04-01 09:53:00 +02:00
|
|
|
clipboard,
|
2020-09-29 20:43:41 +02:00
|
|
|
ContextMenuParams,
|
2020-07-16 18:10:33 +02:00
|
|
|
DidFailLoadEvent,
|
2020-05-25 06:16:28 +02:00
|
|
|
ipcRenderer,
|
|
|
|
PageFaviconUpdatedEvent,
|
|
|
|
remote,
|
2020-04-01 09:53:00 +02:00
|
|
|
shell,
|
2020-05-25 06:16:28 +02:00
|
|
|
UpdateTargetUrlEvent,
|
2020-09-29 20:43:41 +02:00
|
|
|
WebContents,
|
|
|
|
WebviewTag,
|
2020-05-25 06:16:28 +02:00
|
|
|
} 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-01-10 15:14:41 +01:00
|
|
|
const {
|
|
|
|
Menu,
|
|
|
|
MenuItem,
|
|
|
|
dialog,
|
2020-04-01 09:24:10 +02:00
|
|
|
session,
|
2020-01-10 15:14:41 +01:00
|
|
|
} = remote;
|
|
|
|
|
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
|
|
|
|
|
|
|
// Service context menu
|
2020-05-25 06:16:28 +02:00
|
|
|
function openServiceContextMenu(event: Event, serviceId: number) {
|
2020-01-10 15:14:41 +01:00
|
|
|
event.preventDefault();
|
2020-01-12 13:07:37 +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-12 13:07:37 +01:00
|
|
|
|
|
|
|
const menu = new Menu();
|
|
|
|
const ready = service.view && service.viewReady, notReady = !service.view && !service.viewReady;
|
|
|
|
menu.append(new MenuItem({
|
|
|
|
label: 'Home', click: () => {
|
2020-09-29 20:43:41 +02:00
|
|
|
service.view?.loadURL(service.url)
|
2020-01-12 13:07:37 +01:00
|
|
|
.catch(console.error);
|
|
|
|
},
|
|
|
|
enabled: ready,
|
|
|
|
}));
|
|
|
|
menu.append(new MenuItem({
|
|
|
|
label: ready ? 'Reload' : 'Load', click: () => {
|
|
|
|
reloadService(serviceId);
|
|
|
|
},
|
|
|
|
enabled: ready || notReady,
|
|
|
|
}));
|
|
|
|
menu.append(new MenuItem({
|
|
|
|
label: 'Close', click: () => {
|
|
|
|
unloadService(serviceId);
|
|
|
|
},
|
|
|
|
enabled: ready,
|
|
|
|
}));
|
2020-04-01 14:10:26 +02:00
|
|
|
|
2020-01-12 13:07:37 +01:00
|
|
|
menu.append(new MenuItem({type: "separator"}));
|
2020-04-01 14:10:26 +02:00
|
|
|
|
2020-11-03 08:30:29 +01:00
|
|
|
menu.append(new MenuItem({
|
|
|
|
label: 'Reset zoom level', click: () => {
|
|
|
|
if (service.view) {
|
|
|
|
service.view.setZoomFactor(1);
|
|
|
|
service.view.setZoomLevel(0);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
enabled: ready && service.view?.getZoomFactor() !== 1 && service.view?.getZoomLevel() !== 0,
|
|
|
|
}));
|
|
|
|
menu.append(new MenuItem({
|
|
|
|
label: 'Zoom in', click: () => {
|
|
|
|
if (service.view) {
|
|
|
|
service.view.setZoomLevel(service.view.getZoomLevel() + 1);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
enabled: ready,
|
|
|
|
}));
|
|
|
|
menu.append(new MenuItem({
|
|
|
|
label: 'Zoom out', click: () => {
|
|
|
|
if (service.view) {
|
|
|
|
service.view.setZoomLevel(service.view.getZoomLevel() - 1);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
enabled: ready,
|
|
|
|
}));
|
|
|
|
|
|
|
|
menu.append(new MenuItem({type: "separator"}));
|
|
|
|
|
2020-09-29 20:43:41 +02:00
|
|
|
const permissionsMenu = [];
|
2020-04-01 14:10:26 +02:00
|
|
|
if (ready) {
|
2020-09-29 20:43:41 +02:00
|
|
|
for (const domain of Object.keys(service.permissions)) {
|
|
|
|
const domainPermissionsMenu = [];
|
2020-04-01 14:10:26 +02:00
|
|
|
|
2020-09-29 20:43:41 +02:00
|
|
|
const domainPermissions = service.permissions[domain];
|
|
|
|
if (domainPermissions) {
|
2020-04-01 14:10:26 +02:00
|
|
|
for (const permission of domainPermissions) {
|
|
|
|
domainPermissionsMenu.push({
|
|
|
|
label: (permission.authorized ? '✓' : '❌') + ' ' + permission.name,
|
|
|
|
submenu: [{
|
|
|
|
label: 'Toggle',
|
|
|
|
click: () => {
|
|
|
|
permission.authorized = !permission.authorized;
|
|
|
|
updateServicePermissions(serviceId);
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
label: 'Forget',
|
|
|
|
click: () => {
|
2020-09-29 20:43:41 +02:00
|
|
|
service.permissions[domain] = domainPermissions.filter(p => p !== permission);
|
2020-04-01 14:10:26 +02:00
|
|
|
},
|
|
|
|
}],
|
|
|
|
});
|
|
|
|
}
|
2020-09-29 20:43:41 +02:00
|
|
|
}
|
2020-04-01 14:10:26 +02:00
|
|
|
|
2020-09-29 20:43:41 +02:00
|
|
|
if (domainPermissionsMenu.length > 0) {
|
|
|
|
permissionsMenu.push({
|
|
|
|
label: domain,
|
|
|
|
submenu: domainPermissionsMenu,
|
|
|
|
});
|
2020-04-01 14:10:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
menu.append(new MenuItem({
|
|
|
|
label: 'Permissions',
|
|
|
|
enabled: ready,
|
|
|
|
submenu: permissionsMenu,
|
|
|
|
}));
|
|
|
|
|
|
|
|
menu.append(new MenuItem({type: "separator"}));
|
|
|
|
|
2020-01-12 13:07:37 +01:00
|
|
|
menu.append(new MenuItem({
|
|
|
|
label: 'Edit', click: () => {
|
|
|
|
ipcRenderer.send('openServiceSettings', serviceId);
|
2020-09-29 20:43:41 +02:00
|
|
|
},
|
2020-01-12 13:07:37 +01:00
|
|
|
}));
|
|
|
|
menu.append(new MenuItem({
|
|
|
|
label: 'Delete', click: () => {
|
|
|
|
dialog.showMessageBox(remote.getCurrentWindow(), {
|
|
|
|
type: 'question',
|
|
|
|
title: 'Confirm',
|
|
|
|
message: 'Are you sure you want to delete this service?',
|
|
|
|
buttons: ['Cancel', 'Confirm'],
|
|
|
|
cancelId: 0,
|
|
|
|
}).then(result => {
|
|
|
|
if (result.response === 1) {
|
|
|
|
ipcRenderer.send('deleteService', serviceId);
|
|
|
|
}
|
|
|
|
}).catch(console.error);
|
2020-09-29 20:43:41 +02:00
|
|
|
},
|
2020-01-12 13:07:37 +01:00
|
|
|
}));
|
|
|
|
menu.popup({window: remote.getCurrentWindow()});
|
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
|
|
|
|
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
|
|
|
});
|
|
|
|
button.addEventListener('contextmenu', e => openServiceContextMenu(e, index));
|
|
|
|
|
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');
|
2020-05-25 06:16:28 +02:00
|
|
|
addButton?.addEventListener('click', () => ipcRenderer.send('openServiceSettings', 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');
|
|
|
|
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-09-29 20:43:41 +02:00
|
|
|
const webContents = remote.webContents.fromId(view.getWebContentsId());
|
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') {
|
|
|
|
webContents.setUserAgent(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
|
|
|
|
setContextMenu(webContents);
|
|
|
|
|
2020-04-01 09:24:10 +02:00
|
|
|
// Set permission request handler
|
2020-05-25 06:16:28 +02:00
|
|
|
function getUrlDomain(url: string) {
|
2020-09-29 20:43:41 +02:00
|
|
|
const matches = url.match(/^https?:\/\/((.+?)\/|(.+))/i);
|
2020-05-25 06:16:28 +02:00
|
|
|
if (matches !== null) {
|
|
|
|
let domain = matches[1];
|
|
|
|
if (domain.endsWith('/')) domain = domain.substr(0, domain.length - 1);
|
|
|
|
return domain;
|
|
|
|
}
|
|
|
|
|
|
|
|
return '';
|
2020-04-01 13:39:20 +02:00
|
|
|
}
|
|
|
|
|
2020-05-25 06:16:28 +02:00
|
|
|
function getDomainPermissions(domain: string) {
|
2020-04-01 13:39:20 +02:00
|
|
|
let domainPermissions = service.permissions[domain];
|
|
|
|
if (!domainPermissions) domainPermissions = service.permissions[domain] = [];
|
|
|
|
return domainPermissions;
|
|
|
|
}
|
|
|
|
|
2020-09-29 20:43:41 +02:00
|
|
|
const serviceSession = session.fromPartition(view.partition);
|
|
|
|
serviceSession.setPermissionRequestHandler((webContents, permissionName, callback, details) => {
|
|
|
|
const domain = getUrlDomain(details.requestingUrl);
|
|
|
|
const domainPermissions = getDomainPermissions(domain);
|
2020-04-01 13:39:20 +02:00
|
|
|
|
2020-09-29 20:43:41 +02:00
|
|
|
const existingPermissions = domainPermissions.filter(p => p.name === permissionName);
|
2020-04-01 13:39:20 +02:00
|
|
|
if (existingPermissions.length > 0) {
|
|
|
|
callback(existingPermissions[0].authorized);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
dialog.showMessageBox(remote.getCurrentWindow(), {
|
|
|
|
type: 'question',
|
|
|
|
title: 'Grant ' + permissionName + ' permission',
|
|
|
|
message: 'Do you wish to grant the ' + permissionName + ' permission to ' + domain + '?',
|
|
|
|
buttons: ['Deny', 'Authorize'],
|
|
|
|
cancelId: 0,
|
|
|
|
}).then(result => {
|
|
|
|
const authorized = result.response === 1;
|
|
|
|
|
|
|
|
domainPermissions.push({
|
|
|
|
name: permissionName,
|
|
|
|
authorized: authorized,
|
|
|
|
});
|
|
|
|
updateServicePermissions(serviceId);
|
|
|
|
|
|
|
|
console.log(authorized ? 'Granted' : 'Denied', permissionName, 'for domain', domain);
|
|
|
|
callback(authorized);
|
|
|
|
}).catch(console.error);
|
2020-09-29 20:43:41 +02:00
|
|
|
});
|
2020-04-01 13:39:20 +02:00
|
|
|
serviceSession.setPermissionCheckHandler((webContents1, permissionName, requestingOrigin, details) => {
|
|
|
|
console.log('Permission check', permissionName, requestingOrigin, details);
|
2020-09-29 20:43:41 +02:00
|
|
|
const domain = getUrlDomain(details.requestingUrl);
|
|
|
|
const domainPermissions = getDomainPermissions(domain);
|
2020-04-01 13:39:20 +02:00
|
|
|
|
2020-09-29 20:43:41 +02:00
|
|
|
const existingPermissions = domainPermissions.filter(p => p.name === permissionName);
|
2020-04-01 13:39:20 +02:00
|
|
|
return existingPermissions.length > 0 && existingPermissions[0].authorized;
|
|
|
|
});
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-25 06:16:28 +02:00
|
|
|
function updateServicePermissions(serviceId: number) {
|
2020-04-01 13:39:20 +02:00
|
|
|
const service = services[serviceId];
|
2020-09-29 20:43:41 +02:00
|
|
|
if (!service) throw new Error('Service doesn\'t exist.');
|
|
|
|
|
2020-04-01 13:39:20 +02:00
|
|
|
ipcRenderer.send('updateServicePermissions', serviceId, service.permissions);
|
|
|
|
}
|
|
|
|
|
2020-01-10 15:14:41 +01:00
|
|
|
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) {
|
2020-01-12 13:29:24 +01:00
|
|
|
ipcRenderer.send('updateWindowTitle', null);
|
2020-09-29 20:43:41 +02:00
|
|
|
} else {
|
|
|
|
const service = services[selectedServiceId];
|
|
|
|
if (service?.viewReady && service.view) {
|
|
|
|
ipcRenderer.send('updateWindowTitle', selectedServiceId, remote.webContents.fromId(service.view.getWebContentsId()).getTitle());
|
|
|
|
}
|
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;
|
2020-02-20 12:46:53 +01:00
|
|
|
if (view) remote.webContents.fromId(view.getWebContentsId()).goForward();
|
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;
|
2020-02-20 12:46:53 +01:00
|
|
|
if (view) remote.webContents.fromId(view.getWebContentsId()).goBack();
|
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-05-25 06:16:28 +02:00
|
|
|
function setContextMenu(webContents: WebContents) {
|
2020-09-29 20:43:41 +02:00
|
|
|
webContents.on('context-menu', (event, props: ContextMenuParams) => {
|
2020-04-01 09:53:00 +02:00
|
|
|
const menu = new Menu();
|
|
|
|
const {editFlags} = props;
|
|
|
|
|
|
|
|
// linkURL
|
|
|
|
if (props.linkURL.length > 0) {
|
|
|
|
if (menu.items.length > 0) {
|
|
|
|
menu.append(new MenuItem({type: 'separator'}));
|
|
|
|
}
|
|
|
|
|
|
|
|
menu.append(new MenuItem({
|
|
|
|
label: 'Copy link URL',
|
|
|
|
click: () => {
|
|
|
|
clipboard.writeText(props.linkURL);
|
|
|
|
},
|
|
|
|
}));
|
|
|
|
menu.append(new MenuItem({
|
|
|
|
label: 'Open URL in default browser',
|
|
|
|
click: () => {
|
|
|
|
if (props.linkURL.startsWith('https://')) {
|
2020-05-19 05:18:15 +02:00
|
|
|
shell.openExternal(props.linkURL)
|
|
|
|
.catch(console.error);
|
2020-04-01 09:53:00 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Image
|
|
|
|
if (props.hasImageContents) {
|
|
|
|
if (menu.items.length > 0) {
|
|
|
|
menu.append(new MenuItem({type: 'separator'}));
|
|
|
|
}
|
|
|
|
|
|
|
|
menu.append(new MenuItem({
|
|
|
|
label: 'Copy image',
|
|
|
|
click: () => {
|
|
|
|
webContents.copyImageAt(props.x, props.y);
|
|
|
|
},
|
|
|
|
}));
|
|
|
|
|
|
|
|
menu.append(new MenuItem({
|
|
|
|
label: 'Save image as',
|
|
|
|
click: () => {
|
|
|
|
webContents.downloadURL(props.srcURL);
|
|
|
|
},
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Text clipboard
|
2020-09-29 20:43:41 +02:00
|
|
|
if (editFlags.canUndo || editFlags.canRedo || editFlags.canCut || editFlags.canCopy || editFlags.canPaste ||
|
|
|
|
editFlags.canDelete) {
|
2020-04-01 09:53:00 +02:00
|
|
|
if (editFlags.canUndo || editFlags.canRedo) {
|
|
|
|
if (menu.items.length > 0) {
|
|
|
|
menu.append(new MenuItem({type: 'separator'}));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (editFlags.canUndo) {
|
|
|
|
menu.append(new MenuItem({
|
|
|
|
label: 'Undo',
|
|
|
|
role: 'undo',
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
if (editFlags.canRedo) {
|
|
|
|
menu.append(new MenuItem({
|
|
|
|
label: 'Redo',
|
|
|
|
role: 'redo',
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (menu.items.length > 0) {
|
|
|
|
menu.append(new MenuItem({type: 'separator'}));
|
|
|
|
}
|
|
|
|
|
|
|
|
menu.append(new MenuItem({
|
|
|
|
label: 'Cut',
|
|
|
|
role: 'cut',
|
|
|
|
enabled: editFlags.canCut,
|
|
|
|
}));
|
|
|
|
menu.append(new MenuItem({
|
|
|
|
label: 'Copy',
|
|
|
|
role: 'copy',
|
|
|
|
enabled: editFlags.canCopy,
|
|
|
|
}));
|
|
|
|
menu.append(new MenuItem({
|
|
|
|
label: 'Paste',
|
|
|
|
role: 'paste',
|
|
|
|
enabled: editFlags.canPaste,
|
|
|
|
}));
|
|
|
|
menu.append(new MenuItem({
|
|
|
|
label: 'Delete',
|
|
|
|
role: 'delete',
|
|
|
|
enabled: editFlags.canDelete,
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (editFlags.canSelectAll) {
|
|
|
|
if (menu.items.length > 0) {
|
|
|
|
menu.append(new MenuItem({type: 'separator'}));
|
|
|
|
}
|
|
|
|
|
|
|
|
menu.append(new MenuItem({
|
|
|
|
label: 'Select all',
|
|
|
|
role: 'selectAll',
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Inspect element
|
|
|
|
if (menu.items.length > 0) {
|
|
|
|
menu.append(new MenuItem({type: 'separator'}));
|
|
|
|
}
|
|
|
|
|
|
|
|
menu.append(new MenuItem({
|
|
|
|
label: 'Inspect element',
|
|
|
|
click: () => {
|
|
|
|
webContents.inspectElement(props.x, props.y);
|
|
|
|
},
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
|
|
menu.popup({
|
|
|
|
window: remote.getCurrentWindow(),
|
|
|
|
});
|
|
|
|
});
|
2020-06-24 18:51: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 & {
|
|
|
|
view?: WebviewTag;
|
|
|
|
viewReady?: boolean;
|
|
|
|
li?: NavigationElement;
|
|
|
|
};
|
|
|
|
|
|
|
|
type NavigationElement = HTMLLIElement & {
|
|
|
|
serviceId: number;
|
|
|
|
button: HTMLButtonElement;
|
|
|
|
};
|