Page 1 of 1
Change useragent in runtime
Posted: Thu Jun 07, 2018 6:10 pm
by thefunkyjoint
Hi,
Is it possible to change the useragent in runtime ? Let's say, after the component is created, i want to change the useragent before loading a url and then change it again for the next url.
Thanks
Re: Change useragent in runtime
Posted: Thu Jun 07, 2018 7:07 pm
by salvadordf
You need to use the TChromium.OnBeforeResourceLoad event and replace the user agent header in the header map.
Take a look at the code in TChromium.doOnBeforeResourceLoad (uCEFChromium.pas) to know how to add custom headers.
Make sure to remove the old user agent header and call request.SetHeaderMap in the last step.
Re: Change useragent in runtime
Posted: Fri Mar 08, 2019 10:12 am
by snoop
hello, how to replace it? Can you provide code?
Code: Select all
Header := TCefStringMultimapOwn.Create;
Request.GetHeaderMap(Header);
Header.Append('user-agent', ua);
Request.SetHeaderMap(Header);
is this correct? There is only "append" method, but no replace or delete.
Re: Change useragent in runtime
Posted: Fri Mar 08, 2019 10:46 am
by salvadordf
There's no replace but you can create a new ICefStringMultimap, copy the other values and append the modified value.
Here's an example that replaces the "
Accept-Encoding" HTTP header :
https://github.com/salvadordf/CEF4Delph ... r.pas#L419
Re: Change useragent in runtime
Posted: Fri Mar 08, 2019 5:26 pm
by snoop
thanks, worked like a charm!
Re: Change useragent in runtime
Posted: Tue Mar 12, 2019 11:05 pm
by snoop
hello! It doesnt change useragent completely
If I check my program on whoer.net it shows that it has replaced headers with new user-agent, but uses old build-in user-agent for javascripts
any way to change it?
Re: Change useragent in runtime
Posted: Wed Mar 13, 2019 8:45 am
by salvadordf
I did a couple of tests in whoer.net and I couldn't reproduce this issue.
I added this to the MiniBrowser demo :
Code: Select all
GlobalCEFApp.UserAgent := 'MiniBrowser/1.0';
Then I visited whoer.net and it shows this in the "
Browser" section :
- Headers: MiniBrowser/1.0
- JavaScript: MiniBrowser/1.0
BriskBard uses the same method to change the user agent and whoer shows the modified user agent for JavaScript too.
It's not the first time someone reports this issue as you can see here :
https://www.briskbard.com/forum/viewtopic.php?t=132
The only way I know to modify the user agent is using GlobalCEFApp.UserAgent and replacing the HTTP headers in TChromium.OnBeforeResourceLoad
Re: Change useragent in runtime
Posted: Wed Mar 13, 2019 8:51 am
by salvadordf
You can also use
GlobalCEFApp.ProductVersion to modify part of the user agent string. For example, if I add this to the MiniBrowser demo :
Code: Select all
GlobalCEFApp.ProductVersion := 'MiniBrowser/1.0';
The new user agent is this :
Code: Select all
Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) MiniBrowser/1.0 Safari/537.36
Whoer.net also shows the same modified user agent for the headers and javascript in this case.
Re: Change useragent in runtime
Posted: Wed Mar 13, 2019 8:59 am
by salvadordf
Please, test the MiniBrowser demo to see if you get the same user agent too.
If you get the same user agent then perhaps some other setting in your app is causing the Javascript to use the original user agent string.
Re: Change useragent in runtime
Posted: Wed Mar 13, 2019 10:30 am
by snoop
I need to change it in runtime, and also I have different browsers with different contexts. GlobalCEFApp parameters change useragent globally, so this doesn't suit my situation