简体   繁体   中英

Pass information from JavaScript to AutoIt

Is there a way I can connect JavaScript with AutoIt such that JavaScript passes data (and parameters to work on) to AutoIt.

Actually I am getting mouse coordinates on a webpage using JavaScript. Now I want to pass those mouse coordinates dynamically to an AutoIt script.

Initially I thought of saving a txt file dynamically with JavaScript and reading it from AutoIt. But as you know, you cannot save a txt file using client-side JavaScript (or jQuery).

Kindly suggest a workaround to this problem.

You can do it by attaching to the browser window. That way, autoit can "see" or manipulate everything on the page.

Take a look at the Chrome UDF or IE UDF

Another option would be to install a local web server, then pass any data from javascript to the local web server over HTTP, execing AutoIt in a CGI script (or similar) on the web server.

Just be careful how you setup the web server, disallow or firewall out remote connections, because you probably don't want other computers to exec your AutoIt scripts.

When you say that you want to pass javascript returned coordinates to AutoIT, I'm assuming it's because you want to perform some action on them that only AutoIT can assist you with. This tallies with what the author of this little UDF envisioned when he said

"I wondered if it was possible to "insert" jQuery into an IE page, then calling the rich jQuery API to select DOM elements. This would reduce the no. of lines in my AutoIt script drastically."

A basic set of tutorials is given below.

  • Set text to a button
  • Set text to an input text
  • Submit a google search
  • search Filtering by class for get only results urls

Reference: https://www.autoitscript.com/forum/topic/81025-ie-automation-using-jquery/?do=findComment&comment=1058556

Global $objAppIE, $jQuery, $jQueryFilePath = @ScriptDir & '\jquery-1.9.1.js'
Global $oMyError = ObjEvent ( 'AutoIt.Error', '_MyErrFunc' )

If Not FileExists ( $jQueryFilePath ) Then InetGet ( 'http://code.jquery.com/jquery-1.9.1.js', $jQueryFilePath )
$objAppIE = ObjCreate ( 'InternetExplorer.Application' )
$objAppIE.visible = True
$objAppIE.navigate ( 'http://www.google.com/' )

$jQuery = _InsertJQuery ( $objAppIE )
If IsObj ( $jQuery ) Then
    $jQuery ( ':input[id="gbqfba"]' ).text ( 'Search With Bing' ) ; **set text button**
    Sleep ( 1000 ) ; just for see changes
    $jQuery ( ':input[id="gbqfq"]' ).val ( 'autoit' ) ; **set input text**
    Sleep ( 1000 ) ; just for see changes
    $jQuery ( ':input[id="gbqfba"]' ).submit ( ) ; submit
    _IEPageLoadWait ( $objAppIE )
    Do
        $aLinks = $jQuery ( '.l' ).get ( ) ; **filtering by class for get only results urls.**
    Until $aLinks.length
    ConsoleWrite ( '! Results Nb : ' & $aLinks.length & @Crlf )
    $i=0
    For $aLink In $aLinks
        $i+=1
        ConsoleWrite ( '+ ' & $i & ' $aLink.href : ' & $aLink.href & @Crlf )
    Next
EndIf

Func _InsertjQuery ( $objAppIE )
    Local $objWindow, $objHead, $objScript
    _IEPageLoadWait ( $objAppIE )
    If IsObj ( $objAppIE ) Then
        $objWindow = $objAppIE.document.parentWindow
        $objHead = $objAppIE.document.getElementsByTagName ( 'head' ).item ( 0 )
        If Not IsObj ( $objwindow.jQuery ) Then
            $objScript = $objAppIE.document.createElement ( 'script' )
            $objScript.type = 'text/javascript'
            $objScript.language = 'javascript'
            $objScript.defer = 'defer'
            $Script = FileRead ( $jQueryFilePath )
            $objScript.text = $Script
            $objHead.appendChild ( $objScript )
            While Not ( IsObj ( $objwindow.jQuery ) )
                Sleep ( 100 )
            WEnd
            $objwindow.jQuery.noConflict ( )
        EndIf
        Return $objwindow.jQuery
    EndIf
EndFunc ;==> _InsertjQuery ( )

Func _IEPageLoadWait ( $objAppIE )
    Do
        Sleep ( 50 )
    Until $objAppIE.readyState = 'complete' Or $objAppIE.readyState = 4
    Do
        Sleep ( 50 )
    Until $objAppIE.document.readyState = 'complete' Or $objAppIE.document.readyState = 4
    Do
        Sleep ( 50 )
    Until Not $objAppIE.busy
EndFunc ;==> _IEPageLoadWait ( )

Func _MyErrFunc ( )
    $HexNumber = Hex ( $oMyError.number, 8 )
    If $HexNumber = 80020006 Then Return
    Msgbox ( 0, '', 'We intercepted a COM Error !' & @CRLF & 'Number is: ' & $HexNumber & @CRLF & 'Windescription is: ' & $oMyError.windescription & @CRLF & 'Line Number: ' & $oMyError.scriptLine & @CRLF, 3 )
    Exit
Endfunc ;==> _MyErrFunc ( )

If it is to store only some coords in plain text i would go for pastebin. http://pastebin.com/api is really easy to automate. If you need help with that then let me know.

PS Here is a small Pastebin UDF written in autoit: http://www.autoitscript.com/forum/topic/150838-pastebin-udf/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM