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

    • 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/AutoItObject-Internal/tree/master/Examples
      Version: 2.0.0
      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.
    • xiantez
      By xiantez
      This script used to work on an older version of AutoIT. Currently I am running AutoIT v3.3.14.5 and it's failing.
      Func PublicIP() ;Post public facing IP address Local $url = 'https://www.google.com/search?client=opera&q=what+is+my+ip&sourceid=opera&ie=UTF-8&oe=UTF-8' Local $getIPaddress = BinaryToString(InetRead($url)) Local $sStart = 'clamp:2">' Local $sEnd = '</div>' Local $ipaddress = _StringBetween($getIPaddress, $sStart, $sEnd For $i In $ipaddress MsgBox(0, 'External IP', "Your public IP address is " & $i) Next EndFunc ;==>PublicIP The console output shows:
      "C:\Users\user\Documents\AutoIT\Scripts\WSI Tools.au3" (197) : ==> Variable must be of type "Object".: For $i In $ipaddress For $i In $ipaddress^ ERROR ->14:12:16 AutoIt3.exe ended.rc:1 +>14:12:16 AutoIt3Wrapper Finished. >Exit code: 1 Time: 9.811
    • tuffgong
      By tuffgong
      Good morning. I have a system I am trying to automate that works like this: user fills a column in an Excel spreadsheet with values they would like printed and saves it to a folder on their desktop, they start the script and it formats their data into a text file (adding a prefix) and sends the text file as a .bch file where it needs to go. This is working:
      #include <Array.au3> #include <Excel.au3> #include <File.au3> #include <MsgBoxConstants.au3> Global $sSTCArray Global $sFilename = @DesktopDir & "\Labels\print.txt" Global $sWorkbook = @DesktopDir & "\Labels\Labels.xlsx" Global $oExcel = _Excel_Open(False,False,False,False,True) barcodePrint() Func barcodePrint() $Read = _Excel_BookOpen($oExcel, $sWorkbook, True, False, Default, Default, Default) FileOpen($sFilename, $FO_OVERWRITE) ;Global $oWorkbook = _Excel_BookAttach($oExcel) Global $sSTCArray = _Excel_RangeRead($Read) For $i = 0 to UBound($sSTCArray, 1) - 1 FileWriteLine($sFilename, "!StaticShelving1x3_ZPL," & $sSTCArray[$i]) Next _Excel_Close($oExcel) FileMove($sFilename, "***file path***\print.bch") EndFunc However, it only works if the user first formats the spreadsheet to text. I want to automate that. From what I have read it appears AutoIt does not like formatting cells that already have values. True? Can I pull the values from an un-formatted (default GENERAL format) spreadsheet and go straight to my text file? I have also considered opening a second spreadsheet, formatting it, and copying the values over. Like this:
        
      Global $sPrefix Global $oPath Global $sSTCArray Global $sFilename = @DesktopDir & "\Labels\print.txt" Global $sWorkbook = @DesktopDir & "\Labels\Labels.xlsx" Global $oExcel = _Excel_Open(False,False,False,False,True) Global $aArray Global $bExcel _Excel_BookNew($bExcel) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Button1 $aArray = IniReadSection("***File path***\barcode.ini", "stc/rvt/mgm") $sPrefix = $aArray[1][1] $oPath = $aArray[2][1] barcodePrint() EndSwitch WEnd Func barcodePrint() $oWorkbook = _Excel_BookOpen($bExcel, @DesktopDir & "\Labels\print.xlsx") $oWorkbook.ActiveSheet.Columns("A").NumberFormat = "@" Local $Read = _Excel_BookOpen($oExcel, $sWorkbook, True, False, Default, Default, Default) $oCopy = _Excel_RangeRead($sWorkbook) _Excel_RangeWrite($oWorkbook, Default, $oCopy) FileOpen($sFilename, $FO_OVERWRITE) $sSTCArray = _Excel_RangeRead($Read,"Default","Default",3) ;_ArrayDisplay($sSTCArray) For $i = 0 to UBound($sSTCArray, 1) - 1 FileWriteLine($sFilename, $sPrefix & $sSTCArray[$i]) Next _Excel_Close($oExcel) ;FileMove($sFilename, $oPath) Exit EndFunc This does not like the formatting of $oWorkbook: "Variable must be of type 'Object'".  Do I need this second sheet? If so, how can I format it? Is there a better way to get the Excel values into a .txt file? Any ideas would be appreciated. Thanks!
    • marcoauto
      By marcoauto
      Ciao
      I would like to control an ATEM Video Mixer from autoit. I downloaded his SDK which is written in c ++ and I found the sequences to interface, but I was not able to convert the script to self.
      The instructions say to follow this sequence:
      and to connectTo with C++ is:
      string address = "192.168.1.240"; _BMDSwitcherConnectToFailure failureReason = 0; IBMDSwitcher switcher = null; var discovery = new CBMDSwitcherDiscovery(); discovery.ConnectTo(address, out switcher, out failureReason); From Blackmagic SDK:
      IBMDSwitcherDiscovery::ConnectTo method
      The ConnectTo method connects to the specified switcher and returns an IBMDSwitcher object interface for the switcher.
      Syntax HRESULT ConnectTo (string deviceAddress, IBMDSwitcher** switcherDevice, BMDSwitcherConnectToFailure* failReason); Parameters: deviceAddress in Network hostname or IP address of switcher to connect to. switcherDevice out IBMDSwitcher object interface for the connected switcher. failReason out Reason for connection failure as a BMDSwitcherConnectToFailure value. So, I have I tried these solutions but with non success:
      $DllName =@ScriptDir&"\BMDSwitcherAPI.dll" $result = DllCall($DllName, "none", "IBMDSwitcherDiscovery::ConnectTo" & @CRLF) ConsoleWrite("DLLCall Result: " & $result & @CRLF) and I have tried also create an Object (That I think is the best way solution):
      #include <MsgBoxConstants.au3> $oSwitcher=ObjCreate("IBMDSwitcher") If IsObj($oSwitcher) Then MsgBox(64, "", "Object $oSwitcher created successfully") EndIf $oAtem=ObjCreate("IBMDSwitcherDiscovery") If IsObj($oAtem) Then MsgBox(64, "", "Object $oAtem created successfully") EndIf $failureReason =ObjCreate("_BMDSwitcherConnectToFailure") If IsObj($failureReason) Then MsgBox(64, "", "Object $failureReason created successfully") EndIf $oMyError = ObjEvent("AutoIt.Error", "MyErrFunc") ; Install a custom error handler Global $errore,$oSwitcher1 $oAtem.ConnectTo("192.168.1.36",$oSwitcher,$failureReason); ; This is the custom error handler Func MyErrFunc() $HexNumber = Hex($oMyError.number, 8) MsgBox(0, "", "We intercepted a COM Error !" & @CRLF & _ "Number is: " & $HexNumber & @CRLF & _ "Windescription is: " & $oMyError.windescription & @CRLF & _ "Source is: " & $oMyError.source & @CRLF & _ "Description is: " & $oMyError.description & @CRLF & _ "Helpfile is: " & $oMyError.helpfile & @CRLF & _ "Helpcontext is: " & $oMyError.helpcontext & @CRLF & _ "Lastdllerror is: " & $oMyError.lastdllerror & @CRLF & _ "Scriptline is: " & $oMyError.scriptline) EndFunc ;==>MyErrFunc But the result is:
      We intercepted a COM Error !
      Number is: 000000A9
      Windescription is: Variable must be of type 'Object'.
      Source is: 
      Description is: 
      Helpfile is: 
      Helpcontext is: 
      Lastdllerror is: 0
      Scriptline is: 17
      The BMDSwitcherAPI.dll is registered in system. Can someone help me?
      Grazie
      Marco
    • FrancescoDiMuro
      By FrancescoDiMuro
      Good evening everyone
      I'm working on a little project of mines, and I was trying to use WMI Object.
      The question which I don't find an answer is: 
      Once I do the query with WMI Object, something like "SELECT * FROM Win32_LogonSession", instead of specify the field of the collection returned, ( i.e. $colItems.Caption ), can I loop though each property and each value of the property, writing so one row of code only?
      Hope my question was clear enough.
      Thanks in advance.

      Best Regards.
×