现在Google翻译退出了中国市场,需要魔法上网才能使用。所以现在只剩下一个Gui界面供大家参考使用
/* ============= Google划词翻译 ============ 使用说明: 选中文字后,长按鼠标右键可划词翻译,显示双语译文供查看选择 连按两次Ctrl+C 划词翻译,显示双语译文供查看选择 长按鼠标右键 也能划词翻译 1、单击分隔栏能长久显示提示框 2、左键拖动分隔栏可移动提示框,双击关闭提示框 */ #NoEnv ; #NoTrayIcon ;去掉注释可以关闭托盘图标显示 #SingleInstance Force ; 系统低于Win7且IE版本低于10.0自检退出,并弹出IE浏览器更新地址 if A_OSVersion in WIN_VISTA,WIN_XP,WIN_2003,WIN_2000 { MsgBox, 16, 操作系统不支持此脚本, 此脚本需要Windows 7或更高版本的支持 ExitApp } RegRead, IEVersion, HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer, svcVersion if (Format("{:d}", IEVersion)<10) { if (A_Is64bitOS=1) { MsgBox, 560, IE浏览器版本太低【64位】, 请将IE浏览器版本更新至10.0以上 Run https://www.onlinedown.net/soft/455445.htm } else { MsgBox, 560, IE浏览器版本太低【32位】, 请将IE浏览器版本更新至10.0以上 Run https://www.onlinedown.net/soft/455439.htm } ExitApp } GoogleTranslate("预加载") Return ; 长按鼠标右键划词翻译,会改变剪贴板内容。【可自行做还原剪贴板的优化】 $RButton:: KeyWait, RButton, T0.6 if (ErrorLevel=1) { Send ^c 翻译显示Gui("auto", "zh-CN") } else Send {RButton} Return ~^c:: t:=-oldt+(oldt:=A_TickCount) if (t<100 || t>400) Return else oldt:=0 翻译显示Gui("auto", "zh-CN") Return 翻译显示Gui(from := "auto", to := "zh-CN") { Static OriginalText, OriginalTextCopy, Result, ResultCopy, ClickTip ClipWait 2 OriginalText := Clipboard Result := GoogleTranslate(OriginalText, from, to) Result := Result="" ? GoogleTranslate(OriginalText, from, to) : Result ; 加一行改成显示中文译文和日文译文:OriginalText := GoogleTranslate(OriginalText, from, "ja") if (StrLen(Result)>=180) or (StrLen(OriginalText)>=500) DynamicWidth := "w950" else if (StrLen(Result)>=100) or (StrLen(OriginalText)>=240) DynamicWidth := "w700" else if (StrLen(Result)>=45) or (StrLen(OriginalText)>=110) DynamicWidth := "w500" Gui GoogleTranslate: Destroy Gui GoogleTranslate: +ToolWindow -Caption +AlwaysOnTop border -DPIScale Gui GoogleTranslate: Color, cDDDDDD Gui GoogleTranslate: Font, s13 c2D2D2D Q5, Microsoft YaHei UI Gui GoogleTranslate: Margin, 5, 5 Gui GoogleTranslate: Add, Text, %DynamicWidth% vResultCopy gDestroyGui, %Result% Gui GoogleTranslate: Font, s10 c333333 Q5, Microsoft YaHei UI Separator := DynamicWidth="" ? "-----" : StrReplace(Format("{:" (RegExReplace(DynamicWidth, "\D")/18)//(A_ScreenDPI/96) "}", " ")," ","-") Gui GoogleTranslate: Add, Text, Center %DynamicWidth% vClickTip gMoveGui, % Separator " 单双击关闭 / 右击可复制 " Separator Gui GoogleTranslate: Font, s13 c2D2D2D Q5, Microsoft YaHei UI Gui GoogleTranslate: Add, Text, %DynamicWidth% vOriginalTextCopy gDestroyGui, %OriginalText% CoordMode Mouse MouseGetPos, xpos, ypos, HwndID Gui GoogleTranslate: Show, % "x" xpos+10 " y" ypos+10 WinActivate ahk_id %HwndID% SetTimer DestroyGui, -4000 Return MoveGui: PostMessage, 0xA1, 2 if (A_GuiEvent="Normal") SetTimer DestroyGui, Off else if (A_GuiEvent="DoubleClick") Gui GoogleTranslate: Destroy Return GoogleTranslateGuiContextMenu: if (A_GuiControl="ResultCopy") Clipboard := Result, MoveTip("译文已复制") else if (A_GuiControl="OriginalTextCopy") Clipboard := OriginalText, MoveTip("原文已复制") else if (A_GuiControl="ClickTip") { MoveTip("单击 文字区域关闭提示框`n右击 中/英区域复制相应内容到剪切板`n`n单击 分隔栏能长久显示提示框`n左键拖动 分隔栏可移动提示框,双击 关闭提示框", 5000) Return } DestroyGui: Gui GoogleTranslate: Destroy Return } MoveTip(s:="", t:=1000) { SetTimer %A_ThisFunc%, % s="" ? "Off" : "-" t ToolTip %s% } ; https://www.autohotkey.com/boards/viewtopic.php?t=63835 GoogleTranslate(str, from := "auto", to := "zh-CN") { static JS := CreateScriptObj(), _ := 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 ? "" : "/" txt) } } } if !IsObject(oJSON[1]) MainTransText := trans := Trim(trans, ",`n ") else trans := MainTransText . "`n" . Trim(trans, ",`n ") from := oJSON[2] trans := Trim(trans, ",`n") Return trans } SendRequest(JS, str, tl, sl, proxy) { static http ComObjError(false) if !http { http := ComObjCreate("WinHttp.WinHttpRequest.5.1") ( proxy && http.SetProxy(2, proxy) ) http.open("GET", "https://translate.google.com", true) http.SetRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0") http.send() http.WaitForResponse(-1) } http.open("POST", "https://translate.googleapis.com/translate_a/single?client=gtx" ; or "https://clients5.google.com/translate_a/t?client=dict-chrome-ex" . "&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=0&ssel=0&tsel=0&pc=1&kc=1" . "&tk=" . JS.("tk").(str), true) 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, JS, _JS if !doc { doc := ComObjCreate("htmlfile") doc.write("<meta http-equiv='X-UA-Compatible' content='IE=9'>") JS := doc.parentWindow if (doc.documentMode < 9) JS.execScript() _JS := ObjBindMethod(JS, "eval") } Return _JS }
声明:站内资源为整理优化好的代码上传分享与学习研究,如果是开源代码基本都会标明出处,方便大家扩展学习路径。请不要恶意搬运,破坏站长辛苦整理维护的劳动成果。本站为爱好者分享站点,所有内容不作为商业行为。如若本站上传内容侵犯了原著者的合法权益,请联系我们进行删除下架。
评论(0)