你怎么能编程方式检测如果JavaScript启用/禁用的Windows桌面应用程序? (WebBrowser控件)你怎么、控件、应用程序、桌面

2023-09-03 00:27:01 作者:╰我的心好冷额

我有写HTML到WebBrowser控件在.NET的WinForms应用程序的应用程序。

I have an application which writes HTML to a WebBrowser control in a .NET winforms application.

我想检测某种方式编程如果互联网设置有使用Javascript(或活动脚本,而)禁用。

I want to detect somehow programatically if the Internet Settings have Javascript (or Active Scripting rather) disabled.

我猜你需要使用类似的WMI查询IE的安全设置。

I'm guessing you need to use something like WMI to query the IE Security Settings.

编辑#1:这一点很重要,我知道如果启动Javascript显示HTML,这样它修改DOM解决方案,以显示一个警告,或者使用标签并不适用于我的具体情况之前。在我的情况下,如果JavaScript不可用,我会显示在本地RichTextBox的内容,而不是,我也想报告JS是否启用回服务器应用程序,这样我可以告诉谁发出警报管理员5%或75%用户有JS启用。

EDIT #1: It's important I know if javascript is enabled prior to displaying the HTML so solutions which modify the DOM to display a warning or that use tags are not applicable in my particular case. In my case, if javascript isn't available i'll display content in a native RichTextBox instead and I also want to report whether JS is enabled back to the server application so I can tell the admin who sends alerts that 5% or 75% of users have JS enabled or not.

推荐答案

感谢@ Kickaha的建议。下面是该检查注册表,看它是否被设置的简单方法。也许某些情况下,这可能会抛出一个异常,所以一定要处理。

Thanks to @Kickaha's suggestion. Here's a simple method which checks the registry to see if it's set. Probably some cases where this could throw an exception so be sure to handle them.

    const string DWORD_FOR_ACTIVE_SCRIPTING = "1400";
    const string VALUE_FOR_DISABLED = "3";
    const string VALUE_FOR_ENABLED = "0";

    public static bool IsJavascriptEnabled( )
    {
        bool retVal = true;
        //get the registry key for Zone 3(Internet Zone)
        Microsoft.Win32.RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\3", true);

        if (key != null)
        {
            Object value = key.GetValue(DWORD_FOR_ACTIVE_SCRIPTING, VALUE_FOR_ENABLED);
            if( value.ToString().Equals(VALUE_FOR_DISABLED) )
            {
                retVal = false;
            }
        }
        return retVal;
    }

注:保留此code样品短(因为我只关心Internet区域)的利益 - 这种方法只检查Internet区域。您可以修改3处OpenSubKey行尾来改变该区域。

Note: in the interest of keep this code sample short (and because I only cared about the Internet Zone) - this method only checks the internet zone. You can modify the 3 at end of OpenSubKey line to change the zone.