inspector.ts 2.3 KB

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