Catch event info from IE/JavaScript COM

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Bruttosozialprodukt
Posts: 463
Joined: 24 Jan 2014, 22:28

Catch event info from IE/JavaScript COM

09 Jan 2016, 13:33

So I managed to register a mousemove event in an existing IE window, but every time my function gets called, I don't get the event info that you would normally get with Javascript. A mousemove event for instance should send info about the current mouse position to the callback function.

This is basically what I'm doing atm:

Code: Select all

window.call___back := Func("msg")
window.addEventListener("mousemove",window.call___back)

msg(e:="") {
    TrayTip, Mouse position on page, % "x: " e.pageX "`ny: " e.pageY
}
The TrayTip never shows any coordinates and I'm assuming e was simply not sent to the function.

Any idea how I could get the missing event info?
(I'm not interested getting the info in JavaScript btw. It has to be the AHK function.)

Here is the whole script so far:
(Open an IE window, navigate to a website, make sure the window is active, press F3 and move the mouse over the page.)

Code: Select all

#Persistent
active:={ie:IeGet()}
ActiveIeMonitorWithParam := Func("ActiveIeMonitor").Bind(active)
SetTimer, % ActiveIeMonitorWithParam, 100

width := 400
height := 200
Gui, +AlwaysOnTop +hwndHWND
Gui, Add, Edit, vGUI_InfoBox w%width% h%height%
Gui, Show, % "x" A_ScreenWidth-width-30 " y" 5, Bruttos iWB2 Learner
WinSet, Transparent, 200, ahk_id %HWND%

Return

F3::
    IE := active.ie
    If !ComObjType(IE,"IID")
        Return
    doc := IE.document
    document := doc.documentElement
    window := ComObj(9,ComObjQuery(IE,"{332C4427-26CB-11D0-B483-00C04FD90119}","{332C4427-26CB-11D0-B483-00C04FD90119}"),1)
    info := "Title: " doc.title "`n"
    info .= "Url: " IE.locationUrl "`n"
        
    ;window.eval("alert('injected javascript!');")
    ;window.eval("window.addEventListener('click',call___back);")
    
    window.call___back := Func("msg")
    window.addEventListener("mousemove",window.call___back)
    
    GuiControl,, GUI_InfoBox, % info
Return

msg(e:="") {
    Global document
    traytip, Mouse position on page, % "x: " e.pageX "`ny: " e.pageY
    ;MsgBox % document.elementFromPoint(e.pageX, e.pageY).innerHTML
}

IeGet(hWnd:=0) {
    WinGetTitle, title, % (hWnd ? "ahk_id " hWnd : "A")
    For window in ComObjCreate("Shell.Application").windows
        If (InStr(window.fullName, "iexplore.exe") && window.document.title . " - Internet Explorer" = title)
            Return window
    Return {}
}

ActiveIeMonitor(ByRef active) { ;we're using active.ie because it doesn't appear to be possible to pass a COM object directly using .Bind and SetTimer...
    active.ie := IeGet()
}

GuiClose() {
    ExitApp
}
User avatar
Blackholyman
Posts: 1293
Joined: 29 Sep 2013, 22:57
Location: Denmark
Contact:

Re: Catch event info from IE/JavaScript COM

10 Jan 2016, 07:25

did not have time to really test it

but but did try isobject(e) did return 0 so your function does not seem to get the event object
Also check out:
Courses on AutoHotkey

My Autohotkey Blog
:dance:
Bruttosozialprodukt
Posts: 463
Joined: 24 Jan 2014, 22:28

Re: Catch event info from IE/JavaScript COM

13 Jan 2016, 06:21

I'm pretty sure nothing gets passed to the function. But there has to be some way to make it happen. :/
I also checked if a second parameter is passed. But the answer is no.
I mean, if you only write:

Code: Select all

msg(e) {
    Global document
    traytip, Mouse position on page, % "x: " e.pageX "`ny: " e.pageY
}
nothing happens at all, clearly indicating that the required argument was not passed.

I did something similar to this using a ScriptControl. And that required using .AddObject() and ComDispatch()...

Maybe I need something similar for the IE object?
Coco
Posts: 771
Joined: 29 Sep 2013, 20:37
Contact:

Re: Catch event info from IE/JavaScript COM

13 Jan 2016, 09:05

It seems that the argument(s) are not getting passed unless you specify a reference to a JavaScript function. A solution is to use a JavaScript function as the listener and from there call your AHK function. Something like:

Code: Select all

; Inject JavaScript
window.eval("function eventListener(listener) { return function(e) { return listener(e); }; }")
; You can dynamically specify any AHK function
window.addEventListener("mousemove", window.eventListener(Func("OnMouseMove")))
Bruttosozialprodukt
Posts: 463
Joined: 24 Jan 2014, 22:28

Re: Catch event info from IE/JavaScript COM

14 Jan 2016, 17:19

I'm not 100% happy with that, but it will do the job for now. Thank you Coco. :)
It does a pretty good job in action: https://autohotkey.com/boards/viewtopic.php?p=66826

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 188 guests