Jump to content

pop-ups using embeded Obj in GUI blocks script from working (onbeforeunload IE)


Hasoth
 Share

Recommended Posts

Hi,

I've encountered a problem while I tried to close popup in application that is using embeded GUI object. If site has function "onbeforeunload" asking if you really want to exit you will get a popup. In embeded gui object it completly freezes functions and I can't click/close/do anything. I've created sample code with microsoft function example and in external window. Any idea how to close popup in GUI embeded IE? I've tried navigating using _IENavigate($oIE, $sURL, 0) or  changing focus _IEaction($oIE,"focus") but nothing works.

#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <IE.au3>

HotKeySet("{F1}", "myExit")

;GUI setup
$GUI_main = GUICreate("Menu", 800, 800, -1, -1)
Global $oIE =_IECreateEmbedded()
$ObjectIE = GUICtrlCreateObj($oIE, 0, 30, 800, 770)
$cButton1 = GUICtrlCreateButton("Start test inside GUI window", 0, 0, 400, 30)
$cButton2 = GUICtrlCreateButton("Start test outside GUI window", 400, 0, 400, 30)
GUISetState(@SW_SHOW, $GUI_main)

;GUI
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $GUI_main
        Case $ObjectIE
        Case $cButton1
            fTestFunction1()
        Case $cButton2
            fTestFunction2()
    EndSwitch
WEnd

;function inside GUI
Func fTestFunction1()
    Local $sURL = "http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/refs/onbeforeunload.htm"
    $oIE.Navigate($sURL)
    _IELoadWait($oIE)
    $oIE.Navigate("https://www.google.com")
    MsgBox($MB_TOPMOST, "", "should appear while question box opened", 2)
    Sleep(2500)
    Send("!o")
EndFunc

;function outside GUI
Func fTestFunction2()
    Local $sURL = "http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/refs/onbeforeunload.htm"
    $oSecondIE = _IECreate ($sURL)
    _IELoadWait($oSecondIE)
    $oSecondIE.Navigate("https://www.google.com")
    MsgBox($MB_TOPMOST, "", "should appear while question box opened", 2)
    Sleep(2500)
    Send("!o")
EndFunc

;shortcut to exit
Func myExit()
    GUIDelete($GUI_main)
    Exit
EndFunc

 

Edited by Hasoth
Link to comment
Share on other sites

Tricky. This popup is really blocking   :)
Here is a workaround 

#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <IE.au3>

HotKeySet("{ESC}", "myExit")

;GUI setup
$GUI_main = GUICreate("Menu", 800, 800, -1, -1)
Global $oIE =_IECreateEmbedded()
$ObjectIE = GUICtrlCreateObj($oIE, 0, 30, 800, 770)
$cButton1 = GUICtrlCreateButton("Start test inside GUI window", 0, 0, 400, 30)
GUISetState(@SW_SHOW, $GUI_main)

;GUI
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $cButton1
            fTestFunction1()
    EndSwitch
WEnd

;function inside GUI
Func fTestFunction1()
    Local $sURL = "http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/refs/onbeforeunload.htm"
    $oIE.Navigate($sURL)
    _IELoadWait($oIE)
    _popup_kill()
    $oIE.Navigate("https://www.google.com")
    MsgBox($MB_TOPMOST, "", "done", 2)
    Sleep(2500)
    Send("!o")
EndFunc

Func _popup_kill()
    Local $file = @TempDir & "\popup_kill.au3"
    Local $txt =  '#include <MsgBoxConstants.au3>' & @CRLF & _
            'While 1' & @CRLF & _
            'If WinExists("[CLASS:#32770]", "Any string value") Then' & @CRLF & _
            '  $hwnd = WinGetHandle("[CLASS:#32770]", "Any string value")' & @CRLF & _
            '  MsgBox($MB_TOPMOST, "", "handle of the popup : " & $hwnd, 2)' & @CRLF & _
            '  WinActivate($hwnd)' & @CRLF & _
            '  ControlClick($hwnd, "", "[CLASS:Button; INSTANCE:1]")' & @CRLF & _
            '  Exit' & @CRLF & _
            'EndIf' & @CRLF & _
            'WEnd'
    Local $hFile = FileOpen($file, 2)
    FileWrite($hFile, $txt)
    FileClose($hFile)
    
    Local $RunScript = Run(@AutoItExe & ' /AutoIt3ExecuteScript "' & $file & '"', @TempDir)
    ProcessWait($RunScript)
    FileDelete($file)
EndFunc

;shortcut to exit
Func myExit()
    GUIDelete($GUI_main)
    Exit
EndFunc

 

Link to comment
Share on other sites

another simpler workaround could be like this:

change this code in your listing

$oIE.Navigate("https://www.google.com")

with this:

$dummy = $oIE.Navigate("https://www.google.com") + Send("!o")

and remove the Send("!o") located after the Sleep(2500) statement.

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

Even better, this solution which is not a workaround   :)

#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <IE.au3>

HotKeySet("{ESC}", "myExit")

;GUI setup
$GUI_main = GUICreate("Menu", 800, 800, -1, -1)
Global $oIE =_IECreateEmbedded()
$ObjectIE = GUICtrlCreateObj($oIE, 0, 30, 800, 770)
$cButton1 = GUICtrlCreateButton("Start test inside GUI window", 0, 0, 400, 30)
GUISetState(@SW_SHOW, $GUI_main)

;GUI
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $cButton1
            fTestFunction1()
    EndSwitch
WEnd

;function inside GUI
Func fTestFunction1()
    Local $sURL = "http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/refs/onbeforeunload.htm"
    $oIE.Navigate($sURL)
    _IELoadWait($oIE)
    _IEHeadInsertEventScript($oIE, "window", "onbeforeunload", "return;")
    Sleep(2000)
    $oIE.Navigate("https://www.google.com")
    MsgBox($MB_TOPMOST, "", "done", 2)
EndFunc


;shortcut to exit
Func myExit()
    GUIDelete($GUI_main)
    Exit
EndFunc

 

Edited by mikell
Link to comment
Share on other sites

isn't this way a bit invasive?
since the original web page is "counterfeited", also other statements (maybe necessary) that may be present in the "OnBeforeUnload" function will be nullified as well.

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

Thank you very much for help! You both helped me a lot!

I didn't know I can even use combination of navigating object with sending a key, this is brilliant!

$dummy = $oIE.Navigate("https://www.google.com") + Send("!o")

on the other hand I'll probably use mikell's method because I don't need to know if there's a need to send "o" key. I'll just prevent not wanted event.

_IEHeadInsertEventScript($oIE, "window", "onbeforeunload", "return;")

Guys you are the best :)

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...