inspector.ts 2.2 KB

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