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
|
* `marginsType` Integer - Specifies the type of margins to use. Uses 0 for
|
||||||
default margin, 1 for no margin, and 2 for minimum margin.
|
default margin, 1 for no margin, and 2 for minimum margin.
|
||||||
* `pageSize` String - Specify page size of the generated PDF. Can be `A3`,
|
* `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.
|
* `printBackground` Boolean - Whether to print CSS backgrounds.
|
||||||
* `printSelectionOnly` Boolean - Whether to print selection only.
|
* `printSelectionOnly` Boolean - Whether to print selection only.
|
||||||
* `landscape` Boolean - `true` for landscape, `false` for portrait.
|
* `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
|
```javascript
|
||||||
const {BrowserWindow} = require('electron');
|
const {BrowserWindow} = require('electron');
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const {deprecate, Menu, session} = require('electron')
|
const electron = require('electron')
|
||||||
|
const {deprecate, Menu} = electron
|
||||||
const {EventEmitter} = require('events')
|
const {EventEmitter} = require('events')
|
||||||
|
|
||||||
const bindings = process.atomBinding('app')
|
const bindings = process.atomBinding('app')
|
||||||
|
@ -49,7 +50,7 @@ app.allowNTLMCredentialsForAllDomains = function (allow) {
|
||||||
if (!this.isReady()) {
|
if (!this.isReady()) {
|
||||||
this.commandLine.appendSwitch('auth-server-whitelist', domains)
|
this.commandLine.appendSwitch('auth-server-whitelist', domains)
|
||||||
} else {
|
} else {
|
||||||
session.defaultSession.allowNTLMCredentialsForDomains(domains)
|
electron.session.defaultSession.allowNTLMCredentialsForDomains(domains)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,18 +1,22 @@
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const {EventEmitter} = require('events')
|
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 binding = process.atomBinding('web_contents')
|
||||||
const debuggerBinding = process.atomBinding('debugger')
|
const debuggerBinding = process.atomBinding('debugger')
|
||||||
|
|
||||||
let nextId = 0
|
let nextId = 0
|
||||||
|
const getNextId = function () {
|
||||||
let getNextId = function () {
|
|
||||||
return ++nextId
|
return ++nextId
|
||||||
}
|
}
|
||||||
|
|
||||||
let PDFPageSize = {
|
// Stock page sizes
|
||||||
|
const PDFPageSizes = {
|
||||||
A5: {
|
A5: {
|
||||||
custom_display_name: 'A5',
|
custom_display_name: 'A5',
|
||||||
height_microns: 210000,
|
height_microns: 210000,
|
||||||
|
@ -52,6 +56,31 @@ let PDFPageSize = {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Default printing setting
|
||||||
|
const defaultPrintingSetting = {
|
||||||
|
pageRage: [],
|
||||||
|
mediaSize: {},
|
||||||
|
landscape: false,
|
||||||
|
color: 2,
|
||||||
|
headerFooterEnabled: false,
|
||||||
|
marginsType: 0,
|
||||||
|
isFirstRequest: false,
|
||||||
|
requestID: getNextId(),
|
||||||
|
previewModifiable: true,
|
||||||
|
printToPDF: true,
|
||||||
|
printWithCloudPrint: false,
|
||||||
|
printWithPrivet: false,
|
||||||
|
printWithExtension: false,
|
||||||
|
deviceName: 'Save as PDF',
|
||||||
|
generateDraftData: true,
|
||||||
|
fitToPageEnabled: false,
|
||||||
|
duplex: 0,
|
||||||
|
copies: 1,
|
||||||
|
collate: true,
|
||||||
|
shouldPrintBackgrounds: false,
|
||||||
|
shouldPrintSelectionOnly: false
|
||||||
|
}
|
||||||
|
|
||||||
// Following methods are mapped to webFrame.
|
// Following methods are mapped to webFrame.
|
||||||
const webFrameMethods = [
|
const webFrameMethods = [
|
||||||
'insertText',
|
'insertText',
|
||||||
|
@ -60,9 +89,9 @@ const webFrameMethods = [
|
||||||
'setZoomLevelLimits'
|
'setZoomLevelLimits'
|
||||||
]
|
]
|
||||||
|
|
||||||
let wrapWebContents = function (webContents) {
|
// Add JavaScript wrappers for WebContents class.
|
||||||
|
const wrapWebContents = function (webContents) {
|
||||||
// webContents is an EventEmitter.
|
// webContents is an EventEmitter.
|
||||||
var controller, method, name, ref1
|
|
||||||
Object.setPrototypeOf(webContents, EventEmitter.prototype)
|
Object.setPrototypeOf(webContents, EventEmitter.prototype)
|
||||||
|
|
||||||
// Every remote callback from renderer process would add a listenter to the
|
// Every remote callback from renderer process would add a listenter to the
|
||||||
|
@ -81,21 +110,18 @@ let wrapWebContents = function (webContents) {
|
||||||
webContents.sendToAll = sendWrapper.bind(null, true)
|
webContents.sendToAll = sendWrapper.bind(null, true)
|
||||||
|
|
||||||
// The navigation controller.
|
// The navigation controller.
|
||||||
controller = new NavigationController(webContents)
|
const controller = new NavigationController(webContents)
|
||||||
ref1 = NavigationController.prototype
|
for (const name in NavigationController.prototype) {
|
||||||
for (name in ref1) {
|
const method = NavigationController.prototype[name]
|
||||||
method = ref1[name]
|
|
||||||
if (method instanceof Function) {
|
if (method instanceof Function) {
|
||||||
(function (name, method) {
|
webContents[name] = function () {
|
||||||
webContents[name] = function () {
|
return method.apply(controller, arguments)
|
||||||
return method.apply(controller, arguments)
|
}
|
||||||
}
|
|
||||||
})(name, method)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mapping webFrame methods.
|
// Mapping webFrame methods.
|
||||||
for (let method of webFrameMethods) {
|
for (const method of webFrameMethods) {
|
||||||
webContents[method] = function (...args) {
|
webContents[method] = function (...args) {
|
||||||
this.send('ELECTRON_INTERNAL_RENDERER_WEB_FRAME_METHOD', method, args)
|
this.send('ELECTRON_INTERNAL_RENDERER_WEB_FRAME_METHOD', method, args)
|
||||||
}
|
}
|
||||||
|
@ -111,7 +137,7 @@ let wrapWebContents = function (webContents) {
|
||||||
// Make sure webContents.executeJavaScript would run the code only when the
|
// Make sure webContents.executeJavaScript would run the code only when the
|
||||||
// webContents has been loaded.
|
// webContents has been loaded.
|
||||||
webContents.executeJavaScript = function (code, hasUserGesture, callback) {
|
webContents.executeJavaScript = function (code, hasUserGesture, callback) {
|
||||||
let requestId = getNextId()
|
const requestId = getNextId()
|
||||||
if (typeof hasUserGesture === 'function') {
|
if (typeof hasUserGesture === 'function') {
|
||||||
callback = hasUserGesture
|
callback = hasUserGesture
|
||||||
hasUserGesture = false
|
hasUserGesture = false
|
||||||
|
@ -127,18 +153,16 @@ let wrapWebContents = function (webContents) {
|
||||||
|
|
||||||
// Dispatch IPC messages to the ipc module.
|
// Dispatch IPC messages to the ipc module.
|
||||||
webContents.on('ipc-message', function (event, [channel, ...args]) {
|
webContents.on('ipc-message', function (event, [channel, ...args]) {
|
||||||
ipcMain.emit.apply(ipcMain, [channel, event].concat(args))
|
ipcMain.emit(channel, event, ...args)
|
||||||
})
|
})
|
||||||
webContents.on('ipc-message-sync', function (event, [channel, ...args]) {
|
webContents.on('ipc-message-sync', function (event, [channel, ...args]) {
|
||||||
Object.defineProperty(event, 'returnValue', {
|
Object.defineProperty(event, 'returnValue', {
|
||||||
set: function (value) {
|
set: function (value) {
|
||||||
return event.sendReply(JSON.stringify(value))
|
return event.sendReply(JSON.stringify(value))
|
||||||
},
|
},
|
||||||
get: function () {
|
get: function () {}
|
||||||
return undefined
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
return ipcMain.emit.apply(ipcMain, [channel, event].concat(args))
|
ipcMain.emit(channel, event, ...args)
|
||||||
})
|
})
|
||||||
|
|
||||||
// Handle context menu action request from pepper plugin.
|
// Handle context menu action request from pepper plugin.
|
||||||
|
@ -160,30 +184,7 @@ let wrapWebContents = function (webContents) {
|
||||||
})
|
})
|
||||||
|
|
||||||
webContents.printToPDF = function (options, callback) {
|
webContents.printToPDF = function (options, callback) {
|
||||||
var printingSetting
|
const printingSetting = Object.assign({}, defaultPrintingSetting)
|
||||||
printingSetting = {
|
|
||||||
pageRage: [],
|
|
||||||
mediaSize: {},
|
|
||||||
landscape: false,
|
|
||||||
color: 2,
|
|
||||||
headerFooterEnabled: false,
|
|
||||||
marginsType: 0,
|
|
||||||
isFirstRequest: false,
|
|
||||||
requestID: getNextId(),
|
|
||||||
previewModifiable: true,
|
|
||||||
printToPDF: true,
|
|
||||||
printWithCloudPrint: false,
|
|
||||||
printWithPrivet: false,
|
|
||||||
printWithExtension: false,
|
|
||||||
deviceName: 'Save as PDF',
|
|
||||||
generateDraftData: true,
|
|
||||||
fitToPageEnabled: false,
|
|
||||||
duplex: 0,
|
|
||||||
copies: 1,
|
|
||||||
collate: true,
|
|
||||||
shouldPrintBackgrounds: false,
|
|
||||||
shouldPrintSelectionOnly: false
|
|
||||||
}
|
|
||||||
if (options.landscape) {
|
if (options.landscape) {
|
||||||
printingSetting.landscape = options.landscape
|
printingSetting.landscape = options.landscape
|
||||||
}
|
}
|
||||||
|
@ -198,40 +199,40 @@ let wrapWebContents = function (webContents) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options.pageSize) {
|
if (options.pageSize) {
|
||||||
let height = 0
|
const pageSize = options.pageSize
|
||||||
let width = 0
|
if (typeof pageSize === 'object') {
|
||||||
if (typeof options.pageSize === 'object') {
|
if (!pageSize.height || !pageSize.width) {
|
||||||
|
return callback(new Error('Must define height and width for pageSize'))
|
||||||
|
}
|
||||||
// Dimensions in Microns
|
// Dimensions in Microns
|
||||||
// 1 meter = 10^6 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 = {
|
printingSetting.mediaSize = {
|
||||||
height_microns: height,
|
|
||||||
name: 'CUSTOM',
|
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]) {
|
} else if (PDFPageSizes[pageSize]) {
|
||||||
printingSetting.mediaSize = PDFPageSize[options.pageSize]
|
printingSetting.mediaSize = PDFPageSizes[pageSize]
|
||||||
} else {
|
} else {
|
||||||
printingSetting.mediaSize = PDFPageSize['A4']
|
return callback(new Error(`Does not support pageSize with ${pageSize}`))
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
printingSetting.mediaSize = PDFPageSizes['A4']
|
||||||
}
|
}
|
||||||
|
|
||||||
return this._printToPDF(printingSetting, callback)
|
this._printToPDF(printingSetting, callback)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wrapper for native class.
|
binding._setWrapWebContents(wrapWebContents)
|
||||||
let wrapDebugger = function (webContentsDebugger) {
|
|
||||||
|
// Add JavaScript wrappers for Debugger class.
|
||||||
|
const wrapDebugger = function (webContentsDebugger) {
|
||||||
// debugger is an EventEmitter.
|
// debugger is an EventEmitter.
|
||||||
Object.setPrototypeOf(webContentsDebugger, EventEmitter.prototype)
|
Object.setPrototypeOf(webContentsDebugger, EventEmitter.prototype)
|
||||||
}
|
}
|
||||||
|
|
||||||
binding._setWrapWebContents(wrapWebContents)
|
|
||||||
debuggerBinding._setWrapDebugger(wrapDebugger)
|
debuggerBinding._setWrapDebugger(wrapDebugger)
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue