如何在新的Delphi TEdgeBrowser中设置用户代理?

3

我需要更改TEdgeBrowser中的用户代理

如何在新的Delphi TEdgeBrowser VCL中设置用户代理?

同时欢迎任何解决方法!

2个回答

1
根据Microsoft文档,这仅在预发布版中可用。因此,在Delphi TEdgeBrowser中尚不可用。

好的,Edge浏览器在某个地方存储UserAgent。可以尝试在注册表中设置代理作为解决方法。我会尝试使用Regmon找到它。 - AC1D
@AC1D 由于我在Edge的API中没有看到UserAgent是可配置的,你认为它必须被存储在某个地方吗?难道不可能是硬编码的吗? - Remy Lebeau
即使某些地方已经硬编码,您仍可以通过以下方式更改:存储在C:\Users%username%\AppData\Local\Microsoft\Edge\User Data\Default\Preferences文件中的用户代理->自定义用户代理字符串。此外,您还可以在“网络条件”选项卡上更改用户代理。您还可以通过扩展程序进行更改,甚至可能通过JavaScript进行更改。但这与问题无关。 - AC1D

0

我找到了一个解决方案。

微软已经更新了WebView2并添加了更改用户代理的功能。但是Embarcadero尚未更新该组件。

创建新接口。

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.

创建处理程序 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;

还需要从微软更新WebView2组件。

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

如果您想更新所有接口,请阅读本文。

WebView2(TEdgeBrowser)已更新Delphi接口(例如ICoreWebView2Controller2)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接