在 WinForms WebBrowser 控件中禁用 javascript?

问题描述 投票:0回答:3

如何在 WinForms 的 WebBrowser 上完全禁用 javascript?

c# javascript winforms webbrowser-control
3个回答
1
投票

你不能。客户端应用程序无法禁用浏览器中的 JavaScript。


0
投票

用这个你可以禁用来自 javascript 的警告

webBrowser.ScriptErrorsSuppressed = true;

0
投票

显然接受的答案是错误的。不仅因为theEric Law这么说,还因为i这么说。

我不知道 C#/WinForms 的等效项,但 WebBrowser 控件是一个标准的 Windows 组件,可以在任何语言中使用。我正在使用 Delphi 的它。

你这样做的方法是实现

IInternetSecurityManager
(这似乎已经在FCL中定义):

IInternetSecurityManager = interface
   ['{79eac9ee-baf9-11ce-8c82-00aa004ba90b}']
   function SetSecuritySite(Site: IInternetSecurityMgrSite): HResult; stdcall;
   function GetSecuritySite(out Site: IInternetSecurityMgrSite): HResult; stdcall;
   function MapUrlToZone(pwszUrl: LPCWSTR; out dwZone: DWORD; dwFlags: DWORD): HResult; stdcall;
   function GetSecurityId(pwszUrl: LPCWSTR; pbSecurityId: Pointer; var cbSecurityId: DWORD; dwReserved: DWORD): HResult; stdcall;
   function ProcessUrlAction(pwszUrl: LPCWSTR; dwAction: DWORD; pPolicy: Pointer; cbPolicy: DWORD; pContext: Pointer; cbContext: DWORD; dwFlags, dwReserved: DWORD): HResult; stdcall;
   function QueryCustomPolicy(pwszUrl: LPCWSTR; const guidKey: TGUID; out pPolicy: Pointer; out cbPolicy: DWORD; pContext: Pointer; cbContext: DWORD; dwReserved: DWORD): HResult; stdcall;
   function SetZoneMapping(dwZone: DWORD; lpszPattern: LPCWSTR; dwFlags: DWORD): HResult; stdcall;
   function GetZoneMappings(dwZone: DWORD; out enumString: IEnumString; dwFlags: DWORD): HResult; stdcall;
end;

具体是 ProcessUrlAction 方法:

确定指定操作的策略并显示用户界面(如果策略指示应查询用户)。

当浏览器询问是否允许运行脚本时 (

URLACTION_SCRIPT_RUN
),您说 “否” (
URLPOLICY_DISALLOW
)。

function TEmbeddedSecurityManager.ProcessUrlAction(pwszUrl: LPCWSTR; dwAction: DWORD; pPolicy: Pointer; cbPolicy: DWORD;
  pContext: Pointer; cbContext, dwFlags, dwReserved: DWORD): HResult;
begin
    // For other actions, defer to the default Internet Explorer security manager
    Result := INET_E_DEFAULT_ACTION;

{
    From Eric Law (yes, *the* Eric Law):

        https://stackoverflow.com/a/10623739/12597
        You can control the features of the Web Browser control using an IInternetSecurityManager and/or the ambient flags.
        – EricLaw,  Sep 5, 2013 at 23:10
}

    // URLACTION_SCRIPT_RUN corresponds to the action of running scripts (JavaScript)
    if dwAction = URLACTION_SCRIPT_RUN then
    begin
        // Set policy to URLPOLICY_DISALLOW to block running scripts
        if cbPolicy >= SizeOf(DWORD) then
        begin
            PDWORD(pPolicy)^ := URLPOLICY_DISALLOW;
            Result := S_OK;
        end
        else
            Result := E_FAIL;
    end;
end;

当浏览器在

QueryService
期间请求时,您向浏览器提供 IInternetSecurityManager 的实现:

function TfrmTextViewer.wbQueryService(const rsid, iid: TGUID; out Obj: IInterface): HRESULT;
var
    sam: IInternetSecurityManager;
begin
    Result := E_NOINTERFACE;

    //rsid ==> Service Identifier
    //iid  ==> Interface identifier
    if IsEqualGUID(rsid, IInternetSecurityManager) and IsEqualGUID(iid, IInternetSecurityManager) then
    begin
        sam := TEmbeddedSecurityManager.Create;
        Obj := sam;
        Result := S_OK;
    end;
end;

我会让不是我的人将所有内容翻译成 C#。

© www.soinside.com 2019 - 2024. All rights reserved.