electron/lib/renderer/inspector.js

66 lines
2.1 KiB
JavaScript
Raw Normal View History

'use strict'
const ipcRenderer = require('@electron/internal/renderer/ipc-renderer-internal')
const ipcRendererUtils = require('@electron/internal/renderer/ipc-renderer-internal-utils')
2016-03-25 19:57:17 +00:00
window.onload = function () {
2016-01-14 18:35:29 +00:00
// Use menu API to show context menu.
2016-03-30 21:56:30 +00:00
window.InspectorFrontendHost.showContextMenuAtPoint = createMenu
2016-01-12 02:40:23 +00:00
// correct for Chromium returning undefined for filesystem
window.Persistence.FileSystemWorkspaceBinding.completeURL = completeURL
2016-01-14 18:35:29 +00:00
// Use dialog API to override file chooser dialog.
window.UI.createFileSelectorElement = createFileSelectorElement
2016-03-25 19:57:17 +00:00
}
2016-01-12 02:40:23 +00:00
// Extra / is needed as a result of MacOS requiring absolute paths
function completeURL (project, path) {
project = 'file:///'
return `${project}${path}`
}
2016-11-27 19:38:48 +00:00
window.confirm = function (message, title) {
return ipcRendererUtils.invokeSync('ELECTRON_INSPECTOR_CONFIRM', message, title)
2016-03-25 19:57:17 +00:00
}
2016-01-12 02:40:23 +00:00
ipcRenderer.on('ELECTRON_INSPECTOR_CONTEXT_MENU_CLICK', function (event, id) {
window.DevToolsAPI.contextMenuItemSelected(id)
})
2016-01-12 02:40:23 +00:00
ipcRenderer.on('ELECTRON_INSPECTOR_CONTEXT_MENU_CLOSE', function () {
window.DevToolsAPI.contextMenuCleared()
})
2016-01-12 02:40:23 +00:00
const useEditMenuItems = function (x, y, items) {
return items.length === 0 && document.elementsFromPoint(x, y).some(function (element) {
return element.nodeName === 'INPUT' || element.nodeName === 'TEXTAREA' || element.isContentEditable
})
}
const createMenu = function (x, y, items) {
const isEditMenu = useEditMenuItems(x, y, items)
ipcRendererUtils.invoke('ELECTRON_INSPECTOR_CONTEXT_MENU', items, isEditMenu)
}
2016-07-12 18:31:40 +00:00
const showFileChooserDialog = function (callback) {
ipcRendererUtils.invoke('ELECTRON_INSPECTOR_SELECT_FILE').then(([path, data]) => {
if (path && data) {
callback(dataToHtml5FileObject(path, data))
}
})
2016-03-25 19:57:17 +00:00
}
2016-01-12 02:40:23 +00:00
const dataToHtml5FileObject = function (path, data) {
const blob = new Blob([data])
2016-03-25 19:57:17 +00:00
blob.name = path
return blob
}
2016-01-12 02:40:23 +00:00
2016-07-12 18:31:40 +00:00
const createFileSelectorElement = function (callback) {
const fileSelectorElement = document.createElement('span')
2016-03-25 19:57:17 +00:00
fileSelectorElement.style.display = 'none'
fileSelectorElement.click = showFileChooserDialog.bind(this, callback)
return fileSelectorElement
}