【问题标题】:Pass information from JavaScript to AutoIt将信息从 JavaScript 传递到 AutoIt
【发布时间】:2014-09-18 04:42:25
【问题描述】:

有没有一种方法可以将 JavaScript 与 AutoIt 连接起来,以便 JavaScript 将数据(和要处理的参数)传递给 AutoIt。

实际上,我正在使用 JavaScript 在网页上获取鼠标坐标。现在我想将这些鼠标坐标动态传递给 AutoIt 脚本。

最初我想用 JavaScript 动态保存一个 txt 文件并从 AutoIt 读取它。但如您所知,您无法使用客户端 JavaScript(或 jQuery)保存 txt 文件。

请提出解决此问题的方法。

【问题讨论】:

    标签: javascript jquery google-chrome web autoit


    【解决方案1】:

    当您说要将 javascript 返回的坐标传递给 AutoIT 时,我假设这是因为您想对它们执行一些只有 AutoIT 可以帮助您的操作。 这与这个小 UDF 的作者所说的相符

    “我想知道是否可以将 jQuery “插入”到 IE 页面中,然后调用丰富的 jQuery API 来选择 DOM 元素。这将大大减少我的 AutoIt 脚本中的行数。”

    下面给出了一组基本教程。

    • 为按钮设置文本
    • 将文本设置为输入文本
    • 提交谷歌搜索
    • 按类别搜索过滤以仅获取结果网址

    参考: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 ( )
    

    【讨论】:

      【解决方案2】:

      您可以通过附加到浏览器窗口来做到这一点。 这样,autoit 就可以“看到”或操作页面上的所有内容。

      看看Chrome UDFIE UDF

      【讨论】:

      • 我可以安装 AutoIt 扩展但无法启用它,因为它在 Chrome 网上应用店中不可用。 @Milos 有什么想法吗?
      • 也许改用IE?
      • 好吧,我必须做很多事情,比如 WebGL 和其他东西。 IE?嗯。
      • 带有 autoit 的 IE 自动化是迄今为止最完整和最简单的浏览器自动化。无需扩展或任何外部“助手”即可操作页面。 AutoIt 帮助文件中为 IE UDF 上的每个功能提供全面的帮助支持。
      【解决方案3】:

      另一种选择是安装本地 Web 服务器,然后通过 HTTP 将任何数据从 javascript 传递到本地 Web 服务器,在 Web 服务器上的 CGI 脚本(或类似脚本)中执行 AutoIt。

      请注意您如何设置 Web 服务器、禁止或防火墙排除远程连接,因为您可能不希望其他计算机执行您的 AutoIt 脚本。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-11-06
        • 1970-01-01
        相关资源
        最近更新 更多