How do I suppress script errors when using the WPF WebBrowser control?

前端 未结 8 1643
余生分开走
余生分开走 2020-12-02 09:34

I have a WPF application that uses the WPF WebBrowser control to display interesting web pages to our developers on a flatscreen display (like a news feed).

The tro

相关标签:
8条回答
  • 2020-12-02 10:36

    you can use this trick

    vb.net

    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
    Private Const WM_CLOSE As Short = &H10s
    

    and call last lib :

     dim hwnd 
        dim vreturnvalue
        hwnd = FindWindow(vbNullString,"script error")
        if hwnd<>0 then vreturnvalue = SendMessage(hwnd, WM_CLOSE,  &O0s, &O0s)
    
    0 讨论(0)
  • 2020-12-02 10:39

    I've also found an interesting way to disable JavaScript errors. But you need to use at least .Net Framework 4.0 because of using elegant dynamic type.

    You need to subscribe to the LoadCompleted event of the WebBrowser element:

    <WebBrowser x:Name="Browser" 
                LoadCompleted="Browser_OnLoadCompleted" />
    

    After that you need to write an event handler that looks like below:

        void Browser_OnLoadCompleted(object sender, NavigationEventArgs e)
        {
            var browser = sender as WebBrowser;
    
            if (browser == null || browser.Document == null)
                return;
    
            dynamic document = browser.Document;
    
            if (document.readyState != "complete")
                return;
    
            dynamic script = document.createElement("script");
            script.type = @"text/javascript";
            script.text = @"window.onerror = function(msg,url,line){return true;}";
            document.head.appendChild(script);
        }
    
    0 讨论(0)
提交回复
热议问题