none
求助:向Webbrowser注入脚本代码后出现权限访问问题 RRS feed

  • 问题

  • Private Sub SetAOnLoadScript()
            Static Setted As Boolean
            If Setted = False Then
                Dim Reader As IO.StreamReader = New IO.StreamReader(WebBrowser1.DocumentStream, System.Text.Encoding.GetEncoding("GBK"))
                Dim HtmlText As String = Reader.ReadToEnd()
                
                If Strings.InStr(HtmlText, "window.onload=loadComplete;") = 0 Then
                    Dim basehref As String = "<base href=""http://" & WebBrowser1.Url.DnsSafeHost & """>"
                    HtmlText = HtmlText.Replace("<HEAD>", "<HEAD>" & basehref)
    
    
                    Dim ScriptText As String =
                        "<script>" &
                        "function loadComplete()" &
                        "{" &
                        "window.external.LogonSubPlatform();" &
                        "}" &
                        "window.onload=loadComplete;" &
                        "</script>"
    
                    HtmlText = HtmlText.Replace("</HEAD>", ScriptText & "</HEAD>")
                    WebBrowser1.Stop()
                    WebBrowser1.DocumentText = HtmlText
                    Setted = True
                    WaitScriptTimerA = New Timer With {.Interval = 2000, .Enabled = True}
                End If
            End If
        End Sub
        Public Sub LogonSubPlatform()
            'MsgBox("LoadComplete")
            LoadComplete = True
        End Sub

    运行后可以成功调用函数LogonSubPlatform,但是此时在LogonSubPlatform中不能访问Webbrowser控件的内容,以下代码会出现拒绝访问的错误

    Dim a = WebBrowser1.Document.Window.Frames()
                    For Each F As HtmlWindow In a
                        Debug.Print(F.Document.All.Count)
                    Next
    求助求助啊,各位!!


    编程是永无止境的,向大家学习

    2012年10月22日 10:01

答案