Jump to content
Hasoth

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

Recommended Posts

Hasoth

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

Share this post


Link to post
Share on other sites
mikell

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

 

Share this post


Link to post
Share on other sites
Chimp

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.

  • Like 1

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

Share this post


Link to post
Share on other sites
mikell

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
  • Like 1

Share this post


Link to post
Share on other sites
Chimp

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.


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

Share this post


Link to post
Share on other sites
mikell

Yes... but usually these statements are not so important - IMHO maybe wrong - when OnBeforeUnload is used to fire a popup

Share this post


Link to post
Share on other sites
Hasoth

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 :)

Share this post


Link to post
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

  • Similar Content

    • Fenzik
      By Fenzik
       Hello all"
      I have curious problem with com object implementation of Sapi 5.1.
      In some cases }Some Voice engines] the metods for retrieve the voice parameters fails with error :Member not exists:.
      But the Retrieved Voice object can speak the given text, so It exists and work.
      Example of this type of Engine can be this one: http://download.kobavision.be/KobaSpeech3/KobaSpeech 3 With Vocalizer Serena - English (Great Britain).exe (can work as demo)
      So my question is> Is there some way to workaround or solve this issue?
      What i tryed:
      1. Typical use of Sapi.spvoice object:
      $oMyError = ObjEvent("AutoIt.Error","MyErrFunc"); Install a custom error handler
       
        $spvoice = ObjCreate("sapi.spvoice")
      for $voice in $spvoice.getvoices()
        msgbox(0, "Voice", $voice.getdescription())
      next
      Func MyErrFunc()
      $HexNumber = hex($oMyError.number, 8)
      Msgbox(0,"","We intercepted a COM Error !" & @CRLF &"Number is: " & $HexNumber & @CRLF &"Windescription is: " & $oMyError.windescription)
      SetError(1)
      Endfunc

      2. Implement workaround based on Nvda Screen reader sapi5 Library at https://github.com/nvaccess/nvda/blob/master/source/synthDrivers/sapi5.py
      Thys code in Pascal should work, so i tryed to reproduce it in Autoit.
      Pascal code just as example:
                   SOTokens:=SpVoice.GetVoices('','');
                   for i:=0 to SOTokens.Count-1 do
                   try
                        SOToken:=SOTokens.Item(I); s:=SOToken.GetDescription(0);
      end
      In Autoit I tryed it like this:
      $oMyError = ObjEvent("AutoIt.Error","MyErrFunc"); Install a custom error handler
        $spvoice = ObjCreate("sapi.spvoice")
      for $i = 0 to $spvoice.getvoices.count-1
      $name = $spvoice.getvoices.item($i).getdescription
      msgbox(0,"Voice", $name)
      next
      Func MyErrFunc()
      $HexNumber = hex($oMyError.number, 8)
      Msgbox(0,"","We intercepted a COM Error !" & @CRLF &"Number is: " & $HexNumber & @CRLF &"Windescription is: " & $oMyError.windescription)
      SetError(1)
      Endfunc
      Both of this methods returning same Error ("Member not exists.").
      Thanks a lot for help.
      Znefyg
    • SchneiMi
      By SchneiMi
      Hello,
      following my previous question, I have moved all potentially instable object interactions into Executes. But it Looks like "="-assignments to object Attributes cannot be done with Execute, only method calls. Using an "$obj = 1" construct, it compares (Eval) instead of sets (Execute) the value.
      I have tested multiple different combinations, using Execute and Assign, but it seems not to work with object Attributes. :-(
      Gives following Output:
      The assign Action using apply (a3) Fails, while assigning it directly, without Assign() works fine.
      The execute versions compare and do not assign, in both cases. Though, the "Execute" topic in the help file says it executes, not evaluates.
      I have found a similar, old thread, which explains this behavious but does not give a solution. https://www.autoitscript.com/forum/topic/110228-pass-object-property-as-a-variable/  
      Is there a way to assign to a com object's Attribute? Or is there anything new to this unexpected behaviour of Execute (at least compared to the help file description and Python's exec).
       
      Any help is appreciated, and thank you for all the help so far.
      Regards, Michael
       
    • Simpel
      By Simpel
      Hi,
      since some days I become this error message exiting my app:

      Eventviewer shows following data:
      Name der fehlerhaften Anwendung: autoit3.exe, Version: 3.3.14.2, Zeitstempel: 0x55fc1979 Name des fehlerhaften Moduls: ntdll.dll, Version: 6.1.7601.23864, Zeitstempel: 0x595fa490 Ausnahmecode: 0xc000000d Fehleroffset: 0x000987e0 I stripped my code from 1500 lines down to 70:
      #include <GUIConstants.au3> Global $g_sPathToPDF = ; path to some pdf file to show Opt("GUIOnEventMode", 1) ; default ist 0 ; 1 bedeutet, daß bei Klick direkt die darunterbeschriebene Funktion ausgeführt wird Global $g_hGUI_MAIN ; Haupt-GUI Global $g_hDummy_Main ; Dummy um Fokus in der Haupt-GUI unsichtbar zu setzen Global $g_hGUI_Pruefen ; GUI zum Prüfen aller PDF Global $g_hGUI_PDF ; GUI PDF-Ansicht der ausgewählten PDF Global $g_hPDF ; ActiveX control welches das PDF enthält Global $g_oAcrobatReader ; AcrobatReaderObjekt in dem die PDF gezeigt werden _GUI_Main() GUISetOnEvent ($GUI_EVENT_CLOSE, "_Exit_Main" , $g_hGUI_MAIN) While 1 Sleep(1) WEnd Exit Func _GUI_Main() ; GUI-MAIN $g_hGUI_MAIN = GUICreate("MAIN", 390, 390, 763, 372) GUISetFont(12) GUICtrlCreateButton("NEXT", 20, 20, 350, 55, $BS_DEFPUSHBUTTON) ; Default-Knopf GUICtrlSetOnEvent(-1, "_GUI_Pruefen") GUISetState(@SW_SHOW, $g_hGUI_MAIN) ; GUI anzeigen EndFunc Func _GUI_Pruefen() ; GUI zum Prüfen der PDF GUISetState(@SW_HIDE, $g_hGUI_MAIN) ; MAIN-GUI ausblenden Opt("GUIOnEventMode", 0) ; wieder auf Default gesetzt $g_hGUI_Pruefen = GUICreate("RIGHT", 490,950, 1057, 91, -1, $WS_EX_APPWINDOW, $g_hGUI_MAIN) _AcrobatShow($g_sPathToPDF, "", 367, 91, 674, 950, $g_hGUI_Pruefen) ; PDF-GUI erstellen GUISetState(@SW_SHOW, $g_hGUI_Pruefen) ; GUI-Prüfen anzeigen Local $msg While 1 $msg = GuiGetMsg() ; Aktion mit der GUI registrieren Switch $msg ; je nach Aktion mit der GUI Case $GUI_EVENT_CLOSE ; X gedrückt $g_oAcrobatReader = "" ; zerstöre das Objekt AcrobatReader GUIDelete($g_hGUI_PDF) ; lösche die GUI-PDF GUIDelete($g_hGUI_Pruefen) ; lösche die GUI-Prüfen Opt("GUIOnEventMode", 1) ; Default 0 GUISetState(@SW_SHOW, $g_hGUI_MAIN) ; MAIN-GUI wieder zeigen Return EndSwitch WEnd EndFunc Func _Exit_Main() ; ausführen, wenn die MAIN-GUI schließt ConsoleWrite("EXIT" & @CRLF) Exit EndFunc Func _AcrobatShow($sFile, $sTitle = "PDF ", $iLeft = 50, $iTop = 0, $iWidth = 1000, $iHeight = 700, $hWnd = "") ; GUI-PDF erstellen If FileExists($sFile) Then ; wenn das PDF existiert $g_oAcrobatReader = ObjCreate("AcroPDF.PDF.1") $g_oAcrobatReader.src = $sFile ; Quelle ist das File $g_oAcrobatReader.SetLayoutMode("SinglePage") ; default "SinglePage" $g_oAcrobatReader.SetPageMode("none") ; default "none" $g_oAcrobatReader.SetShowToolbar(0) ; Tool-Bar nicht zeigen 0 $g_oAcrobatReader.SetShowScrollbars(0) ; Scroll-Balken nicht zeigen 0 $g_oAcrobatReader.SetView("fit") ; "fit" falls wer eigene Einstellungen im Reader gespeichert hat $g_hGUI_PDF = GUICreate($sTitle, $iWidth, $iHeight, $iLeft, $iTop, -1, -1, $hWnd) ; GUI als Child zu GUI-PRUEFEN erstellen - es soll nicht aktiviert werden $g_hPDF = GUICtrlCreateObj($g_oAcrobatReader, 0, 0, $iWidth, $iHeight) ; Objekt für das PDF erstellen GUICtrlSetStyle($g_hPDF, $WS_VISIBLE) ; PDF anzeigen GUISetState(@SW_SHOW, $g_hGUI_PDF) ; GUI-PDF anzeigen Else MsgBox(0, 'ERROR', "No PDF found.") EndIf EndFunc Do following steps to prove:
      - start app
      - click "next" on main gui
      - wait minimum 5 seconds (until the arrows left and right on "gui left" disappear)
      - close gui left or right
      - close main gui
      - look on console written "EXIT" the last code line before exit
      - now windows error message above appears
      The funny thing is if I don't wait the 5 seconds (before the half transparent arrows disappear) closing the gui then I will get no win error message.
      If I comment _AcrobatShow() out then the error never appears. So it seemed to be an acrobat reader issue. Every week at work there are a lot of updates, but there is no chance to know which one. But since one update this error happens.
      Any solutions? Regards, Conrad
    • genius257
      By genius257
      I've made a library, based on AutoItObject UDF with the goal of implementing getter and setter functionality and make it possible to define new object properties in as few steps as possible.
      Thank you to @trancexx for getting me on the right track, and all users in Hooking into the IDispatch interface for the code to get me going.
      If I've forgotten to add credit, please let me know
      Example:
      #include "AutoItObject_Internal.au3" $myCar = IDispatch() $myCar.make = 'Ford' $myCar.model = 'Mustang' $myCar.year = 1969 $myCar.__defineGetter('DisplayCar', DisplayCar) Func DisplayCar($oThis) Return 'A Beautiful ' & $oThis.parent.year & ' ' & $oThis.parent.make & ' ' & $oThis.parent.model EndFunc MsgBox(0, "", $myCar.DisplayCar) More examples: https://github.com/genius257/AutoIt-projects/tree/master/AutoItObject Internal/Examples
      Version: 1.0.3
      AutoItObject_Internal.au3
      Documentation:
       

      Edit2 (19th March 2017):
      First of all, sorry about the lack of updates on this project. I always start too many projects and end up ignoring old projects, if I run into problems ^^'.
      So I've started moving my AutoIt scripts to GitHub. I will still post the most recent script version here.
    • WoodGrain
      By WoodGrain
      Hi All,
      I've coded the small script below, but it can't seem to get the instance of Windows Media player as it keeps going to @error, I've not used com objects before so any assistance would be appreciate. I already have WMP open and minimised. I retrieved "WMPlayerApp" from the AutoIT info tool, I've included a copy below.
      I'm using these sources:
      https://msdn.microsoft.com/en-us/library/dd564085.aspx
      https://msdn.microsoft.com/en-us/library/dd564018.aspx
      $oWMP = ObjGet("", "WMPlayerApp") If @error Then MsgBox(0, "Can't get WMP", "Couldn't connect to the WMP instance") Exit EndIf $wmpPlayState = $oWMP.playState MsgBox(0, "Play State", $wmpPlayState) $wmpSongName = $oWMP.currentMedia.name MsgBox(0, "Play State", $wmpSongName) I've also seen references to the below, but I want to get an existing open WMP:
      ObjCreate("wmplayer.OCX") and have looked at the WMP.udf but can't see how it will do either of the functions I've coded above.
       
×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.