Disclosure Statement: This site contains affiliate links, which means that I may receive a commission if you make a purchase using these links. As an eBay Partner, I earn from qualifying purchases.
Change useragent in runtime
-
- Posts: 513
- Joined: Thu Aug 10, 2017 12:40 pm
Change useragent in runtime
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
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
- salvadordf
- Posts: 4565
- Joined: Thu Feb 02, 2017 12:24 pm
- Location: Spain
- Contact:
Re: Change useragent in runtime
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.
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
hello, how to replace it? Can you provide code?
is this correct? There is only "append" method, but no replace or delete.
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.
- salvadordf
- Posts: 4565
- Joined: Thu Feb 02, 2017 12:24 pm
- Location: Spain
- Contact:
Re: Change useragent in runtime
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
Here's an example that replaces the "Accept-Encoding" HTTP header :
https://github.com/salvadordf/CEF4Delph ... r.pas#L419
Re: Change useragent in runtime
thanks, worked like a charm!
Re: Change useragent in runtime
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?
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?
- salvadordf
- Posts: 4565
- Joined: Thu Feb 02, 2017 12:24 pm
- Location: Spain
- Contact:
Re: Change useragent in runtime
I did a couple of tests in whoer.net and I couldn't reproduce this issue.
I added this to the MiniBrowser demo :
Then I visited whoer.net and it shows this in the "Browser" section :
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
I added this to the MiniBrowser demo :
Code: Select all
GlobalCEFApp.UserAgent := 'MiniBrowser/1.0';
- 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
- salvadordf
- Posts: 4565
- Joined: Thu Feb 02, 2017 12:24 pm
- Location: Spain
- Contact:
Re: Change useragent in runtime
You can also use GlobalCEFApp.ProductVersion to modify part of the user agent string. For example, if I add this to the MiniBrowser demo :
The new user agent is this :
Whoer.net also shows the same modified user agent for the headers and javascript in this case.
Code: Select all
GlobalCEFApp.ProductVersion := 'MiniBrowser/1.0';
Code: Select all
Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) MiniBrowser/1.0 Safari/537.36
- salvadordf
- Posts: 4565
- Joined: Thu Feb 02, 2017 12:24 pm
- Location: Spain
- Contact:
Re: Change useragent in runtime
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.
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
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