Jump to content
Chimp

Autoit <-> Javascript roundtrip

Recommended Posts

Chimp

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


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
Chimp

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


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
junkew

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);

 

Share this post


Link to post
Share on other sites
Chimp

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

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
Chimp

..... 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
  • Like 1

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
genius257

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:

 

  • Like 2

Share this post


Link to post
Share on other sites
Chimp

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)

 


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
genius257
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
  • Like 1

Share this post


Link to post
Share on other sites
Chimp

thanks for the clarifications... :)


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
Chimp

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

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
genius257

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
  • Like 1

Share this post


Link to post
Share on other sites
Chimp
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 :)

 


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
genius257
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 ;)

Share this post


Link to post
Share on other sites
Chimp

Thanks @genius257 for the example in post#13, nice one.

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


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
genius257
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?

Share this post


Link to post
Share on other sites
Chimp

... 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

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
genius257

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

 

Share this post


Link to post
Share on other sites
Chimp

@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

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

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

  • Similar Content

    • Seminko
      By Seminko
      Is there a way to grab non-hardcoded but rather javascript generated data from a webpage?
      Tried a get request as well as _IEBodyReadHTML but both seem to grab the code without the javascript generated data.
      $oHTTP = ObjCreate("winhttp.winhttprequest.5.1") $oHTTP.Open("GET", "link", False) $oHTTP.Send() $oReceived = $oHTTP.ResponseText $oStatusCode = $oHTTP.Status Global $DataArray[10][5] If $oStatusCode <> 200 Then Exit MsgBox(1, "Error", "Status Code <> 200") EndIf FileWrite(@ScriptDir & "\output.txt", $oReceived) ; //////// #include <IE.au3> Local $FullLink = "link" Local $oIE = _IECreate($FullLink, 0, 0) _IELoadWait($oIE) Local $sText = _IEBodyReadHTML($oIE) FileWrite(@ScriptDir & "\output.txt", $sText)  
    • guinness
      By guinness
      Just trying out the latest version of AutoIt and thinking more functional
      #include <Array.au3> ; Example ; An example of filtering, mapping and reducing arrays, using a function reference. ; This is similiar to how it would be done in the likes of JavaScript ; i.e. more functional (declarative) than procedural (imperative) ; Filter example Local $aiFilteredBefore[] = [1, 2, 3, 50, 30, 40, 20, 30] Local $aiFilteredAfter = _ArrayFilter($aiFilteredBefore, GtrThan30) _ArrayDisplay($aiFilteredAfter, '_ArrayFilter::') ; Map example Local $aiMappedBefore[] = [1, 2, 3, 4, 5, 6, 7, 8, 9] Local $aiMappedAfter = _ArrayMap($aiMappedBefore, MultiplyByTwo) _ArrayDisplay($aiMappedAfter, '_ArrayMap::') ; Reduce example ; Sum all values in the array Local $aiReducedBefore[] = [1, 2, 3, 50, 30, 40, 20, 30] ConsoleWrite('_ArrayReduce:: ' & _ArrayReduce($aiReducedBefore, SumValues) & @CRLF) ; Passing an empty array, will return the initial value; otherwise, sets @error to 4 ; if no initial value is defined Local $aEmpty[] = [] ConsoleWrite('_ArrayReduce:: ' & _ArrayReduce($aEmpty, SumValues, 0) & @CRLF) ; Array callback functions (for the examples only) Func GtrThan30($iValue) Return $iValue > 30 EndFunc ;==>GtrThan30 Func MultiplyByTwo($iValue, $iIndex, $aiArray) ; Notice how the function is called with the optional arguments "index" and "original array" ConsoleWrite('Index:: ' & $iIndex & ', Array:: ' & _ArrayToString($aiArray) & @CRLF) Return $iValue * 2 EndFunc ;==>MultiplyByTwo Func SumValues($a, $b) Return $a + $b EndFunc ;==>SumValues ; Functions ; The callback function is invoked with fn(value, [index, [array]]) Func _ArrayFilter($avArray, $hFunc) If Not IsArray($avArray) Then ; Null is more appropriate than returning the likes of -1 or an empty array Return SetError(1, 0, Null) EndIf If Not IsFunc($hFunc) Then Return SetError(2, 0, Null) EndIf Local Const $iLength = UBound($avArray) Local $avFiltered[$iLength] If $iLength = 0 Then Return $avFiltered EndIf Local $iIndex = 0 For $i = 0 To $iLength - 1 Local $bIsFiltered = __ArrayCall($hFunc, 3, $avArray[$i], $i, $avArray) If @error Then Return SetError(@error, @extended, Null) ElseIf $bIsFiltered Then $avFiltered[$iIndex] = $avArray[$i] $iIndex += 1 EndIf Next ReDim $avFiltered[$iIndex] Return $avFiltered EndFunc ;==>_ArrayFilter ; The callback function is invoked with fn(value, [index, [array]]) Func _ArrayMap($avArray, $hFunc) If Not IsArray($avArray) Then ; Null is more appropriate than returning the likes of -1 or an empty array Return SetError(1, 0, Null) EndIf If Not IsFunc($hFunc) Then Return SetError(2, 0, Null) EndIf Local Const $iLength = UBound($avArray) Local $avMapped[$iLength] If $iLength = 0 Then Return $avMapped EndIf For $i = 0 To $iLength - 1 $avMapped[$i] = __ArrayCall($hFunc, 3, $avArray[$i], $i, $avArray) If @error Then Return SetError(@error, @extended, Null) EndIf Next Return $avMapped EndFunc ;==>_ArrayMap ; The callback function is invoked with fn(current, value, [index, [array]]) Func _ArrayReduce($avArray, $hFunc, $vInitial = Default) If Not IsArray($avArray) Then ; Null is more appropriate than returning the likes of -1 or an empty array Return SetError(1, 0, Null) EndIf If Not IsFunc($hFunc) Then Return SetError(2, 0, Null) EndIf Local $bHasInitial = @NumParams >= 3 Local $iLength = UBound($avArray) If $iLength = 0 Then If Not $bHasInitial Then Return SetError(4, 0, Null) EndIf Return $vInitial EndIf For $i = 0 To $iLength - 1 If $bHasInitial Then $vInitial = __ArrayCall($hFunc, 3, $vInitial, $avArray[$i], $i, $avArray) If @error Then Return SetError(@error, @extended, Null) EndIf Else $bHasInitial = True $vInitial = $avArray[$i] EndIf Next Return $vInitial EndFunc ;==>_ArrayReduce Func __ArrayCall($hFunc, $iError, $vArg1 = Default, $vArg2 = Default, $vArg3 = Default, $vArg4 = Default) Local Const $CALL_ERROR = 0xDEAD Local Const $CALL_EXTENDED = 0xBEEF Local $vRet = Call($hFunc, $vArg1) If @error = $CALL_ERROR And @extended = $CALL_EXTENDED Then $vRet = Call($hFunc, $vArg1, $vArg2) If @error = $CALL_ERROR And @extended = $CALL_EXTENDED Then $vRet = Call($hFunc, $vArg1, $vArg2, $vArg3) If @error = $CALL_ERROR And @extended = $CALL_EXTENDED Then $vRet = Call($hFunc, $vArg1, $vArg2, $vArg3, $vArg4) If @error = $CALL_ERROR And @extended = $CALL_EXTENDED Then ; The function exists, but there is no appropriate function signature Return SetError($iError, 0, Null) EndIf EndIf EndIf EndIf Return SetError(@error, @extended, $vRet) EndFunc ;==>__ArrayCall  
    • Chimp
      By Chimp
      An example on how to inject jQuery into a web page
      It can be useful to manage the page from AutoIt using jQuery.
      Idea from here: http://www.learningjquery.com/2009/04/better-stronger-safer-jquerify-bookmarklet
      Suggestions and improvements are welcome
      #include <ie.au3> Example() Func Example() Local $oIE = _IECreate("www.google.com") Local $jQuery = _jQuerify($oIE) MsgBox(0, "Version", "jQuery version: " & $jQuery.fn.jquery) MsgBox(0, "Example", "click ok to exit." & @CRLF & "Google logo will fade out by jQuery...") $jQuery('#hplogo').fadeOut(3000) ; jQuery will fade out the google logo EndFunc ;==>Example ; #FUNCTION# ==================================================================================================================== ; Name ..........: _jQuerify ; Description ...: ; Syntax ........: _jQuerify(Byref $oIE) ; Parameters ....: $oIE - Object variable of an InternetExplorer.Application. ; Return values .: an object variable pointing to the jQuery library ; Author ........: Chimp ; Modified ......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: ; =============================================================================================================================== Func _jQuerify(ByRef $oIE) Local $jsEval, $jQuery, $otherlib = False ; create a reference to the javascript eval() function $oIE.document.parentWindow.setTimeout('document.head.eval = eval', 0) Do Sleep(250) $jsEval = Execute('$oIE.Document.head.eval') Until IsObj($jsEval) ; if jQuery is not already loaded then load it If $jsEval("typeof jQuery=='undefined'") Then ; check if the '$' (dollar) name is already in use by other library If $jsEval("typeof $=='function'") Then $otherlib = True Local $oScript = $oIE.document.createElement('script'); $oScript.type = 'text/javascript' ; If you want to load jQuery from a disk file use the following statement ; where i.e. jquery-1.9.1.js is the file containing the jQuery source ; (or also use a string variable containing the whole jQuery listing) ;~ $oScript.TextContent = FileRead(@ScriptDir & "\jquery-1.9.1.js") ; <--- from a file ; If you want to download jQuery from the web use this statement $oScript.src = 'https://code.jquery.com/jquery-latest.min.js' ; <--- from an url $oIE.document.getElementsByTagName('head').item(0).appendChild($oScript) Do Sleep(250) Until $jsEval("typeof jQuery == 'function'") EndIf Do Sleep(250) $jQuery = $jsEval("jQuery") Until IsObj($jQuery) If $otherlib Then $jsEval('jQuery.noConflict();') Return $jQuery EndFunc ;==>_jQuerify  
    • XaelloNegative
      By XaelloNegative
      Hi there.
      Im having some kind of a coder block here, 
      <td class="treeview ctl00_CenterContent_objTreeView_2" style="white-space:nowrap;"> <input type="checkbox" name="ctl00_CenterContent_objTreeViewn1CheckBox" id="ctl00_CenterContent_objTreeViewn1CheckBox"> <span class="ctl00_CenterContent_objTreeView_0 treeview ctl00_CenterContent_objTreeView_1" id="ctl00_CenterContent_objTreeViewt1" style="border-style:none;font-size:1em;"> <i class="fa fa-file-code-o fa-fw"> </i> <span data-placement="bottom" data-toggle="tooltip" title="" data-original-title="Tool tip for said part">REFUNDS</span></span></td> this is a code directly from the website im working with. I was thinking of inserting a javascript that would get the "REFUNDS" part. Any ideas on how to do so?
      Any thoughts will be highly appreciated.
    • Patryk
      By Patryk
      Dear Colleagues,
      I have no idea how to refer to style.display in my loop. There is a button like: 
      <input name="ctl00$bodyPlaceholder$btnFecharProcessamento" class="button" id="bodyPlaceholder_btnFecharProcessamento" style="display: none;" onclick="FecharJanelaProcessamento();return false;" type="submit" value="Fechar"> I want it to be clicked as soon as button's style change from style="display: none;" to  style= ="display: inline-block;" 
      This is what I got, but it's not working...
      Func GetButtons() $oButtons = _IEGetObjByName($oForm,"ctl00$bodyPlaceholder$btnFecharProcessamento",-1) $i = 0 While $i <> 1 For $oBtn In $oButtons If _IEFormElementGetValue($oBtn) = "Fechar" And $oBtn.document.style.display = "display: inline-block;" Then $i = 1 _IEAction($oBtn, "focus") _IEAction($oBtn, "click") ExitLoop EndIf Next WEnd EndFunc  
      I will really appreciate if you could help me with these loop!
×