Code: Select all
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
#SingleInstance, Force
DetectHiddenWindows, On
SoundBeep, 2500
F1::
^5::
While Clipboard && A_Index<10 {
Clipboard:= ""
Sleep, 50
}
While !Clipboard && A_Index<5 {
Sleep, 100
SendInput, ^c
}
if !Clipboard {
MsgBox,,, No copied text to translate!, 2.5
Return
}
r:= GoogleTranslate(c:= RTrim(Clipboard, "`n`r"), , "ru")
loop, 7
if StrLen(r[A_Index])>(l:=10000)
r[A_Index]:= SubStr(r[A_Index], 1, l)
;[int, lng, Main, trn, syn, def, exp]
d:= (r.1? r.1 "`n": "") (r.2? r.2 "`n`n": "") r.3 (r.4||r.5||r.6||r.7? "`n`n": "")
. (r.4? r.4 (r.5||r.6||r.7? "`n`n": ""): "") (r.5? r.5 (r.6||r.7? "`n`n": ""): "")
. (r.6? r.6 (r.7? "`n`n": ""): "") (r.7? r.7: ""), ww:=wh:=g:= 0
Gosub doGui
gui show, Hide
WinGetPos,,, ww,, ahk_id %hGui%
ww:= ww>A_ScreenWidth*.8? A_ScreenWidth*.8: ww
Gosub doGui
gui show, Hide
WinGetPos,,,, wh, ahk_id %hGui%
wh>=A_ScreenHeight*.9? (wh:=A_ScreenHeight*.9, ww+=Ceil(15*A_ScreenDPI/96) ): ""
Gosub doGui
gui Show
SendMessage, 0xB1, 0, 0,, ahk_id %hEdt%
Return
doGui:
gui, Destroy
gui, Margin, 0, 0
gui font, s12
gui color,, BEC8BE
gui, % "+hWndhGui -DPIScale " (++g<3? "-Caption": "")
gui add, Edit, % (ww? "w" ww: "") (wh? " h" wh: "") (wh<A_ScreenHeight*.9? " -VScroll": "") " +hwndhEdt", % d
GroupAdd, TrGui, % "ahk_id " hGui
Return
#IfWinActive, ahk_group TrGui
GuiClose:
Esc:: Gui, Hide
#IfWinActive
#Esc::
KeyWait, Esc
KeyWait, LWin
SoundBeep, 1200
ExitApp
F2:: Gui, show
GoogleTranslate(str, from := "auto", to := "en") { ;function by RRR based on teadrinkers GoogleTranslate()
static JS := CreateScriptObj(), _ := JS.( GetJScript() ) := JS.("delete ActiveXObject; delete GetObject;")
json := SendRequest(JS, str, to, from, proxy := "")
oJSON := JS.("(" . json . ")")
step1:= oJSON[0][0][8][0][0][1], step2:= oJSON[0][0][8][1][0][1]
int:= (step1? "1: " step1: "") (step2? " 2: " step2: "")
loop, % lln:=oJSON[8][0].length
lng1.= oJSON[8][0][A_Index-1] (A_Index<lln? ", ": "")
loop, % lln:=oJSON[8][0].length
lng2.= oJSON[8][3][A_Index-1] (A_Index<lln? ", ": "")
lng := "a: " oJSON[2] " b: " lng1 " c: " lng2
Loop % oJSON[0].length
Src.= oJSON[0][A_Index-1][1] ((Prn:= ojson[0][1][3])&&A_Index=1? "`n[" Prn "]": "")
Src!=(Dym:= ojson[7][1])? Src.= (Dym? "`n►►►Did you mean: ▌" Dym "?": "")
: Src.= (Dym? "`n►►►Showing result for ▌" Dym "`n►►►Translate instead ▌" (Src:=Str): "")
Loop % oJSON[0].length
mTr .= oJSON[0][A_Index-1][0]
Main:= Src "`n----------------------`n" mTr
loop, % ojson[1].length
{
trn.= "♦ " ojson[1][cW:=A_Index-1][0] "`n"
Loop, % ojson[1][cW][2].length
{
Loop, % ln:=ojson[1][cW][2][tW:=A_Index-1][1].length
tr.= ojson[1][cW][2][tW][1][A_Index-1] (A_Index<ln? ", ": "")
;. (Mod(A_Index, 5)=0&&A_Index<ln? "`n ": "")
trn.= " ▪ " ((ar:=ojson[1][cW][2][A_Index-1][4])? ar " ": "")
. ojson[1][cW][2][A_Index-1][0] " ► " tr "`n", tr:= ""
}
} trn:= (trn? "▬ Translations of ►" oJSON[1][0][3] "◄`n" RTrim(trn, "`n"): "")
loop, % ojson[11].length
{
syn.= "♦ " ojson[11][cW:=A_Index-1][0] " ►" ojson[11][cW][2] "◄`n"
Loop, % ojson[11][cw].length
Loop, % ojson[11][cW][cN:=A_Index].length
Loop, % ojson[11][cW][cN][sW:=A_Index-1].length
Loop, % ln:=ojson[11][cW][cN][sW][tW:=A_Index-1].length
syn.= (A_Index=1? " ▪ ": "") ojson[11][cW][cN][sW][tW][A_Index-1] (A_Index<ln? ", ": "`n")
;. (Mod(A_Index, 5)=0&&A_Index<ln? "`n ": "")
} syn:= (syn? "▬ Synonims`n" RTrim(syn, "`n"): "")
loop, % ojson[12].length
{
def.= "♦ " ojson[12][cW:=A_Index-1][0] " ►" ojson[12][cW][2] "◄`n"
loop, % ojson[12][cW][1].length
qu:= ojson[12][0][1][A_Index-1][2]
, def.= " ▪ " ojson[12][cW][1][A_Index-1][0] (qu? "`n """ qu """" "`n": "`n")
} def:= (def? "▬ Definitions`n" RTrim(def, "`n "): "")
loop, % ln:=ojson[13][0].length
exp.= " ♦ " ojson[13][0][A_Index-1][0] (A_Index<ln? "`n": "")
exp:= (exp? "▬ Examples`n" StrReplace(StrReplace(exp, "<b>", "▌"), "</b>"): "")
Return [int, lng, Main, trn, syn, def, exp]
}
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=webapp&sl=" Broken Link for safety
. sl . "&tl=" . tl . "&hl=" . sl ; sl in place of tl by RRR
. "&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"
. "&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
}
CreateScriptObj() {
static doc
doc := ComObjCreate("htmlfile")
doc.write("<meta http-equiv='X-UA-Compatible' content='IE=9'>")
Return ObjBindMethod(doc.parentWindow, "eval")
}