Jump to content

_IENavigate_kill_popup function


Herb191
 Share

Recommended Posts

I use a lot of IE functions in my scripts and find it really annoying when I try to use _IENavigate to leave a webpage and I get popups saying things like "Are you sure you want to leave this page?". I searched the forum and could not find a solution that I liked so I created this function. I hope others find it useful and as always I am open to any constructive criticism (that's how we learn). :D

_ProcessGetParent() function taken from MrCreatoR's post (thanks MrCreatoR)

#include <IE.au3>

;This page currently has displays a popup when you try to navigate away from the page
$any_url = "aquaponics4you.com"
;$any_url = "yahoo.com"

$oIE = _IECreate($any_url)
$oIE = _IENavigate_kill_popup($oIE, "Google.com")

Func _IENavigate_kill_popup($IEObject, $URL, $visible = 1, $wait = 1)
    If Not IsObj($IEObject) Then Return SetError(1, 0, 0)
    $closed_process = False

    ;gets the IE process
    $pid = WinGetProcess(_IEPropertyGet($IEObject, "hwnd"))

    ;Trys to navigate to URL
    _IENavigate($IEObject, $URL, 0)

    ;Gets list of all process
    $aProc_List = ProcessList()

    ;Checks to see if any process were opened by the IE window. If process where opened it closes them and the IE window.
    For $i = 1 To $aProc_List[0][0]
        If _ProcessGetParent($aProc_List[$i][1]) = $pid Then
            ProcessClose($aProc_List[$i][1])
            $closed_process = True
        EndIf
    Next

    ;will create a new IE with old IE object varable if it no longer exists.
    If $closed_process = True Then
        $IEObject = _IECreate($URL, 0, $wait)
        Return $IEObject
    Else
        If $wait = 1 Then _IELoadWait($IEObject)
        Return $IEObject
    EndIf
EndFunc   ;==>_IENavigate_stop_pop

Func _ProcessGetParent($iPID)
    Local $wbemFlagReturnImmediately = 0x10
    Local $wbemFlagForwardOnly = 0x20
    Local $colItems = ""
    Local $strComputer = "localhost"

    $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
    $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_Process", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

    If IsObj($colItems) Then
        For $objItem In $colItems
            If $objItem.ProcessID = $iPID Then Return $objItem.ParentProcessID
        Next
    EndIf

    Return SetError(1, 0, 0)
EndFunc   ;==>_ProcessGetParent
Edited by Herb191
Link to comment
Share on other sites

I found some errors in my code so I changed the function so that it would work right.

#include <IE.au3>

;This page currently has displays a popup when you try to navigate away from the page
$any_url = "http://aquaponics4you.com/"
;$any_url = "http://www.yahoo.com/"

$oIE = _IECreate($any_url)
$oIE = _IENavigate_kill_popup($oIE, "http://www.google.com/")

Func _IENavigate_kill_popup($IEObject, $URL, $visible = 1)
    If Not IsObj($IEObject) Then Return SetError(1, 0, 0)
    $closed_process = False

    $list_of_windows = WinList()

    ;Trys to navigate to URL
    _IENavigate($IEObject, $URL, 0)
    _IELoadWait($IEObject, 0, 5000)

    If _IEPropertyGet($IEObject, "locationurl") <> $URL Then
        $closed_process = True
        $hwnd = _IEPropertyGet($IEObject, "hwnd")

        Do ;will loop until the IE window can close
            If WinClose($hwnd) = 1 And WinExists($hwnd) = 1 Then
                $list_of_windows2 = WinList()
                For $i = 1 To $list_of_windows2[0][0]
                    For $i2 = 1 To $list_of_windows[0][0]
                        $win_existed_before = False
                        If $list_of_windows2[$i][1] = $list_of_windows[$i2][1] Then
                            $win_existed_before = True
                            ExitLoop
                        EndIf
                    Next
                    ;if the window did not exist before it will close it
                    If $win_existed_before = False Then WinClose($list_of_windows2[$i][1])
                Next
            EndIf
        Until WinClose($hwnd) = 0
    EndIf

    ;will create a new IE object if old IE object made popups.
    If $closed_process = True Then
        $IEObject = _IECreate($URL, 0)
        Return $IEObject
    Else
        _IELoadWait($IEObject)
        Return $IEObject
    EndIf
EndFunc   ;==>_IENavigate_kill_popup
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...