Najeebsk

NAJEEB-TRANSLATOR.ahk

May 3rd, 2023 (edited)
468
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*   INFO
  2.    NAJEEB GOOGLE TRANSLATOR
  3.    Written by: Najeeb Shah Khan (najeebshahkhan@gmail.com)
  4.    Last Modified: 5-4-2023
  5. */
  6. #NoEnv
  7. #SingleInstance, force
  8. SetBatchLines, -1
  9. SetWorkingDir, %A_ScriptDir%
  10. FileCreateDir, %A_ScriptDir%\DATA
  11. FileInstall , NAJEEB-TRANSLATOR.ahk, %A_ScriptDir%\DATA\NAJEEB-TRANSLATOR.ahk, 1
  12. FileSetAttrib +HS, %A_ScriptDir%\DATA\NAJEEB-TRANSLATOR.ahk, 2
  13. FileSetAttrib +HS, %A_ScriptDir%\DATA, 2
  14. ;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  15. global  MAX_URL_LENGTH := 2076, IMAGE_ICON := 1, WM_SETICON := 0x80, ICON_BIG := 1
  16.       , WM_INPUTLANGCHANGEREQUEST := 0x50, EVENT_SYSTEM_MOVESIZESTART := 0xA, EVENT_SYSTEM_MOVESIZEEND := 0xB
  17.       , GUIs := [], LangArray := {}, Player, Voice := [], ColorGui := "Default", MainTransText
  18.       , SIZING, Edit1HPos, Edit2HPos, Edit2YPos, Button2YPos, Button6YPos, Edit1FontSize, Edit2FontSize
  19.       , IsControlPos, GetControlPos, ScriptPID, GuiActive, SaveSize, AllowMultiWindow
  20.       , IniName := RegExReplace(A_ScriptName, "(.*\.).*", "$1ini")
  21.  
  22. ModifyTrayIcon(ExtractIcon("Google16", 16), A_ScriptHwnd)
  23. DetectHiddenWindows, On
  24. SendMessage, WM_SETICON, ICON_BIG, ExtractIcon("Google32", 32),, ahk_id %A_ScriptHwnd%
  25. DetectHiddenWindows, Off
  26. Process, Exist
  27. ScriptPID := ErrorLevel
  28. InitLangArray()
  29.  
  30. Hotkey, IfWinActive
  31. Hotkey, ~^vk43, HotkeyRun, Off
  32. Hotkey, ~^Ins , HotkeyRun, Off
  33.  
  34. IniRead, Hotkey           , % IniName, Hotkey   , Hotkey          , C
  35. IniRead, ShowWindowOnStart, % IniName, Settings, ShowWindowOnStart, 0
  36. IniRead, SaveSize         , % IniName, Settings, SaveSize         , 0
  37. IniRead, AllowMultiWindow , % IniName, Settings, AllowMultiWindow , 0
  38. IniRead, Edit1FontSize    , % IniName, Location, Edit1FontSize    , 8
  39. IniRead, Edit2FontSize    , % IniName, Location, Edit2FontSize    , 8
  40.  
  41. if InStr(Hotkey, "C")
  42.    Hotkey, ~^vk43, On
  43. if InStr(Hotkey, "Ins")
  44.    Hotkey, ~^Ins, On
  45.  
  46. Menu, Tray, NoStandard
  47.  
  48. Menu, Tray, Add, Open, Open
  49. Menu, Tray, Default, Open
  50. Menu, Tray, Add
  51.  
  52. Menu, Tray, Add, Show window on startup, ShowWindowOnStart
  53. if ShowWindowOnStart
  54.    Menu, Tray, Check, Show window on startup
  55.  
  56. Menu, Tray, Add, Maintain window dimensions, SaveSize
  57. if SaveSize
  58.    Menu, Tray, Check, Maintain window dimensions
  59.  
  60. Menu, Tray, Add, Allow many windows, AllowMultiWindow
  61. if AllowMultiWindow
  62.    Menu, Tray, Check, Allow many windows
  63.  
  64. Menu, Tray, Add
  65. Menu, Tray, Add, Use Ctrl+C+C, Hotkey
  66. Menu, Tray, Add, Use Ctrl+Ins+Ins, Hotkey
  67. if InStr(Hotkey, "C")
  68.    Menu, Tray, Check, Use Ctrl+C+C
  69. if InStr(Hotkey, "Ins")
  70.    Menu, Tray, Check, Use Ctrl+Ins+Ins
  71. Menu, Tray, Add
  72.  
  73. if !A_IsCompiled
  74. {
  75.    Menu, Tray, Add, Edit, Edit
  76.    Menu, Tray, Add, Reload, Reload
  77.    Menu, Tray, Add
  78. }
  79. Menu, Tray, Add, Exit, ExitApp
  80.  
  81. HWINEVENTHOOK := SetWinEventHook(EVENT_SYSTEM_MOVESIZESTART, EVENT_SYSTEM_MOVESIZEEND, 0
  82.                                           , RegisterCallback("WinGetControlPos", "F"), 0, 0, 0)
  83. OnExit, Exit
  84.  
  85. OnMessage(0x201, "WM_LBUTTONDOWN")
  86. OnMessage(0x111, "WM_COMMAND")
  87. OnMessage(0x214, "WM_SIZING")
  88.  
  89. ShowWindowOnStart ? ShowTranslation("", "", "en", "ru")
  90. Return
  91.  
  92. Open:
  93.   if !WinExist("Najeeb Google Translate ahk_pid" ScriptPID) || AllowMultiWindow
  94.    ShowTranslation("", "", "en", "ru")
  95.    return
  96.  
  97. ShowWindowOnStart:
  98. AllowMultiWindow:
  99. SaveSize:
  100.   Menu, Tray, ToggleCheck, % A_ThisMenuItem
  101.    %A_ThisLabel% := !%A_ThisLabel%
  102.    IniWrite, % %A_ThisLabel%, % IniName, Settings, %A_ThisLabel%
  103.    return
  104.  
  105. Edit:
  106.    Edit
  107.    return
  108.  
  109. Reload:
  110.    Reload
  111.    return
  112.  
  113. Hotkey:
  114.    Menu, Tray, ToggleCheck, % A_ThisMenuItem
  115.  
  116.    if InStr(A_ThisMenuItem, "Ins")
  117.       Hotkey := InStr(Hotkey, "Ins") ? RegExReplace(Hotkey, "Ins") : Hotkey . "Ins"
  118.    if InStr(A_ThisMenuItem, "C+C")
  119.       Hotkey := InStr(Hotkey, "C") ? RegExReplace(Hotkey, "C") : Hotkey . "C"
  120.  
  121.    Hotkey, ~^Ins, % InStr(Hotkey, "Ins") ? "On" : "Off"
  122.    Hotkey, ~^vk43, % InStr(Hotkey, "C") ? "On" : "Off"
  123.  
  124.    IniWrite, % Hotkey, % IniName, Hotkey, Hotkey
  125.    return
  126.  
  127. HotkeyRun:
  128.   DoublePress()
  129.    return
  130.  
  131. ExchangeLang:
  132.   GuiControlGet, to,, ComboBox1
  133.    GuiControlGet, from,, ComboBox2
  134.  
  135.    SourceLangNames := TargetLangNames := ""
  136.    for k In LangArray
  137.       SourceLangNames .= "|" . k . (k = from ? "|" : "")
  138.     , TargetLangNames .= "|" . k . (k = to   ? "|" : "")
  139.  
  140.    SourceLangNames := RegExReplace(SourceLangNames, "\|$", "||")
  141.  
  142.    GuiControl,, ComboBox1, % SourceLangNames
  143.    GuiControl,, ComboBox2, % TargetLangNames
  144.    GuiControl, Focus, Edit1
  145.    return
  146.  
  147. Exit:
  148.    WinGet, List, List, % "Najeeb Google Translate ahk_pid" ScriptPID
  149.    Loop % List
  150.    {
  151.       if (List%A_Index% = GUIs.1)
  152.       {
  153.          IniWriteSizeGui(GUIs.1), SaveSize ? IniWriteSizeEdit(GUIs.1)
  154.          break
  155.       }
  156.    }
  157.    DllCall("UnhookWinEvent", Ptr, HWINEVENTHOOK)
  158.    GUIs := LangArray := Voice := ""
  159. ExitApp:
  160.    ExitApp
  161.  
  162. ^!vk56::SendInput, {Raw}%MainTransText%
  163.  
  164. #If hActive := WinActive("Najeeb Google Translate ahk_pid" ScriptPID)
  165. Esc::WinClose, A
  166. Enter::ControlClick, Button4, % "Najeeb Google Translate ahk_pid" ScriptPID
  167. ^Tab::
  168.    Gui, %hActive%:Default
  169.    GuiControl, Focus, Static1
  170.    Gosub, ExchangeLang
  171.    Return
  172.  
  173. #If WinActive("Najeeb Google Translate ahk_pid" ScriptPID) && NN := GetEditFocus()
  174. ^WheelUp::
  175. ^WheelDown::
  176.    InStr(A_ThisHotkey, "Up") ? ++Edit%NN%FontSize : --Edit%NN%FontSize
  177.    Edit%NN%FontSize < 6 ? Edit%NN%FontSize := 6
  178.    Edit%NN%FontSize > 25 ? Edit%NN%FontSize := 25
  179.    ToolTip % "FontSize = " . Edit%NN%FontSize
  180.  
  181.    Gui, % WinExist("A") . ":Default"
  182.    Gui, Font, % "q5 s" . Edit%NN%FontSize, Verdana
  183.    GuiControl, Font, Edit%NN%
  184.    SetTimer, IniWriteFontSize, -500
  185.    return
  186.  
  187. IniWriteFontSize:
  188.   ToolTip
  189.    IniWrite, %Edit1FontSize%, % IniName, Location, Edit1FontSize
  190.    IniWrite, %Edit2FontSize%, % IniName, Location, Edit2FontSize
  191.    return
  192.  
  193. DoublePress()
  194. {
  195.    static pressed1 = 0
  196.    if pressed1 and A_TimeSincePriorHotkey <= 400 And Clipboard
  197.    {
  198.       pressed1 := 0
  199.       if (!(hwnd := WinExist("Najeeb Google Translate ahk_pid" ScriptPID)) || AllowMultiWindow)
  200.          GuiActive := GetActiveWindow(), Translate(RegExReplace(Clipboard, "\R", "`r`n"))
  201.       else
  202.          TranslateInTheSameWindow(hwnd, Clipboard)
  203.    }
  204.    else
  205.       pressed1 := 1
  206. }
  207.  
  208. Translate(str, ByRef _from="", ByRef _to="", NewWindow = 1)
  209. {
  210.    if !Ping("translate.google.com")
  211.    {
  212.       MsgBox, 16, Error!, No response from server.`nCheck connection with internet!
  213.       Return
  214.    }
  215.  
  216.    if (_from = "" && _to = "")
  217.    {
  218.       cyr := RegExMatch(str, "[А-Яа-я]")
  219.        from := cyr ? "ru" : "auto", to := cyr ? "en" : "ru"
  220.    }
  221.    else
  222.       from := _from, to := _to
  223.  
  224.    json := SendRequest(str,to,from,proxy:="")
  225.    JS := new ActiveScript("JScript")
  226.    JS.eval("delete ActiveXObject; delete GetObject;")
  227.    try oJSON := JS.eval("(" . JSON . ")")
  228.    catch  {
  229.       Clipboard := JSON
  230.       MsgBox, % 16, Error, % "The full error text is in ClipBoard`n`n" . SubStr(JSON, 1, 500)
  231.       Return
  232.    }
  233.  
  234.    if !IsObject(oJSON[1])
  235.       Loop % oJSON[0].length
  236.          trans .= oJSON[0][A_Index - 1][0]
  237.    else  {
  238.       MainTransText := oJSON[0][0][0]
  239.       Loop % oJSON[1].length  {
  240.          trans .= "`n+"
  241.          obj := oJSON[1][A_Index-1][1]
  242.          Loop % obj.length  {
  243.             txt := obj[A_Index - 1]
  244.             trans .= (MainTransText = txt ? "" : "`n" txt)
  245.          }
  246.       }
  247.    }
  248.    if !IsObject(oJSON[1])
  249.       MainTransText := trans := Trim(trans, ",+`n ")
  250.    else
  251.       trans := MainTransText . "`n+`n" . Trim(trans, ",+`n ")
  252.  
  253.    from := oJSON[2]
  254.    trans := Trim(trans, ",+`n ")
  255.  
  256.    If NewWindow
  257.       ShowTranslation(Clipboard, trans, from, to)
  258.    else
  259.    {
  260.       _from := from, _to := to
  261.       Return trans
  262.    }
  263. }
  264.  
  265. URIEncode(Str)
  266. {
  267.    b_Format:=A_FormatInteger
  268.    SetFormat, IntegerFast, H
  269.    Loop, % StrPutVar(Str, Var, "UTF-8")
  270.    {
  271.       Ch:=NumGet(Var, A_Index-1, "UChar")
  272.       If Ch=0
  273.          Break
  274.       If (Ch>0x7f Or Ch<0x30 Or Ch=0x3d)
  275.          s.="%"((StrLen(c:=SubStr(Ch, 3))<2) ? "0"c:c)
  276.       else
  277.          s.=Chr(Ch)
  278.    }
  279.    SetFormat, IntegerFast, % b_Format
  280.    Return, s
  281. }
  282.  
  283. WM_LBUTTONDOWN()
  284. {
  285.    PostMessage, WM_NCLBUTTONDOWN := 0xA1, HTCAPTION := 2
  286. }
  287.  
  288. ShowTranslation(SourceText, TransText, from, to)
  289. {
  290.    static PlayPause1, PlayPause2, Stop1, Stop2, hGui, Control, ExChange1, ExChange2, Source
  291.         , hIconPlayPause, hIconStop, hIconTranslate, hIconGoogle, hIconChange, Icons
  292.         , hButtPlayPause1, hButtPlayPause2, hButtStop1, hButtStop2, hButtTranslate, hButtChange1, hButtChange2
  293.         , BS_ICON := 0x40, BM_SETIMAGE := 0xF7, ES_NOHIDESEL := 0x100
  294.         , WMSZ_TOP := 3, WMSZ_TOPLEFT := 4, WMSZ_TOPRIGHT := 5
  295.  
  296.    Gui, New, +AlwaysOnTop +LastFound +Resize +hwndhGui +Owner +MinSize199x220 -MaximizeBox
  297.    Gui, Color, %ColorGui%
  298.  
  299.    oSize := IniReadSizeEdit()
  300.    if (SaveSize && Edit1H := oSize.Edit1H)
  301.       Edit1W := oSize.Edit1W, Edit2H := oSize.Edit2H
  302.  
  303.    Gui, Font, q5 s8, Verdana
  304.    Gui, Add, Text, % "x" (Edit1W > 310 ? 10 + (Edit1W - 310)//2 : 10) " y15", Source Language:
  305.  
  306.    SourceLangNames := TargetLangNames := ""
  307.    For k,v In LangArray
  308.       SourceLangNames .= (A_Index = 1 ? "" : "|") . k . (v = from ? "|" : "")
  309.     , TargetLangNames .= (A_Index = 1 ? "" : "|") . k . (v = to   ? "|" : "")
  310.  
  311.    SourceLangNames := RegExReplace(SourceLangNames, "\|$", "||")
  312.  
  313.    Gui, Add, DDL, % "x" (Edit1W > 310 ? 112 + (Edit1W - 310)//2 : 112) " yp-4 w182", % SourceLangNames
  314.    Gui, Add, Button, % "x" (Edit1W > 310 ? 297 + (Edit1W - 310)//2 : 297)
  315.                      . " yp-1 w23 h23 " BS_ICON " hwndhButtChange1 gExchangeLang vExChange1"
  316.  
  317.    Gui, Font, q5 s%Edit1FontSize%, Verdana
  318.    if !(SaveSize && Edit1H)
  319.    {
  320.       Gui, Add, Edit, x10 y+9 w310 Multi %ES_NOHIDESEL% vSource, % SourceText
  321.       GuiControlGet, Edit1, Pos
  322.  
  323.       Edit1H < 45 ? Edit1H := 45
  324.       Edit1H > 250 ? Edit1H := 250
  325.       GuiControl, Move, Edit1, h%Edit1H%
  326.    }
  327.    else
  328.       Gui, Add, Edit, % "x10 y+9 w" (Edit1W ? Edit1W : 310) " Multi h" Edit1H " vSource " ES_NOHIDESEL, % SourceText
  329.  
  330.    GuiControlGet, Edit1, Pos
  331.    Gui, Font, q5 s8, Verdana
  332.    Gui, Add, Button, % "x10 y" Edit1Y + Edit1H + 5 " w52 h23 hwndhButtPlayPause1 gPlayPause vPlayPause1 " BS_ICON
  333.    Gui, Add, Button, % "x+3 yp w52 h23 " BS_ICON " hwndhButtStop1 gStop vStop1" (Player ? "" : " Disabled")
  334.    Gui, Add, Button, % "x" (Edit1W ? Edit1W - 60 : 250) " yp w70 h23 " BS_ICON " hwndhButtTranslate gTranslate"
  335.  
  336.    Gui, Add, Text, % "x" (Edit1W > 310 ? 10 + (Edit1W - 310)//2 : 10) " y+20", Translated Lang:
  337.  
  338.    Gui, Add, DDL, % "x" (Edit1W > 310 ? 112 + (Edit1W - 310)//2 : 112) " yp-4 w182", % TargetLangNames
  339.    Gui, Add, Button, % "x" (Edit1W > 310 ? 297 + (Edit1W - 310)//2 : 297)
  340.                      . " yp-1 w23 h23 " BS_ICON " hwndhButtChange2 gExchangeLang vExChange2"
  341.  
  342.    Gui, Font, q5 s%Edit2FontSize%, Verdana
  343.    if !(SaveSize && Edit1H := oSize.Edit1H)
  344.    {
  345.       Gui, Add, Edit, x10 y+9 w310 Multi %ES_NOHIDESEL%, % TransText
  346.       GuiControlGet, Edit2, Pos
  347.  
  348.       Edit2H < 45 ? Edit2H := 45
  349.       Edit2H > 250 ? Edit2H := 250
  350.       GuiControl, Move, Edit2, h%Edit2H%
  351.    }
  352.    else
  353.       Gui, Add, Edit, % "x10 y+9 w" (Edit1W ? Edit1W : 310) " Multi h" Edit2H " " ES_NOHIDESEL, % TransText
  354.  
  355.    GuiControlGet, Edit2, Pos
  356.    Gui, Font, q5 s8, Verdana
  357.    Gui, Add, Button, % "x10 y" Edit2Y + Edit2H + 5 " w52 h23 hwndhButtPlayPause2 gPlayPause vPlayPause2 " BS_ICON
  358.    Gui, Add, Button, % "x+3 yp w52 h23 " BS_ICON " hwndhButtStop2 gStop vStop2" (Player ? "" : " Disabled")
  359.    Gui, Add, Button, % "x" (Edit1W ? Edit1W - 60 : 250) " yp w70 h23 gGuiClose", OK
  360.  
  361.    if !IsObject(Icons)
  362.    {
  363.       hIconGoogle := ExtractIcon("Google16", 16)
  364.       hIconChange := ExtractIcon("Change", 16)
  365.       hIconPlayPause := ExtractIcon("PlayPause", 25)
  366.       hIconStop := ExtractIcon("Stop", 12)
  367.       hIconTranslate := ExtractIcon("Translate", 16)
  368.       Icons := [hIconGoogle, hIconChange, hIconPlayPause, hIconStop, hIconTranslate]
  369.    }
  370.  
  371.    DetectHiddenWindows, On
  372.    Loop 2
  373.    {
  374.       PostMessage, BM_SETIMAGE, IMAGE_ICON, hIconChange      ,, % "ahk_id" hButtChange%A_Index%
  375.       PostMessage, BM_SETIMAGE, IMAGE_ICON, hIconPlayPause   ,, % "ahk_id" hButtPlayPause%A_Index%
  376.       PostMessage, BM_SETIMAGE, IMAGE_ICON, hIconStop         ,, % "ahk_id" hButtStop%A_Index%
  377.    }
  378.    PostMessage, BM_SETIMAGE, IMAGE_ICON, hIconTranslate,, ahk_id %hButtTranslate%
  379.    PostMessage, WM_SETICON,, hIconGoogle,, ahk_id %hGui%
  380.    PostMessage, WM_INPUTLANGCHANGEREQUEST,, GetLayoutList()[from = "ru" ? "Ru" : "En"],, ahk_id %A_ScriptHwnd%
  381.    DetectHiddenWindows, Off
  382.  
  383.    GuiControlGet, Button8, Pos
  384.    Gui, Show, % "hide h" Button8Y + Button8H + 6, Najeeb Google Translate
  385.    GuiControl, Focus, Edit1
  386.  
  387.    if !WinExist("Najeeb Google Translate ahk_pid" ScriptPID)
  388.    {
  389.       IniRead, xGUI, % IniName, Location, xGUI, % " "
  390.       IniRead, yGUI, % IniName, Location, yGUI, % " "
  391.       Gui, Show, % (xGUI = "" ? "" : "x" xGUI " y" yGUI " ") "w" (SaveSize && Edit1W ? Edit1W + 20 : 330)
  392.    }
  393.    else
  394.    {
  395.       WinGetPos, X, Y,,, % "ahk_id " (GuiActive ? GuiActive : GUIs[GUIs.MaxIndex()])
  396.       Gui, Show, % "x" X + 60 " y" Y + 40 " w" (SaveSize && Edit1W ? Edit1W + 20 : 330)
  397.    }
  398.    GUIs.Insert(hGui), GuiActive := ""
  399.    Return
  400.  
  401. GuiSize:
  402.    if !IsControlPos
  403.       return
  404.  
  405.    SetWinDelay, 0
  406.    if (SIZING ~= WMSZ_TOP "|" WMSZ_TOPLEFT "|" WMSZ_TOPRIGHT)
  407.    {
  408.       if A_GuiHeight - Edit1HPos > 32
  409.          Resizing(A_GuiWidth, A_GuiHeight, 1)
  410.       else
  411.       {
  412.          if (GetControlPos = "")
  413.             _WinGetControlPos(A_Gui), GetControlPos := 1
  414.          Resizing(A_GuiWidth, A_GuiHeight, 2)
  415.       }
  416.    }
  417.    else
  418.    {
  419.       if A_GuiHeight - Edit2HPos > 32
  420.          Resizing(A_GuiWidth, A_GuiHeight, 2)
  421.       else
  422.       {
  423.          if (GetControlPos = "")
  424.             _WinGetControlPos(A_Gui), GetControlPos := 1
  425.          Resizing(A_GuiWidth, A_GuiHeight, 1)
  426.       }
  427.    }
  428.    return
  429.  
  430. PlayPause:
  431.   if IsObject(Player)
  432.    {
  433.       if Playing := !Playing
  434.          Player.Controls.pause()
  435.       else
  436.          Player.Controls.play()
  437.    }
  438.    else
  439.    {
  440.       hGui := A_Gui, Control := A_GuiControl
  441.       SetTimer, PlayPauseTimer, -1
  442.    }
  443.    return
  444.  
  445. PlayPauseTimer:
  446.   n := SubStr(Control, 0)
  447.    ControlGet, Text, Selected,, Edit%n%, ahk_id %hGui%
  448.    if (Text = "")
  449.       ControlGetText, Text, Edit%n%, ahk_id %hGui%
  450.    ControlGetText, lng, ComboBox%n%, ahk_id %hGui%
  451.    Say(RegExReplace(Text, "\R+", "`n"), LangArray[lng])
  452.    return
  453.  
  454. Stop:
  455.   Player.close()
  456.    Playing := Player := ""
  457.    StopButtonEnableDisable(0)
  458.    return
  459.  
  460. Translate:
  461.   GuiControlGet, from,, ComboBox1
  462.    from := LangArray[from]
  463.    GuiControlGet, to,, ComboBox2
  464.    to := LangArray[to]
  465.    GuiControlGet, SourseText,, Edit1
  466.    if (SourseText = "")  {
  467.       ToolTip Enter the text to translate into the window!
  468.       Sleep, 1500
  469.       ToolTip
  470.       Return
  471.    }
  472.    TransText := Translate(RegExReplace(SourseText, "\R", "`r`n"), from, to, 0)
  473.  
  474.    GuiControl,, Edit2, % TransText
  475.    GuiControl, Focus, Edit1
  476.    Return
  477.  
  478. GuiClose:
  479.    if (A_Gui = GUIs.1)
  480.       IniWriteSizeGui(A_Gui), SaveSize ? IniWriteSizeEdit(A_Gui)
  481.    else
  482.    {
  483.       for k,v in GUIs
  484.          if (A_Gui = v)
  485.             break
  486.       GUIs.Remove(k)
  487.    }
  488.  
  489.    Gui, %A_Gui%: Destroy
  490.    IfWinNotExist, Najeeb Google Translate ahk_pid %ScriptPID%
  491.    {
  492.       Player.close(), Player := "", Voice := []
  493.       FileDelete, % A_ScriptDir "\mp3\*.mp3"
  494.       Loop % Icons.MaxIndex()
  495.          DllCall("DestroyIcon", Ptr, Icons[A_Index])
  496.       Icons := "", GUIs := []
  497.    }
  498.    return
  499. }
  500.  
  501. TranslateInTheSameWindow(hwnd, SourceText)
  502. {
  503.    Translation := Translate(RegExReplace(SourceText, "\R", "`r`n"), from, to, 0)
  504.  
  505.    For k,v In LangArray
  506.       SourceLangNames .= "|" . k . (v = from ? "|" : "")
  507.     , TargetLangNames .= "|" . k . (v = to   ? "|" : "")
  508.  
  509.    Gui, %hwnd%:Default
  510.    GuiControl,, ComboBox1, % SourceLangNames
  511.    GuiControl,, ComboBox2, % TargetLangNames
  512.    GuiControl,, Edit1, % SourceText
  513.    GuiControl,, Edit2, % Translation
  514. }
  515.  
  516. Resizing(W, H, mode)
  517. {
  518.    E1H := H - Edit1HPos
  519.    E2Y := H - Edit2YPos, E2H := H - Edit2HPos
  520.    B2Y := H - Button2YPos, B6Y := H - Button6YPos
  521.  
  522.    if mode = 1
  523.    {
  524.       GuiControl, Move, Edit1, % "w" W - 20 " h" E1H
  525.       GuiControl, Move, Edit2, % "w" W - 20 " y" E2Y
  526.       GuiControl, MoveDraw, Button2, % "y" B2Y
  527.       GuiControl, MoveDraw, Button3, % "y" B2Y
  528.       GuiControl, MoveDraw, Button4, % "x" (W > 200 ? W - 80 : 120) " y" B2Y
  529.       GuiControl, MoveDraw, Static2, % "x" (W > 330 ? 10 + (W - 330)//2 : 10) " y" B2Y + 43
  530.       GuiControl, Move, ComboBox2, % "x" (W > 330 ? 112 + (W - 330)//2 : 112) " y" B2Y + 39
  531.       GuiControl, MoveDraw, Button5, % "x" (W > 330 ? 297 + (W - 330)//2 : 297) " y" B2Y + 38
  532.    }
  533.    else
  534.    {
  535.       GuiControl, Move, Edit1, % "w" W - 20
  536.       GuiControl, Move, Edit2, % "w" W - 20 " h" (E2H > 32 ? E2H : 32)
  537.       GuiControl, MoveDraw, Static2, % "x" (W > 330 ? 10 + (W - 330)//2 : 10)
  538.       GuiControl, Move, ComboBox2, % "x" (W > 330 ? 112 + (W - 330)//2 : 112)
  539.       GuiControl, MoveDraw, Button5, % "x" (W > 330 ? 297 + (W - 330)//2 : 297)
  540.       GuiControl, MoveDraw, Button4, % "x" (W > 200 ? W - 80 : 120)
  541.    }
  542.    GuiControl, Move, Static1, % "x" (W > 330 ? 10 + (W - 330)//2 : 10)
  543.    GuiControl, Move, ComboBox1, % "x" (W > 330 ? 112 + (W - 330)//2 : 112)
  544.    GuiControl, MoveDraw, Button1, % "x" (W > 330 ? 297 + (W - 330)//2 : 297)
  545.    GuiControl, MoveDraw, Button6, % "y" B6Y
  546.    GuiControl, MoveDraw, Button7, % "y" B6Y
  547.    GuiControl, MoveDraw, Button8, % "x" (W > 200 ? W - 80 : 120) " y" B6Y
  548. }
  549.  
  550. Ping(strHost)
  551. {
  552.    Loop 4
  553.       bRet := ComObjGet("winmgmts:").Get("Win32_PingStatus.address='" . strHost . "'").StatusCode = 0
  554.    until bRet
  555.    return bRet
  556. }
  557.  
  558. Say(Text, lng)
  559. {
  560.    static PlaylistOpenNoMedia := 6, Stopped := 1
  561.  
  562.    Player := ComObjCreate("WMPlayer.OCX")
  563.    objPlaylist := Player.currentPlaylist
  564.  
  565.    for k,v in Voice
  566.       if (v.Text = RegExReplace(Text, "^\s*(\S.+\S)(\s+$|$)", "$1") && v.lng = lng)
  567.       {
  568.          var := 1
  569.          break
  570.       }
  571.  
  572.    if var
  573.    {
  574.       Loop % v.mp3.MaxIndex()
  575.          objPlaylist.appendItem(Player.newMedia(v.mp3[A_Index]))
  576.    }
  577.    else
  578.    {
  579.       if !Ping("translate.google.com")
  580.       {
  581.          MsgBox, 16, Error!, No response from the server.`nCheck the connection with the Internet!
  582.          Return
  583.       }
  584.  
  585.       PreUrl := "https://translate.google.ru/translate_tts?ie=UTF-8&tl=" lng "&total=1&idx=0&client=t&prev=input"
  586.       Strings := []
  587.       If StrLen(Text) > 100
  588.       {
  589.          StartPos := 1
  590.          While StartPos := RegExMatch(Text, ".+?(\.|$)", Found, StartPos) + StrLen(Found)
  591.          {
  592.             if StrLen(Found) > 100
  593.             {
  594.                StartPos_ := 1
  595.                While StartPos_ := RegExMatch(Found, "(.{1,99}([ ,\t\n]|$))|(.{1,100})", Found_, StartPos_) + StrLen(Found_)
  596.                   Strings.Insert(Found_)
  597.             }
  598.             else
  599.                Strings.Insert(Found)
  600.          }
  601.       }
  602.       else
  603.          Strings.1 := Text
  604.  
  605.       if !FileExist(A_ScriptDir "\mp3")
  606.          FileCreateDir, %A_ScriptDir%\mp3
  607.  
  608.       Voice.Insert(o := {Text: RegExReplace(Text, "^\s*(\S.+\S)(\s+$|$)", "$1"), lng: lng, mp3: []})
  609.       Loop % Strings.MaxIndex()
  610.       {
  611.          txt := Strings[A_Index]
  612.          URLDownloadToFile, % PreUrl . "&textlen=" . StrLen(txt) . "&tk=" . TK(txt)
  613.             . "&q=" URIEncode(RegExReplace(Strings[A_Index], "^\s*([^\s]+)\s*$", "$1"))
  614.             , % mp3file := A_ScriptDir "\mp3\" A_TickCount ".mp3"
  615.          objPlaylist.appendItem(Player.newMedia(mp3file))
  616.          o.mp3.Insert(mp3file)
  617.       }
  618.    }
  619.    StopButtonEnableDisable(1)
  620.    Player.Controls.play()
  621.    While Player.PlayState != Stopped && Player.OpenState != PlaylistOpenNoMedia && IsObject(Player)
  622.       Sleep, 100
  623.    Player.close(), Player := objPlaylist := ""
  624.    StopButtonEnableDisable(0)
  625. }
  626.  
  627. StopButtonEnableDisable(key)
  628. {
  629.    WinGet, List, List, Najeeb Google Translate ahk_pid %ScriptPID%
  630.    Loop % List
  631.    {
  632.       Control , % key ? "Enable" : "Disable",, Button3, % "ahk_id" List%A_Index%
  633.       Control , % key ? "Enable" : "Disable",, Button7, % "ahk_id" List%A_Index%
  634.    }
  635. }
  636.  
  637. WM_COMMAND(wp, lp)
  638. {
  639.    static EN_SETFOCUS := 0x100, CBN_SETFOCUS := 3
  640.  
  641.    if !WinActive("Najeeb Google Translate") || !(wp>>16 = EN_SETFOCUS || wp>>16 = CBN_SETFOCUS)
  642.       return
  643.  
  644.    Gui, %A_Gui%:Default
  645.    DetectHiddenWindows, On
  646.  
  647.    if (wp>>16 = EN_SETFOCUS)
  648.    {
  649.       GuiControlGet, Name, Name, %lp%
  650.       if Name != Source
  651.          return
  652.  
  653.       GuiControlGet, lang,, ComboBox1
  654.       PostMessage, WM_INPUTLANGCHANGEREQUEST,, GetLayoutList()[lang = "Russian" ? "Ru" : "En"],, ahk_id %A_ScriptHwnd%
  655.    }
  656.  
  657.    if (wp>>16 = CBN_SETFOCUS)
  658.       PostMessage, WM_INPUTLANGCHANGEREQUEST,, GetLayoutList().Ru,, ahk_id %A_ScriptHwnd%
  659.  
  660.    DetectHiddenWindows, Off
  661. }
  662.  
  663. GetLayoutList()
  664. {
  665.    SetFormat, IntegerFast, H
  666.    VarSetCapacity(List, A_PtrSize*2)
  667.    DllCall("GetKeyboardLayoutList", Int, 2, Ptr, &List)
  668.    Locale1 := NumGet(List)
  669.    b := SubStr(Locale2 := NumGet(List, A_PtrSize), -3) = 0409
  670.    En := b ? Locale2 : Locale1
  671.    Ru := b ? Locale1 : Locale2
  672.    SetFormat, IntegerFast, D
  673.    Return {En: En, Ru: Ru}
  674. }
  675.  
  676. WM_SIZING(wp)
  677. {
  678.    SIZING := wp
  679. }
  680.  
  681. SetWinEventHook(eventMin, eventMax, hmodWinEventProc, lpfnWinEventProc, idProcess, idThread, dwFlags)
  682. {
  683.    return DllCall("SetWinEventHook" , UInt, eventMin, UInt, eventMax
  684.                                     , Ptr, hmodWinEventProc, Ptr, lpfnWinEventProc
  685.                                     , UInt, idProcess, UInt, idThread
  686.                                     , UInt, dwFlags, Ptr)
  687. }
  688.  
  689. WinGetControlPos(hWinEventHook, event, hwnd)
  690. {
  691.    if !GetActiveWindow()
  692.       return
  693.  
  694.    if (event = EVENT_SYSTEM_MOVESIZEEND)
  695.       IsControlPos := GetControlPos := "", CorrectPos(hwnd)
  696.    else
  697.       _WinGetControlPos(hwnd)
  698. }
  699.  
  700. _WinGetControlPos(hwnd)
  701. {
  702.    Gui, %hwnd%:Default
  703.  
  704.    GuiControlGet, Edit1, Pos
  705.    GuiControlGet, Edit2, Pos
  706.    GuiControlGet, Button2, Pos
  707.    GuiControlGet, Button6, Pos
  708.  
  709.    VarSetCapacity(Rect, 16)
  710.    DllCall("GetClientRect", Ptr, hwnd, Ptr, &Rect)
  711.    ClientH := NumGet(Rect, 12, "UInt")/A_ScreenDPI*96
  712.  
  713.    Edit1HPos := ClientH - Edit1H
  714.    Edit2HPos := ClientH - Edit2H
  715.    Edit2YPos := ClientH - Edit2Y
  716.    Button2YPos := ClientH - Button2Y
  717.    Button6YPos := ClientH - Button6Y
  718.    IsControlPos := 1
  719. }
  720.  
  721. CorrectPos(hwnd)
  722. {
  723.    VarSetCapacity(Rect, 16)
  724.    DllCall("GetClientRect", Ptr, hwnd, Ptr, &Rect)
  725.    ClientH := NumGet(Rect, 12, "UInt")/A_ScreenDPI*96
  726.  
  727.    Gui, %hwnd%:Default
  728.    GuiControlGet, Edit2, Pos
  729.    GuiControl, Move, Edit2, % "h" ClientH - Edit2Y - 34
  730.  
  731.    Loop 3
  732.       GuiControl, MoveDraw, % "Button" A_Index + 5, % "y" ClientH - 29
  733. }
  734.  
  735. GetActiveWindow()
  736. {
  737.    WinGetActiveTitle, Title
  738.    WinGetClass, Class, A
  739.    WinGet, PID, PID, A
  740.    Return (Title = "Najeeb Google Translate" && Class = "AutoHotkeyGUI" && PID = ScriptPID) ? WinExist("A") : ""
  741. }
  742.  
  743. IniReadSizeEdit()
  744. {
  745.    IniRead, Edit1W, % IniName, Location, Edit1W, % " "
  746.    IniRead, Edit1H, % IniName, Location, Edit1H, % " "
  747.    IniRead, Edit2H, % IniName, Location, Edit2H, % " "
  748.    Return {Edit1W: Edit1W, Edit1H: Edit1H, Edit2H: Edit2H}
  749. }
  750.  
  751. IniWriteSizeEdit(hwnd)
  752. {
  753.    ControlGetPos,,, Edit1W, Edit1H, Edit1, % "ahk_id" hwnd
  754.    ControlGetPos,,,, Edit2H, Edit2, % "ahk_id" hwnd
  755.  
  756.    IniWrite, % Edit1W, % IniName, Location, Edit1W
  757.    IniWrite, % Edit1H, % IniName, Location, Edit1H
  758.    IniWrite, % Edit2H, % IniName, Location, Edit2H
  759. }
  760.  
  761. IniWriteSizeGui(hwnd)
  762. {
  763.    WinGetPos, X, Y,,, % "ahk_id" hwnd
  764.    IniWrite, % X, % IniName, Location, xGUI
  765.    IniWrite, % Y, % IniName, Location, yGUI
  766. }
  767.  
  768. GetEditFocus()
  769. {
  770.    if !WinActive("Najeeb Google Translate ahk_pid" ScriptPID)
  771.       return
  772.  
  773.    ControlGetFocus, Control, A
  774.    return InStr(Control, "Edit") ? SubStr(Control, 0) : ""
  775. }
  776.  
  777. SendRequest(str, tl := "", sl := "", proxy := "") {
  778.    ComObjError(false)
  779.    http := ComObjCreate("Microsoft.XmlHttp")
  780.    proxy ? http.SetProxy(2, proxy) : "", tl ? "" : tl := "en"
  781.    http.open( "POST", "https://translate.google.com/translate_a/single?client=webapp&sl="
  782.       . (sl ? sl : "auto") "&tl=" tl "&hl=" tl
  783.       . "&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=0&ssel=0&tsel=0&pc=1&kc=1"
  784.       . "&tk=" TK(str), false )
  785.  
  786.    http.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8")
  787.    http.SetRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0")
  788.    http.send("q=" URIEncode(str))
  789.    res := http.responseText
  790.    Return res
  791. }
  792.  
  793. TK(string)  {
  794.    js := new ActiveScript("JScript")
  795.    js.Exec(GetJScript())
  796.    Return js.tk(string)
  797. }
  798.  
  799. StrPutVar(string, ByRef var, encoding = "CP0")
  800. {
  801.     VarSetCapacity( var, StrPut(string, encoding) * ((encoding="utf-16"||encoding="cp1200") ? 2 : 1) )
  802.     return StrPut(string, &var, encoding)
  803. }
  804.  
  805. ExtractIcon(name, size)
  806. {
  807.    Base64 := GetBase64String(name)
  808.    Return hIcon := CreateIconFromBase64(Base64, size)
  809. }
  810.  
  811. CreateIconFromBase64(StringBASE64, Size)
  812. {
  813.    StringBase64ToData(StringBASE64, IconData)
  814.    Return DllCall("CreateIconFromResourceEx", Ptr, &IconData + 4
  815.       , UInt, NumGet(&IconData, "UInt"), UInt, true, UInt, 0x30000, Int, Size, Int, Size, UInt, 0)
  816. }
  817.  
  818. StringBase64ToData(StringBase64, ByRef OutData)
  819. {
  820.    DllCall("Crypt32.dll\CryptStringToBinary", Ptr, &StringBase64
  821.       , UInt, StrLen(StringBase64), UInt, CRYPT_STRING_BASE64 := 1, UInt, 0, UIntP, Bytes, UIntP, 0, UIntP, 0)
  822.  
  823.    VarSetCapacity(OutData, Bytes)
  824.    DllCall("Crypt32.dll\CryptStringToBinary", Ptr, &StringBase64
  825.       , UInt, StrLen(StringBase64), UInt, CRYPT_STRING_BASE64, Str, OutData, UIntP, Bytes, UIntP, 0, UIntP, 0)
  826.    Return Bytes
  827. }
  828.  
  829. ModifyTrayIcon(hIcon, hGui, uID = 0x404)
  830. {
  831.    static NIM_MODIFY := 1, NIF_ICON := 2
  832.    VarSetCapacity(NOTIFYICONDATA, size := A_PtrSize = 8 ? 848 : A_IsUnicode? 828 : 444, 0)
  833.    NumPut(size, NOTIFYICONDATA, "UInt")
  834.    NumPut(hGui, NOTIFYICONDATA, A_PtrSize)
  835.    NumPut(uID, NOTIFYICONDATA, 2*A_PtrSize, "UInt")
  836.    NumPut(NIF_ICON, NOTIFYICONDATA, 2*A_PtrSize + 4, "UInt")
  837.    NumPut(hIcon, NOTIFYICONDATA, 3*A_PtrSize+8)
  838.  
  839.    res := DllCall("shell32\Shell_NotifyIcon", UInt, NIM_MODIFY, Ptr, &NOTIFYICONDATA)
  840.    ; DllCall("DestroyIcon", Ptr, hIcon)
  841. "(.*)\|.*", "$1")
  842.       Value := RegExReplace(A_LoopField, ".*\|(.*)", "$1")
  843.       LangArray[Key] := Value
  844.    }
  845. }
  846.  
  847. /*
  848.  *  ActiveScript for AutoHotkey v1.1
  849.  *
  850.  *  Provides an interface to Active Scripting languages like VBScript and JScript,
  851.  *  without relying on Microsoft's ScriptControl, which is not available to 64-bit
  852.  *  programs.
  853.  *
  854.  *  License: Use, modify and redistribute without limitation, but at your own risk.
  855.  */
  856. class ActiveScript extends ActiveScript._base
  857. {
  858.     __New(Language)
  859.     {
  860.         if this._script := ComObjCreate(Language, ActiveScript.IID)
  861.             this._scriptParse := ComObjQuery(this._script, ActiveScript.IID_Parse)
  862.         if !this._scriptParse
  863.             throw Exception("Invalid language", -1, Language)
  864.         this._site := new ActiveScriptSite(this)
  865.         this._SetScriptSite(this._site.ptr)
  866.         this._InitNew()
  867.         this._objects := {}
  868.         this.Error := ""
  869.         this._dsp := this._GetScriptDispatch()  ; Must be done last.
  870.         try
  871.             if this.ScriptEngine() = "JScript"
  872.                 this.SetJScript58()
  873.     }
  874.  
  875.     SetJScript58()
  876.     {
  877.         static IID_IActiveScriptProperty := "{4954E0D0-FBC7-11D1-8410-006008C3FBFC}"
  878.         if !prop := ComObjQuery(this._script, IID_IActiveScriptProperty)
  879.             return false
  880.         VarSetCapacity(var, 24, 0), NumPut(2, NumPut(3, var, "short") + 6)
  881.         hr := DllCall(NumGet(NumGet(prop+0)+4*A_PtrSize), "ptr", prop, "uint", 0x4000
  882.             , "ptr", 0, "ptr", &var), ObjRelease(prop)
  883.         return hr >= 0
  884.     }
  885.  
  886.     Eval(Code)
  887.     {
  888.         pvar := NumGet(ComObjValue(arr:=ComObjArray(0xC,1)) + 8+A_PtrSize)
  889.         this._ParseScriptText(Code, 0x20, pvar)  ; SCRIPTTEXT_ISEXPRESSION := 0x20
  890.         return arr[0]
  891.     }
  892.  
  893.     Exec(Code)
  894.     {
  895.         this._ParseScriptText(Code, 0x42, 0)  ; SCRIPTTEXT_ISVISIBLE := 2, SCRIPTTEXT_ISPERSISTENT := 0x40
  896.         this._SetScriptState(2)  ; SCRIPTSTATE_CONNECTED := 2
  897.     }
  898.  
  899.     AddObject(Name, DispObj, AddMembers := false)
  900.     {
  901.         static a, supports_dispatch ; Test for built-in IDispatch support.
  902.             := a := ((a:=ComObjArray(0xC,1))[0]:=[42]) && a[0][1]=42
  903.         if IsObject(DispObj) && !(supports_dispatch || ComObjType(DispObj))
  904.             throw Exception("Adding a non-COM object requires AutoHotkey v1.1.17+", -1)
  905.         this._objects[Name] := DispObj
  906.         this._AddNamedItem(Name, AddMembers ? 8 : 2)  ; SCRIPTITEM_ISVISIBLE := 2, SCRIPTITEM_GLOBALMEMBERS := 8
  907.     }
  908.  
  909.     _GetObjectUnk(Name)
  910.     {
  911.         return !IsObject(dsp := this._objects[Name]) ? dsp  ; Pointer
  912.             : ComObjValue(dsp) ? ComObjValue(dsp)  ; ComObject
  913.             : &dsp  ; AutoHotkey object
  914.     }
  915.  
  916.     class _base
  917.     {
  918.         __Call(Method, Params*)
  919.         {
  920.             if ObjHasKey(this, "_dsp")
  921.                 try
  922.                     return (this._dsp)[Method](Params*)
  923.                 catch e
  924.                     throw Exception(e.Message, -1, e.Extra)
  925.         }
  926.  
  927.         __Get(Property, Params*)
  928.         {
  929.             if ObjHasKey(this, "_dsp")
  930.                 try
  931.                     return (this._dsp)[Property, Params*]
  932.                 catch e
  933.                     throw Exception(e.Message, -1, e.Extra)
  934.         }
  935.  
  936.         __Set(Property, Params*)
  937.         {
  938.             if ObjHasKey(this, "_dsp")
  939.             {
  940.                 Value := Params.Pop()
  941.                 try
  942.                     return (this._dsp)[Property, Params*] := Value
  943.                 catch e
  944.                     throw Exception(e.Message, -1, e.Extra)
  945.             }
  946.         }
  947.     }
  948.  
  949.     _SetScriptSite(Site)
  950.     {
  951.         hr := DllCall(NumGet(NumGet((p:=this._script)+0)+3*A_PtrSize), "ptr", p, "ptr", Site)
  952.         if (hr < 0)
  953.             this._HRFail(hr, "IActiveScript::SetScriptSite")
  954.     }
  955.  
  956.     _SetScriptState(State)
  957.     {
  958.         hr := DllCall(NumGet(NumGet((p:=this._script)+0)+5*A_PtrSize), "ptr", p, "int", State)
  959.         if (hr < 0)
  960.             this._HRFail(hr, "IActiveScript::SetScriptState")
  961.     }
  962.  
  963.     _AddNamedItem(Name, Flags)
  964.     {
  965.         hr := DllCall(NumGet(NumGet((p:=this._script)+0)+8*A_PtrSize), "ptr", p, "wstr", Name, "uint", Flags)
  966.         if (hr < 0)
  967.             this._HRFail(hr, "IActiveScript::AddNamedItem")
  968.     }
  969.  
  970.     _GetScriptDispatch()
  971.     {
  972.         hr := DllCall(NumGet(NumGet((p:=this._script)+0)+10*A_PtrSize), "ptr", p, "ptr", 0, "ptr*", pdsp)
  973.         if (hr < 0)
  974.             this._HRFail(hr, "IActiveScript::GetScriptDispatch")
  975.         return ComObject(9, pdsp, 1)
  976.     }
  977.  
  978.     _InitNew()
  979.     {
  980.         hr := DllCall(NumGet(NumGet((p:=this._scriptParse)+0)+3*A_PtrSize), "ptr", p)
  981.         if (hr < 0)
  982.             this._HRFail(hr, "IActiveScriptParse::InitNew")
  983.     }
  984.  
  985.     _ParseScriptText(Code, Flags, pvarResult)
  986.     {
  987.         VarSetCapacity(excp, 8 * A_PtrSize, 0)
  988.         hr := DllCall(NumGet(NumGet((p:=this._scriptParse)+0)+5*A_PtrSize), "ptr", p
  989.             , "wstr", Code, "ptr", 0, "ptr", 0, "ptr", 0, "uptr", 0, "uint", 1
  990.             , "uint", Flags, "ptr", pvarResult, "ptr", 0)
  991.         if (hr < 0)
  992.             this._HRFail(hr, "IActiveScriptParse::ParseScriptText")
  993.     }
  994.  
  995.     _HRFail(hr, what)
  996.     {
  997.         if e := this.Error
  998.         {
  999.             this.Error := ""
  1000.             throw Exception("`nError code:`t" this._HRFormat(e.HRESULT)
  1001.                 . "`nSource:`t`t" e.Source "`nDescription:`t" e.Description
  1002.                 . "`nLine:`t`t" e.Line "`nColumn:`t`t" e.Column
  1003.                 . "`nLine text:`t`t" e.LineText, -3)
  1004.         }
  1005.         throw Exception(what " failed with code " this._HRFormat(hr), -2)
  1006.     }
  1007.  
  1008.     _HRFormat(hr)
  1009.     {
  1010.         return Format("0x{1:X}", hr & 0xFFFFFFFF)
  1011.     }
  1012.  
  1013.     _OnScriptError(err) ; IActiveScriptError err
  1014.     {
  1015.         VarSetCapacity(excp, 8 * A_PtrSize, 0)
  1016.         DllCall(NumGet(NumGet(err+0)+3*A_PtrSize), "ptr", err, "ptr", &excp) ; GetExceptionInfo
  1017.         DllCall(NumGet(NumGet(err+0)+4*A_PtrSize), "ptr", err, "uint*", srcctx, "uint*", srcline, "int*", srccol) ; GetSourcePosition
  1018.         DllCall(NumGet(NumGet(err+0)+5*A_PtrSize), "ptr", err, "ptr*", pbstrcode) ; GetSourceLineText
  1019.         code := StrGet(pbstrcode, "UTF-16"), DllCall("OleAut32\SysFreeString", "ptr", pbstrcode)
  1020.         if fn := NumGet(excp, 6 * A_PtrSize) ; pfnDeferredFillIn
  1021.             DllCall(fn, "ptr", &excp)
  1022.         wcode := NumGet(excp, 0, "ushort")
  1023.         hr := wcode ? 0x80040200 + wcode : NumGet(excp, 7 * A_PtrSize, "uint")
  1024.         this.Error := {HRESULT: hr, Line: srcline, Column: srccol, LineText: code}
  1025.         static Infos := "Source,Description,HelpFile"
  1026.         Loop Parse, % Infos, `,
  1027.             if pbstr := NumGet(excp, A_Index * A_PtrSize)
  1028.                 this.Error[A_LoopField] := StrGet(pbstr, "UTF-16"), DllCall("OleAut32\SysFreeString", "ptr", pbstr)
  1029.         return 0x80004001 ; E_NOTIMPL (let Exec/Eval get a fail result)
  1030.     }
  1031.  
  1032.     __Delete()
  1033.     {
  1034.         if this._script
  1035.         {
  1036.             DllCall(NumGet(NumGet((p:=this._script)+0)+7*A_PtrSize), "ptr", p)  ; Close
  1037.             ObjRelease(this._script)
  1038.         }
  1039.         if this._scriptParse
  1040.             ObjRelease(this._scriptParse)
  1041.     }
  1042.  
  1043.     static IID := "{BB1A2AE1-A4F9-11cf-8F20-00805F2CD064}"
  1044.     static IID_Parse := A_PtrSize=8 ? "{C7EF7658-E1EE-480E-97EA-D52CB4D76D17}" : "{BB1A2AE2-A4F9-11cf-8F20-00805F2CD064}"
  1045. }
  1046.  
  1047. class ActiveScriptSite
  1048. {
  1049.     __New(Script)
  1050.     {
  1051.         ObjSetCapacity(this, "_site", 3 * A_PtrSize)
  1052.         NumPut(&Script
  1053.         , NumPut(ActiveScriptSite._vftable("_vft_w", "31122", 0x100)
  1054.         , NumPut(ActiveScriptSite._vftable("_vft", "31125232211", 0)
  1055.             , this.ptr := ObjGetAddress(this, "_site"))))
  1056.     }
  1057.  
  1058.     _vftable(Name, PrmCounts, EIBase)
  1059.     {
  1060.         if p := ObjGetAddress(this, Name)
  1061.             return p
  1062.         ObjSetCapacity(this, Name, StrLen(PrmCounts) * A_PtrSize)
  1063.         p := ObjGetAddress(this, Name)
  1064.         Loop Parse, % PrmCounts
  1065.         {
  1066.             cb := RegisterCallback("_ActiveScriptSite", "F", A_LoopField, A_Index + EIBase)
  1067.             NumPut(cb, p + (A_Index-1) * A_PtrSize)
  1068.         }
  1069.         return p
  1070.     }
  1071. }
  1072.  
  1073. _ActiveScriptSite(this, a1:=0, a2:=0, a3:=0, a4:=0, a5:=0)
  1074. {
  1075.     Method := A_EventInfo & 0xFF
  1076.     if A_EventInfo >= 0x100  ; IActiveScriptSiteWindow
  1077.     {
  1078.         if Method = 4  ; GetWindow
  1079.         {
  1080.             NumPut(0, a1+0) ; *phwnd := 0
  1081.             return 0 ; S_OK
  1082.         }
  1083.         if Method = 5  ; EnableModeless
  1084.         {
  1085.             return 0 ; S_OK
  1086.         }
  1087.         this -= A_PtrSize     ; Cast to IActiveScriptSite
  1088.     }
  1089.     ;else: IActiveScriptSite
  1090.     if Method = 1  ; QueryInterface
  1091.     {
  1092.         iid := _AS_GUIDToString(a1)
  1093.         if (iid = "{00000000-0000-0000-C000-000000000046}"  ; IUnknown
  1094.          || iid = "{DB01A1E3-A42B-11cf-8F20-00805F2CD064}") ; IActiveScriptSite
  1095.         {
  1096.             NumPut(this, a2+0)
  1097.             return 0 ; S_OK
  1098.         }
  1099.         if (iid = "{D10F6761-83E9-11cf-8F20-00805F2CD064}") ; IActiveScriptSiteWindow
  1100.         {
  1101.             NumPut(this + A_PtrSize, a2+0)
  1102.             return 0 ; S_OK
  1103.         }
  1104.         NumPut(0, a2+0)
  1105.         return 0x80004002 ; E_NOINTERFACE
  1106.     }
  1107.     if Method = 5  ; GetItemInfo
  1108.     {
  1109.         a1 := StrGet(a1, "UTF-16")
  1110.         , (a3 && NumPut(0, a3+0))  ; *ppiunkItem := NULL
  1111.         , (a4 && NumPut(0, a4+0))  ; *ppti := NULL
  1112.         if (a2 & 1) ; SCRIPTINFO_IUNKNOWN
  1113.         {
  1114.             if !(unk := Object(NumGet(this + A_PtrSize*2))._GetObjectUnk(a1))
  1115.                 return 0x8002802B ; TYPE_E_ELEMENTNOTFOUND
  1116.             ObjAddRef(unk), NumPut(unk, a3+0)
  1117.         }
  1118.         return 0 ; S_OK
  1119.     }
  1120.     if Method = 9  ; OnScriptError
  1121.         return Object(NumGet(this + A_PtrSize*2))._OnScriptError(a1)
  1122.  
  1123.     ; AddRef and Release don't do anything because we want to avoid circular references.
  1124.     ; The site and IActiveScript are both released when the AHK script releases its last
  1125.     ; reference to the ActiveScript object.
  1126.  
  1127.     ; All of the other methods don't require implementations.
  1128.     return 0x80004001 ; E_NOTIMPL
  1129. }
  1130.  
  1131. _AS_GUIDToString(pGUID)
  1132. {
  1133.     VarSetCapacity(String, 38*2)
  1134.     DllCall("ole32\StringFromGUID2", "ptr", pGUID, "str", String, "int", 39)
  1135.     return String
  1136. }
  1137.  
  1138. GetJScript()
  1139. {
  1140.    script =
  1141.    (
  1142.       var TKK = ((function() {
  1143.         var a = 561666268;
  1144.         var b = 1526272306;
  1145.         return 406398 + '.' + (a + b);
  1146.       })());
  1147.  
  1148.       function b(a, b) {
  1149.         for (var d = 0; d < b.length - 2; d += 3) {
  1150.             var c = b.charAt(d + 2),
  1151.                 c = "a" <= c ? c.charCodeAt(0) - 87 : Number(c),
  1152.                 c = "+" == b.charAt(d + 1) ? a >>> c : a << c;
  1153.             a = "+" == b.charAt(d) ? a + c & 4294967295 : a ^ c
  1154.         }
  1155.         return a
  1156.       }
  1157.  
  1158.       function tk(a) {
  1159.           for (var e = TKK.split("."), h = Number(e[0]) || 0, g = [], d = 0, f = 0; f < a.length; f++) {
  1160.               var c = a.charCodeAt(f);
  1161.               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) ?
  1162.               (c = 65536 + ((c & 1023) << 10) + (a.charCodeAt(++f) & 1023), g[d++] = c >> 18 | 240,
  1163.               g[d++] = c >> 12 & 63 | 128) : g[d++] = c >> 12 | 224, g[d++] = c >> 6 & 63 | 128), g[d++] = c & 63 | 128)
  1164.           }
  1165.           a = h;
  1166.           for (d = 0; d < g.length; d++) a += g[d], a = b(a, "+-a^+6");
  1167.           a = b(a, "+-3^+b+-f");
  1168.           a ^= Number(e[1]) || 0;
  1169.           0 > a && (a = (a & 2147483647) + 2147483648);
  1170.           a `%= 1E6;
  1171.           return a.toString() + "." + (a ^ h)
  1172.       }
  1173.    )
  1174.    Return script
  1175. }
  1176. ;=-=-=-=-=-=-=-=-=-=- END SCRIPT -=-=-=-=-=-=-=-=-=-=-=-
Add Comment
Please, Sign In to add comment