2019-07-17 17:23:52 +00:00
|
|
|
import { dialog, Menu } from 'electron'
|
|
|
|
import * as fs from 'fs'
|
|
|
|
import * as url from 'url'
|
2019-02-01 18:56:46 +00:00
|
|
|
|
2019-08-23 22:45:50 +00:00
|
|
|
import { ipcMainInternal } from '@electron/internal/browser/ipc-main-internal'
|
|
|
|
import * as ipcMainUtils from '@electron/internal/browser/ipc-main-internal-utils'
|
2019-02-01 18:56:46 +00:00
|
|
|
|
2019-07-17 17:23:52 +00:00
|
|
|
const convertToMenuTemplate = function (items: ContextMenuItem[], handler: (id: number) => void) {
|
2019-02-01 18:56:46 +00:00
|
|
|
return items.map(function (item) {
|
2019-07-17 17:23:52 +00:00
|
|
|
const transformed: Electron.MenuItemConstructorOptions = item.type === 'subMenu' ? {
|
2019-02-01 18:56:46 +00:00
|
|
|
type: 'submenu',
|
|
|
|
label: item.label,
|
|
|
|
enabled: item.enabled,
|
2019-03-19 17:37:43 +00:00
|
|
|
submenu: convertToMenuTemplate(item.subItems, handler)
|
2019-02-01 18:56:46 +00:00
|
|
|
} : item.type === 'separator' ? {
|
|
|
|
type: 'separator'
|
|
|
|
} : item.type === 'checkbox' ? {
|
|
|
|
type: 'checkbox',
|
|
|
|
label: item.label,
|
|
|
|
enabled: item.enabled,
|
|
|
|
checked: item.checked
|
|
|
|
} : {
|
|
|
|
type: 'normal',
|
|
|
|
label: item.label,
|
|
|
|
enabled: item.enabled
|
|
|
|
}
|
|
|
|
|
|
|
|
if (item.id != null) {
|
2019-03-19 17:37:43 +00:00
|
|
|
transformed.click = () => handler(item.id)
|
2019-02-01 18:56:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return transformed
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-07-17 17:23:52 +00:00
|
|
|
const getEditMenuItems = function (): Electron.MenuItemConstructorOptions[] {
|
2019-02-01 18:56:46 +00:00
|
|
|
return [
|
|
|
|
{ role: 'undo' },
|
|
|
|
{ role: 'redo' },
|
|
|
|
{ type: 'separator' },
|
|
|
|
{ role: 'cut' },
|
|
|
|
{ role: 'copy' },
|
|
|
|
{ role: 'paste' },
|
2019-07-31 21:13:56 +00:00
|
|
|
{ role: 'pasteAndMatchStyle' },
|
2019-02-01 18:56:46 +00:00
|
|
|
{ role: 'delete' },
|
2019-07-31 21:13:56 +00:00
|
|
|
{ role: 'selectAll' }
|
2019-02-01 18:56:46 +00:00
|
|
|
]
|
|
|
|
}
|
|
|
|
|
2019-07-17 17:23:52 +00:00
|
|
|
const isChromeDevTools = function (pageURL: string) {
|
2019-02-01 18:56:46 +00:00
|
|
|
const { protocol } = url.parse(pageURL)
|
2019-05-17 22:37:09 +00:00
|
|
|
return protocol === 'devtools:'
|
2019-02-01 18:56:46 +00:00
|
|
|
}
|
|
|
|
|
2019-07-17 17:23:52 +00:00
|
|
|
const assertChromeDevTools = function (contents: Electron.WebContents, api: string) {
|
2019-02-01 18:56:46 +00:00
|
|
|
const pageURL = contents._getURL()
|
|
|
|
if (!isChromeDevTools(pageURL)) {
|
|
|
|
console.error(`Blocked ${pageURL} from calling ${api}`)
|
|
|
|
throw new Error(`Blocked ${api}`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-23 22:45:50 +00:00
|
|
|
ipcMainInternal.handle('ELECTRON_INSPECTOR_CONTEXT_MENU', function (event: Electron.IpcMainInvokeEvent, items: ContextMenuItem[], isEditMenu: boolean) {
|
2019-03-19 17:37:43 +00:00
|
|
|
return new Promise(resolve => {
|
|
|
|
assertChromeDevTools(event.sender, 'window.InspectorFrontendHost.showContextMenuAtPoint()')
|
|
|
|
|
|
|
|
const template = isEditMenu ? getEditMenuItems() : convertToMenuTemplate(items, resolve)
|
|
|
|
const menu = Menu.buildFromTemplate(template)
|
|
|
|
const window = event.sender.getOwnerBrowserWindow()
|
|
|
|
|
2019-06-03 21:48:01 +00:00
|
|
|
menu.popup({ window, callback: () => resolve() })
|
2019-03-19 17:37:43 +00:00
|
|
|
})
|
2019-02-01 18:56:46 +00:00
|
|
|
})
|
|
|
|
|
2019-08-23 22:45:50 +00:00
|
|
|
ipcMainInternal.handle('ELECTRON_INSPECTOR_SELECT_FILE', async function (event: Electron.IpcMainInvokeEvent) {
|
2019-03-06 21:22:45 +00:00
|
|
|
assertChromeDevTools(event.sender, 'window.UI.createFileSelectorElement()')
|
|
|
|
|
|
|
|
const result = await dialog.showOpenDialog({})
|
|
|
|
if (result.canceled) return []
|
|
|
|
|
|
|
|
const path = result.filePaths[0]
|
2019-04-29 18:18:03 +00:00
|
|
|
const data = await fs.promises.readFile(path)
|
2019-03-06 21:22:45 +00:00
|
|
|
|
|
|
|
return [path, data]
|
2019-02-01 18:56:46 +00:00
|
|
|
})
|
|
|
|
|
2019-08-23 22:45:50 +00:00
|
|
|
ipcMainUtils.handleSync('ELECTRON_INSPECTOR_CONFIRM', async function (event: Electron.IpcMainInvokeEvent, message: string = '', title: string = '') {
|
2019-03-14 22:29:40 +00:00
|
|
|
assertChromeDevTools(event.sender, 'window.confirm()')
|
|
|
|
|
|
|
|
const options = {
|
|
|
|
message: String(message),
|
|
|
|
title: String(title),
|
|
|
|
buttons: ['OK', 'Cancel'],
|
|
|
|
cancelId: 1
|
|
|
|
}
|
|
|
|
const window = event.sender.getOwnerBrowserWindow()
|
|
|
|
const { response } = await dialog.showMessageBox(window, options)
|
|
|
|
return response === 0
|
2019-02-01 18:56:46 +00:00
|
|
|
})
|