c# - 在 WebBrowser 控件中修改 Javascript 变量

标签 c# javascript vb.net webbrowser-control

我有一个网页,当访问该网页时,使用以下内容声明一个名为 date 的变量:

var date=new Date("03 Oct 2013 16:04:19");

然后该日期显示在页面顶部。我有办法修改那个日期变量吗? (而不仅仅是可见的 HTML 源代码)

我一直在尝试使用 InvokeScript,但发现它很难掌握,如果有人知道并可以发布一些与此直接相关的示例,我将不胜感激。谢谢。

最佳答案

您可以使用 JavaScript 的 eval 注入(inject)任何 JavaScript 代码, 它适用于任何 IE 版本。您需要确保该页面至少有一个 <script>标记,但这很简单:

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Call WebBrowser1.Navigate("http://example.com")
    End Sub

    Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        '
        ' use WebBrowser1.Document.InvokeScript to inject script
        '
        ' make sure the page has at least one script element, so eval works
        WebBrowser1.Document.Body.AppendChild(WebBrowser1.Document.CreateElement("script"))
        WebBrowser1.Document.InvokeScript("eval", New [Object]() {"(function() { window.newDate=new Date('03 Oct 2013 16:04:19'); })()"})
        Dim result As String = WebBrowser1.Document.InvokeScript("eval", New [Object]() {"(function() { return window.newDate.toString(); })()"})
        MessageBox.Show(result)

    End Sub

End Class

或者,您可以使用VB.NET 后期绑定(bind)来调用eval直接,而不是 Document.InvokeScript ,这可能更容易编码和阅读:

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Call WebBrowser1.Navigate("http://example.com")
    End Sub

    Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted

        '
        ' use VB late binding to call eval directly (seamlessly provided by.NET DLR)
        '
        Dim htmlDocument = WebBrowser1.Document.DomDocument
        Dim htmlWindow = htmlDocument.parentWindow
        ' make sure the page has at least one script element, so eval works
        htmlDocument.body.appendChild(htmlDocument.createElement("script"))
        htmlWindow.eval("var anotherDate = new Date('04 Oct 2013 16:04:19').toString()")
        MessageBox.Show(htmlWindow.anotherDate)
        ' the above shows we don't have to use JavaScript anonymous function,
        ' but it's always a good coding style to do so, to scope the context:
        htmlWindow.eval("window.createNewDate = function(){ return new Date().toString(); }")
        MessageBox.Show(htmlWindow.eval("window.createNewDate()"))

        ' we can also mix late binding and InvokeScript
        MessageBox.Show(WebBrowser1.Document.InvokeScript("createNewDate"))

    End Sub

End Class

关于c# - 在 WebBrowser 控件中修改 Javascript 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19227155/

相关文章:

c# - 在 wCF 中使用 Stream 而不是 byte[]?

javascript - 在 Web 应用程序中绘制有向无环图 - 只有奇怪的方法可用吗?

vb.net - 保护变量免受空值影响

vb.net - 在代码中初始化新标签 - 崩溃

c# - 在 .NET 3.5 C# 中,有没有办法检测 NVIDIA SLI 模式是否处于事件状态

c# - 将 3D 点投影到 2D 屏幕坐标

javascript - 如何将表单从 html 发布到 php

javascript - Node.js Promises in Promises in Promises 中的嵌套回调未解析

vb.net - 修剪图像(裁剪以删除所有空白区域)

c# - 我应该在创建任何实例之前调用 static init()