' NexTap 1.1 · 按键精灵手机版 Q 语言源码模板
' 需要 Url.Get 和 Encode.JsonToTable。复制到脚本顶部。
' 尚未在按键运行环境实测；PC 版不适用此模板。
' URL 参数只拼数字。事务用 | 分隔动作，失败即停止并释放锁。
Dim NextapBase = "http://127.0.0.1:9099"

Function NextapRequest(path)
    Dim raw, result
    raw = Url.Get(NextapBase & path)
    If raw = "" Then
        TracePrint "NexTap: 请求失败，不自动重试"
        NextapRequest = {"ok":False}
        Exit Function
    End If
    result = Encode.JsonToTable(raw)
    NextapRequest = result
End Function

Function NextapClick(x, y)
    NextapClick = NextapRequest("/i8/actions/click?x=" & x & "&y=" & y)
End Function
Function NextapMove(x, y)
    NextapMove = NextapRequest("/i8/actions/move?x=" & x & "&y=" & y)
End Function
Function NextapSlide(x, y, x1, y1, ms)
    NextapSlide = NextapRequest("/i8/actions/slide?x=" & x & "&y=" & y & "&x1=" & x1 & "&y1=" & y1 & "&duration_ms=" & ms)
End Function

Function NextapTransaction(commands)
    Dim lock, token, steps, i, result, released, succeeded
    lock = NextapRequest("/i8/lock/acquire")
    If lock["ok"] <> True Then
        NextapTransaction = False
        Exit Function
    End If
    token = lock["token"]
    steps = Split(commands, "|")
    succeeded = True
    For i = 0 To UBound(steps)
        result = NextapRequest("/i8/actions/" & steps[i] & "&token=" & token)
        If result["ok"] <> True Then
            succeeded = False
            Exit For
        End If
    Next
    released = NextapRequest("/i8/lock/release?token=" & token)
    NextapTransaction = succeeded And released["ok"]
End Function
