diff --git a/atom/browser/api/atom_api_web_contents.cc b/atom/browser/api/atom_api_web_contents.cc index e228a064f413..9894241f74ee 100644 --- a/atom/browser/api/atom_api_web_contents.cc +++ b/atom/browser/api/atom_api_web_contents.cc @@ -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; diff --git a/atom/browser/lib/guest-view-manager.coffee b/atom/browser/lib/guest-view-manager.coffee index 43f4d7c98c5f..a02a1676e8e6 100644 --- a/atom/browser/lib/guest-view-manager.coffee +++ b/atom/browser/lib/guest-view-manager.coffee @@ -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 diff --git a/atom/renderer/lib/web-view/web-view-attributes.coffee b/atom/renderer/lib/web-view/web-view-attributes.coffee index 05bd6141203f..51dd2475a4e0 100644 --- a/atom/renderer/lib/web-view/web-view-attributes.coffee +++ b/atom/renderer/lib/web-view/web-view-attributes.coffee @@ -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) diff --git a/atom/renderer/lib/web-view/web-view-constants.coffee b/atom/renderer/lib/web-view/web-view-constants.coffee index cda715423dd4..deb678e6a1e8 100644 --- a/atom/renderer/lib/web-view/web-view-constants.coffee +++ b/atom/renderer/lib/web-view/web-view-constants.coffee @@ -14,6 +14,7 @@ module.exports = ATTRIBUTE_PLUGINS: 'plugins' ATTRIBUTE_DISABLEWEBSECURITY: 'disablewebsecurity' ATTRIBUTE_PRELOAD: 'preload' + ATTRIBUTE_USERAGENT: 'useragent' # Internal attribute. ATTRIBUTE_INTERNALINSTANCEID: 'internalinstanceid' diff --git a/docs/api/web-view-tag.md b/docs/api/web-view-tag.md index 89ae678b3c46..3e008a5db258 100644 --- a/docs/api/web-view-tag.md +++ b/docs/api/web-view-tag.md @@ -112,6 +112,14 @@ after this script has done execution. Sets the referrer URL for the guest page. +### useragent + +```html + +``` + +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 diff --git a/spec/fixtures/pages/useragent.html b/spec/fixtures/pages/useragent.html new file mode 100644 index 000000000000..4e19f5b8276a --- /dev/null +++ b/spec/fixtures/pages/useragent.html @@ -0,0 +1,7 @@ + + + + + diff --git a/spec/webview-spec.coffee b/spec/webview-spec.coffee index 54bc2e78817f..6d624eb9d81b 100644 --- a/spec/webview-spec.coffee +++ b/spec/webview-spec.coffee @@ -99,6 +99,18 @@ describe ' 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 = "