Page 1 of 1

UserAgentBrandVersion and UserAgentMetadata

Posted: Mon Apr 10, 2023 9:23 am
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"}

Re: UserAgentBrandVersion and UserAgentMetadata

Posted: Tue Apr 11, 2023 10:34 am
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.

Re: UserAgentBrandVersion and UserAgentMetadata

Posted: Wed Apr 12, 2023 6:49 am
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);

Re: UserAgentBrandVersion and UserAgentMetadata

Posted: Thu Apr 13, 2023 9:45 am
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

Re: UserAgentBrandVersion and UserAgentMetadata

Posted: Thu Apr 13, 2023 10:54 am
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);

Re: UserAgentBrandVersion and UserAgentMetadata

Posted: Thu Apr 13, 2023 12:49 pm
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.

Re: UserAgentBrandVersion and UserAgentMetadata

Posted: Fri Apr 14, 2023 11:08 am
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"}]

Re: UserAgentBrandVersion and UserAgentMetadata

Posted: Sat Apr 15, 2023 3:54 pm
by salvadordf
Try adding items to a list like this :
https://github.com/salvadordf/CEF4Delphi/blob/a0e2bcfbd83922991d041958bae0cc015a8eb4f3/source/uCEFChromiumCore.pas#L4402

Re: UserAgentBrandVersion and UserAgentMetadata

Posted: Sun Apr 16, 2023 12:40 pm
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"}]

Re: UserAgentBrandVersion and UserAgentMetadata

Posted: Tue Apr 18, 2023 8:35 am
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"}]