VB.NET WebBrowser禁用javascript

发布于 2024-10-01 13:55:04 字数 47 浏览 0 评论 0原文

有没有办法在 vb.net 中禁用 javascript webbrowser?

Is there a way to disable javascript webbrowser in vb.net?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

江城子 2024-10-08 13:55:04

对我有用:

Private Function TrimScript(ByVal htmlDocText As String) As String


    While htmlDocText.ToLower().IndexOf("<script type=""text/javascript"">") > -1
        Dim s_index As Integer = htmlDocText.ToLower().IndexOf("<script type=""text/javascript"">")
        Dim e_index As Integer = htmlDocText.ToLower().IndexOf("</script>")
        htmlDocText = htmlDocText.Remove(s_index, e_index - s_index)
    End While
    Return htmlDocText

End Function

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim webClient As New System.Net.WebClient
    Dim result As String = webClient.DownloadString(yourUrl)
    Dim wb As New WebBrowser
    wb.Navigate("")
    Do While wb.ReadyState <> WebBrowserReadyState.Complete
        Application.DoEvents()
    Loop
    Dim script As String = TrimScript(result)
    wb.DocumentText = script

End Sub

works for me:

Private Function TrimScript(ByVal htmlDocText As String) As String


    While htmlDocText.ToLower().IndexOf("<script type=""text/javascript"">") > -1
        Dim s_index As Integer = htmlDocText.ToLower().IndexOf("<script type=""text/javascript"">")
        Dim e_index As Integer = htmlDocText.ToLower().IndexOf("</script>")
        htmlDocText = htmlDocText.Remove(s_index, e_index - s_index)
    End While
    Return htmlDocText

End Function

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim webClient As New System.Net.WebClient
    Dim result As String = webClient.DownloadString(yourUrl)
    Dim wb As New WebBrowser
    wb.Navigate("")
    Do While wb.ReadyState <> WebBrowserReadyState.Complete
        Application.DoEvents()
    Loop
    Dim script As String = TrimScript(result)
    wb.DocumentText = script

End Sub
吖咩 2024-10-08 13:55:04

简短的答案是:不。

稍长的答案是:不,网络浏览器控制 API 不允许禁用标准浏览器功能。

The short answer is: No.

The slightly longer answer is: No, the web-browser control API does not allow disabling standard browser functionality.

初见终念 2024-10-08 13:55:04

不,真的......但是如果您收到弹出的烦人的错误消息,提示脚本正在运行,那么您可以将网络浏览器的抑制错误属性设置为“true”

No really...but if you getting that annoying error message that pops up saying a script is running then you can turn the property of the webbrowser's suppress-errors "true"

酒几许 2024-10-08 13:55:04

您想禁用哪个弹出消息?如果是警报消息,请尝试此操作,显然是根据您的特定需求解析窗口或框架对象,我刚刚假设了顶级文档,但如果您需要 iframe,则可以使用 window.frames(0) 访问它。对于第一帧等等...(关于 JavaScript 部分)...这里是一些代码,假设 WB 是您的网络浏览器控件...

WB.Document.parentWindow.execScript "window.alert = function () { } ;", "JScript"

您必须在整个页面加载完成后才运行上面的代码,我知道这是很难做到的(并且完整的版本尚未发布)但是我一直在这样做(完整的证据)现在已经有一段时间了,如果您阅读我之前标记为“webbrowser”和“webbrowser-control”的一些答案,您可以收集有关如何准确执行此操作的提示,但如果您愿意,请回到手头的问题要取消 .confirm JavaScript 消息,只需将 window.alert 替换为 window.confirm(当然,用正确的对象限定您的 window.对象以到达您正在使用的文档层次结构)。您还可以使用上述技术和新的 IE9 .prompt 方法禁用 .print 方法。

如果要完全禁用 JavaScript,可以使用注册表来执行此操作,并且必须在 Web 浏览器控件加载到内存中之前更改注册表,并且每次更改它(打开和关闭)时都必须重新加载 Web 浏览器控件出入内存(或者只是重新启动您的应用程序)。

注册表项为 \HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\ - 键名为 1400,禁用它的值为 3,然后启用它是0。

当然,因为Zones键下有5个区域,所以您需要为活动区域或所有区域更改它以确保确定。但是,如果您只想抑制 js 对话框弹出消息,则实际上不需要这样做。

让我知道你进展如何,以及我是否可以提供进一步的帮助。

Which popup message do you want to disable? If it's the alert message, try this, obviously resolving the window or frame object to your particular needs, I’ve just assumed top-level document, but if you need an iframe you can access it using window.frames(0). for the first frame and so on... (re the JavaScript part)... here is some code, assuming WB is your webbrowser control...

WB.Document.parentWindow.execScript "window.alert = function () { };", "JScript"

You must run the above code only after the entire page is done loading, i understand this is very difficult to do (and a full-proof version hasn't been published yet) however I have been doing it (full proof) for some time now, and you can gather hints on how to do this accurately if you read some of my previous answers labelled "webbrowser" and "webbrowser-control", but getting back to the question at hand, if you want to cancel the .confirm JavaScript message, just replace window.alert with window.confirm (of course, qualifying your window. object with the correct object to reach the document hierarchy you are working with). You can also disable the .print method with the above technique and the new IE9 .prompt method as well.

If you want to disable JavaScript entirely, you can use the registry to do this, and you must make the registry change before the webbrowser control loads into memory, and every time you change it (on & off) you must reload the webbrowser control out and into memory (or just restart your application).

The registry key is \HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\ - the keyname is 1400 and the value to disable it is 3, and to enable it is 0.

Of course, because there are 5 zones under the Zones key, you need to either change it for the active zone or for all zones to be sure. However, you really don't need to do this if all you want to do si supress js dialog popup messages.

Let me know how you go, and if I can help further.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文