SmOke_N

HTMLDocumentEvents UDF - Track IE document events

7 posts in this topic

#1 ·  Posted (edited)

I've seen a few questions since I've been back on how to handle IE events.  I've answered a couple, but the last time I answered one, they said it looked complicated.

So here's an attempt to help them and others easily track html document events (basic, 2, 3, and 4).

There are links within the source to msdn explaining the events they capture.  I've provided two "simple" examples.

Things to note:

When doing these, since they are callback methods, I wanted to treat them like we would GUIRegisterMsg a bit.

So the 3 params you'll have to have in your event handler (if you don't use the internal) are (names can be different, but the data will be specific when sent to it).

Param1: Event object variable

Param2: Target object variable (target is: object.currentTarget, object.Target, object.srcElement if you're wondering)

Param3: Type variable (to hold a string)

Take a look at the example if you have questions.

As always, if you see something that could be better or added let me know.

Changes:

;    B0.0.2    Fixed:    Error for include in examples
;    B0.0.2    Fixed:    Missing volatile functions (Major)

Downloads:

2015-01-03 HTMLDocumentEvents.B001.zip

2015-01-04 HTMLDocumentEvents.B002.zip

Edited by SmOke_N
2 people like this

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

this looks interesting!
thanks

I tried to get the innertext of the clicked object, but I do something wrong (when I click, application crashes)
here is what I tried modifyng your example without success; where am I wrong? thanks.

#include "HTMLDocumentEvents.au3"

_Example()
ConsoleWrite(@error & ":" & @extended & @CRLF)

Func _Example()

    Local $oIE = _IECreate()
    If Not IsObj($oIE) Then
        Return SetError(1, 0, 0)
    EndIf

    _IEDocWriteHTML($oIE, Example_GetHTML())

    Local $oDoc = _IEDocGetObj($oIE)

    ; use built in handler leaving out 3rd param
    ; using HTMLDocumentEvent2 (2 is the shortcut); see header
    Local $oDocEvent = _IEHTMLDocEvent_CreateListener($oDoc, 2)
    If @error Then
        Return SetError(2, @error, _IEQuit($oIE))
    EndIf

    Local $aMsg, $oElement
    Local Enum $iEvent, $iTarget, $iType
    While WinExists(HWnd($oIE.hwnd))
        $aMsg = _IEHTMLDocEvent_GetMsgArray()
        For $iMsg = 0 To UBound($aMsg) - 1
            Switch $aMsg[$iMsg][$iType]
                Case "click"
                    ConsoleWrite(@HOUR & ":" & @MIN & ":" & @SEC & "    Clicked" & @CRLF)
                    $oElement = $aMsg[$iMsg][$iTarget] ; <----- my (wrong) attempt to get innertext -----
                    ConsoleWrite($oElement.innertext) ; <---- here application crashes -----
                EndSwitch
        Next
    WEnd
EndFunc   ;==>_Example

Func Example_GetHTML()
    Local $sHTML = "0x3C21444F43545950452068746D6C3E0D0A3C68746D6C3E0D0A3C626F6"
    $sHTML &= "4793E0D0A0D0A3C646976206F6E6D6F7573656F7665723D226D4F766572287468"
    $sHTML &= "69732922206F6E6D6F7573656F75743D226D4F757428746869732922200D0A737"
    $sHTML &= "4796C653D226261636B67726F756E642D636F6C6F723A234439344133383B7769"
    $sHTML &= "6474683A31323070783B6865696768743A323070783B70616464696E673A34307"
    $sHTML &= "0783B223E0D0A4D6F757365204F766572204D653C2F6469763E0D0A0D0A3C6831"
    $sHTML &= "206F6E636C69636B3D226368616E676554657874287468697329223E436C69636"
    $sHTML &= "B206F6E20746869732074657874213C2F68313E0D0A0D0A3C646976206F6E6D6F"
    $sHTML &= "757365646F776E3D226D446F776E28746869732922206F6E6D6F75736575703D2"
    $sHTML &= "26D5570287468697329220D0A7374796C653D226261636B67726F756E642D636F"
    $sHTML &= "6C6F723A234439344133383B77696474683A393070783B6865696768743A32307"
    $sHTML &= "0783B70616464696E673A343070783B223E0D0A436C69636B204D653C2F646976"
    $sHTML &= "3E0D0A0D0A3C7363726970743E0D0A66756E6374696F6E206D4F766572286F626"
    $sHTML &= "A29207B0D0A202020206F626A2E696E6E657248544D4C203D20225468616E6B20"
    $sHTML &= "596F75220D0A7D0D0A0D0A66756E6374696F6E206D4F7574286F626A29207B0D0"
    $sHTML &= "A202020206F626A2E696E6E657248544D4C203D20224D6F757365204F76657220"
    $sHTML &= "4D65220D0A7D0D0A0D0A66756E6374696F6E206368616E6765546578742869642"
    $sHTML &= "9207B200D0A2020202069642E696E6E657248544D4C203D20224F6F6F70732122"
    $sHTML &= "3B0D0A7D0D0A0D0A66756E6374696F6E206D446F776E286F626A29207B0D0A202"
    $sHTML &= "020206F626A2E7374796C652E6261636B67726F756E64436F6C6F72203D202223"
    $sHTML &= "316563356535223B0D0A202020206F626A2E696E6E657248544D4C203D2022526"
    $sHTML &= "56C65617365204D65223B0D0A7D0D0A0D0A66756E6374696F6E206D5570286F62"
    $sHTML &= "6A29207B0D0A202020206F626A2E7374796C652E6261636B67726F756E64436F6"
    $sHTML &= "C6F723D2223443934413338223B0D0A202020206F626A2E696E6E657248544D4C"
    $sHTML &= "3D225468616E6B20596F75223B0D0A7D0D0A3C2F7363726970743E0D0A0D0A3C2"
    $sHTML &= "F626F64793E0D0A3C2F68746D6C3E20"
    Return BinaryToString($sHTML)
EndFunc   ;==>Example_GetHTML
Edited by Chimp

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

Typo in example "Example.PersonalHandler.au3"

#include "HHTMLDocumentEvents.au3"

should be

#include "HTMLDocumentEvents.au3"

 

 


My UDFs, Abbrevs and Snippets
If you like my post, just click the like button :) 

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

