Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capturing Javascript Alert in Internet Explorer using VB.Net

I found a similar thread here but answer is not shared by the inquirer.

I’m using SHDocVw.InternetExplorer APIs in my Vb.Net WinForms application to record user actions from Internet Explorer in my application.

  1. How do I know that a javascript alert is opened in the Internet Explorer?
  2. How to get the text of that alert box?

I don't want to inject any javascript. Is there any way by which I can directly know about the alert opening and some way of hooking it to get its text?

EDIT 1:

WindowStateChanged event is fired when a javascript alert is opened in Internet Explorer but this event is fired in many other cases also such as opening of modal dialog window, minimize Internet Explorer etc..

like image 715
prem Avatar asked Dec 26 '22 05:12

prem


1 Answers

With below code you can auto close JavaScript alert windows, get message text or send click message to buttons on alert window.

WindowStateChanged event does not fire on JavaScript alert show (on my environment). if it fires, below code can be used.

<DllImport("user32.dll", SetLastError:=True)> _
Private Shared Function GetForegroundWindow() As IntPtr
End Function

Private Sub aaxWebBrowser1_WindowStateChanged(sender As Object, e As AxSHDocVw.DWebBrowserEvents2_WindowStateChangedEvent)
    HandleJavascriptAlertwindows(GetForegroundWindow(), False)
End Sub 

or you can use ForeGroundWindowWatcher class to catch JavaScript alert windows. if AxWebbrowser on a form you should change Me.ParentForm.Handle to Me.Handle

Private ForeGroundWindowWatcher_ As ForeGroundWindowWatcher

Private Sub StartForeGroundWatcher()
    ForeGroundWindowWatcher_ = New ForeGroundWindowWatcher
    AddHandler ForeGroundWindowWatcher_.ForegroundWindowHasChanged, AddressOf ForeGroundWindowWatcher_ForegroundWindowHasChanged
    ForeGroundWindowWatcher_.Startt()
End Sub

Private Sub StopForeGroundWatcher()
    If ForeGroundWindowWatcher_ IsNot Nothing Then
        RemoveHandler ForeGroundWindowWatcher_.ForegroundWindowHasChanged, AddressOf ForeGroundWindowWatcher_ForegroundWindowHasChanged
        ForeGroundWindowWatcher_.Stopp()
    End If
End Sub

Private Sub btnStartForeGroundWatcher_Click(sender As System.Object, e As System.EventArgs) Handles btnStartForeGroundWatcher.Click
    'Should be called inside form_load event
    StartForeGroundWatcher()
End Sub

Private Sub btnStopForeGroundWatcher_Click(sender As System.Object, e As System.EventArgs) Handles btnStopForeGroundWatcher.Click
    'Should be called inside form_Closing event
    StopForeGroundWatcher()
End Sub

Private Sub ForeGroundWindowWatcher_ForegroundWindowHasChanged(sender As Object, ForeGroundWindowHandle As IntPtr)
    HandleJavascriptAlertwindows(ForeGroundWindowHandle, False)
End Sub

Private LastForeGroundWindowHandle As IntPtr = IntPtr.Zero

