inspector.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import { ipcRendererInternal } from '@electron/internal/renderer/ipc-renderer-internal';
  2. import * as ipcRendererUtils from '@electron/internal/renderer/ipc-renderer-internal-utils';
  3. import { IPC_MESSAGES } from '../common/ipc-messages';
  4. window.onload = function () {
  5. // Use menu API to show context menu.
  6. window.InspectorFrontendHost!.showContextMenuAtPoint = createMenu;
  7. // correct for Chromium returning undefined for filesystem
  8. window.Persistence!.FileSystemWorkspaceBinding.completeURL = completeURL;
  9. // Use dialog API to override file chooser dialog.
  10. window.UI!.createFileSelectorElement = createFileSelectorElement;
  11. };
  12. // Extra / is needed as a result of MacOS requiring absolute paths
  13. function completeURL (project: string, path: string) {
  14. project = 'file:///';
  15. return `${project}${path}`;
  16. }
  17. // The DOM implementation expects (message?: string) => boolean
  18. window.confirm = function (message?: string, title?: string) {
  19. return ipcRendererUtils.invokeSync(IPC_MESSAGES.INSPECTOR_CONFIRM, message, title) as boolean;
  20. };
  21. const useEditMenuItems = function (x: number, y: number, items: ContextMenuItem[]) {
  22. return items.length === 0 && document.elementsFromPoint(x, y).some(function (element) {
  23. return element.nodeName === 'INPUT' ||
  24. element.nodeName === 'TEXTAREA' ||
  25. (element as HTMLElement).isContentEditable;
  26. });
  27. };
  28. const createMenu = function (x: number, y: number, items: ContextMenuItem[]) {
  29. const isEditMenu = useEditMenuItems(x, y, items);
  30. ipcRendererInternal.invoke<number>(IPC_MESSAGES.INSPECTOR_CONTEXT_MENU, items, isEditMenu).then(id => {
  31. if (typeof id === 'number') {
  32. window.DevToolsAPI!.contextMenuItemSelected(id);
  33. }
  34. window.DevToolsAPI!.contextMenuCleared();
  35. });
  36. };
  37. const showFileChooserDialog = function (callback: (blob: File) => void) {
  38. ipcRendererInternal.invoke<[ string, any ]>(IPC_MESSAGES.INSPECTOR_SELECT_FILE).then(([path, data]) => {
  39. if (path && data) {
  40. callback(dataToHtml5FileObject(path, data));
  41. }
  42. });
  43. };
  44. const dataToHtml5FileObject = function (path: string, data: any) {
  45. return new File([data], path);
  46. };
  47. const createFileSelectorElement = function (this: any, callback: () => void) {
  48. const fileSelectorElement = document.createElement('span');
  49. fileSelectorElement.style.display = 'none';
  50. fileSelectorElement.click = showFileChooserDialog.bind(this, callback);
  51. return fileSelectorElement;
  52. };