Sign in to follow this  
Followers 0
gil900

Nead to get active url from IE

9 posts in this topic

Hello,

I made this code:

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiEdit.au3>

#include <WinAPIEx.au3>
#include <IE.au3>
#Include <Array.au3>

#AutoIt3Wrapper_Run_AU3Check=n

Test("http://www.autoitscript.com/site/")

Func Test($Url)
    Local $Form1 , $Button1 , $IE_Pid , $Close_Button , $IECreatePseudo , $oIE , $aMain , $nMsg

    #region ### START Koda GUI section ### Form=
    $Form1 = GUICreate("", 915, 664)
    $Close_Button = GUICtrlCreateButton("Close", 488, 632, 89, 25)
    GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
    $Open_Button = GUICtrlCreateButton("Open in browser", 328, 632, 105, 25)
    GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
    GUISetState(@SW_SHOW)
    $IECreatePseudo = IECreatePseudoEmbedded($Url, $Form1, 8, 8, 897, 617)
    If IsArray($IECreatePseudo) Then
        $oIE = $IECreatePseudo[0]
        $aMain = $IECreatePseudo[1]
;~      $oIE.Focus()
        If IsObj($oIE) Then
            ;GUICtrlSetPos($oIE, 0, 0, 600, 632)
            _IEAction($oIE, "focus" )
        EndIf
    EndIf

    #endregion ### END Koda GUI section ###
    WinSetOnTop($Form1, "", 1)
    ;Sleep(100)

    While 1
        ;ToolTip()
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE , $Close_Button
;~              ConsoleWrite("Test"&@CRLF)
                ;ContinueLoop

                WinClose($aMain[1])
                _IEQuit($oIE)
                If ProcessWaitClose($aMain[0],2) = 0 Then ProcessClose($aMain[0])
                ExitLoop
            Case $Open_Button

                ConsoleWrite($oIE.LocationURL&@CRLF) ; This will print the current active URL
                ShellExecute($oIE.LocationURL) ;    but not if you change the page... this is my prblem
        EndSwitch
    WEnd
    GUIDelete($Form1)
EndFunc


; #FUNCTION# ====================================================================================================================
; Name ..........: IECreatePseudoEmbedded
; Description ...: Create IE frame in GUI with the laset IE verison that installed on the computer
; Syntax ........: IECreatePseudoEmbedded($sURL, $h_Parent, $i_Left, $i_Top, $i_Width, $i_Height[, $wait = 5000])
; Parameters ....: $sURL                - The URL to load.
;                  $h_Parent            - The handle of the GUI to create the IE frame in..
;                  $i_Left              - X pos.
;                  $i_Top               - Y Pos
;                  $i_Width             - The width of the frame.
;                  $i_Height            - The height of the frame.
;                  $wait                - [optional] max time to wait for the page to load. Default is 5000 ms.
; Return values .: If error:
;                   -1 = can't open internet explorer
;                   -2 = loading time limit reached
;                   -3 = object error occurred
;               .: Id not error:
;                   Array Variable:
;                       [0] = $o_object - Object variable of an InternetExplorer.Application, Window or Frame object
;                           the Object is for IE functions in IE.au3
;                       [1] = Another array:
;                               [0] = the pid of the IE frame
;                               [1] = the handle of the IE frame
; Author ........: gil900
; Modified ......: Yes and No ;)
; Inspired from .: http://www.autoitscript.com/forum/topic/152173-pseudo-embed-google-chrome/
;                  http://www.autoitscript.com/forum/topic/138980-iecreatepseudoembedded/
;                  http://www.autoitscript.com/forum/topic/99234-iecreate2/#entry712594
;                  AutoIt3\Include\IE.au3

; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: yes
;
; ===============================================================================================================================
Func IECreatePseudoEmbedded($sURL, $h_Parent, $i_Left, $i_Top, $i_Width, $i_Height, $wait = 5000)
    Local $Output[2] = [-1,-1] , $acMain[2] , $o_IE , $timer , $GIECPE_Edit1 , $pid , $o_Shell , $o_ShellWindows , $o_window

    $GIECPE_Edit1 = GUICtrlCreateEdit("loading...", $i_Left, $i_Top, $i_Width, $i_Height,$ES_READONLY+$ES_CENTER)
    GUICtrlSetFont(-1, 35, 400, 2, "Tahoma")
    GUICtrlSetColor(-1, 0x646464)
    $pid = ShellExecute(@HomeDrive&"\Program Files\Internet Explorer\iexplore.exe","-k "&$sURL,"","",@SW_MINIMIZE)
    If $pid = 0 Then
        GUICtrlSetData($GIECPE_Edit1, "Error in opening Internet Explorer")
        Return -1
    EndIf

    $timer = TimerInit()
    While 1
        If TimerDiff($timer) >= $wait Then
            GUICtrlSetData($GIECPE_Edit1, "Error: loading time limit reached")
            ProcessClose($pid)
            Return -2
        EndIf
        $o_Shell = ObjCreate("Shell.Application")
        $o_ShellWindows = $o_Shell.Windows()
        For $o_window In $o_ShellWindows
            If $o_window.LocationURL = $sURL Then
                $o_IE = $o_window
                $Output[0] = $o_IE
                ExitLoop 2
            EndIf
        Next
    WEnd
    If Not IsObj($o_IE) Then
        GUICtrlSetData($GIECPE_Edit1, "Error: object error occurred")
        Return -3
    EndIf

    $acMain[1] = _IEPropertyGet($o_IE, "hwnd")
    $acMain[0] = WinGetProcess($acMain[1])
    $Output[1] = $acMain

    GUISetState(@SW_HIDE, $h_Parent)
    GUICtrlDelete($GIECPE_Edit1)
    _WinAPI_SetParent($acMain[1], $h_Parent)
    _WinAPI_SetWindowLong($acMain[1], $GWL_STYLE, $WS_POPUP + $WS_VISIBLE)

    GUISetState(@SW_SHOW, $h_Parent)


    ControlMove($acMain[1], "", "", $i_Left + 1, $i_Top + 1, $i_Width - 2, $i_Height - 2)
    WinActivate($acMain[1])
    Return $Output
EndFunc

The problem is with the button "Open in browser" in lines 51-55.

The button should open the active in the normal browser.

The problem is that the button not functioning properly if i change the page..

this is because in that case, $oIE.LocationURL is equal to "about:blank"

the variable $oIE.LocationURL is changing to "about:blank" instead of the new URL.

Does anyone have an idea how to solve this problem?

Thanks for helpers!

Share this post


Link to post
Share on other sites



#3 ·  Posted (edited)

You are trying to open a page between navigations?

I'd say wait for the page to finish loading before trying to get the location.

Yes.