Private Sub HandleJavascriptAlertwindows(ForeGroundWindowHandle As IntPtr, preventFromShowing As Boolean)
    Try
        'source http://www.vbforums.com/showthread.php?761005-WebBrowser-Control-how-to-handle-Javascript-generated-alert-window-%28onbeforeunload%29

        'Dim hwnd = FindWindow("#32770", "Windows Internet Explorer")' this finds windows from its title.
        Dim hwnd = ForeGroundWindowHandle 'GetForegroundWindow() 'get the foreground window
        If hwnd = IntPtr.Zero Then Return

        'Get forgroundwindow cvlassname. Classname of JavaScript alert windows is "#32770"
        Dim ForeGroundWindowClassName As String = ForeGroundWindowHelper.GetClassNameFromHandle(hwnd)
        Dim MeHandle As IntPtr = Me.ParentForm.Handle 'use Me.Handle if ME is a form. 
        'Debug.WriteLine(WindowClassName)

        Dim AlertMessage As String = ""

        Debug.WriteLine("ForeGroundWindowHandle: " + ForeGroundWindowHandle.ToString)
        Debug.WriteLine("MeHandle: " + MeHandle.ToString)
        Debug.WriteLine("ForeGroundWindowClassName: " + ForeGroundWindowClassName)

        If String.CompareOrdinal("#32770", ForeGroundWindowClassName) = 0 Then

            If LastForeGroundWindowHandle <> IntPtr.Zero AndAlso LastForeGroundWindowHandle <> MeHandle Then
                'Alert might be shown another webbrowser.
                Return
            End If

            For Each ch As ForeGroundWindowHelper.WindowChildInfo In ForeGroundWindowHelper.GetChildWindows(hwnd)
                Debug.WriteLine("Text: " + ch.Text)
                Debug.WriteLine("ClassName: " + ch.ClassName)
                If ch.ClassName.ToLower = "static" Then
                    AlertMessage = ch.Text
                End If

                If preventFromShowing Then

                    'close alert by sending ESC
                    SendKeys.Send("{ESC}")

                    'or

                    ''send click event to specific button
                    'If ch.ClassName = "Button" AndAlso ch.Text = "OK" Then
                    '    ForeGroundWindowHelper.SendClickEvet2Button(ch.hWnd)
                    '    Exit For
                    'End If
                End If

            Next
        End If

        Debug.WriteLine("AlertMessage: " + AlertMessage)
        LastForeGroundWindowHandle = ForeGroundWindowHandle

    Catch ex As Exception
        Debug.WriteLine(ex.Message)
    End Try
End Sub

original source Detect active window changed using C# without polling

Imports System.Runtime.InteropServices
Imports System.Text

Public Class ForeGroundWindowWatcher
    'original source https://stackoverflow.com/questions/4372055/detect-active-window-changed-using-c-sharp-without-polling
    'Stephen Lee Parker

    Public Event ForeGroundWindowHasChanged(sender As Object, ForeGroundWindowHandle As IntPtr)
    Private dele As WinEventDelegate = Nothing

    Private Const WINEVENT_OUTOFCONTEXT As UInteger = 0
    Private Const EVENT_SYSTEM_FOREGROUND As UInteger = 3
    Private m_hhook As IntPtr = IntPtr.Zero

    Public Sub Startt()

        If m_hhook <> IntPtr.Zero Then
            Return
        End If

        dele = New WinEventDelegate(AddressOf WinEventProc)
        m_hhook = SetWinEventHook(EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_FOREGROUND, IntPtr.Zero, dele, 0, 0, WINEVENT_OUTOFCONTEXT)

        If m_hhook = IntPtr.Zero Then
            Throw New Exception("SetWinEventHook failed")
        End If

    End Sub

    Public Sub Stopp()
        If m_hhook <> IntPtr.Zero Then
            UnhookWinEvent(m_hhook)
        End If
    End Sub

    Private Delegate Sub WinEventDelegate(hWinEventHook As IntPtr,
                                          eventType As UInteger,
                                          hwnd As IntPtr,
                                          idObject As Integer,
                                          idChild As Integer,
                                          dwEventThread As UInteger,
                                          dwmsEventTime As UInteger)

    <DllImport("user32.dll")> _
    Private Shared Function SetWinEventHook(eventMin As UInteger,
                                            eventMax As UInteger,
                                            hmodWinEventProc As IntPtr,
                                            lpfnWinEventProc As WinEventDelegate,
                                            idProcess As UInteger,
                                            idThread As UInteger,
                                            dwFlags As UInteger) As IntPtr
    End Function

    <DllImport("user32.dll")> _
    Private Shared Function UnhookWinEvent(hWinEventHook As IntPtr) As Boolean
    End Function

    Public Sub WinEventProc(hWinEventHook As IntPtr,
                            eventType As UInteger,
                            hwnd As IntPtr,
                            idObject As Integer,
                            idChild As Integer,
                            dwEventThread As UInteger,
                            dwmsEventTime As UInteger)

        RaiseEvent ForeGroundWindowHasChanged(Me, hwnd)

    End Sub

