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.

UserAgentBrandVersion and UserAgentMetadata

Post Reply
dilfich
Posts: 330
Joined: Thu Nov 30, 2017 1:17 am

UserAgentBrandVersion and UserAgentMetadata

Post by dilfich »

Hello everyone!

It is clear that the function is experimental, but does it still work or not?
Or am I doing it wrong?

https://chromedevtools.github.io/devtools-protocol/tot/Emulation/#type-UserAgentMetadata

Code: Select all

 TempParams := TCefDictionaryValueRef.New;
 TempParams.SetString('brand', 'Google Chrome');
 TempParams.SetString('version', '111');
 Chromium1.ExecuteDevToolsMethod(0, 'Emulation.UserAgentBrandVersion', TempParams);
Or how else to cheat the script check?

replace
"userAgentData":{"brands":[{"brand":"Chromium","version":"111"}],"mobile":false,"platform":"Windows"}
on
"userAgentData":{"brands":[{"brand":"Google Chrome","version":"111"},{"brand":"Not(A:Brand","version":"8"},{"brand":"Chromium","version":"111"}],"mobile":false,"platform":"Windows"}
User avatar
salvadordf
Posts: 4057
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: UserAgentBrandVersion and UserAgentMetadata

Post by salvadordf »

Hi,

Emulation.UserAgentBrandVersion is a data type, not a method that can be executed.
https://chromedevtools.github.io/devtools-protocol/tot/Emulation/

Execute the Emulation.setUserAgentOverride method with a userAgentMetadata parameter.
The Emulation.UserAgentMetadata data type has the Emulation.UserAgentBrandVersion field that you need.

... but remember that all this has a "EXPERIMENTAL" tag and it might not work at all.
dilfich
Posts: 330
Joined: Thu Nov 30, 2017 1:17 am

Re: UserAgentBrandVersion and UserAgentMetadata

Post by dilfich »

Am I doing wrong or is it not working? :(

https://browserleaks.com/client-hints

Code: Select all

 TempDict.SetString('platform', 'Windows');
 TempDict.SetString('architecture', 'x86');
 TempDict.SetString('bitness', '64');
 TempParams.SetDictionary('UserAgentMetadata', TempDict);

 Chromium1.ExecuteDevToolsMethod(0, 'Emulation.setUserAgentOverride', TempParams);
User avatar
salvadordf
Posts: 4057
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: UserAgentBrandVersion and UserAgentMetadata

Post by salvadordf »

The Emulation.setUserAgentOverride method has different parameters :
  • userAgent : string -> User agent to use.
  • acceptLanguage (OPTIONAL) : string -> Browser langugage to emulate.
  • platform (OPTIONAL) : string -> The platform navigator.platform should return.
  • userAgentMetadata (OPTIONAL) : UserAgentMetadata -> To be sent in Sec-CH-UA-* headers and returned in navigator.userAgentData
https://chromedevtools.github.io/devtools-protocol/tot/Emulation/#method-setUserAgentOverride

The UserAgentMetadata type has these parameters :
  • brands (OPTIONAL) : array[ UserAgentBrandVersion ] -> Brands appearing in Sec-CH-UA.
  • fullVersionList (OPTIONAL) : array[ UserAgentBrandVersion ] -> Brands appearing in Sec-CH-UA-Full-Version-List.
  • fullVersion (OPTIONAL) : string -> Deprecated
  • platform : string
  • platformVersion : string
  • architecture : string
  • model : string
  • mobile : boolean
  • bitness (OPTIONAL) : string
  • wow64 (OPTIONAL) : boolean
https://chromedevtools.github.io/devtools-protocol/tot/Emulation/#type-UserAgentMetadata

It's necessary to add all parameters unless they are marked as optional.

In the case of Emulation.setUserAgentOverride you need to add the "userAgent" parameter and "userAgentMetadata" but the UserAgentMetadata type is a nested dictionary.

See the MobileBrowser demo to know how to add nested parameters :
https://github.com/salvadordf/CEF4Delphi/blob/424975e0e136c4db3fac6c33a875132ea9a69eee/demos/Delphi_VCL/MobileBrowser/uMobileBrowser.pas#L294
dilfich
Posts: 330
Joined: Thu Nov 30, 2017 1:17 am

Re: UserAgentBrandVersion and UserAgentMetadata

Post by dilfich »

I didn't fully show it, but that's how I looked. What am I doing wrong?
TempParams := TCefDictionaryValueRef.New;
TempParams.SetString('userAgent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36');

TempDict := TCefDictionaryValueRef.New;
TempDict.SetString('platform', 'Windows');
TempDict.SetString('platformVersion', '10.0.0');
TempDict.SetString('architecture', 'x86');
TempDict.SetString('model', '');
TempDict.SetBool('mobile', False);
TempParams.SetDictionary('UserAgentMetadata', TempDict);

Chromium1.ExecuteDevToolsMethod(0, 'Emulation.setUserAgentOverride', TempParams);
User avatar
salvadordf
Posts: 4057
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: UserAgentBrandVersion and UserAgentMetadata

Post by salvadordf »

I just added your code to the MobileBrowser demo like this :

Code: Select all

procedure TForm1.OverrideUserAgentBtnClick(Sender: TObject);
var
  TempParams, TempDict : ICefDictionaryValue;
begin
  try
    TempParams := TCefDictionaryValueRef.New;
    TempParams.SetString('userAgent', UserAgentCb.Text);

    TempDict := TCefDictionaryValueRef.New;
    TempDict.SetString('platform', 'Linux');
    TempDict.SetString('platformVersion', '10.1.0');
    TempDict.SetString('architecture', 'x86');
    TempDict.SetString('model', 'Mint');
    TempDict.SetBool('mobile', False);
    TempParams.SetDictionary('userAgentMetadata', TempDict);

    FPendingMsgID := DEVTOOLS_SETUSERAGENTOVERRIDE_MSGID;
    Chromium1.ExecuteDevToolsMethod(0, 'Emulation.setUserAgentOverride', TempParams);
  finally
    TempParams := nil;
  end;
end;
I saw this result in the LogMem control : "Successful SetUserAgentOverride".

Then I navigated to https://browserleaks.com/client-hints

These are the results :
Your Web Browser
HTTP User-Agent Mozilla/5.0 (Linux; Android 11; M2102K1G) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.101 Mobile Safari/537.36
Client Hints JavaScript API
API Support ✔True
brands [{"brand":"Chromium","version":"112"}]
mobile false
platform Linux
platformVersion 10.1.0
architecture x86
bitness empty
wow64 false
model Mint
uaFullVersion 112.0.5615.49
fullVersionList []
The new values in the "userAgentMetadata" dictionary seem to work.

The cause of this issue was an incorrect parameter name. The correct name is "userAgentMetadata" and not "UserAgentMetadata". The DevTools methods are case sensitive.
dilfich
Posts: 330
Joined: Thu Nov 30, 2017 1:17 am

Re: UserAgentBrandVersion and UserAgentMetadata

Post by dilfich »

Really register :D

What should I do here?
brands array[ UserAgentBrandVersion ]
How do I add an array?
brands [{"brand":"Google Chrome","version":"111"},
{"brand":"Not(A:Brand","version":"8"},
{"brand":"Chromium","version":"111"}]
User avatar
salvadordf
Posts: 4057
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: UserAgentBrandVersion and UserAgentMetadata

Post by salvadordf »

Try adding items to a list like this :
https://github.com/salvadordf/CEF4Delphi/blob/a0e2bcfbd83922991d041958bae0cc015a8eb4f3/source/uCEFChromiumCore.pas#L4402
dilfich
Posts: 330
Joined: Thu Nov 30, 2017 1:17 am

Re: UserAgentBrandVersion and UserAgentMetadata

Post by dilfich »

That's how it turned out, but not exactly what I wanted.
brands [{"brand":"Google Chrome","version":"777"}]
Or is it not right? :?

Code: Select all

...
 TempArr := TCefDictionaryValueRef.New;
 TempArr.SetString('brand', 'Google Chrome');
 TempArr.SetString('version', '777');

 TempList := TCefListValueRef.New;
 TempList.SetDictionary(0, TempArr);

 TempDict.SetList('brands', TempList);
...
But how do I add a few?
{"brand":"Google Chrome","version":"111"},{"brand":"Not(A:Brand","version":"8"},{"brand":"Chromium","version":"111"}]
dilfich
Posts: 330
Joined: Thu Nov 30, 2017 1:17 am

