Merge pull request #5824 from electron/cleanup-web-contents
Cleanup the web-contents.js code
This commit is contained in:
commit
e46ef5a15a
3 changed files with 72 additions and 67 deletions
|
@ -691,7 +691,8 @@ size.
|
|||
* `marginsType` Integer - Specifies the type of margins to use. Uses 0 for
|
||||
default margin, 1 for no margin, and 2 for minimum margin.
|
||||
* `pageSize` String - Specify page size of the generated PDF. Can be `A3`,
|
||||
`A4`, `A5`, `Legal`, `Letter`, `Tabloid` or an Object containing `height` & `width` in Microns.
|
||||
`A4`, `A5`, `Legal`, `Letter`, `Tabloid` or an Object containing `height`
|
||||
and `width` in microns.
|
||||
* `printBackground` Boolean - Whether to print CSS backgrounds.
|
||||
* `printSelectionOnly` Boolean - Whether to print selection only.
|
||||
* `landscape` Boolean - `true` for landscape, `false` for portrait.
|
||||
|
@ -714,6 +715,8 @@ By default, an empty `options` will be regarded as:
|
|||
}
|
||||
```
|
||||
|
||||
An example of `webContents.printToPDF`:
|
||||
|
||||
```javascript
|
||||
const {BrowserWindow} = require('electron');
|
||||
const fs = require('fs');
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
'use strict'
|
||||
|
||||
const {deprecate, Menu, session} = require('electron')
|
||||
const electron = require('electron')
|
||||
const {deprecate, Menu} = electron
|
||||
const {EventEmitter} = require('events')
|
||||
|
||||
const bindings = process.atomBinding('app')
|
||||
|
@ -49,7 +50,7 @@ app.allowNTLMCredentialsForAllDomains = function (allow) {
|
|||
if (!this.isReady()) {
|
||||
this.commandLine.appendSwitch('auth-server-whitelist', domains)
|
||||
} else {
|
||||
session.defaultSession.allowNTLMCredentialsForDomains(domains)
|
||||
electron.session.defaultSession.allowNTLMCredentialsForDomains(domains)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,18 +1,22 @@
|
|||
'use strict'
|
||||
|
||||
const {EventEmitter} = require('events')
|
||||
const {ipcMain, Menu, NavigationController} = require('electron')
|
||||
const {ipcMain, session, Menu, NavigationController} = require('electron')
|
||||
|
||||
// session is not used here, the purpose is to make sure session is initalized
|
||||
// before the webContents module.
|
||||
session
|
||||
|
||||
const binding = process.atomBinding('web_contents')
|
||||
const debuggerBinding = process.atomBinding('debugger')
|
||||
|
||||
let nextId = 0
|
||||
|
||||
let getNextId = function () {
|
||||
const getNextId = function () {
|
||||
return ++nextId
|
||||
}
|
||||
|
||||
let PDFPageSize = {
|
||||
// Stock page sizes
|
||||
const PDFPageSizes = {
|
||||
A5: {
|
||||
custom_display_name: 'A5',
|
||||
height_microns: 210000,
|
||||
|
@ -52,116 +56,8 @@ let PDFPageSize = {
|
|||
}
|
||||
}
|
||||
|
||||
// Following methods are mapped to webFrame.
|
||||
const webFrameMethods = [
|
||||
'insertText',
|
||||
'setZoomFactor',
|
||||
'setZoomLevel',
|
||||
'setZoomLevelLimits'
|
||||
]
|
||||
|
||||
let wrapWebContents = function (webContents) {
|
||||
// webContents is an EventEmitter.
|
||||
var controller, method, name, ref1
|
||||
Object.setPrototypeOf(webContents, EventEmitter.prototype)
|
||||
|
||||
// Every remote callback from renderer process would add a listenter to the
|
||||
// render-view-deleted event, so ignore the listenters warning.
|
||||
webContents.setMaxListeners(0)
|
||||
|
||||
// WebContents::send(channel, args..)
|
||||
// WebContents::sendToAll(channel, args..)
|
||||
const sendWrapper = (allFrames, channel, ...args) => {
|
||||
if (channel == null) {
|
||||
throw new Error('Missing required channel argument')
|
||||
}
|
||||
return webContents._send(allFrames, channel, args)
|
||||
}
|
||||
webContents.send = sendWrapper.bind(null, false)
|
||||
webContents.sendToAll = sendWrapper.bind(null, true)
|
||||
|
||||
// The navigation controller.
|
||||
controller = new NavigationController(webContents)
|
||||
ref1 = NavigationController.prototype
|
||||
for (name in ref1) {
|
||||
method = ref1[name]
|
||||
if (method instanceof Function) {
|
||||
(function (name, method) {
|
||||
webContents[name] = function () {
|
||||
return method.apply(controller, arguments)
|
||||
}
|
||||
})(name, method)
|
||||
}
|
||||
}
|
||||
|
||||
// Mapping webFrame methods.
|
||||
for (let method of webFrameMethods) {
|
||||
webContents[method] = function (...args) {
|
||||
this.send('ELECTRON_INTERNAL_RENDERER_WEB_FRAME_METHOD', method, args)
|
||||
}
|
||||
}
|
||||
|
||||
const asyncWebFrameMethods = function (requestId, method, callback, ...args) {
|
||||
this.send('ELECTRON_INTERNAL_RENDERER_ASYNC_WEB_FRAME_METHOD', requestId, method, args)
|
||||
ipcMain.once(`ELECTRON_INTERNAL_BROWSER_ASYNC_WEB_FRAME_RESPONSE_${requestId}`, function (event, result) {
|
||||
if (callback) callback(result)
|
||||
})
|
||||
}
|
||||
|
||||
// Make sure webContents.executeJavaScript would run the code only when the
|
||||
// webContents has been loaded.
|
||||
webContents.executeJavaScript = function (code, hasUserGesture, callback) {
|
||||
let requestId = getNextId()
|
||||
if (typeof hasUserGesture === 'function') {
|
||||
callback = hasUserGesture
|
||||
hasUserGesture = false
|
||||
}
|
||||
if (this.getURL() && !this.isLoadingMainFrame()) {
|
||||
asyncWebFrameMethods.call(this, requestId, 'executeJavaScript', callback, code, hasUserGesture)
|
||||
} else {
|
||||
this.once('did-finish-load', () => {
|
||||
asyncWebFrameMethods.call(this, requestId, 'executeJavaScript', callback, code, hasUserGesture)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Dispatch IPC messages to the ipc module.
|
||||
webContents.on('ipc-message', function (event, [channel, ...args]) {
|
||||
ipcMain.emit.apply(ipcMain, [channel, event].concat(args))
|
||||
})
|
||||
webContents.on('ipc-message-sync', function (event, [channel, ...args]) {
|
||||
Object.defineProperty(event, 'returnValue', {
|
||||
set: function (value) {
|
||||
return event.sendReply(JSON.stringify(value))
|
||||
},
|
||||
get: function () {
|
||||
return undefined
|
||||
}
|
||||
})
|
||||
return ipcMain.emit.apply(ipcMain, [channel, event].concat(args))
|
||||
})
|
||||
|
||||
// Handle context menu action request from pepper plugin.
|
||||
webContents.on('pepper-context-menu', function (event, params) {
|
||||
const menu = Menu.buildFromTemplate(params.menu)
|
||||
menu.popup(params.x, params.y)
|
||||
})
|
||||
|
||||
// The devtools requests the webContents to reload.
|
||||
webContents.on('devtools-reload-page', function () {
|
||||
webContents.reload()
|
||||
})
|
||||
|
||||
// Delays the page-title-updated event to next tick.
|
||||
webContents.on('-page-title-updated', function (...args) {
|
||||
setImmediate(() => {
|
||||
this.emit.apply(this, ['page-title-updated'].concat(args))
|
||||
})
|
||||
})
|
||||
|
||||
webContents.printToPDF = function (options, callback) {
|
||||
var printingSetting
|
||||
printingSetting = {
|
||||
// Default printing setting
|
||||
const defaultPrintingSetting = {
|
||||
pageRage: [],
|
||||
mediaSize: {},
|
||||
landscape: false,
|
||||
|
@ -184,6 +80,111 @@ let wrapWebContents = function (webContents) {
|
|||
shouldPrintBackgrounds: false,
|
||||
shouldPrintSelectionOnly: false
|
||||
}
|
||||
|
||||
// Following methods are mapped to webFrame.
|
||||
const webFrameMethods = [
|
||||
'insertText',
|
||||
'setZoomFactor',
|
||||
'setZoomLevel',
|
||||
'setZoomLevelLimits'
|
||||
]
|
||||
|
||||
// Add JavaScript wrappers for WebContents class.
|
||||
const wrapWebContents = function (webContents) {
|
||||
// webContents is an EventEmitter.
|
||||
Object.setPrototypeOf(webContents, EventEmitter.prototype)
|
||||
|
||||
// Every remote callback from renderer process would add a listenter to the
|
||||
// render-view-deleted event, so ignore the listenters warning.
|
||||
webContents.setMaxListeners(0)
|
||||
|
||||
// WebContents::send(channel, args..)
|
||||
// WebContents::sendToAll(channel, args..)
|
||||
const sendWrapper = (allFrames, channel, ...args) => {
|
||||
if (channel == null) {
|
||||
throw new Error('Missing required channel argument')
|
||||
}
|
||||
return webContents._send(allFrames, channel, args)
|
||||
}
|
||||
webContents.send = sendWrapper.bind(null, false)
|
||||
webContents.sendToAll = sendWrapper.bind(null, true)
|
||||
|
||||
// The navigation controller.
|
||||
const controller = new NavigationController(webContents)
|
||||
for (const name in NavigationController.prototype) {
|
||||
const method = NavigationController.prototype[name]
|
||||
if (method instanceof Function) {
|
||||
webContents[name] = function () {
|
||||
return method.apply(controller, arguments)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Mapping webFrame methods.
|
||||
for (const method of webFrameMethods) {
|
||||
webContents[method] = function (...args) {
|
||||
this.send('ELECTRON_INTERNAL_RENDERER_WEB_FRAME_METHOD', method, args)
|
||||
}
|
||||
}
|
||||
|
||||
const asyncWebFrameMethods = function (requestId, method, callback, ...args) {
|
||||
this.send('ELECTRON_INTERNAL_RENDERER_ASYNC_WEB_FRAME_METHOD', requestId, method, args)
|
||||
ipcMain.once(`ELECTRON_INTERNAL_BROWSER_ASYNC_WEB_FRAME_RESPONSE_${requestId}`, function (event, result) {
|
||||
if (callback) callback(result)
|
||||
})
|
||||
}
|
||||
|
||||
// Make sure webContents.executeJavaScript would run the code only when the
|
||||
// webContents has been loaded.
|
||||
webContents.executeJavaScript = function (code, hasUserGesture, callback) {
|
||||
const requestId = getNextId()
|
||||
if (typeof hasUserGesture === 'function') {
|
||||
callback = hasUserGesture
|
||||
hasUserGesture = false
|
||||
}
|
||||
if (this.getURL() && !this.isLoadingMainFrame()) {
|
||||
asyncWebFrameMethods.call(this, requestId, 'executeJavaScript', callback, code, hasUserGesture)
|
||||
} else {
|
||||
this.once('did-finish-load', () => {
|
||||
asyncWebFrameMethods.call(this, requestId, 'executeJavaScript', callback, code, hasUserGesture)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Dispatch IPC messages to the ipc module.
|
||||
webContents.on('ipc-message', function (event, [channel, ...args]) {
|
||||
ipcMain.emit(channel, event, ...args)
|
||||
})
|
||||
webContents.on('ipc-message-sync', function (event, [channel, ...args]) {
|
||||
Object.defineProperty(event, 'returnValue', {
|
||||
set: function (value) {
|
||||
return event.sendReply(JSON.stringify(value))
|
||||
},
|
||||
get: function () {}
|
||||
})
|
||||
ipcMain.emit(channel, event, ...args)
|
||||
})
|
||||
|
||||
// Handle context menu action request from pepper plugin.
|
||||
webContents.on('pepper-context-menu', function (event, params) {
|
||||
const menu = Menu.buildFromTemplate(params.menu)
|
||||
menu.popup(params.x, params.y)
|
||||
})
|
||||
|
||||
// The devtools requests the webContents to reload.
|
||||
webContents.on('devtools-reload-page', function () {
|
||||
webContents.reload()
|
||||
})
|
||||
|
||||
// Delays the page-title-updated event to next tick.
|
||||
webContents.on('-page-title-updated', function (...args) {
|
||||
setImmediate(() => {
|
||||
this.emit.apply(this, ['page-title-updated'].concat(args))
|
||||
})
|
||||
})
|
||||
|
||||
webContents.printToPDF = function (options, callback) {
|
||||
const printingSetting = Object.assign({}, defaultPrintingSetting)
|
||||
if (options.landscape) {
|
||||
printingSetting.landscape = options.landscape
|
||||
}
|
||||
|
@ -198,40 +199,40 @@ let wrapWebContents = function (webContents) {
|
|||
}
|
||||
|
||||
if (options.pageSize) {
|
||||
let height = 0
|
||||
let width = 0
|
||||
if (typeof options.pageSize === 'object') {
|
||||
const pageSize = options.pageSize
|
||||
if (typeof pageSize === 'object') {
|
||||
if (!pageSize.height || !pageSize.width) {
|
||||
return callback(new Error('Must define height and width for pageSize'))
|
||||
}
|
||||
// Dimensions in Microns
|
||||
// 1 meter = 10^6 microns
|
||||
height = options.pageSize.height ? options.pageSize.height : 0
|
||||
width = options.pageSize.width ? options.pageSize.width : 0
|
||||
}
|
||||
|
||||
if (height > 0 && width > 0) {
|
||||
printingSetting.mediaSize = {
|
||||
height_microns: height,
|
||||
name: 'CUSTOM',
|
||||
width_microns: width,
|
||||
custom_display_name: 'Custom'
|
||||
custom_display_name: 'Custom',
|
||||
height_microns: pageSize.height,
|
||||
width_microns: pageSize.width
|
||||
}
|
||||
} else if (PDFPageSize[options.pageSize]) {
|
||||
printingSetting.mediaSize = PDFPageSize[options.pageSize]
|
||||
} else if (PDFPageSizes[pageSize]) {
|
||||
printingSetting.mediaSize = PDFPageSizes[pageSize]
|
||||
} else {
|
||||
printingSetting.mediaSize = PDFPageSize['A4']
|
||||
return callback(new Error(`Does not support pageSize with ${pageSize}`))
|
||||
}
|
||||
} else {
|
||||
printingSetting.mediaSize = PDFPageSizes['A4']
|
||||
}
|
||||
|
||||
this._printToPDF(printingSetting, callback)
|
||||
}
|
||||
}
|
||||
|
||||
return this._printToPDF(printingSetting, callback)
|
||||
}
|
||||
}
|
||||
binding._setWrapWebContents(wrapWebContents)
|
||||
|
||||
// Wrapper for native class.
|
||||
let wrapDebugger = function (webContentsDebugger) {
|
||||
// Add JavaScript wrappers for Debugger class.
|
||||
const wrapDebugger = function (webContentsDebugger) {
|
||||
// debugger is an EventEmitter.
|
||||
Object.setPrototypeOf(webContentsDebugger, EventEmitter.prototype)
|
||||
}
|
||||
|
||||
binding._setWrapWebContents(wrapWebContents)
|
||||
debuggerBinding._setWrapDebugger(wrapDebugger)
|
||||
|
||||
module.exports = {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue