Jump to content

Autoit <-> Javascript roundtrip


Gianni
 Share

Recommended Posts

How can I do to let return a value from javascript to autoit?

I would like to use some javascript functions, passing values from AutoIt and get result back to autoit,
for example, this little snippet formats a number passed from autoit and show the result in an javascript alert box, but nothing is returned back to autoit.

#include <ie.au3>
Global $oIE = _IECreate("about:blank", 0, 0, 1, 0) ; use an hidden ie instance as a javascript "exposer"
$r = 1000000
$result = $oIE.document.parentwindow.execscript('alert(parseInt( ' & $r & ' ).toLocaleString());')
MsgBox(0, 0, $result)

any suggestions on how to have the result returned back to autoit is welcome
(avoiding temporary storage on phantom hidden fields or such alchemies please)
Thank you

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

Hi Iczer
I had a look in your udf, but have not found there a way to easly call a javascript function and get back the result to AutoIt.
(even the function _ffcmd() from the ff.au3 seems a bit "hard coded" to me)
thanks for the replay

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

I assume with execscript that in the javascript you have to do a return "hello world".

I am not sure what you mean with your alchemy part but this is another way of having your things done

with AutoIT you can enter in any browser in any addressbar a bookmarklet/favlet short piece of javascript

javascript:<script>;void(0);

the void(0) is there that the active page does not get screwed

as you can do anything in those short scripts you can add an inputbox at the bottom of the page of pixelsize 1 by 1.

in the javascript you put your information in that inputbox which you then can read with regular autoit functions.

if you do not like that approach you allways can do an alert function in the addressbar and then read the modal dialog and click away the ok button

so type on any page this in your addressbar (with copy paste you loose javascript: so reenter that manually)

javascript:alert(document.body.innerHTML);void(0);

 

Link to comment
Share on other sites

Thanks @junkew for your tips, I will give it a try.
...I still wonder if there is some other way to get results from javascript functions returned directly from the javascript function itself instead of from a "middleman" field (inputbox, alert.. or similar brokers)...

Edited by Chimp

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

  • 1 year later...

..... I had given up on this concept, but coming again on this, I see that I was very close to the goal,

I've a way to build a bridge between AutoIt and Javascript.

in this way also the many external javascript library could be easly used from AutoIt.

here a basic example on how to use native javascript functions from AutoIt:

