- webContents·续4
- contents.printToPDF(options)
- contents.addWorkSpace(path)
- contents.removeWorkSpace(path)
- contents.setDevToolsWebContents(devToolsWebContents)
- contents.openDevTools([options])
- contents.closeDevTools()
- contents.isDevToolsOpened()
- contents.isDevToolsFocused()
- contents.toggleDevTools()
- contents.inspectElement(x, y)
- contents.inspectSharedWorker()
- contents.inspectSharedWorkerById(workerId)
- contents.getAllSharedWorkers()
- contents.inspectServiceWorker()
- contents.send(channel, ...args)
- contents.sendToFrame(frameId, channel, ...args)
webContents·续4
-
horizontal Number (optional) - The horizontal dpi.
-
vertical Number (optional) - The vertical dpi.
-
header String (optional) - String to be printed as page header.
-
footer String (optional) - String to be printed as page footer.
-
callback Function (可选)
-
success Boolean - Indicates success of the print call.
-
failureReason String - Error description called back if the print fails.
Prints window’s web page. When silent is set to true, Electron will pick the system’s default printer if deviceName is empty and the default settings for printing.
Use page-break-before: always; CSS style to force to print to a new page.
Example usage:
const options = { silent: true, deviceName: 'My-Printer' }win.webContents.print(options, (success, errorType) => { if (!success) console.log(errorType)})
contents.printToPDF(options)
-
options Object
-
marginsType Integer (optional) - Specifies the type of margins to use. Uses 0 for default margin, 1 for no margin, and 2 for minimum margin.
-
pageSize String | Size (optional) - Specify page size of the generated PDF. Can be A3, A4, A5, Legal, Letter, Tabloid or an Object containing height and width in microns.
-
printBackground Boolean (optional) - Whether to print CSS backgrounds.
-
printSelectionOnly Boolean (optional) - Whether to print selection only.
-
landscape Boolean (optional) - true for landscape, false for portrait.
Returns Promise<Buffer> - Resolves with the generated PDF data.
Prints window’s web page as PDF with Chromium’s preview printing custom settings.
The landscape will be ignored if @page CSS at-rule is used in the web page.
By default, an empty options will be regarded as:
{ marginsType: 0, printBackground: false, printSelectionOnly: false, landscape: false}
Use page-break-before: always; CSS style to force to print to a new page.
An example of webContents.printToPDF:
const { BrowserWindow } = require('electron')const fs = require('fs')let win = new BrowserWindow({ width: 800, height: 600 })win.loadURL('http://github.com')win.webContents.on('did-finish-load', () => { // Use default printing options win.webContents.printToPDF({}).then(data => { fs.writeFile('/tmp/print.pdf', data, (error) => { if (error) throw error console.log('Write PDF successfully.') }) }).catch(error => { console.log(error) })})
contents.addWorkSpace(path)
- path String
Adds the specified path to DevTools workspace. Must be used after DevTools creation:
const { BrowserWindow } = require('electron')let win = new BrowserWindow()win.webContents.on('devtools-opened', () => { win.webContents.addWorkSpace(__dirname)})
contents.removeWorkSpace(path)
- path String
Removes the specified path from DevTools workspace.
contents.setDevToolsWebContents(devToolsWebContents)
- devToolsWebContents WebContents
Uses the devToolsWebContents as the target WebContents to show devtools.
The devToolsWebContents must not have done any navigation, and it should not be used for other purposes after the call.
By default Electron manages the devtools by creating an internal WebContents with native view, which developers have very limited control of. With the setDevToolsWebContents method, developers can use any WebContents to show the devtools in it, including BrowserWindow, BrowserView and <webview> tag.
Note that closing the devtools does not destroy the devToolsWebContents, it is caller’s responsibility to destroy devToolsWebContents.
An example of showing devtools in a <webview> tag:
<html><head> <style type="text/css"> * { margin: 0; } #browser { height: 70%; } #devtools { height: 30%; } </style></head><body> <webview id="browser" src="https://github.com"></webview> <webview id="devtools"></webview> <script> const browserView = document.getElementById('browser') const devtoolsView = document.getElementById('devtools') browserView.addEventListener('dom-ready', () => { const browser = browserView.getWebContents() browser.setDevToolsWebContents(devtoolsView.getWebContents()) browser.openDevTools() }) </script></body></html>
An example of showing devtools in a BrowserWindow:
const { app, BrowserWindow } = require('electron')let win = nulllet devtools = nullapp.once('ready', () => { win = new BrowserWindow() devtools = new BrowserWindow() win.loadURL('https://github.com') win.webContents.setDevToolsWebContents(devtools.webContents) win.webContents.openDevTools({ mode: 'detach' })})
contents.openDevTools([options])
-
options Object (optional)
-
mode String - Opens the devtools with specified dock state, can be right, bottom, undocked, detach. Defaults to last used dock state. In undocked mode it’s possible to dock back. In detach mode it’s not.
-
activate Boolean (optional) - Whether to bring the opened devtools window to the foreground. The default is true.
Opens the devtools.
When contents is a <webview> tag, the mode would be detach by default, explicitly passing an empty mode can force using last used dock state.
contents.closeDevTools()
关闭开发者工具。
contents.isDevToolsOpened()
返回Boolean - 开发者工具是否处于开启状态。
contents.isDevToolsFocused()
返回Boolean - 开发者工具是否处于当前执行状态。
contents.toggleDevTools()
切换开发工具
contents.inspectElement(x, y)
-
x Integer
-
y Integer
开始检查位于(x, y) 的元素。
contents.inspectSharedWorker()
Opens the developer tools for the shared worker context.
contents.inspectSharedWorkerById(workerId)
- workerId String
Inspects the shared worker based on its ID.
contents.getAllSharedWorkers()
Returns SharedWorkerInfo[] - Information about all Shared Workers.
contents.inspectServiceWorker()
Opens the developer tools for the service worker context.
contents.send(channel, ...args)
-
channel String
-
...args any[]
Send an asynchronous message to the renderer process via channel, along with arguments. Arguments will be serialized with the Structured Clone Algorithm, just like [postMessage][], so prototype chains will not be included. Sending Functions, Promises, Symbols, WeakMaps, or WeakSets will throw an exception.
NOTE: Sending non-standard JavaScript types such as DOM objects or special Electron objects is deprecated, and will begin throwing an exception starting with Electron 9.
The renderer process can handle the message by listening to channel with the ipcRenderer module.
An example of sending messages from the main process to the renderer process:
// 在主进程中.const { app, BrowserWindow } = require('electron')let win = nullapp.on('ready', () => { win = new BrowserWindow({ width: 800, height: 600 }) win.loadURL(`file://${__dirname}/index.html`) win.webContents.on('did-finish-load', () => { win.webContents.send('ping', 'whoooooooh!') })})
<!-- index.html --><html><body> <script> require('electron').ipcRenderer.on('ping', (event, message) => { console.log(message) // Prints 'whoooooooh!' }) </script></body></html>
contents.sendToFrame(frameId, channel, ...args)
-
frameId Integer
-
channel String
-
...args any[]
Send an asynchronous message to a specific frame in a renderer process via channel, along with arguments. Arguments will be serialized with the Structured Clone Algorithm, just like [postMessage][], so prototype chains will not be included. Sending Functions, Promises, Symbols, WeakMaps, or WeakSets will throw an exception.
NOTE: Sending non-standard JavaScript types such as DOM objects or special Electron objects is deprecated, and will begin throwing an exception starting with Electron 9.
The renderer process can handle the message by listening to channel with the ipcRenderer module.
If you want to get the frameId of a given renderer context you should use the webFrame.routingId value. 如下:
// In a renderer processconsole.log('My frameId is:', require('electron').webFrame.routingId)
