修改WebBrowser控件中的Javascript变量

时间:2022-11-16 09:28:40

I have a webpage that when visited, declares a variable called date using the following:

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

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

That date is then displayed at the top of the page. Is there a way for me to modify that date variable? (and not just the visible HTML source)

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

I have been experimenting with InvokeScript but am finding it hard to grasp, if anybody knows and could post some examples relating directly to this I'd be hugely grateful. Thankyou.

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

2 个解决方案

#1


3  

You can inject any JavaScript code using JavaScript's eval, it works with any IE version. You'd need to make sure the page has at least one <script> tag, but this is easy:

您可以使用JavaScript的eval注入任何JavaScript代码,它适用于任何IE版本。您需要确保该页面至少有一个

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

Alternatively, you can use VB.NET late binding to call eval directly, instead of Document.InvokeScript, which might be easier to code and read:

或者,您可以使用VB.NET后期绑定直接调用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

#2


2  

According to the documentation, you need to invoke an existing script defined in the client:
JavaScript:

根据文档,您需要调用客户端中定义的现有脚本:JavaScript:

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

function test(date) {
  alert(date);
  extDate = date;
}

You could also call eval and run an anonymous function. This would be the preferred method if you have no control over the page source. Essentially, you would be invoking and running code in the JavaScript interpreter.

您也可以调用eval并运行匿名函数。如果您无法控制页面源,这将是首选方法。从本质上讲,您将在JavaScript解释器中调用和运行代码。

C#:

private void InvokeTestMethod(DateTime date)
{
    if (webBrowser1.Document != null)
    {
        webBrowser1.Document.Body.AppendChild(webBrowser1.Document.CreateElement("script"));
        webBrowser1.Document.InvokeScript("eval", (Object)"(function() { window.date=new Date('03 Oct 2013 16:04:19'); })()");
        webBrowser1.Document.InvokeScript("eval", (Object)"(function() { alert(window.newDate.toString()); })()");
        webBrowser1.Document.InvokeScript("eval", (Object)"(function() { window.date=new Date('" + date.ToString("dd MMM yyyy HH:mm:ss") + "'); })()");
        webBrowser1.Document.InvokeScript("eval", (Object)"(function() { alert(window.newDate.toString()); })()");
    }
}

private void Test()
{
    InvokeTestMethod(DateTime.Now);
}

VB.NET

Private Sub InvokeTestMethod([date] As DateTime)
    If webBrowser1.Document IsNot Nothing Then
        webBrowser1.Document.Body.AppendChild(webBrowser1.Document.CreateElement("script"))
        webBrowser1.Document.InvokeScript("eval", new [Object]() {"(function() { window.date=new Date('03 Oct 2013 16:04:19'); })()"}))
        webBrowser1.Document.InvokeScript("eval", new [Object]() {"(function() { alert(window.newDate.toString()); })()"}))
        webBrowser1.Document.InvokeScript("eval", new [Object]() {"(function() { window.date=new Date('" + [date].ToString("dd MMM yyyy HH:mm:ss") + "'); })()"})
        webBrowser1.Document.InvokeScript("eval", new [Object]() {"(function() { alert(window.newDate.toString()); })()"}))
    End If
End Sub

Private Sub Test()
    InvokeTestMethod(DateTime.Now)
End Sub

http://msdn.microsoft.com/en-us/library/system.windows.forms.htmldocument.invokescript.aspx

By using eval, you can invoke anonymous JavaScript functions and run your own code within the context of the web page. In the last two calls to eval, I set the date using DateTime.Now and format the date in a way that JavaScript can understand.

通过使用eval,您可以调用匿名JavaScript函数并在Web页面的上下文中运行您自己的代码。在最后两次调用eval时,我使用DateTime.Now设置日期,并以JavaScript可以理解的方式格式化日期。

#1


3  

You can inject any JavaScript code using JavaScript's eval, it works with any IE version. You'd need to make sure the page has at least one <script> tag, but this is easy:

您可以使用JavaScript的eval注入任何JavaScript代码,它适用于任何IE版本。您需要确保该页面至少有一个

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

Alternatively, you can use VB.NET late binding to call eval directly, instead of Document.InvokeScript, which might be easier to code and read:

或者,您可以使用VB.NET后期绑定直接调用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

#2


2  

According to the documentation, you need to invoke an existing script defined in the client:
JavaScript:

根据文档,您需要调用客户端中定义的现有脚本:JavaScript:

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

function test(date) {
  alert(date);
  extDate = date;
}

You could also call eval and run an anonymous function. This would be the preferred method if you have no control over the page source. Essentially, you would be invoking and running code in the JavaScript interpreter.

您也可以调用eval并运行匿名函数。如果您无法控制页面源,这将是首选方法。从本质上讲,您将在JavaScript解释器中调用和运行代码。

C#:

private void InvokeTestMethod(DateTime date)
{
    if (webBrowser1.Document != null)
    {
        webBrowser1.Document.Body.AppendChild(webBrowser1.Document.CreateElement("script"));
        webBrowser1.Document.InvokeScript("eval", (Object)"(function() { window.date=new Date('03 Oct 2013 16:04:19'); })()");
        webBrowser1.Document.InvokeScript("eval", (Object)"(function() { alert(window.newDate.toString()); })()");
        webBrowser1.Document.InvokeScript("eval", (Object)"(function() { window.date=new Date('" + date.ToString("dd MMM yyyy HH:mm:ss") + "'); })()");
        webBrowser1.Document.InvokeScript("eval", (Object)"(function() { alert(window.newDate.toString()); })()");
    }
}

private void Test()
{
    InvokeTestMethod(DateTime.Now);
}

VB.NET

Private Sub InvokeTestMethod([date] As DateTime)
    If webBrowser1.Document IsNot Nothing Then
        webBrowser1.Document.Body.AppendChild(webBrowser1.Document.CreateElement("script"))
        webBrowser1.Document.InvokeScript("eval", new [Object]() {"(function() { window.date=new Date('03 Oct 2013 16:04:19'); })()"}))
        webBrowser1.Document.InvokeScript("eval", new [Object]() {"(function() { alert(window.newDate.toString()); })()"}))
        webBrowser1.Document.InvokeScript("eval", new [Object]() {"(function() { window.date=new Date('" + [date].ToString("dd MMM yyyy HH:mm:ss") + "'); })()"})
        webBrowser1.Document.InvokeScript("eval", new [Object]() {"(function() { alert(window.newDate.toString()); })()"}))
    End If
End Sub

Private Sub Test()
    InvokeTestMethod(DateTime.Now)
End Sub

http://msdn.microsoft.com/en-us/library/system.windows.forms.htmldocument.invokescript.aspx

By using eval, you can invoke anonymous JavaScript functions and run your own code within the context of the web page. In the last two calls to eval, I set the date using DateTime.Now and format the date in a way that JavaScript can understand.

通过使用eval,您可以调用匿名JavaScript函数并在Web页面的上下文中运行您自己的代码。在最后两次调用eval时,我使用DateTime.Now设置日期,并以JavaScript可以理解的方式格式化日期。