Set user agent correctly on QtWebEngine

This commit is contained in:
Florian Bruhin 2017-05-16 06:46:45 +02:00
parent 086139110d
commit d0dd1644af
3 changed files with 22 additions and 0 deletions

View File

@ -79,6 +79,8 @@ Fixed
- The validation for colors in stylesheets is now less strict,
allowing for all valid Qt values.
- data: URLs now aren't added to the history anymore.
- window.navigator.userAgent is now set correctly when customizing the user
agent on QtWebEngine.
v0.10.1
-------

View File

@ -147,12 +147,26 @@ def _init_stylesheet(profile):
profile.scripts().insert(script)
def _set_user_agent(profile):
"""Set the user agent for the given profile.
We override this per request in the URL interceptor (to allow for per-domain
user agents), but this one still gets used for things like
window.navigator.userAgent in JS.
"""
user_agent = config.get('network', 'user-agent')
profile.setHttpUserAgent(user_agent)
def update_settings(section, option):
"""Update global settings when qwebsettings changed."""
websettings.update_mappings(MAPPINGS, section, option)
if section == 'ui' and option in ['hide-scrollbar', 'user-stylesheet']:
_init_stylesheet(default_profile)
_init_stylesheet(private_profile)
elif section == 'network' and option == 'user-agent':
_set_user_agent(default_profile)
_set_user_agent(private_profile)
def _init_profiles():
@ -164,10 +178,12 @@ def _init_profiles():
default_profile.setPersistentStoragePath(
os.path.join(standarddir.data(), 'webengine'))
_init_stylesheet(default_profile)
_set_user_agent(default_profile)
private_profile = QWebEngineProfile()
assert private_profile.isOffTheRecord()
_init_stylesheet(private_profile)
_set_user_agent(private_profile)
def init(args):

View File

@ -471,12 +471,16 @@ Feature: Various utility commands.
Scenario: Setting a custom user-agent header
When I set network -> user-agent to toaster
And I open headers
And I run :jseval console.log(window.navigator.userAgent)
Then the header User-Agent should be set to toaster
And the javascript message "toaster" should be logged
Scenario: Setting the default user-agent header
When I set network -> user-agent to <empty>
And I open headers
And I run :jseval console.log(window.navigator.userAgent)
Then the header User-Agent should be set to Mozilla/5.0 *
And the javascript message "Mozilla/5.0 *" should be logged
## :messages