3

訪問したときに、次を使用して date という変数を宣言するWebページがあります。

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

その日付がページの上部に表示されます。その日付変数を変更する方法はありますか? (目に見える HTML ソースだけでなく)

私は InvokeScript を試してきましたが、把握するのが難しいと感じています。誰かが知っていて、これに直接関連するいくつかの例を投稿できれば、非常に感謝しています。ありがとうございました。

4

2 に答える 2

3

JavaScript のevalを使用して任意の JavaScript コードを挿入できます。これは、任意の IE バージョンで動作します。ページに少なくとも 1 つの<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 レイト バインディングを使用して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
于 2013-10-08T07:56:39.583 に答える
2

ドキュメントによると、クライアントで定義された既存のスクリプトを呼び出す必要があります:
JavaScript:

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

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

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

eval を使用すると、匿名のJavaScript 関数を呼び出して、Web ページのコンテキスト内で独自のコードを実行できます。最後の 2 回の eval 呼び出しでは、日付を設定し、DateTime.NowJavaScript が理解できるように日付をフォーマットしています。

于 2013-10-07T14:26:40.210 に答える