#include <IE.au3>
; *** create a minimal 'html' page listing for the browser
Local $sHTML = "<HTML><HEAD><script>" & @CRLF ; html opening tags
; here, within the HEAD tag, we insert the javascript code
; sence the .execscript method is no longer supported (https://msdn.microsoft.com/en-us/library/ms536420(v=vs.85).aspx)
; we have to use the eval method instead. A reference to the global object is needed to access the eval method. See here:
; http://stackoverflow.com/questions/9642491/getting-a-reference-to-the-global-object-in-an-unknown-environment-in-strict-mod
; http://perfectionkills.com/unnecessarily-comprehensive-look-into-a-rather-insignificant-issue-of-global-objects-creation/#ecmascript_5_strict_mode
$sHTML &= "var global = (1,eval)('this');" & @CRLF ; the 'global' variable get a handle to the global object

$sHTML &= "</script></HEAD></HTML>" & @CRLF ; html closing tags
; *** end of html page listing

Local $oIE = _IECreate("about:blank",0,0,0) ; an hidden browser (used as javascript engine provider)

_IEDocWriteHTML($oIE, $sHTML) ; inject above html (and library) in browser
_IEAction($oIE, "refresh")

; https://msdn.microsoft.com/en-us/library/52f50e9t(v=vs.94).aspx
Local $ohJS = $oIE.document.parentwindow.global ; $ohJS is a reference to the Global Obj
; ---- now the javascript engine can be used in our AutoIt script using the $ohJS reference ----

; example of use:
$i = 1000000 ; an unformatted number
; we use a javascript method to format our number
; and we get the result from javascript directly in an AutoIt variable by the javascript eval method ...
Local $result = $ohJS.eval('parseInt( ' & $i & ' ).toLocaleString();')
MsgBox(0,"Debug", $result)

_IEQuit($oIE)

Here, another example on how to use an external javascript library called moment.js.
it's a library similar to the #include <date.au3> but written in javascript.
you have to download the library and save it in the same path of this script before you run it:

#include <IE.au3>
;
; to load the moment.js library, get it from the site: http://momentjs.com/
; and save it to the script path prior to run this script
;
Local $sJScript = FileRead(".\moment.js") ; load library from disk
; you could also get it 'on the fly' from the net using InetRead() like this:
; Local $dJScript = InetRead("http://momentjs.com/downloads/moment.js")
; Local $sJScript = BinaryToString($dJScript)

; *** create a minimal 'html' page listing for the browser
Local $sHTML = "<HTML><HEAD><script>" & @CRLF ; html opening tags
;
; here, within the HEAD tag, we insert the javascript code
;
; sence the .execscript method is no longer supported (https://msdn.microsoft.com/en-us/library/ms536420(v=vs.85).aspx)
; we have to use the eval method instead. A reference to the global object is needed to access the eval method.
; see here:
; http://stackoverflow.com/questions/9642491/getting-a-reference-to-the-global-object-in-an-unknown-environment-in-strict-mod
; http://perfectionkills.com/unnecessarily-comprehensive-look-into-a-rather-insignificant-issue-of-global-objects-creation/#ecmascript_5_strict_mode
$sHTML &= "var global = (1,eval)('this');" & @CRLF ; the 'global' variable get a handle to the global object

; here we include the moment.js library within the HEAD tag (something like #include in AutoIt)
$sHTML &= $sJScript  & @CRLF

$sHTML &= "</script></HEAD></HTML>" & @CRLF ; html closing tags
; *** end of html page listing

Local $oIE = _IECreate("about:blank",0,0,0) ; an hidden browser (used as javascript engine provider)

_IEDocWriteHTML($oIE, $sHTML) ; inject above html (and library) in browser
_IEAction($oIE, "refresh")

; https://msdn.microsoft.com/en-us/library/52f50e9t(v=vs.94).aspx
Local $ohJS = $oIE.document.parentwindow.global ; $ohJS is a reference to the Global Obj

; ---- now the javascript engine can be used in our AutoIt script using the $ohJS reference ----

; example of use:

$i = 1000000 ; an unformatted number

; we use a javascript method to format our number
; and we get the result from javascript directly in an AutoIt variable by the javascript eval method ...
Local $result = $ohJS.eval('parseInt( ' & $i & ' ).toLocaleString();')
MsgBox(0,"Debug", $result)

; here we use a function from the external moment.js library included above...
$result = $ohJS.eval("moment().format('LLLL');")
MsgBox(0, "Debug", $result)

_IEQuit($oIE)

I've tested those 2 draft scripts only on ie9 at the moment, but should work even on ie11 since I use the eval method instead of the now unsupported execScript method.

those 2 examples are just a proof of concept, and I think that can be improved, but in my opinion this "bridge" is an interesting tool....

improvements and suggestions are welcome.
Thank You everybody.

Edited by Chimp

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

If you are only trying to get JavaScript returns with AutoIt I would suggest ScriptControl.

Example:

$oSC = ObjCreate("ScriptControl")
$oSC.Language = "JScript"
$oSC.AddCode("var testvar={a:123,b:456,c:789}"); or could be FileRead like: $oSC.AddCode(FileRead("script.js"))
$testvar = $oSC.Eval("testvar")
MsgBox(0, "", "testvar.a: "&$testvar.a&@CRLF&"testvar.b: "&$testvar.b&@CRLF&"testvar.c: "&$testvar.c)
MsgBox(0, "", "testvar.a+testvar.b+testvar.c = "&$oSC.Eval("testvar.a+testvar.b+testvar.c"))

Additional information here: Using the ScriptControl

Here's a finished solution:

 

Link to comment
Share on other sites

Waw! definitely interesting! Thanks alot @genius257  for the valuable information, the useful example (and the very interesting links). appreciated very much!

nonetheless, I also like the 'bridge' across Javascript and AutoIt as built ln my post above.

...What pro and/or cons (if any) on using the ScriptControl vs the explorer object and viceversa....? :think:

Just to not leave that script from post#7 'incomplete', I've to say that it was not working on win7 x64 + ie11, so here is a modified version that worked also on ie11.

Local $dJScript = InetRead("http://momentjs.com/downloads/moment.js") ; get a library from the net
Local $sJScript = BinaryToString($dJScript)
; Local $sJScript = FileRead(".\moment.js") ; or load library from disk

; *** create a minimal 'html' page listing for the browser
Local $sHTML = "<HTML><HEAD>" & @CRLF
$sHTML &= "<script>" & @CRLF ; Javascripts goes here
; $sHTML &= '"use strict";' & @CRLF ;
$sHTML &= 'var JSglobal = (1,eval)("this");' & @CRLF ; the 'global' variable get a handle to the javascript global object
$sHTML &= $sJScript & @CRLF ; #include <moment.js>
$sHTML &= "</script>" & @CRLF
$sHTML &= "</HEAD></HTML>" & @CRLF ; html closing tags
; *** end of html page listing

Local $oIE = ObjCreate("Shell.Explorer.2")
GUICreate("", 10, 10, @DesktopWidth + 10, @DesktopHeight + 10) ; place the gui out of screen
GUICtrlCreateObj($oIE, 0, 0, 10, 10) ; this render $oIE usable
GUISetState(@SW_HIDE) ; hide GUI

$oIE.navigate('about:blank')
While Not String($oIE.readyState) = 'complete' ; wait for about:blank
    Sleep(100)
WEnd

$oIE.document.Write($sHTML) ; inject lising directly to the HTML document:
$oIE.document.close() ; close the write stream
$oIE.document.execCommand("Refresh")

; this waits till the document is ready to be used (portion of code from IE.au3)
While Not (String($oIE.readyState) = "complete" Or $oIE.readyState = 4)
    Sleep(100)
WEnd
While Not (String($oIE.document.readyState) = "complete" Or $oIE.document.readyState = 4)
    Sleep(100)
WEnd

; https://msdn.microsoft.com/en-us/library/52f50e9t(v=vs.94).aspx
Local $ohJS = $oIE.document.parentwindow.JSglobal ; $ohJS is a reference to the javascript Global Obj
; ---- now the javascript engine can be used in our AutoIt script using the $ohJS reference ----

; simple example of use:
Local $i = 1000000 ; an unformatted number
; we use native javascript method to format our number
; and we get the result from javascript directly in an AutoIt variable by the javascript eval method ...
Local $result = $ohJS.eval('parseInt( ' & $i & ' ).toLocaleString();')
MsgBox(0, "Debug", $result)

; here we use a function from the 'external' moment.js library (included above...)
$result = $ohJS.eval("moment().format('LLLL');")
MsgBox(0, "Debug", $result)

 

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

43 minutes ago, Chimp said:

What pro and/or cons (if any) on using the ScriptControl vs the explorer object and viceversa....? :think:

ScriptControl is missing normal stuff from browser js sutch as native JSON (you can add it with a script). I remember having issues with trying to implement a script for string compression, but a few line changes to scriptcontrol here and there and it worked :) (It was lz-string.js and i believe it was some bitwise functionality).

A big pro with scriptcontrol is the functionallity with something like AutoIt. Using AutoItObject.Au3, you can call AutoIt functions from within Javascript functions. you are also able to add elements from AutoIt in Javascript, however things like AutoIt arrays are Unknown type in Javascript.

But if you are looking for webpage manipulation or such, IE is best.

I would advise to play around with it if you wish :)

Personally i love importing javascript elements into AutoIt, though in some cases the speed of AutoIt is more important than the flexibility of Javascript

 

Edit:

Here is a pdf export of my onenote page with scriptcontrol (seems better than browsing 3-4 pages for the same info)

 

ScriptControl.pdf

Edited by genius257
Link to comment
Share on other sites

Javascript Array to AutoIt

... thanks to this JavaScript Object JSON, and to this method specifically JSON.stringify, it's possible (from IE9  and newer) to transfer an array from Javascript to AutoIt. (and eventually  using this other nice Json.au3 udf you can then recreate the same array in AutoIt)

This following simple script performs what said above, but, running the same script more times, it happens that sometime it works well, and sometime it fails on the JSON.stringify method on line 60. (I'm testing on win7 x64 IE9)

Anyone have any idea why this random error happens or what I'm doing wrong?
Thanks for any hint.

#include <AutoItConstants.au3>
; #include <array.au3>
; #include <Json.au3>

; *** create a minimal 'html' page listing for the browser
Local $sHTML = "<HTML><HEAD>" & @CRLF
$sHTML &= "<script>" & @CRLF ; Javascripts goes here

$sHTML &= 'var JSglobal = (1,eval)("this");' & @CRLF ; the 'global' variable get a handle to the javascript global object

; ============================ this Javascript returns an array
$sHTML &= '   function makeArray() {' & @CRLF
$sHTML &= 'var myArray = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];' & @CRLF
$sHTML &= 'return myArray;' & @CRLF
$sHTML &= '}' & @CRLF
; =============================================================

$sHTML &= "</script>" & @CRLF
$sHTML &= "</HEAD></HTML>" & @CRLF ; html closing tags
; *** end of html page listing

Local $oIE = ObjCreate("Shell.Explorer.2")
GUICreate("", 10, 10, @DesktopWidth + 10, @DesktopHeight + 10) ; place the gui out of screen
GUICtrlCreateObj($oIE, 0, 0, 10, 10) ; this render $oIE usable
GUISetState(@SW_HIDE) ; hide GUI

$oIE.navigate('about:blank')
While Not String($oIE.readyState) = 'complete' ; wait for about:blank
    Sleep(100)
WEnd

$oIE.document.Write($sHTML) ; inject lising directly to the HTML document:
$oIE.document.close() ; close the write stream
$oIE.document.execCommand("Refresh")

; this waits till the document is ready to be used (portion of code from the _IELoadWait() function in IE.au3)
While Not (String($oIE.readyState) = "complete" Or $oIE.readyState = 4)
    Sleep(100)
WEnd
While Not (String($oIE.document.readyState) = "complete" Or $oIE.document.readyState = 4)
    Sleep(100)
WEnd

; https://msdn.microsoft.com/en-us/library/52f50e9t(v=vs.94).aspx
Local $ohJS = $oIE.document.parentwindow.JSglobal ; $ohJS is a reference to the javascript Global Obj
; ---- now the javascript engine can be used in our AutoIt script using the $ohJS reference ----

; This call a javascript function that returns an javascript array
$result1 = $ohJS.eval('makeArray();')

; this obj is (should be) a javascript array (not recognized as is outside the javascript)
_ObjDescription($result1) ; this javascript array have to be 'serialized' in JSON

; Serializes a JavaScript value into 'JavaScript Object Notation' (JSON) text.
; https://msdn.microsoft.com/en-us/library/cc836459(v=vs.85).aspx
$result2 = $ohJS.JSON.stringify($result1) ; this sometime works and sometime fail ...?????? why <--- ??????

MsgBox(0, "Debug JSON string", $result2) ; a json string representing the array

; Local $Data1 = Json_Decode($result2)
; _ArrayDisplay($Data1)

Func _ObjDescription(ByRef $oObj)
    If IsObj($oObj) Then
        $sObjInfos = 'The name of the Object:' & @CRLF & ObjName($oObj, $OBJ_NAME) & @CRLF
        $sObjInfos &= '------------------------------------------------------------' & @CRLF
        $sObjInfos &= 'Description string of the Object:' & @CRLF & ObjName($oObj, $OBJ_STRING) & @CRLF
        $sObjInfos &= '------------------------------------------------------------' & @CRLF
        $sObjInfos &= 'The ProgID of the Object:' & @CRLF & ObjName($oObj, $OBJ_PROGID) & @CRLF
        $sObjInfos &= '------------------------------------------------------------' & @CRLF
        $sObjInfos &= 'The file that is associated with the object in the Registry:' & @CRLF & ObjName($oObj, $OBJ_FILE) & @CRLF
        $sObjInfos &= '------------------------------------------------------------' & @CRLF
        $sObjInfos &= 'Module name in which the object runs:' & @CRLF & ObjName($oObj, $OBJ_MODULE) & @CRLF
        $sObjInfos &= '------------------------------------------------------------' & @CRLF
        $sObjInfos &= "CLSID of the object's coclass:" & @CRLF & ObjName($oObj, $OBJ_CLSID) & @CRLF
        $sObjInfos &= '------------------------------------------------------------' & @CRLF
        $sObjInfos &= "IID of the object's interface:" & @CRLF & ObjName($oObj, $OBJ_IID) & @CRLF
        $sObjInfos &= '------------------------------------------------------------' & @CRLF
    Else
        $sObjInfos = "Is not an object)"
    EndIf
    MsgBox(0, "Debug: Obj info", $sObjInfos)
EndFunc   ;==>_ObjDescription

 

Edited by Chimp
corrections on the _ObjDescription() function

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

When i try to run your code it always fails.

Also the JSON object returns 'undefined' when i try to extract it with eval like below:

MsgBox(0, "", $ohJS.eval("typeof JSON"))

I hate to sound like a broken record, but why "Shell.Explorer.2" and not "ScriptControl"? Some requirements IE is better suited for in your scenario? :)

 

Edit:

Here's an example:

$oSC = ObjCreate("ScriptControl")
$oSC.Language = "JScript"
$oSC.AddCode(BinaryToString(InetRead("https://raw.githubusercontent.com/douglascrockford/JSON-js/master/json2.js")))
$oSC.Eval("Array.prototype.Item = function(i){return this[i];}"); enable fetching of array item with Array.Item function
$myArray = $oSC.Eval('["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]')
For $i=0 To $myArray.length-1
    ConsoleWrite( $myArray.Item($i) & @CRLF )
Next
MsgBox(0, "", $oSC.Eval("JSON").stringify($myArray))

 

Edited by genius257
Link to comment
Share on other sites

10 minutes ago, genius257 said:

When i try to run your code it always fails.

what IE version are you using?

om my system it works 50% of runs (....??)

12 minutes ago, genius257 said:

I hate to sound like a broken record, but why "Shell.Explorer.2" and not "ScriptControl"? Some requirements IE is better suited for in your scenario? :)