End Class

original source WebBrowser Control how to handle Javascript generated alert window

Imports System.Runtime.InteropServices
Imports System.Text

Public Class ForeGroundWindowHelper
    'source http://www.vbforums.com/showthread.php?761005-WebBrowser-Control-how-to-handle-Javascript-generated-alert-window-%28onbeforeunload%29
    'by AgustinTRC

    Public Class WindowChildInfo
        Public hWnd As IntPtr
        Public ClassName As String
        Public Text As String
        Public Sub New(hwnd As IntPtr, clsname As String, text As String)
            Me.hWnd = hwnd
            Me.ClassName = clsname
            Me.Text = text
        End Sub
    End Class

    Private Const BM_CLICK As Integer = &HF5
    Private Const WM_ACTIVATE As Integer = &H6
    Private Const WA_ACTIVE As Integer = 1

    <DllImport("user32.dll", CharSet:=CharSet.Auto, EntryPoint:="FindWindow")> _
    Private Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
    End Function

    <DllImport("user32.dll", EntryPoint:="SendMessage")> _
    Private Shared Function SendMessage(hwnd As IntPtr, wMsg As Integer, wParam As Integer, lParam As Integer) As IntPtr
    End Function

    ' private...
    <DllImport("user32.dll", CharSet:=CharSet.Auto)> _
    Private Shared Sub GetClassName(ByVal hWnd As System.IntPtr, ByVal lpClassName As StringBuilder, ByVal nMaxCount As Integer)
    End Sub

    <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
    Private Shared Function GetWindowText(ByVal hwnd As IntPtr, ByVal lpString As StringBuilder, ByVal cch As Integer) As Integer
    End Function

    <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
    Private Shared Function GetWindowTextLength(ByVal hwnd As IntPtr) As Integer
    End Function


    Private Delegate Function EnumCallBackDelegate(ByVal hwnd As IntPtr, ByVal lParam As IntPtr) As Integer
    Private Declare Function EnumChildWindows Lib "user32" (ByVal hWndParent As IntPtr, ByVal lpEnumFunc As EnumCallBackDelegate, ByVal lParam As IntPtr) As IntPtr


    Private Shared children As List(Of WindowChildInfo)
    Public Shared Function GetChildWindows(ByVal hwnd As IntPtr) As List(Of WindowChildInfo)
        children = New List(Of WindowChildInfo)
        EnumChildWindows(hwnd, AddressOf EnumProc, Nothing)
        Return children
    End Function

    Private Shared Function EnumProc(ByVal hwnd As IntPtr, ByVal lParam As IntPtr) As Int32
        If hwnd <> IntPtr.Zero Then
            children.Add(New WindowChildInfo(hwnd, GetClassNameFromHandle(hwnd), GetText(hwnd)))
        End If
        Return 1
    End Function

    Public Shared Function GetClassNameFromHandle(ByVal hWnd As IntPtr) As String
        Dim sbClassName As New StringBuilder("", 256)
        Call GetClassName(hWnd, sbClassName, 256)
        Return sbClassName.ToString
    End Function

    Private Shared Function GetText(ByVal hWnd As IntPtr) As String
        Dim length As Integer = GetWindowTextLength(hWnd)
        If length = 0 Then Return ""
        Dim sb As New StringBuilder("", length + 1)
        GetWindowText(hWnd, sb, sb.Capacity)
        Return sb.ToString()
    End Function

    Public Shared Sub SendClickEvet2Button(ByVal hWnd As IntPtr)
        ' activate the button
        SendMessage(hWnd, WM_ACTIVATE, WA_ACTIVE, 0)
        ' send button a click message
        SendMessage(hWnd, BM_CLICK, 0, 0)
    End Sub

End Class
like image 188
khan Avatar answered Jan 13 '23 11:01

khan