3

I need to change the user agent in the TEdgeBrowser

How to set useragent in new Delphi TEdgeBrowser VCL ?

Also any workarounds are welcome!

AC1D
  • 248
  • 5
  • 16

2 Answers2

1

According to Microsoft documentation, this is only available from a pre-release. So it is not [yet] available in Delphi TEdgeBrowser.

fpiette
  • 11,983
  • 1
  • 24
  • 46
  • Well, Edge stores useragent somewhere. Maybe can set an agent somewhere in the registry as a workaround. I will try to find it using Regmon – AC1D Mar 18 '21 at 15:00
  • @AC1D Since the `UserAgent` is not configurable in Edge's API (that I can see), what makes you think it has to be stored anywhere, and is not just hard-coded? – Remy Lebeau Mar 24 '21 at 20:30
  • Even if it is hard-coded somewhere, you can change it in the following ways: User agent stored in C:\Users\%username%\AppData\Local\Microsoft\Edge\User Data\Default\Preferences file -> customUserAgent string. Also you can change user agent on tab "Network conditions". you can also change it via the extension. perhaps even via javascript. but this has nothing to do with the question. – AC1D Mar 25 '21 at 09:05
0

I found a solution.

Microsoft has updated WebView2 and added the ability to change a user agent. But Embarcadero has not updated the component.

Create new interface

unit Webview2Ex;

interface

uses
    WebView2;

const
   IID_ICoreWebView2Settings2: TGUID = '{EE9A0F68-F46C-4E32-AC23-EF8CAC224D2A}';

type

  ICoreWebView2Settings2 = interface(ICoreWebView2Settings)
    ['{EE9A0F68-F46C-4E32-AC23-EF8CAC224D2A}']
    function Get_UserAgent(out UserAgent: PWideChar): HResult; stdcall;
    function Set_UserAgent(UserAgent: PWideChar): HResult; stdcall;
  end;

implementation

end.

Create handler onCreateWebViewCompleted

 procedure TAiForm.RBrowserCreateWebViewCompleted(Sender: TCustomEdgeBrowser;
          AResult: HRESULT);
        var
            Ctrl2     : ICoreWebView2Settings2;
            HR        : HRESULT;
            UA        : PWideChar;
        begin
//You must query SettingsInterface2 from SettingsInterface it's important
         Sender.SettingsInterface.QueryInterface(IID_ICoreWebView2Settings2, Ctrl2);
            if not Assigned(Ctrl2) then
                raise Exception.Create('ICoreWebView2Settings2 not found');
        
            UA := 'NEW UA';
            HR := Ctrl2.Set_UserAgent(UA);
        
            HR := Ctrl2.Get_UserAgent(UA);
            if not SUCCEEDED(HR) then
                raise Exception.Create('Get_UserAgent failed')
                else ShowMessage(ua);
        end;

Also need update WebView2 component from Microsoft

https://developer.microsoft.com/en-us/microsoft-edge/webview2/

If you want update all interfaces read this article

WebView2 (TEdgeBrowser) updated Delphi interface (e.g. ICoreWebView2Controller2)

AC1D
  • 248
  • 5
  • 16