II would interact with objects on the embedded IE obj transfer arrays and eventually also interact with canvas... seems that this is not allowed with "ScriptControl", right?

p.s.

you don't sound at all like a broken record :)

 

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

11 minutes ago, Chimp said:

what IE version are you using?

Version: 11.633.10586.0

11 minutes ago, Chimp said:

II would interact with objects on the embedded IE obj transfer arrays and eventually also interact with canvas... seems that this is not allowed with "ScriptControl", right?

The interaction with a canvas is indeed impractical, if not impossible with "ScriptControl"

11 minutes ago, Chimp said:

you don't sound at all like a broken record :)

Thank you ;)

Link to comment
Share on other sites

3 minutes ago, Chimp said:

And is there some way to attach to an IE obj embedded in an AutoIt GUI (and related dom objects on it)?

I'm not sure what you mean? The IE obj itself or objects created within?

Link to comment
Share on other sites

... both

EDIT:

or better, I think you have first to attch to the IE obj and then using it to 'navigate' into it's DOM, or there is a way to directly attach to objects created within it?

Edited by Chimp

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

Then something like the embedded object?

Opt("GuiOnEventMode", 1)

$oIE = ObjCreate("Shell.Explorer.2")
$hGui = GUICreate("Title", 700, 320)
GUISetOnEvent(-3, "_MyExit", $hGui)
GUISetState()
$hIE = GUICtrlCreateObj($oIE, 10, 10, 680, 300)
$oIE.navigate('about:blank')
$oIE.document.Write('<!DOCTYPE HTML><html><head><style>body{background-color:#010;color:#FFF;}</style></head><body>this is the body</body></html>')
While 1
    Sleep(10)
WEnd

Func _MyExit()
    Exit
EndFunc

 

Link to comment
Share on other sites

@genius257 ...sorry for the delay...

... that listing for example could be the 'basic' gui, then, in short, I would 'write' for example this page (a timeline from this site) into the embbedded IE, and finally use "ScriptControl" (or whatever) to interact with the timeline so to use it as a graphic frontend where to display data, while data is managed by AutoIt on an SQLite database for example...
I was trying now to do something like that, but I'm having problems on opening the above page into the embedded IE (the timeline is not shown...)
I will come back to this tomorrow.... 02:58 AM here....

.....thanks for any help.... :)

Edited by Chimp

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
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
 Share

×
×
  • Create New...