2024-10-03 03:52:01 +00:00
|
|
|
import { IPC_MESSAGES } from '@electron/internal//common/ipc-messages';
|
2019-08-23 22:45:50 +00:00
|
|
|
import { ipcMainInternal } from '@electron/internal/browser/ipc-main-internal';
|
|
|
|
import * as ipcMainUtils from '@electron/internal/browser/ipc-main-internal-utils';
|
2024-10-03 03:52:01 +00:00
|
|
|
|
|
|
|
import { dialog, Menu } from 'electron/main';
|
|
|
|
|
|
|
|
import * as fs from 'fs';
|
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) {
|
2024-10-01 18:09:28 +00:00
|
|
|
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
|
|
|
|
};
|
2019-02-01 18:56:46 +00:00
|
|
|
|
|
|
|
if (item.id != null) {
|
2019-03-19 17:37:43 +00:00
|
|
|
transformed.click = () => handler(item.id);
|
2019-02-01 18:56:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return transformed;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
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' }
|
2019-02-01 18:56:46 +00:00
|
|
|
];
|
|
|
|
};
|
|
|
|
|
2019-07-17 17:23:52 +00:00
|
|
|
const isChromeDevTools = function (pageURL: string) {
|
2023-06-26 17:51:59 +00:00
|
|
|
const { protocol } = new URL(pageURL);
|
2019-05-17 22:37:09 +00:00
|
|
|
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)) {
|
|
|
|
console.error(`Blocked ${pageURL} from calling ${api}`);
|
|
|
|
throw new Error(`Blocked ${api}`);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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 => {
|
2019-03-19 17:37:43 +00:00
|
|
|
assertChromeDevTools(event.sender, 'window.InspectorFrontendHost.showContextMenuAtPoint()');
|
|
|
|
|
|
|
|
const template = isEditMenu ? getEditMenuItems() : convertToMenuTemplate(items, resolve);
|
|
|
|
const menu = Menu.buildFromTemplate(template);
|
2021-07-27 05:48:12 +00:00
|
|
|
const window = event.sender.getOwnerBrowserWindow()!;
|
2019-03-19 17:37:43 +00:00
|
|
|
|
2019-06-03 21:48:01 +00:00
|
|
|
menu.popup({ window, callback: () => resolve() });
|
2019-03-19 17:37:43 +00:00
|
|
|
});
|
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) {
|
2019-03-06 21:22:45 +00:00
|
|
|
assertChromeDevTools(event.sender, 'window.UI.createFileSelectorElement()');
|
|
|
|
|
|
|
|
const result = await dialog.showOpenDialog({});
|
|
|
|
if (result.canceled) return [];
|
|
|
|
|
|
|
|
const path = result.filePaths[0];
|
2019-04-29 18:18:03 +00:00
|
|
|
const data = await fs.promises.readFile(path);
|
2019-03-06 21:22:45 +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 = '') {
|
2019-03-14 22:29:40 +00:00
|
|
|
assertChromeDevTools(event.sender, 'window.confirm()');
|
|
|
|
|
|
|
|
const options = {
|
|
|
|
message: String(message),
|
|
|
|
title: String(title),
|
|
|
|
buttons: ['OK', 'Cancel'],
|
|
|
|
cancelId: 1
|
|
|
|
};
|
2021-07-27 05:48:12 +00:00
|
|
|
const window = event.sender.getOwnerBrowserWindow()!;
|
2019-03-14 22:29:40 +00:00
|
|
|
const { response } = await dialog.showMessageBox(window, options);
|
|
|
|
return response === 0;
|
2019-02-01 18:56:46 +00:00
|
|
|
});
|