2024-10-03 02:10:44 +00:00
|
|
|
import { IPC_MESSAGES } from '@electron/internal/common/ipc-messages';
|
2021-06-24 18:36:22 +00:00
|
|
|
import { internalContextBridge } from '@electron/internal/renderer/api/context-bridge';
|
2019-08-23 22:45:50 +00:00
|
|
|
import { ipcRendererInternal } from '@electron/internal/renderer/ipc-renderer-internal';
|
|
|
|
import * as ipcRendererUtils from '@electron/internal/renderer/ipc-renderer-internal-utils';
|
2024-10-03 02:10:44 +00:00
|
|
|
|
2021-06-24 18:36:22 +00:00
|
|
|
import { webFrame } from 'electron/renderer';
|
2019-02-26 02:10:26 +00:00
|
|
|
|
2021-06-24 18:36:22 +00:00
|
|
|
const { contextIsolationEnabled } = internalContextBridge;
|
2019-02-26 02:10:26 +00:00
|
|
|
|
2021-06-24 18:36:22 +00:00
|
|
|
/* Corrects for some Inspector adaptations needed in Electron.
|
|
|
|
* 1) Use menu API to show context menu.
|
|
|
|
*/
|
|
|
|
window.onload = function () {
|
|
|
|
if (contextIsolationEnabled) {
|
|
|
|
internalContextBridge.overrideGlobalValueFromIsolatedWorld([
|
|
|
|
'InspectorFrontendHost', 'showContextMenuAtPoint'
|
|
|
|
], createMenu);
|
|
|
|
} else {
|
|
|
|
window.InspectorFrontendHost!.showContextMenuAtPoint = createMenu;
|
|
|
|
}
|
2019-02-26 02:10:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// The DOM implementation expects (message?: string) => boolean
|
2020-10-08 01:01:23 +00:00
|
|
|
window.confirm = function (message?: string, title?: string) {
|
2020-10-13 21:11:06 +00:00
|
|
|
return ipcRendererUtils.invokeSync(IPC_MESSAGES.INSPECTOR_CONFIRM, message, title) as boolean;
|
2019-02-26 02:10:26 +00:00
|
|
|
};
|
|
|
|
|
2019-07-17 17:23:52 +00:00
|
|
|
const useEditMenuItems = function (x: number, y: number, items: ContextMenuItem[]) {
|
2022-01-10 22:31:39 +00:00
|
|
|
return items.length === 0 && document.elementsFromPoint(x, y).some(element => {
|
2019-02-26 02:10:26 +00:00
|
|
|
return element.nodeName === 'INPUT' ||
|
|
|
|
element.nodeName === 'TEXTAREA' ||
|
|
|
|
(element as HTMLElement).isContentEditable;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2019-07-17 17:23:52 +00:00
|
|
|
const createMenu = function (x: number, y: number, items: ContextMenuItem[]) {
|
2019-02-26 02:10:26 +00:00
|
|
|
const isEditMenu = useEditMenuItems(x, y, items);
|
2020-10-13 21:11:06 +00:00
|
|
|
ipcRendererInternal.invoke<number>(IPC_MESSAGES.INSPECTOR_CONTEXT_MENU, items, isEditMenu).then(id => {
|
2019-03-19 17:37:43 +00:00
|
|
|
if (typeof id === 'number') {
|
2021-06-24 18:36:22 +00:00
|
|
|
webFrame.executeJavaScript(`window.DevToolsAPI.contextMenuItemSelected(${JSON.stringify(id)})`);
|
2019-03-19 17:37:43 +00:00
|
|
|
}
|
2021-06-24 18:36:22 +00:00
|
|
|
|
|
|
|
webFrame.executeJavaScript('window.DevToolsAPI.contextMenuCleared()');
|
2019-03-19 17:37:43 +00:00
|
|
|
});
|
2019-02-26 02:10:26 +00:00
|
|
|
};
|