зеркало из https://github.com/electron/electron.git
Merge pull request #5824 from electron/cleanup-web-contents
Cleanup the web-contents.js code
This commit is contained in:
Коммит
e46ef5a15a
|
@ -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,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.
|
||||
const webFrameMethods = [
|
||||
'insertText',
|
||||
|
@ -60,9 +89,9 @@ const webFrameMethods = [
|
|||
'setZoomLevelLimits'
|
||||
]
|
||||
|
||||
let wrapWebContents = function (webContents) {
|
||||
// Add JavaScript wrappers for WebContents class.
|
||||
const 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
|
||||
|
@ -81,21 +110,18 @@ let wrapWebContents = function (webContents) {
|
|||
webContents.sendToAll = sendWrapper.bind(null, true)
|
||||
|
||||
// The navigation controller.
|
||||
controller = new NavigationController(webContents)
|
||||
ref1 = NavigationController.prototype
|
||||
for (name in ref1) {
|
||||
method = ref1[name]
|
||||
const controller = new NavigationController(webContents)
|
||||
for (const name in NavigationController.prototype) {
|
||||
const method = NavigationController.prototype[name]
|
||||
if (method instanceof Function) {
|
||||
(function (name, method) {
|
||||
webContents[name] = function () {
|
||||
return method.apply(controller, arguments)
|
||||
}
|
||||
})(name, method)
|
||||
webContents[name] = function () {
|
||||
return method.apply(controller, arguments)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Mapping webFrame methods.
|
||||
for (let method of webFrameMethods) {
|
||||
for (const method of webFrameMethods) {
|
||||
webContents[method] = function (...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
|
||||
// webContents has been loaded.
|
||||
webContents.executeJavaScript = function (code, hasUserGesture, callback) {
|
||||
let requestId = getNextId()
|
||||
const requestId = getNextId()
|
||||
if (typeof hasUserGesture === 'function') {
|
||||
callback = hasUserGesture
|
||||
hasUserGesture = false
|
||||
|
@ -127,18 +153,16 @@ let wrapWebContents = function (webContents) {
|
|||
|
||||
// Dispatch IPC messages to the ipc module.
|
||||
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]) {
|
||||
Object.defineProperty(event, 'returnValue', {
|
||||
set: function (value) {
|
||||
return event.sendReply(JSON.stringify(value))
|
||||
},
|
||||
get: function () {
|
||||
return undefined
|
||||
}
|
||||
get: function () {}
|
||||
})
|
||||
return ipcMain.emit.apply(ipcMain, [channel, event].concat(args))
|
||||
ipcMain.emit(channel, event, ...args)
|
||||
})
|
||||
|
||||
// Handle context menu action request from pepper plugin.
|
||||
|
@ -160,30 +184,7 @@ let wrapWebContents = function (webContents) {
|
|||
})
|
||||
|
||||
webContents.printToPDF = function (options, callback) {
|
||||
var printingSetting
|
||||
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
|
||||
}
|
||||
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']
|
||||
}
|
||||
|
||||
return this._printToPDF(printingSetting, callback)
|
||||
this._printToPDF(printingSetting, callback)
|
||||
}
|
||||
}
|
||||
|
||||
// Wrapper for native class.
|
||||
let wrapDebugger = function (webContentsDebugger) {
|
||||
binding._setWrapWebContents(wrapWebContents)
|
||||
|
||||
// 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 = {
|
||||
|
|
Загрузка…
Ссылка в новой задаче