Merge pull request #1587 from atom/agent-as-attribute
Allow User Agent on WebView to be set as an attribute
This commit is contained in:
commit
a9a331938e
7 changed files with 52 additions and 6 deletions
|
@ -418,6 +418,10 @@ void WebContents::LoadURL(const GURL& url, const mate::Dictionary& options) {
|
|||
params.referrer = content::Referrer(http_referrer.GetAsReferrer(),
|
||||
blink::WebReferrerPolicyDefault);
|
||||
|
||||
std::string user_agent;
|
||||
if (options.Get("useragent", &user_agent))
|
||||
SetUserAgent(user_agent);
|
||||
|
||||
params.transition_type = ui::PAGE_TRANSITION_TYPED;
|
||||
params.should_clear_history_list = true;
|
||||
params.override_user_agent = content::NavigationController::UA_OVERRIDE_TRUE;
|
||||
|
|
|
@ -57,11 +57,13 @@ createGuest = (embedder, params) ->
|
|||
min = width: params.minwidth, height: params.minheight
|
||||
max = width: params.maxwidth, height: params.maxheight
|
||||
@setAutoSize params.autosize, min, max
|
||||
|
||||
if params.src
|
||||
if params.httpreferrer
|
||||
@loadUrl params.src, {httpreferrer: params.httpreferrer}
|
||||
else
|
||||
@loadUrl params.src
|
||||
opts = {}
|
||||
opts.httpreferrer = params.httpreferrer if params.httpreferrer
|
||||
opts.useragent = params.useragent if params.useragent
|
||||
@loadUrl params.src, opts
|
||||
|
||||
if params.allowtransparency?
|
||||
@setAllowTransparency params.allowtransparency
|
||||
|
||||
|
|
|
@ -158,15 +158,26 @@ class SrcAttribute extends WebViewAttribute
|
|||
return
|
||||
|
||||
# Navigate to |this.src|.
|
||||
opts = {}
|
||||
httpreferrer = @webViewImpl.attributes[webViewConstants.ATTRIBUTE_HTTPREFERRER].getValue()
|
||||
urlOptions = if httpreferrer then {httpreferrer} else {}
|
||||
remote.getGuestWebContents(@webViewImpl.guestInstanceId).loadUrl @getValue(), urlOptions
|
||||
if httpreferrer then opts.httpreferrer = httpreferrer
|
||||
|
||||
useragent = @webViewImpl.attributes[webViewConstants.ATTRIBUTE_USERAGENT].getValue()
|
||||
if useragent then opts.useragent = useragent
|
||||
|
||||
guestContents = remote.getGuestWebContents(@webViewImpl.guestInstanceId)
|
||||
guestContents.loadUrl @getValue(), opts
|
||||
|
||||
# Attribute specifies HTTP referrer.
|
||||
class HttpReferrerAttribute extends WebViewAttribute
|
||||
constructor: (webViewImpl) ->
|
||||
super webViewConstants.ATTRIBUTE_HTTPREFERRER, webViewImpl
|
||||
|
||||
# Attribute specifies user agent
|
||||
class UserAgentAttribute extends WebViewAttribute
|
||||
constructor: (webViewImpl) ->
|
||||
super webViewConstants.ATTRIBUTE_USERAGENT, webViewImpl
|
||||
|
||||
# Attribute that set preload script.
|
||||
class PreloadAttribute extends WebViewAttribute
|
||||
constructor: (webViewImpl) ->
|
||||
|
@ -190,6 +201,7 @@ WebViewImpl::setupWebViewAttributes = ->
|
|||
@attributes[webViewConstants.ATTRIBUTE_PARTITION] = new PartitionAttribute(this)
|
||||
@attributes[webViewConstants.ATTRIBUTE_SRC] = new SrcAttribute(this)
|
||||
@attributes[webViewConstants.ATTRIBUTE_HTTPREFERRER] = new HttpReferrerAttribute(this)
|
||||
@attributes[webViewConstants.ATTRIBUTE_USERAGENT] = new UserAgentAttribute(this)
|
||||
@attributes[webViewConstants.ATTRIBUTE_NODEINTEGRATION] = new BooleanAttribute(webViewConstants.ATTRIBUTE_NODEINTEGRATION, this)
|
||||
@attributes[webViewConstants.ATTRIBUTE_PLUGINS] = new BooleanAttribute(webViewConstants.ATTRIBUTE_PLUGINS, this)
|
||||
@attributes[webViewConstants.ATTRIBUTE_DISABLEWEBSECURITY] = new BooleanAttribute(webViewConstants.ATTRIBUTE_DISABLEWEBSECURITY, this)
|
||||
|
|
|
@ -14,6 +14,7 @@ module.exports =
|
|||
ATTRIBUTE_PLUGINS: 'plugins'
|
||||
ATTRIBUTE_DISABLEWEBSECURITY: 'disablewebsecurity'
|
||||
ATTRIBUTE_PRELOAD: 'preload'
|
||||
ATTRIBUTE_USERAGENT: 'useragent'
|
||||
|
||||
# Internal attribute.
|
||||
ATTRIBUTE_INTERNALINSTANCEID: 'internalinstanceid'
|
||||
|
|
|
@ -112,6 +112,14 @@ after this script has done execution.
|
|||
|
||||
Sets the referrer URL for the guest page.
|
||||
|
||||
### useragent
|
||||
|
||||
```html
|
||||
<webview src="https://www.github.com/" useragent="Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; AS; rv:11.0) like Gecko"></webview>
|
||||
```
|
||||
|
||||
Sets the user agent for the guest page before the page is navigated to. Once the page is loaded, use the `setUserAgent` method to change the user agent.
|
||||
|
||||
### disablewebsecurity
|
||||
|
||||
```html
|
||||
|
|
7
spec/fixtures/pages/useragent.html
vendored
Normal file
7
spec/fixtures/pages/useragent.html
vendored
Normal file
|
@ -0,0 +1,7 @@
|
|||
<html>
|
||||
<body>
|
||||
<script type="text/javascript" charset="utf-8">
|
||||
console.log(navigator.userAgent);
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
|
@ -99,6 +99,18 @@ describe '<webview> tag', ->
|
|||
webview.src = "file://#{fixtures}/pages/referrer.html"
|
||||
document.body.appendChild webview
|
||||
|
||||
describe 'useragent attribute', ->
|
||||
it 'sets the user agent', (done) ->
|
||||
referrer = 'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; AS; rv:11.0) like Gecko'
|
||||
listener = (e) ->
|
||||
assert.equal e.message, referrer
|
||||
webview.removeEventListener 'console-message', listener
|
||||
done()
|
||||
webview.addEventListener 'console-message', listener
|
||||
webview.setAttribute 'useragent', referrer
|
||||
webview.src = "file://#{fixtures}/pages/useragent.html"
|
||||
document.body.appendChild webview
|
||||
|
||||
describe 'disablewebsecurity attribute', ->
|
||||
it 'does not disable web security when not set', (done) ->
|
||||
src = "
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue