2019-02-01 18:56:46 +00:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const { dialog, Menu } = require('electron')
|
|
|
|
const fs = require('fs')
|
|
|
|
const url = require('url')
|
2019-03-06 21:22:45 +00:00
|
|
|
const util = require('util')
|
2019-02-01 18:56:46 +00:00
|
|
|
|
|
|
|
const ipcMainUtils = require('@electron/internal/browser/ipc-main-internal-utils')
|
|
|
|
|
2019-03-06 21:22:45 +00:00
|
|
|
const readFile = util.promisify(fs.readFile)
|
|
|
|
|
2019-03-19 17:37:43 +00:00
|
|
|
const convertToMenuTemplate = function (items, handler) {
|
2019-02-01 18:56:46 +00:00
|
|
|
return items.map(function (item) {
|
|
|
|
const transformed = item.type === 'subMenu' ? {
|
|
|
|
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
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const getEditMenuItems = function () {
|
|
|
|
return [
|
|
|
|
{ role: 'undo' },
|
|
|
|
{ role: 'redo' },
|
|
|
|
{ type: 'separator' },
|
|
|
|
{ role: 'cut' },
|
|
|
|
{ role: 'copy' },
|
|
|
|
{ role: 'paste' },
|
|
|
|
{ role: 'pasteAndMatchStyle' },
|
|
|
|
{ role: 'delete' },
|
|
|
|
{ role: 'selectAll' }
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
const isChromeDevTools = function (pageURL) {
|
|
|
|
const { protocol } = url.parse(pageURL)
|
|
|
|
return protocol === 'chrome-devtools:'
|
|
|
|
}
|
|
|
|
|
|
|
|
const assertChromeDevTools = function (contents, api) {
|
|
|
|
const pageURL = contents._getURL()
|
|
|
|
if (!isChromeDevTools(pageURL)) {
|
|
|
|
console.error(`Blocked ${pageURL} from calling ${api}`)
|
|
|
|
throw new Error(`Blocked ${api}`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ipcMainUtils.handle('ELECTRON_INSPECTOR_CONTEXT_MENU', function (event, items, isEditMenu) {
|
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()
|
|
|
|
|
|
|
|
menu.once('menu-will-close', () => {
|
|
|
|
// menu-will-close is emitted before the click handler, but needs to be sent after.
|
|
|
|
// otherwise, DevToolsAPI.contextMenuCleared() would be called before
|
|
|
|
// DevToolsAPI.contextMenuItemSelected(id) and the menu will not work properly.
|
|
|
|
setTimeout(() => resolve())
|
2019-02-01 18:56:46 +00:00
|
|
|
})
|
|
|
|
|
2019-03-19 17:37:43 +00:00
|
|
|
menu.popup({ window })
|
|
|
|
})
|
2019-02-01 18:56:46 +00:00
|
|
|
})
|
|
|
|
|
2019-03-06 21:22:45 +00:00
|
|
|
ipcMainUtils.handle('ELECTRON_INSPECTOR_SELECT_FILE', async function (event) {
|
|
|
|
assertChromeDevTools(event.sender, 'window.UI.createFileSelectorElement()')
|
|
|
|
|
|
|
|
const result = await dialog.showOpenDialog({})
|
|
|
|
if (result.canceled) return []
|
|
|
|
|
|
|
|
const path = result.filePaths[0]
|
|
|
|
const data = await readFile(path)
|
|
|
|
|
|
|
|
return [path, data]
|
2019-02-01 18:56:46 +00:00
|
|
|
})
|
|
|
|
|
2019-03-14 22:29:40 +00:00
|
|
|
ipcMainUtils.handle('ELECTRON_INSPECTOR_CONFIRM', async function (event, message = '', title = '') {
|
|
|
|
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
|
|
|
})
|