electron/lib/renderer/inspector.js

66 lines
2.1 KiB
JavaScript
Raw Normal View History

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