#SingleInstance, Force
#Include <JSON>
; JSON库: https://github.com/cocobelgica/AutoHotkey-JSON
; 参考小海 cosea https://zhuanlan.zhihu.com/p/103357456
; todo&bug:
; 先将就只提取translation,(得添加base.explain)分行只翻译第一行,其他
; 太长好像翻译不了 包含奇怪的字符可能也不行
; 判断是否有选中文字再执行(获取光标单词?)
removeTip:
ToolTip
return
CapsLock & `::
ToolTip
return
MButton::
Clipboard=
Send, ^c
ClipWait,2
If ErrorLevel ;
{
MsgBox, errorr
Return
}
word=%Clipboard%
result:=YouDaoApi(word)
; Clipboard=%result% ;json 结果,可以去json.cn看看结构
value := JSON.Load(result)
s:=value.translation[1]
ToolTip, %s%
SetTimer, removeTip,-1000
Return
YouDaoApi(KeyWord)
{
KeyWord:=Sub_UrlEncode(KeyWord,"utf-8")
url:="http://fanyi.youdao.com/fanyiapi.do?keyfrom=xxxxxxxx&key=1360116736&type=data&doctype=json&version=1.1&q=" . KeyWord
WebRequest := ComObjCreate("WinHttp.WinHttpRequest.5.1")
WebRequest.Open("GET", url)
WebRequest.Send()
result := WebRequest.ResponseText
Return result
}
Sub_UrlEncode(str, enc="UTF-8")
{
hex := "00", func := "msvcrt\" . (A_IsUnicode ? "swprintf" : "sprintf")
VarSetCapacity(buff, size:=StrPut(str, enc)), StrPut(str, &buff, enc)
While (code := NumGet(buff, A_Index - 1, "UChar")) && DllCall(func, "Str", hex, "Str", "%%%02X", "UChar", code, "Cdecl")
encoded .= hex
Return encoded
}
/*
json有用的有
中文单词:
translation
base.explains[]
base.phonetic
英文单词:
translation
base.[us-|uk-|]phonetic
base.explains[]
base.phonetic
长文只有translation
*/
标签:autohotkey,KeyWord,WebRequest,json,base,选中,result,translation,鼠标
From: https://www.cnblogs.com/wqzz/p/16749833.html