Javascript Wrapper (similar to Node.js)

Get help with using AutoHotkey and its commands and hotkeys
Aurelain
Posts: 33
Joined: 02 Sep 2014, 15:37

Javascript Wrapper (similar to Node.js)

02 Sep 2014, 16:54

In order to add FileSystem access (and more) to Javascript, I wanted to use AutoHotkey as a proxy. In my view, this would open up a whole new universe of opportunities :).
The problem is I'm stuck at communicating synchronously between the browser (ActiveX IE) and the wrapper (AHK).

Firstly, I know we can react to events inside the browser using the ComObjConnect method. See the bare minimum code below:

Upon user event:

Code: [Select all] [Download] (Script.ahk)GeSHi © Codebox Plus

Gui Add, ActiveX, w800 h600 vwb, Shell.Explorer
wb.Navigate("about:blank")
wb.document.write("<input type='button' id='btn' value='Say Hi!'>")
btn := wb.document.All.btn
ComObjConnect(btn, "btn_")
btn_onclick() {
MsgBox, AHK says Hi!
}
Gui Show

Upon browser events, optionally triggered by Javascript:

Code: [Select all] [Expand] [Download] (Script.ahk)GeSHi © Codebox Plus



However, what I want is a more direct connection to AHK from Javascript code, so as not to rely on events (asynchronous), but on direct function calls (synchronous).
That would allow the following code in Javascript:
line 1: window.test = 1;
line 2: API.callAHK(); // here AHK applies a change of value on "window.test"
line 3: alert(window.test); // JS outputs a different value (not 1)


Things I have tried:
  1. Triggering a browser event (let's say "BeforeNavigate2") and hoping AHK catches it before the next line of code in JS. It did't, it caught it after a few miliseconds.
  2. Manually triggering the "click" event of an element that AHK has hooked. I had high hopes for this, because in Javascript the events are triggered instantaneously, even before the next line of code is processed. However, the manual trigger DID NOT arrive in AHK! See sample code below, which uses the modern IE engine:

    Code: [Select all] [Expand] [Download] (Script.ahk)GeSHi © Codebox Plus

3. Just for laughs, I tried assigning to a Javascript Function (referenced by AHK as a COM object) the value of an AHK Function. I know, a naive way of looking at things :)... It obviously didn't work.

Code: [Select all] [Download] (Script.ahk)GeSHi © Codebox Plus

Gui Add, ActiveX, w800 h600 vwb, Shell.Explorer
wb.Navigate("about:blank")
wb.document.write("<script>window.test = function(){alert('JS says Hi!');}</script><input type='button' onclick='test()' value='Say Hi!'>")
wb.document.parentwindow.test := Func("hello")
hello(){
MsgBox, AHK says Hi!
}
Gui Show


Sooo... any ideas?
lexikos
Posts: 6041
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: Javascript Wrapper (similar to Node.js)

02 Sep 2014, 18:29

Try ComDispatch by fincs (or Coco's version for AutoHotkey v2).
Aurelain
Posts: 33
Joined: 02 Sep 2014, 15:37

It works!

03 Sep 2014, 01:46

Thanks Lexikos and Coco!
I think I have everything I need to get started. I'll post a new thread if something worthwhile gets produced.
The final working sample is below:

Code: [Select all] [Expand] [Download] (Script.ahk)GeSHi © Codebox Plus


For the libraries, get them from Coco's thread.

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot] and 48 guests