electron/lib/renderer/inspector.js

97 lines
2.7 KiB
JavaScript
Raw Normal View History

2016-03-25 19:57:17 +00:00
window.onload = function () {
// Make sure |window.chrome| is defined for devtools extensions.
hijackSetInjectedScript(InspectorFrontendHost)
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
2016-01-14 18:35:29 +00:00
// Use dialog API to override file chooser dialog.
2016-05-19 22:28:08 +00:00
window.WebInspector.createFileSelectorElement = createFileSelectorElement
2016-03-25 19:57:17 +00:00
}
2016-01-12 02:40:23 +00:00
const hijackSetInjectedScript = function (InspectorFrontendHost) {
const {setInjectedScriptForOrigin} = InspectorFrontendHost
InspectorFrontendHost.setInjectedScriptForOrigin = function (origin, source) {
const wrapped = `(function (...args) {
window.chrome = {}
const original = ${source}
original(...args)
})`
setInjectedScriptForOrigin(origin, wrapped)
}
}
2016-03-25 19:57:17 +00:00
var convertToMenuTemplate = function (items) {
var fn, i, item, len, template
template = []
fn = function (item) {
var transformed
2016-01-12 02:40:23 +00:00
transformed = item.type === 'subMenu' ? {
type: 'submenu',
label: item.label,
enabled: item.enabled,
submenu: convertToMenuTemplate(item.subItems)
} : 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
2016-03-25 19:57:17 +00:00
}
2016-01-12 02:40:23 +00:00
if (item.id != null) {
2016-03-25 19:57:17 +00:00
transformed.click = function () {
2016-03-30 21:56:30 +00:00
window.DevToolsAPI.contextMenuItemSelected(item.id)
return window.DevToolsAPI.contextMenuCleared()
2016-03-25 19:57:17 +00:00
}
2016-01-12 02:40:23 +00:00
}
2016-03-25 19:57:17 +00:00
return template.push(transformed)
}
2016-01-12 02:40:23 +00:00
for (i = 0, len = items.length; i < len; i++) {
2016-03-25 19:57:17 +00:00
item = items[i]
fn(item)
2016-01-12 02:40:23 +00:00
}
2016-03-25 19:57:17 +00:00
return template
}
2016-01-12 02:40:23 +00:00
2016-03-25 19:57:17 +00:00
var createMenu = function (x, y, items) {
const remote = require('electron').remote
const Menu = remote.Menu
const menu = Menu.buildFromTemplate(convertToMenuTemplate(items))
2016-01-12 02:40:23 +00:00
2016-01-14 18:35:29 +00:00
// The menu is expected to show asynchronously.
2016-03-25 19:57:17 +00:00
return setTimeout(function () {
return menu.popup(remote.getCurrentWindow())
})
}
2016-01-12 02:40:23 +00:00
2016-03-25 19:57:17 +00:00
var showFileChooserDialog = function (callback) {
var dialog, files, remote
remote = require('electron').remote
dialog = remote.dialog
files = dialog.showOpenDialog({})
2016-01-12 02:40:23 +00:00
if (files != null) {
2016-03-25 19:57:17 +00:00
return callback(pathToHtml5FileObject(files[0]))
2016-01-12 02:40:23 +00:00
}
2016-03-25 19:57:17 +00:00
}
2016-01-12 02:40:23 +00:00
2016-03-25 19:57:17 +00:00
var pathToHtml5FileObject = function (path) {
var blob, fs
fs = require('fs')
blob = new Blob([fs.readFileSync(path)])
blob.name = path
return blob
}
2016-01-12 02:40:23 +00:00
2016-03-25 19:57:17 +00:00
var createFileSelectorElement = function (callback) {
var fileSelectorElement
fileSelectorElement = document.createElement('span')
fileSelectorElement.style.display = 'none'
fileSelectorElement.click = showFileChooserDialog.bind(this, callback)
return fileSelectorElement
}