Why can AHK inject functions into IE such that they can be called from JS so easily?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
sancarn
Posts: 224
Joined: 01 Mar 2016, 14:52

Why can AHK inject functions into IE such that they can be called from JS so easily?

23 Jun 2018, 08:51

Hi all,

So last week I posted this stackoverflow question. For those who don't already know you can inject AHK functions into Internet Explorer such that they can be called from JavaScript. An Example:

Code: Select all

#Persistent

html =
(
<html>
  <head>
    <script>
      document.setVar = function`(name,val`){
        document[name]=val;
      }
    </script>
  </head>
  <body>
    <h1>Hello!</h1>
  </body>
</html>
)

ie := ComObjCreate("InternetExplorer.Application")
ie.navigate("about:blank")
sleep, 2000


msgbox, %html%
ie.document.writeln(html)
ie.visible := true
ie.document.setVar("someFunc",Func("someFunc"))


someFunc(){
  msgbox, "hello"
}
By registering someFunc we can now call it directly from JavaScript using document.someFunc()

My question is, why does this work so well?! What are the conditions that ie.document.setVar("someFunc",Func("someFunc")) actually injects the function into JS, like on a low level I mean. Does someFunc need to be wrapped in some IDispatch? Or does it have to have some other COM properties?

The reason I am asking is because I can't for the life of me figure out how I can do the same in Ruby... So far everything I have tried seemed like it could work, but nothing really did... Any ideas?

Note:
I am using Ruby on Windows (Cygwin), FYI.

EDIT:

I've also seen that Function objects are a little different in AHK. Is this part of the reason? Do passed functions have to be actual objects which have a Call() and a __Call() method?

Code: Select all

class YourClassName {
    Call(a, b) {  ; Declare parameters as needed, or an array*.
        ;...
    }
    __Call(method, args*) {
        if (method = "")  ; For %fn%() or fn.()
            return this.Call(args*)
        if (IsObject(method))  ; If this function object is being used as a method.
            return this.Call(method, args*)
    }
    ;...
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 306 guests