BrowserView
A BrowserView
can be used to embed additional web content into a BrowserWindow. It is like a child window, except that it is positioned relative to its owning window. It is meant to be an alternative to the webview
tag.
Class: BrowserView
Create and control views.
Process: Main
This module cannot be used until the ready
event of the app
module is emitted.
Example
// In the main process.
const { app, BrowserView, BrowserWindow } = require('electron')
app.whenReady().then(() => {
const win = new BrowserWindow({ width: 800, height: 600 })
const view = new BrowserView()
win.setBrowserView(view)
view.setBounds({ x: 0, y: 0, width: 300, height: 300 })
view.webContents.loadURL('https://electronjs.org')
})
new BrowserView([options])
Experimental
options
Object (optional)webPreferences
WebPreferences (optional) - Settings of web page’s features.devTools
boolean (optional) - Whether to enable DevTools. If it is set tofalse
, can not useBrowserWindow.webContents.openDevTools()
to open DevTools. Default istrue
.nodeIntegration
boolean (optional) - Whether node integration is enabled. Default isfalse
.nodeIntegrationInWorker
boolean (optional) - Whether node integration is enabled in web workers. Default isfalse
. More about this can be found in Multithreading.nodeIntegrationInSubFrames
boolean (optional) - Experimental option for enabling Node.js support in sub-frames such as iframes and child windows. All your preloads will load for every iframe, you can useprocess.isMainFrame
to determine if you are in the main frame or not.preload
string (optional) - Specifies a script that will be loaded before other scripts run in the page. This script will always have access to node APIs no matter whether node integration is turned on or off. The value should be the absolute file path to the script. When node integration is turned off, the preload script can reintroduce Node global symbols back to the global scope. See example here.sandbox
boolean (optional) - If set, this will sandbox the renderer associated with the window, making it compatible with the Chromium OS-level sandbox and disabling the Node.js engine. This is not the same as thenodeIntegration
option and the APIs available to the preload script are more limited. Read more about the option here.session
Session (optional) - Sets the session used by the page. Instead of passing the Session object directly, you can also choose to use thepartition
option instead, which accepts a partition string. When bothsession
andpartition
are provided,session
will be preferred. Default is the default session.partition
string (optional) - Sets the session used by the page according to the session’s partition string. Ifpartition
starts withpersist:
, the page will use a persistent session available to all pages in the app with the samepartition
. If there is nopersist:
prefix, the page will use an in-memory session. By assigning the samepartition
, multiple pages can share the same session. Default is the default session.zoomFactor
number (optional) - The default zoom factor of the page,3.0
represents300%
. Default is1.0
.javascript
boolean (optional) - Enables JavaScript support. Default istrue
.webSecurity
boolean (optional) - Whenfalse
, it will disable the same-origin policy (usually using testing websites by people), and setallowRunningInsecureContent
totrue
if this options has not been set by user. Default istrue
.allowRunningInsecureContent
boolean (optional) - Allow an https page to run JavaScript, CSS or plugins from http URLs. Default isfalse
.images
boolean (optional) - Enables image support. Default istrue
.imageAnimationPolicy
string (optional) - Specifies how to run image animations (E.g. GIFs). Can beanimate
,animateOnce
ornoAnimation
. Default isanimate
.textAreasAreResizable
boolean (optional) - Make TextArea elements resizable. Default istrue
.webgl
boolean (optional) - Enables WebGL support. Default istrue
.plugins
boolean (optional) - Whether plugins should be enabled. Default isfalse
.experimentalFeatures
boolean (optional) - Enables Chromium’s experimental features. Default isfalse
.scrollBounce
boolean (optional) macOS - Enables scroll bounce (rubber banding) effect on macOS. Default isfalse
.enableBlinkFeatures
string (optional) - A list of feature strings separated by,
, likeCSSVariables,KeyboardEventKey
to enable. The full list of supported feature strings can be found in the RuntimeEnabledFeatures.json5 file.disableBlinkFeatures
string (optional) - A list of feature strings separated by,
, likeCSSVariables,KeyboardEventKey
to disable. The full list of supported feature strings can be found in the RuntimeEnabledFeatures.json5 file.defaultFontFamily
Object (optional) - Sets the default font for the font-family.standard
string (optional) - Defaults toTimes New Roman
.serif
string (optional) - Defaults toTimes New Roman
.sansSerif
string (optional) - Defaults toArial
.monospace
string (optional) - Defaults toCourier New
.cursive
string (optional) - Defaults toScript
.fantasy
string (optional) - Defaults toImpact
.math
string (optional) - Defaults toLatin Modern Math
.
defaultFontSize
Integer (optional) - Defaults to16
.defaultMonospaceFontSize
Integer (optional) - Defaults to13
.minimumFontSize
Integer (optional) - Defaults to0
.defaultEncoding
string (optional) - Defaults toISO-8859-1
.backgroundThrottling
boolean (optional) - Whether to throttle animations and timers when the page becomes background. This also affects the Page Visibility API. Defaults totrue
.offscreen
boolean (optional) - Whether to enable offscreen rendering for the browser window. Defaults tofalse
. See the offscreen rendering tutorial for more details.contextIsolation
boolean (optional) - Whether to run Electron APIs and the specifiedpreload
script in a separate JavaScript context. Defaults totrue
. The context that thepreload
script runs in will only have access to its own dedicateddocument
andwindow
globals, as well as its own set of JavaScript builtins (Array
,Object
,JSON
, etc.), which are all invisible to the loaded content. The Electron API will only be available in thepreload
script and not the loaded page. This option should be used when loading potentially untrusted remote content to ensure the loaded content cannot tamper with thepreload
script and any Electron APIs being used. This option uses the same technique used by Chrome Content Scripts. You can access this context in the dev tools by selecting the ‘Electron Isolated Context’ entry in the combo box at the top of the Console tab.webviewTag
boolean (optional) - Whether to enable thetag . Defaults tofalse
. Note: Thepreload
script configured for the<webview>
will have node integration enabled when it is executed so you should ensure remote/untrusted content is not able to create a<webview>
tag with a possibly maliciouspreload
script. You can use thewill-attach-webview
event on webContents to strip away thepreload
script and to validate or alter the<webview>
‘s initial settings.additionalArguments
string[] (optional) - A list of strings that will be appended toprocess.argv
in the renderer process of this app. Useful for passing small bits of data down to renderer process preload scripts.safeDialogs
boolean (optional) - Whether to enable browser style consecutive dialog protection. Default isfalse
.safeDialogsMessage
string (optional) - The message to display when consecutive dialog protection is triggered. If not defined the default message would be used, note that currently the default message is in English and not localized.disableDialogs
boolean (optional) - Whether to disable dialogs completely. OverridessafeDialogs
. Default isfalse
.navigateOnDragDrop
boolean (optional) - Whether dragging and dropping a file or link onto the page causes a navigation. Default isfalse
.autoplayPolicy
string (optional) - Autoplay policy to apply to content in the window, can beno-user-gesture-required
,user-gesture-required
,document-user-activation-required
. Defaults tono-user-gesture-required
.disableHtmlFullscreenWindowResize
boolean (optional) - Whether to prevent the window from resizing when entering HTML Fullscreen. Default isfalse
.accessibleTitle
string (optional) - An alternative title string provided only to accessibility tools such as screen readers. This string is not directly visible to users.spellcheck
boolean (optional) - Whether to enable the builtin spellchecker. Default istrue
.enableWebSQL
boolean (optional) - Whether to enable the WebSQL api. Default istrue
.v8CacheOptions
string (optional) - Enforces the v8 code caching policy used by blink. Accepted values arenone
- Disables code cachingcode
- Heuristic based code cachingbypassHeatCheck
- Bypass code caching heuristics but with lazy compilationbypassHeatCheckAndEagerCompile
- Same as above except compilation is eager. Default policy iscode
.
enablePreferredSizeMode
boolean (optional) - Whether to enable preferred size mode. The preferred size is the minimum size needed to contain the layout of the document—without requiring scrolling. Enabling this will cause thepreferred-size-changed
event to be emitted on theWebContents
when the preferred size changes. Default isfalse
.
Instance Properties
Objects created with new BrowserView
have the following properties:
view.webContents
Experimental
A WebContents object owned by this view.
Instance Methods
Objects created with new BrowserView
have the following instance methods:
view.setAutoResize(options)
Experimental
options
Objectwidth
boolean (optional) - Iftrue
, the view’s width will grow and shrink together with the window.false
by default.height
boolean (optional) - Iftrue
, the view’s height will grow and shrink together with the window.false
by default.horizontal
boolean (optional) - Iftrue
, the view’s x position and width will grow and shrink proportionally with the window.false
by default.vertical
boolean (optional) - Iftrue
, the view’s y position and height will grow and shrink proportionally with the window.false
by default.
view.setBounds(bounds)
Experimental
bounds
Rectangle
Resizes and moves the view to the supplied bounds relative to the window.
view.getBounds()
Experimental
Returns Rectangle
The bounds
of this BrowserView instance as Object
.
view.setBackgroundColor(color)
Experimental
color
string - Color in Hex, RGB, ARGB, HSL, HSLA or named CSS color format. The alpha channel is optional for the hex type.
Examples of valid color
values:
- Hex
- RGB
- rgb(([\d]+),\s([\d]+),\s([\d]+))
- e.g. rgb(255, 255, 255)
- rgb(([\d]+),\s([\d]+),\s([\d]+))
- RGBA
- rgba(([\d]+),\s([\d]+),\s([\d]+),\s*([\d.]+))
- e.g. rgba(255, 255, 255, 1.0)
- rgba(([\d]+),\s([\d]+),\s([\d]+),\s*([\d.]+))
- HSL
- hsl((-?[\d.]+),\s([\d.]+)%,\s([\d.]+)%)
- e.g. hsl(200, 20%, 50%)
- hsl((-?[\d.]+),\s([\d.]+)%,\s([\d.]+)%)
- HSLA
- hsla((-?[\d.]+),\s([\d.]+)%,\s([\d.]+)%,\s*([\d.]+))
- e.g. hsla(200, 20%, 50%, 0.5)
- hsla((-?[\d.]+),\s([\d.]+)%,\s([\d.]+)%,\s*([\d.]+))
- Color name
- Options are listed in SkParseColor.cpp
- Similar to CSS Color Module Level 3 keywords, but case-sensitive.
- e.g.
blueviolet
orred
- e.g.
Note: Hex format with alpha takes AARRGGBB
or ARGB
, not RRGGBBA
or RGA
.