Re: UserAgentBrandVersion and UserAgentMetadata

Post by dilfich »

In general, it seems to have worked out! :D

Code: Select all

var
  TempParams: ICefDictionaryValue;
  TempDict: ICefDictionaryValue;
  TempArr: ICefDictionaryValue;
  TempListDict: ICefListValue;
begin
 TempParams := TCefDictionaryValueRef.New;
 TempParams.SetString('userAgent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36');

 TempListDict  := TCefListValueRef.New;

//*********************************************** x1

 TempArr := TCefDictionaryValueRef.New;

 TempArr.SetString('brand', 'Google Chrome');
 TempArr.SetString('version', '111');

 TempListDict.SetDictionary(0, TempArr);

//*********************************************** x2

 TempArr := TCefDictionaryValueRef.New;

 TempArr.SetString('brand', 'Not(A:Brand');
 TempArr.SetString('version', '8');

 TempListDict.SetDictionary(1, TempArr);

//*********************************************** x3

 TempArr := TCefDictionaryValueRef.New;

 TempArr.SetString('brand', 'Chromium');
 TempArr.SetString('version', '111');

 TempListDict.SetDictionary(2, TempArr);

//*********************************************** end

 TempDict := TCefDictionaryValueRef.New;

 TempDict.SetList('brands', TempListDict);

 TempDict.SetString('platform', 'Windows');
 TempDict.SetString('platformVersion', '10.0.0');
 TempDict.SetString('architecture', 'x86');
 TempDict.SetString('model', '');
 TempDict.SetBool('mobile', False);
 TempDict.SetString('bitness', '64');
 TempParams.SetDictionary('userAgentMetadata', TempDict);

 Chromium1.ExecuteDevToolsMethod(0, 'Emulation.setUserAgentOverride', TempParams);
end;
Result: brands [{"brand":"Google Chrome","version":"111"},{"brand":"Not(A:Brand","version":"8"},{"brand":"Chromium","version":"111"}]
Post Reply