electron/lib/renderer/inspector.ts

80 lines
3.2 KiB
TypeScript
Raw Normal View History

import { internalContextBridge } from '@electron/internal/renderer/api/context-bridge';
2020-03-20 20:28:31 +00:00
import { ipcRendererInternal } from '@electron/internal/renderer/ipc-renderer-internal';
import * as ipcRendererUtils from '@electron/internal/renderer/ipc-renderer-internal-utils';
import { webFrame } from 'electron/renderer';
import { IPC_MESSAGES } from '../common/ipc-messages';
const { contextIsolationEnabled } = internalContextBridge;
/* Corrects for some Inspector adaptations needed in Electron.
* 1) Use menu API to show context menu.
* 2) Correct for Chromium returning undefined for filesystem.
* 3) Use dialog API to override file chooser dialog.
*/
window.onload = function () {
if (contextIsolationEnabled) {
internalContextBridge.overrideGlobalValueFromIsolatedWorld([
'InspectorFrontendHost', 'showContextMenuAtPoint'
], createMenu);
internalContextBridge.overrideGlobalValueFromIsolatedWorld([
'Persistence', 'FileSystemWorkspaceBinding', 'completeURL'
], completeURL);
internalContextBridge.overrideGlobalValueFromIsolatedWorld([
'UI', 'createFileSelectorElement'
], createFileSelectorElement);
} else {
window.InspectorFrontendHost!.showContextMenuAtPoint = createMenu;
window.Persistence!.FileSystemWorkspaceBinding.completeURL = completeURL;
window.UI!.createFileSelectorElement = createFileSelectorElement;
}
2020-03-20 20:28:31 +00:00
};
// Extra / is needed as a result of MacOS requiring absolute paths
function completeURL (project: string, path: string) {
2020-03-20 20:28:31 +00:00
project = 'file:///';
return `${project}${path}`;
}
// The DOM implementation expects (message?: string) => boolean
window.confirm = function (message?: string, title?: string) {
return ipcRendererUtils.invokeSync(IPC_MESSAGES.INSPECTOR_CONFIRM, message, title) as boolean;
2020-03-20 20:28:31 +00:00
};
const useEditMenuItems = function (x: number, y: number, items: ContextMenuItem[]) {
return items.length === 0 && document.elementsFromPoint(x, y).some(function (element) {
return element.nodeName === 'INPUT' ||
element.nodeName === 'TEXTAREA' ||
2020-03-20 20:28:31 +00:00
(element as HTMLElement).isContentEditable;
});
};
const createMenu = function (x: number, y: number, items: ContextMenuItem[]) {
2020-03-20 20:28:31 +00:00
const isEditMenu = useEditMenuItems(x, y, items);
ipcRendererInternal.invoke<number>(IPC_MESSAGES.INSPECTOR_CONTEXT_MENU, items, isEditMenu).then(id => {
if (typeof id === 'number') {
webFrame.executeJavaScript(`window.DevToolsAPI.contextMenuItemSelected(${JSON.stringify(id)})`);
}
webFrame.executeJavaScript('window.DevToolsAPI.contextMenuCleared()');
2020-03-20 20:28:31 +00:00
});
};
const showFileChooserDialog = function (callback: (blob: File) => void) {
ipcRendererInternal.invoke<[ string, any ]>(IPC_MESSAGES.INSPECTOR_SELECT_FILE).then(([path, data]) => {
if (path && data) {
2020-03-20 20:28:31 +00:00
callback(dataToHtml5FileObject(path, data));
}
2020-03-20 20:28:31 +00:00
});
};
const dataToHtml5FileObject = function (path: string, data: any) {
2020-03-20 20:28:31 +00:00
return new File([data], path);
};
const createFileSelectorElement = function (this: any, callback: () => void) {
2020-03-20 20:28:31 +00:00
const fileSelectorElement = document.createElement('span');
fileSelectorElement.style.display = 'none';
fileSelectorElement.click = showFileChooserDialog.bind(this, callback);
return fileSelectorElement;
};