electron/lib/browser/devtools.ts

100 lines
3.1 KiB
TypeScript
Raw Normal View History

import { dialog, Menu } from 'electron/main';
2020-03-20 20:28:31 +00:00
import * as fs from 'fs';
import * as url from 'url';
2020-03-20 20:28:31 +00:00
import { ipcMainInternal } from '@electron/internal/browser/ipc-main-internal';
import * as ipcMainUtils from '@electron/internal/browser/ipc-main-internal-utils';
const convertToMenuTemplate = function (items: ContextMenuItem[], handler: (id: number) => void) {
return items.map(function (item) {
const transformed: Electron.MenuItemConstructorOptions = item.type === 'subMenu' ? {
type: 'submenu',
label: item.label,
enabled: item.enabled,
submenu: convertToMenuTemplate(item.subItems, handler)
} : item.type === 'separator' ? {
type: 'separator'
} : item.type === 'checkbox' ? {
type: 'checkbox',
label: item.label,
enabled: item.enabled,
checked: item.checked
} : {
type: 'normal',
label: item.label,
enabled: item.enabled
2020-03-20 20:28:31 +00:00
};
if (item.id != null) {
2020-03-20 20:28:31 +00:00
transformed.click = () => handler(item.id);
}
2020-03-20 20:28:31 +00:00
return transformed;
});
};
const getEditMenuItems = function (): Electron.MenuItemConstructorOptions[] {
return [
{ role: 'undo' },
{ role: 'redo' },
{ type: 'separator' },
{ role: 'cut' },
{ role: 'copy' },
{ role: 'paste' },
{ role: 'pasteAndMatchStyle' },
{ role: 'delete' },
{ role: 'selectAll' }
2020-03-20 20:28:31 +00:00
];
};
const isChromeDevTools = function (pageURL: string) {
2020-03-20 20:28:31 +00:00
const { protocol } = url.parse(pageURL);
return protocol === 'devtools:';
};
const assertChromeDevTools = function (contents: Electron.WebContents, api: string) {
2020-03-20 20:28:31 +00:00
const pageURL = contents._getURL();
if (!isChromeDevTools(pageURL)) {
2020-03-20 20:28:31 +00:00
console.error(`Blocked ${pageURL} from calling ${api}`);
throw new Error(`Blocked ${api}`);
}
2020-03-20 20:28:31 +00:00
};
ipcMainInternal.handle('ELECTRON_INSPECTOR_CONTEXT_MENU', function (event: Electron.IpcMainInvokeEvent, items: ContextMenuItem[], isEditMenu: boolean) {
return new Promise(resolve => {
2020-03-20 20:28:31 +00:00
assertChromeDevTools(event.sender, 'window.InspectorFrontendHost.showContextMenuAtPoint()');
2020-03-20 20:28:31 +00:00
const template = isEditMenu ? getEditMenuItems() : convertToMenuTemplate(items, resolve);
const menu = Menu.buildFromTemplate(template);
const window = event.sender.getOwnerBrowserWindow();
2020-03-20 20:28:31 +00:00
menu.popup({ window, callback: () => resolve() });
});
});
ipcMainInternal.handle('ELECTRON_INSPECTOR_SELECT_FILE', async function (event: Electron.IpcMainInvokeEvent) {
2020-03-20 20:28:31 +00:00
assertChromeDevTools(event.sender, 'window.UI.createFileSelectorElement()');
2020-03-20 20:28:31 +00:00
const result = await dialog.showOpenDialog({});
if (result.canceled) return [];
2020-03-20 20:28:31 +00:00
const path = result.filePaths[0];
const data = await fs.promises.readFile(path);
2020-03-20 20:28:31 +00:00
return [path, data];
});
ipcMainUtils.handleSync('ELECTRON_INSPECTOR_CONFIRM', async function (event: Electron.IpcMainInvokeEvent, message: string = '', title: string = '') {
2020-03-20 20:28:31 +00:00
assertChromeDevTools(event.sender, 'window.confirm()');
const options = {
message: String(message),
title: String(title),
buttons: ['OK', 'Cancel'],
cancelId: 1
2020-03-20 20:28:31 +00:00
};
const window = event.sender.getOwnerBrowserWindow();
const { response } = await dialog.showMessageBox(window, options);
return response === 0;
});