Thank you, but what if the page loads slowly because bad Internet connection? This is not a good solution .. (I'm even not sure if it works)

I'm looking for another solution ..

 

Edited by gil900

Share this post


Link to post
Share on other sites

What do you mean by "if i change the page" ? I tried the script, navigated to forums page and clicked Open in browser, it opened to the page in the forums I was on, I even clicked on Facebook Login which sent me to Facebook and then clicked on Open in browser, that too opened like it should.

Share this post


Link to post
Share on other sites

I believe a navigation does this...

Choose url and navigate, it first goes to about:blank and then goes to url.

I could be wrong, so if you get locationurl before navigation to url all you will get is about blank.

Probably get the url of where you are going before you begin navigation if you take a look at an >old abandoned project of mine you might get an idea of how to do that, look at the ObjEvent function _Evt_BeforeNavigate2.


AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

What do you mean by "if i change the page" ? I tried the script, navigated to forums page and clicked Open in browser, it opened to the page in the forums I was on, I even clicked on Facebook Login which sent me to Facebook and then clicked on Open in browser, that too opened like it should.

For you it worked. I'm glad to hear :)

But in my case it does not always work. If the new page does not load completely, then $oIE.LocationURL = "about:blank" and as a result, that button opens in new browser the page "about:blank" instead of the new page that i navigate to.

"if i change the page" = if i navigate to another page on the site.

I hope that now you understand

Share this post


Link to post
Share on other sites

I spam navigated and it worked every single time.

Latest SciTE version and latest Beta AutoIt

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

I believe a navigation does this...

Choose url and navigate, it first goes to about:blank and then goes to url.

I could be wrong, so if you get locationurl before navigation to url all you will get is about blank.

Probably get the url of where you are going before you begin navigation if you take a look at an >old abandoned project of mine you might get an idea of how to do that, look at the ObjEvent function _Evt_BeforeNavigate2.

Thanks but I can not see how _Evt_BeforeNavigate2 is called.

I only foind this:

 

ObjEvent($oIE, "_Evt_", "DWebBrowserEvents2")

But it is not register the function _Evt_BeforeNavigate2 .

and function DWebBrowserEvents2 does not exist.

Maybe you have a mistake?

 

EDIT:

I figured that it should work (but it is not)

But this example works for me:

http://www.autoitscript.com/autoit3/docs/functions/ObjEvent.htm

I found it useful!

Thanks for direct me in the right direction!

 

 

EDIT2:

I managed to create a solution!

Check if it works correctly

#include <MsgBoxConstants.au3>


HotKeySet("{ESC}","Quit")
Example()


Func Example()
    ; Error monitoring. This will trap all COM errors while alive.
    ; This particular object is declared as local, meaning after the function returns it will not exist.
;~     Local $oErrorHandler = ObjEvent("AutoIt.Error", "_ErrFunc")
    Global $ActiveURL = "http://www.google.com/"
    ; Create Internet Explorer object
    Global $oIE = ObjCreate("InternetExplorer.Application")
    ; Check for errors
    If @error Then Return

    $oIE.Visible = True ; set visibility

    ; Custom sink object
    Local $oIEEvents = ObjEvent($oIE, "_IEEvent_", "DWebBrowserEvents2")

    ; Navigate somewhere
    $oIE.navigate($ActiveURL)
    ; Check for errors while loading
    If @error Then
        $oIE.Quit()
        Return
    EndIf

    ; Wait for page to load
    While 1
        ;If $oIE.readyState = "complete" Or $oIE.readyState = 4 Then ExitLoop
        Sleep(10)
    WEnd

    ; Wait few seconds to see if more events will be fired
    Sleep(3000)


EndFunc   ;==>Example

; BeforeNavigate2 method definition
Func _IEEvent_BeforeNavigate2($IEpDisp, $IEURL, $IEFlags, $IETargetFrameName, $IEPostData, $IEHeaders, $IECancel)
;~     ConsoleWrite("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!--BeforeNavigate2 fired--!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! " & @CRLF & _
;~             "$IEpDisp = " & $IEpDisp() & "  -  " & ObjName($IEpDisp) & @CRLF & _ ; e.g. default property and name for the object
;~             "$IEURL = " & $IEURL & @CRLF & _
;~             "$IEFlags = " & $IEFlags & @CRLF & _
;~             "$IETargetFrameName = " & $IETargetFrameName & @CRLF & _
;~             "$IEPostData = " & $IEPostData & @CRLF & _
;~             "$IEHeaders = " & $IEHeaders & @CRLF & _
;~             "$IECancel = " & $IECancel & @CRLF & _
;~             "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! " & @CRLF & @CRLF)

;~  If $oIE.LocationURL <> "about:blank" Then
;~      If $oIE.LocationURL = $IEURL Then ConsoleWrite($IEURL&@CRLF)
;~  EndIf
    If $oIE.LocationURL <> "" Then
        $ActiveURL =  $oIE.LocationURL
    EndIf

    ConsoleWrite("$ActiveURL: "&$ActiveURL&@CRLF)




EndFunc   ;==>_IEEvent_BeforeNavigate2

; User's COM error function. Will be called if COM error occurs
Func _ErrFunc($oError)
    ; Do anything here.
    ConsoleWrite(@ScriptName & " (" & $oError.scriptline & ") : ==> COM Error intercepted !" & @CRLF & _
            @TAB & "err.number is: " & @TAB & @TAB & "0x" & Hex($oError.number) & @CRLF & _
            @TAB & "err.windescription:" & @TAB & $oError.windescription & @CRLF & _
            @TAB & "err.description is: " & @TAB & $oError.description & @CRLF & _
            @TAB & "err.source is: " & @TAB & @TAB & $oError.source & @CRLF & _
            @TAB & "err.helpfile is: " & @TAB & $oError.helpfile & @CRLF & _
            @TAB & "err.helpcontext is: " & @TAB & $oError.helpcontext & @CRLF & _
            @TAB & "err.lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _
            @TAB & "err.scriptline is: " & @TAB & $oError.scriptline & @CRLF & _
            @TAB & "err.retcode is: " & @TAB & "0x" & Hex($oError.retcode) & @CRLF & @CRLF)
EndFunc   ;==>_ErrFunc

Func Quit()
    $oIE.Quit()
    Exit
EndFunc

 

EDIT3:

for some reson, the function  _IEEvent_BeforeNavigate2 is stopped to be called after some time...

i don't know why...

 

EDIT4:

I Fixed the problem

Edited by gil900

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
Sign in to follow this  
Followers 0