add the 'webpreferences' attribute to webviews

This commit is contained in:
Paul Frazee 2016-10-14 17:16:39 -05:00
Родитель 3dd8377218
Коммит 194b14100e
4 изменённых файлов: 48 добавлений и 0 удалений

Просмотреть файл

@ -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
<webview src="https://github.com" webpreferences="allowDisplayingInsecureContent, javascript=no"></webview>
```
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

Просмотреть файл

@ -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
}

Просмотреть файл

@ -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) => {

Просмотреть файл

@ -18,6 +18,7 @@ module.exports = {
ATTRIBUTE_BLINKFEATURES: 'blinkfeatures',
ATTRIBUTE_DISABLEBLINKFEATURES: 'disableblinkfeatures',
ATTRIBUTE_GUESTINSTANCE: 'guestinstance',
ATTRIBUTE_WEBPREFERENCES: 'webpreferences',
// Internal attribute.
ATTRIBUTE_INTERNALINSTANCEID: 'internalinstanceid',