Tooltip translator error Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Elermino
Posts: 114
Joined: 29 Nov 2021, 17:43

Tooltip translator error

Post by Elermino » 29 Mar 2023, 11:55

Good morning everyone :wave:
This script translates the selected text from "auto" to "spanish" and should display it in a tooltip, but cannot display it and displays an error. The translation script works, just need to show it through the tooltip.

The function OnMouseToolTip() shows the translation output.
I appreciate your help!

Example of error:
https://i.imgur.com/uMXa9Jt_d.webp?maxwidth=760&fidelity=grand

Code:

Code: Select all

!s::
clipboard=
Send ^c
sleep 250
transeng := GoogleTranslate(clipboard)
sleep 3000


MouseIsOver(WinTitle) {
    MouseGetPos,,, Win
    return WinExist(WinTitle . " ahk_id " . Win)
}

OnMouseToolTip(msg, delay := 1000)
{
    MouseGetPos X, Y
    ToolTip %msg%, X, Y
    SetTimer, tooltipchk, %delay%
    return
    tooltipchk:
    if not MouseIsOver("ahk_class tooltips_class32")
    {
        ToolTip
        SetTimer, tooltipchk, off
    }
    return
}

OnMouseToolTip(%transeng%)

Return
GoogleTranslate(str, from := "auto", to := "es")  {
static JS := GetJScripObject(), _ := JS.( GetJScript() ) := JS.("delete ActiveXObject; delete GetObject;")
json := SendRequest(JS, str, to, from, proxy := "")
oJSON := JS.("(" . json . ")")
if !IsObject(oJSON[1])  {
Loop % oJSON[0].length
trans .= oJSON[0][A_Index - 1][0]
}
else  {
MainTransText := oJSON[0][0][0]
Loop % oJSON[1].length  {
trans .= "`n+"
obj := oJSON[1][A_Index-1][1]
Loop % obj.length  {
txt := obj[A_Index - 1]
trans .= (MainTransText = txt ? "" : "`n" txt)
}
}
}
if !IsObject(oJSON[1])
MainTransText := trans := Trim(trans, ",+`n ")
else
trans := MainTransText . "`n+`n" . Trim(trans, ",+`n ")
from := oJSON[2]
trans := Trim(trans, ",+`n ")
Return trans
}
SendRequest(JS, str, tl, sl, proxy) {
ComObjError(false)
http := ComObjCreate("WinHttp.WinHttpRequest.5.1")
( proxy && http.SetProxy(2, proxy) )
http.open( "POST", "https://translate.google.com/translate_a/single?client=t&sl="
. sl . "&tl=" . tl . "&hl=" . tl
. "&dt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&ie=UTF-8&oe=UTF-8&otf=1&ssel=3&tsel=3&pc=1&kc=2"
. "&tk=" . JS.("tk").(str), 1 )
http.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8")
http.SetRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0")
http.send("q=" . URIEncode(str))
http.WaitForResponse(-1)
Return http.responsetext
}
URIEncode(str, encoding := "UTF-8")  {
VarSetCapacity(var, StrPut(str, encoding))
StrPut(str, &var, encoding)
While code := NumGet(Var, A_Index - 1, "UChar")  {
bool := (code > 0x7F || code < 0x30 || code = 0x3D)
UrlStr .= bool ? "%" . Format("{:02X}", code) : Chr(code)
}
Return UrlStr
}
GetJScript()
{
script =
   (
      var TKK = ((function() {
        var a = 561666268;
        var b = 1526272306;
        return 406398 + '.' + (a + b);
      })());

      function b(a, b) {
        for (var d = 0; d < b.length - 2; d += 3) {
            var c = b.charAt(d + 2),
                c = "a" <= c ? c.charCodeAt(0) - 87 : Number(c),
                c = "+" == b.charAt(d + 1) ? a >>> c : a << c;
            a = "+" == b.charAt(d) ? a + c & 4294967295 : a ^ c
        }
        return a
      }

      function tk(a) {
          for (var e = TKK.split("."), h = Number(e[0]) || 0, g = [], d = 0, f = 0; f < a.length; f++) {
              var c = a.charCodeAt(f);
              128 > c ? g[d++] = c : (2048 > c ? g[d++] = c >> 6 | 192 : (55296 == (c & 64512) && f + 1 < a.length && 56320 == (a.charCodeAt(f + 1) & 64512) ?
              (c = 65536 + ((c & 1023) << 10) + (a.charCodeAt(++f) & 1023), g[d++] = c >> 18 | 240,
              g[d++] = c >> 12 & 63 | 128) : g[d++] = c >> 12 | 224, g[d++] = c >> 6 & 63 | 128), g[d++] = c & 63 | 128)
          }
          a = h;
          for (d = 0; d < g.length; d++) a += g[d], a = b(a, "+-a^+6");
          a = b(a, "+-3^+b+-f");
          a ^= Number(e[1]) || 0;
          0 > a && (a = (a & 2147483647) + 2147483648);
          a `%= 1E6;
          return a.toString() + "." + (a ^ h)
      }
)
Return script
}
GetJScripObject()  {
VarSetCapacity(tmpFile, (MAX_PATH := 260) << !!A_IsUnicode, 0)
DllCall("GetTempFileName", Str, A_Temp, Str, "AHK", UInt, 0, Str, tmpFile)
FileAppend,
   (
   <component>
   <public><method name='eval'/></public>
   <script language='JScript'></script>
   </component>
), % tmpFile
JS := ObjBindMethod( ComObjGet("script:" . tmpFile), "eval" )
FileDelete, % tmpFile
Return JS
}
Last edited by gregster on 29 Mar 2023, 12:00, edited 1 time in total.
Reason: Obviously not AHK v2 code; hence, moved topic to v1 help.

gregster
Posts: 9012
Joined: 30 Sep 2013, 06:48

Re: Tooltip translator error  Topic is solved

Post by gregster » 29 Mar 2023, 12:01

Try OnMouseToolTip(transeng) - functions always use expression syntax.

User avatar
Elermino
Posts: 114
Joined: 29 Nov 2021, 17:43

Re: Tooltip translator error

Post by Elermino » 29 Mar 2023, 12:03

gregster wrote:
29 Mar 2023, 12:01
Try OnMouseToolTip(transeng) - functions always use expression syntax.
It works, thanks :salute:

Post Reply

Return to “Ask for Help (v1)”