diff --git a/docs/api/web-view-tag.md b/docs/api/web-view-tag.md index 512300c9f67..c5d7c7b958c 100644 --- a/docs/api/web-view-tag.md +++ b/docs/api/web-view-tag.md @@ -194,6 +194,20 @@ value will fail with a DOM exception. If "on", the guest page will be allowed to open new windows. +### `webpreferences` + +```html + +``` + +A list of strings which specifies the web preferences to be set on the webview, separated by `,`. +The full list of supported preference strings can be found in [BrowserWindow](browser-window.md#new-browserwindowoptions) + +The string follows the same format as the features string in `window.open`. +A name by itself is given a `true` boolean value. +A preference can be set to another value by including an `=`, followed by the value. +Special values `yes` and `1` are interpreted as true, while `no` and `0` are interpreted as false. + ### `blinkfeatures` ```html diff --git a/lib/browser/guest-view-manager.js b/lib/browser/guest-view-manager.js index df2589a9bcf..b62e7cf2bf4 100644 --- a/lib/browser/guest-view-manager.js +++ b/lib/browser/guest-view-manager.js @@ -184,6 +184,31 @@ const attachGuest = function (embedder, elementInstanceId, guestInstanceId, para disableBlinkFeatures: params.disableblinkfeatures } + // parse the 'webpreferences' attribute string, if set + // this uses the same parsing rules as window.open uses for its features + if (typeof params.webpreferences === 'string') { + // split the attribute's value by ',' + let webpreferencesTokens = params.webpreferences.split(/,\s*/) + for (i = 0, len = webpreferencesTokens.length; i < len; i++) { + // expected form is either a name by itself (true boolean flag) + // or a key/value, in the form of 'name=value' + // split the tokens by '=' + let pref = webpreferencesTokens[i] + let prefTokens = pref.split(/\s*=/) + name = prefTokens[0] + value = prefTokens[1] + if (!name) continue + + // interpret the value as a boolean, if possible + value = (value === 'yes' || value === '1') ? true : (value === 'no' || value === '0') ? false : value + if (value === undefined) { + // no value was specified, default it to true + value = true + } + webPreferences[name] = value + } + } + if (params.preload) { webPreferences.preloadURL = params.preload } diff --git a/lib/renderer/web-view/web-view-attributes.js b/lib/renderer/web-view/web-view-attributes.js index 3bbc8331112..7c5cf81e4b6 100644 --- a/lib/renderer/web-view/web-view-attributes.js +++ b/lib/renderer/web-view/web-view-attributes.js @@ -312,6 +312,13 @@ class DisableBlinkFeaturesAttribute extends WebViewAttribute { } } +// Attribute that specifies the web preferences to be enabled. +class WebPreferencesAttribute extends WebViewAttribute { + constructor (webViewImpl) { + super(webViewConstants.ATTRIBUTE_WEBPREFERENCES, webViewImpl) + } +} + // Sets up all of the webview attributes. WebViewImpl.prototype.setupWebViewAttributes = function () { this.attributes = {} @@ -328,6 +335,7 @@ WebViewImpl.prototype.setupWebViewAttributes = function () { this.attributes[webViewConstants.ATTRIBUTE_BLINKFEATURES] = new BlinkFeaturesAttribute(this) this.attributes[webViewConstants.ATTRIBUTE_DISABLEBLINKFEATURES] = new DisableBlinkFeaturesAttribute(this) this.attributes[webViewConstants.ATTRIBUTE_GUESTINSTANCE] = new GuestInstanceAttribute(this) + this.attributes[webviewConstants.ATTRIBUTE_WEBPREFERENCES] = new WebPreferencesAttribute(this) const autosizeAttributes = [webViewConstants.ATTRIBUTE_MAXHEIGHT, webViewConstants.ATTRIBUTE_MAXWIDTH, webViewConstants.ATTRIBUTE_MINHEIGHT, webViewConstants.ATTRIBUTE_MINWIDTH] autosizeAttributes.forEach((attribute) => { diff --git a/lib/renderer/web-view/web-view-constants.js b/lib/renderer/web-view/web-view-constants.js index 5aed6d5ea9e..80fcaf91f24 100644 --- a/lib/renderer/web-view/web-view-constants.js +++ b/lib/renderer/web-view/web-view-constants.js @@ -18,6 +18,7 @@ module.exports = { ATTRIBUTE_BLINKFEATURES: 'blinkfeatures', ATTRIBUTE_DISABLEBLINKFEATURES: 'disableblinkfeatures', ATTRIBUTE_GUESTINSTANCE: 'guestinstance', + ATTRIBUTE_WEBPREFERENCES: 'webpreferences', // Internal attribute. ATTRIBUTE_INTERNALINSTANCEID: 'internalinstanceid',