Merge pull request #2868 from atom/window-open-full

Add allowpopups attribute for webview
This commit is contained in:
Cheng Zhao 2015-09-22 21:52:07 +08:00
commit a6b7dd22e8
8 changed files with 46 additions and 3 deletions

View file

@ -84,6 +84,8 @@ createGuest = (embedder, params) ->
if params.allowtransparency? if params.allowtransparency?
@setAllowTransparency params.allowtransparency @setAllowTransparency params.allowtransparency
guest.allowPopups = params.allowpopups
# Dispatch events to embedder. # Dispatch events to embedder.
for event in supportedWebViewEvents for event in supportedWebViewEvents
do (event) -> do (event) ->

View file

@ -41,7 +41,7 @@ createGuest = (embedder, url, frameName, options) ->
ipc.on 'ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_OPEN', (event, args...) -> ipc.on 'ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_OPEN', (event, args...) ->
[url, frameName, options] = args [url, frameName, options] = args
event.sender.emit 'new-window', event, url, frameName, 'new-window' event.sender.emit 'new-window', event, url, frameName, 'new-window'
if event.sender.isGuest() or event.defaultPrevented if (event.sender.isGuest() and not event.sender.allowPopups) or event.defaultPrevented
event.returnValue = null event.returnValue = null
else else
event.returnValue = createGuest event.sender, args... event.returnValue = createGuest event.sender, args...

View file

@ -70,7 +70,6 @@ window.open = (url, frameName='', features='') ->
if guestId if guestId
new BrowserWindowProxy(guestId) new BrowserWindowProxy(guestId)
else else
console.error 'It is not allowed to open new window from this WebContents'
null null
# Use the dialog API to implement alert(). # Use the dialog API to implement alert().

View file

@ -216,6 +216,7 @@ WebViewImpl::setupWebViewAttributes = ->
@attributes[webViewConstants.ATTRIBUTE_NODEINTEGRATION] = new BooleanAttribute(webViewConstants.ATTRIBUTE_NODEINTEGRATION, this) @attributes[webViewConstants.ATTRIBUTE_NODEINTEGRATION] = new BooleanAttribute(webViewConstants.ATTRIBUTE_NODEINTEGRATION, this)
@attributes[webViewConstants.ATTRIBUTE_PLUGINS] = new BooleanAttribute(webViewConstants.ATTRIBUTE_PLUGINS, this) @attributes[webViewConstants.ATTRIBUTE_PLUGINS] = new BooleanAttribute(webViewConstants.ATTRIBUTE_PLUGINS, this)
@attributes[webViewConstants.ATTRIBUTE_DISABLEWEBSECURITY] = new BooleanAttribute(webViewConstants.ATTRIBUTE_DISABLEWEBSECURITY, this) @attributes[webViewConstants.ATTRIBUTE_DISABLEWEBSECURITY] = new BooleanAttribute(webViewConstants.ATTRIBUTE_DISABLEWEBSECURITY, this)
@attributes[webViewConstants.ATTRIBUTE_ALLOWPOPUPS] = new BooleanAttribute(webViewConstants.ATTRIBUTE_ALLOWPOPUPS, this)
@attributes[webViewConstants.ATTRIBUTE_PRELOAD] = new PreloadAttribute(this) @attributes[webViewConstants.ATTRIBUTE_PRELOAD] = new PreloadAttribute(this)
autosizeAttributes = [ autosizeAttributes = [

View file

@ -13,6 +13,7 @@ module.exports =
ATTRIBUTE_NODEINTEGRATION: 'nodeintegration' ATTRIBUTE_NODEINTEGRATION: 'nodeintegration'
ATTRIBUTE_PLUGINS: 'plugins' ATTRIBUTE_PLUGINS: 'plugins'
ATTRIBUTE_DISABLEWEBSECURITY: 'disablewebsecurity' ATTRIBUTE_DISABLEWEBSECURITY: 'disablewebsecurity'
ATTRIBUTE_ALLOWPOPUPS: 'allowpopups'
ATTRIBUTE_PRELOAD: 'preload' ATTRIBUTE_PRELOAD: 'preload'
ATTRIBUTE_USERAGENT: 'useragent' ATTRIBUTE_USERAGENT: 'useragent'

View file

@ -149,6 +149,14 @@ This value can only be modified before the first navigation, since the session
of an active renderer process cannot change. Subsequent attempts to modify the of an active renderer process cannot change. Subsequent attempts to modify the
value will fail with a DOM exception. value will fail with a DOM exception.
### `allowpopups`
```html
<webview src="https://www.github.com/" allowpopups></webview>
```
If "on", the guest page will be allowed to open new windows.
## Methods ## Methods
The `webview` tag has the following methods: The `webview` tag has the following methods:
@ -496,7 +504,7 @@ Returns:
* `url` String * `url` String
* `frameName` String * `frameName` String
* `disposition` String - Can be `default`, `foreground-tab`, `background-tab`, * `disposition` String - Can be `default`, `foreground-tab`, `background-tab`,
`new-window` and `other` `new-window` and `other`.
Fired when the guest page attempts to open a new browser window. Fired when the guest page attempts to open a new browser window.

View file

@ -0,0 +1,12 @@
<html>
<body>
<script type="text/javascript" charset="utf-8">
var w = window.open('http://host', '', 'show=no');
if (w == null)
console.log('null');
else
console.log('window');
</script>
</body>
</html>

View file

@ -201,6 +201,26 @@ describe '<webview> tag', ->
webview.src = "file://#{fixtures}/pages/partition/one.html" webview.src = "file://#{fixtures}/pages/partition/one.html"
document.body.appendChild webview document.body.appendChild webview
describe 'allowpopups attribute', ->
it 'can not open new window when not set', (done) ->
listener = (e) ->
assert.equal e.message, 'null'
webview.removeEventListener 'console-message', listener
done()
webview.addEventListener 'console-message', listener
webview.src = "file://#{fixtures}/pages/window-open-hide.html"
document.body.appendChild webview
it 'can open new window when set', (done) ->
listener = (e) ->
assert.equal e.message, 'window'
webview.removeEventListener 'console-message', listener
done()
webview.addEventListener 'console-message', listener
webview.setAttribute 'allowpopups', 'on'
webview.src = "file://#{fixtures}/pages/window-open-hide.html"
document.body.appendChild webview
describe 'new-window event', -> describe 'new-window event', ->
it 'emits when window.open is called', (done) -> it 'emits when window.open is called', (done) ->
webview.addEventListener 'new-window', (e) -> webview.addEventListener 'new-window', (e) ->