2020-07-13 16:58:49 +00:00
|
|
|
import { dialog, Menu } from 'electron/main';
|
2020-03-20 20:28:31 +00:00
|
|
|
import * as fs from 'fs';
|
|
|
|
import * as url from 'url';
|
2019-02-01 18:56:46 +00:00
|
|
|
|
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';
|
2020-10-13 21:11:06 +00:00
|
|
|
import { IPC_MESSAGES } from '@electron/internal//common/ipc-messages';
|
2019-02-01 18:56:46 +00:00
|
|
|
|
2019-07-17 17:23:52 +00:00
|
|
|
const convertToMenuTemplate = function (items: ContextMenuItem[], handler: (id: number) => void) {
|
2019-02-01 18:56:46 +00:00
|
|
|
return items.map(function (item) {
|
2019-07-17 17:23:52 +00:00
|
|
|
const transformed: Electron.MenuItemConstructorOptions = item.type === 'subMenu' ? {
|
2019-02-01 18:56:46 +00:00
|
|
|
type: 'submenu',
|
|
|
|
label: item.label,
|
|
|
|
enabled: item.enabled,
|
2019-03-19 17:37:43 +00:00
|
|
|
submenu: convertToMenuTemplate(item.subItems, handler)
|
2019-02-01 18:56:46 +00:00
|
|
|
} : 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
|
|
|
};
|
2019-02-01 18:56:46 +00:00
|
|
|
|
|
|
|
if (item.id != null) {
|
2020-03-20 20:28:31 +00:00
|
|
|
transformed.click = () => handler(item.id);
|
2019-02-01 18:56:46 +00:00
|
|
|
}
|
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
return transformed;
|
|
|
|
});
|
|
|
|
};
|
2019-02-01 18:56:46 +00:00
|
|
|
|
2019-07-17 17:23:52 +00:00
|
|
|
const getEditMenuItems = function (): Electron.MenuItemConstructorOptions[] {
|
2019-02-01 18:56:46 +00:00
|
|
|
return [
|
|
|
|
{ role: 'undo' },
|
|
|
|
{ role: 'redo' },
|
|
|
|
{ type: 'separator' },
|
|
|
|
{ role: 'cut' },
|
|
|
|
{ role: 'copy' },
|
|
|
|
{ role: 'paste' },
|
2019-07-31 21:13:56 +00:00
|
|
|
{ role: 'pasteAndMatchStyle' },
|
2019-02-01 18:56:46 +00:00
|
|
|
{ role: 'delete' },
|
2019-07-31 21:13:56 +00:00
|
|
|
{ role: 'selectAll' }
|
2020-03-20 20:28:31 +00:00
|
|
|
];
|
|
|
|
};
|
2019-02-01 18:56:46 +00:00
|
|
|
|
2019-07-17 17:23:52 +00:00
|
|
|
const isChromeDevTools = function (pageURL: string) {
|
2020-03-20 20:28:31 +00:00
|
|
|
const { protocol } = url.parse(pageURL);
|
|
|
|
return protocol === 'devtools:';
|
|
|
|
};
|
2019-02-01 18:56:46 +00:00
|
|
|
|
2019-07-17 17:23:52 +00:00
|
|
|
const assertChromeDevTools = function (contents: Electron.WebContents, api: string) {
|
2021-06-02 03:39:48 +00:00
|
|
|
const pageURL = contents.getURL();
|
2019-02-01 18:56:46 +00:00
|
|
|
if (!isChromeDevTools(pageURL)) {
|
2020-03-20 20:28:31 +00:00
|
|
|
console.error(`Blocked ${pageURL} from calling ${api}`);
|
|
|
|
throw new Error(`Blocked ${api}`);
|
2019-02-01 18:56:46 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
};
|
2019-02-01 18:56:46 +00:00
|
|
|
|
2020-10-13 21:11:06 +00:00
|
|
|
ipcMainInternal.handle(IPC_MESSAGES.INSPECTOR_CONTEXT_MENU, function (event, items: ContextMenuItem[], isEditMenu: boolean) {
|
2021-01-22 19:25:47 +00:00
|
|
|
return new Promise<number | void>(resolve => {
|
2020-03-20 20:28:31 +00:00
|
|
|
assertChromeDevTools(event.sender, 'window.InspectorFrontendHost.showContextMenuAtPoint()');
|
2019-03-19 17:37:43 +00:00
|
|
|
|
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();
|
2019-03-19 17:37:43 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
menu.popup({ window, callback: () => resolve() });
|
|
|
|
});
|
|
|
|
});
|
2019-02-01 18:56:46 +00:00
|
|
|
|
2020-10-13 21:11:06 +00:00
|
|
|
ipcMainInternal.handle(IPC_MESSAGES.INSPECTOR_SELECT_FILE, async function (event) {
|
2020-03-20 20:28:31 +00:00
|
|
|
assertChromeDevTools(event.sender, 'window.UI.createFileSelectorElement()');
|
2019-03-06 21:22:45 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const result = await dialog.showOpenDialog({});
|
|
|
|
if (result.canceled) return [];
|
2019-03-06 21:22:45 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const path = result.filePaths[0];
|
|
|
|
const data = await fs.promises.readFile(path);
|
2019-03-06 21:22:45 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
return [path, data];
|
|
|
|
});
|
2019-02-01 18:56:46 +00:00
|
|
|
|
2020-10-13 21:11:06 +00:00
|
|
|
ipcMainUtils.handleSync(IPC_MESSAGES.INSPECTOR_CONFIRM, async function (event, message: string = '', title: string = '') {
|
2020-03-20 20:28:31 +00:00
|
|
|
assertChromeDevTools(event.sender, 'window.confirm()');
|
2019-03-14 22:29:40 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
});
|