@Chimp, sometimes it doesn't catch the target object.  In the code, you'll see I cut that out of the handler function (checking for it).

You'll have to check the condition (as you should always anyway).  eg. If IsObj($oElement) Then

@Exit

Thanks, I actually have my example scripts in a base folder, and the code in its own HTMLDocumentEvents folder, so the extra H was a poor attempt of cleaning that up before I put it in the zip.  Thank you for noticing, I'll fix that when/if I update.

Edit:

Chimp, this fixes your issue:

If $aMsg[$iMsg][$iEvent].type Then
                        $oElement = $aMsg[$iMsg][$iTarget] ; <----- my (wrong) attempt to get innertext -----
                        ConsoleWrite($oElement.innertext) ; <---- here application crashes -----
                    EndIf

At least for me.

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

Ok, I found the issue(s).  Fixed and working well.  Download in the first post.


Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Thanks, now it seems to work....  (at least better than before :))
also I had to remove the  If $aMsg[$iMsg][$iEvent].type to allow the execution of my statement.

#include "HTMLDocumentEvents.au3"

_Example()
ConsoleWrite("Return codes: " & @error & ":" & @extended & @CRLF)

Func _Example()
    Local $oIE = _IE_Example("table")
    If Not IsObj($oIE) Then
        Return SetError(1, 0, 0)
    EndIf
    Local $oDoc = _IEDocGetObj($oIE)
    ; use built in handler leaving out 3rd param
    ; using HTMLDocumentEvent2 (2 is the shortcut); see header
    Local $oDocEvent = _IEHTMLDocEvent_CreateListener($oDoc, 2)
    If @error Then
        Return SetError(2, @error, _IEQuit($oIE))
    EndIf
    Local $aMsg
    Local Enum $iEvent, $iTarget, $iType
    While WinExists(HWnd($oIE.hwnd))
        $aMsg = _IEHTMLDocEvent_GetMsgArray()
        For $iMsg = 0 To UBound($aMsg) - 1
            Switch $aMsg[$iMsg][$iType]
                Case "click"
                    ConsoleWrite(@HOUR & ":" & @MIN & ":" & @SEC & " Clicked" & @TAB)
                    ; If $aMsg[$iMsg][$iEvent].type Then
                    ConsoleWrite($aMsg[$iMsg][$iTarget].innertext & @CRLF) ; <----
                    ; EndIf
            EndSwitch
        Next
    WEnd
EndFunc   ;==>_Example
Edited by Chimp

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

Yes, I did that (offered a solution) before I fixed the actual issue that was causing you to crash.


Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

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