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.
If you find these projects useful please consider becoming a sponsor with Patreon, GitHub or Liberapay.

Change useragent in runtime

Post Reply
thefunkyjoint
Posts: 513
Joined: Thu Aug 10, 2017 12:40 pm

Change useragent in runtime

Post 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
User avatar
salvadordf
Posts: 4565
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Change useragent in runtime

Post 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.
snoop
Posts: 75
Joined: Mon Jul 10, 2017 12:06 pm

Re: Change useragent in runtime

Post 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.
User avatar
salvadordf
Posts: 4565
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Change useragent in runtime

Post 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
snoop
Posts: 75
Joined: Mon Jul 10, 2017 12:06 pm

Re: Change useragent in runtime

Post by snoop »

thanks, worked like a charm!
snoop
Posts: 75
Joined: Mon Jul 10, 2017 12:06 pm

Re: Change useragent in runtime

Post 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?
User avatar
salvadordf
Posts: 4565
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Change useragent in runtime

Post 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
User avatar
salvadordf
Posts: 4565
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Change useragent in runtime

Post 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.
User avatar
salvadordf
Posts: 4565
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Change useragent in runtime

Post 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.
snoop
Posts: 75
Joined: Mon Jul 10, 2017 12:06 pm

Re: Change useragent in runtime

Post 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
Post Reply