-
Notifications
You must be signed in to change notification settings - Fork 122
/
thread.ah2
304 lines (284 loc) · 10.4 KB
/
thread.ah2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
;████████╗██╗ ██╗██████╗ ██╗███╗ ██╗ ██████╗ ████████╗██████╗ █████╗ ███╗ ██╗███████╗██╗ █████╗ ████████╗██╗ ██████╗ ███╗ ██╗
;╚══██╔══╝╚██╗ ██╔╝██╔══██╗██║████╗ ██║██╔════╝ ╚══██╔══╝██╔══██╗██╔══██╗████╗ ██║██╔════╝██║ ██╔══██╗╚══██╔══╝██║██╔═══██╗████╗ ██║
; ██║ ╚████╔╝ ██████╔╝██║██╔██╗ ██║██║ ███╗ ██║ ██████╔╝███████║██╔██╗ ██║███████╗██║ ███████║ ██║ ██║██║ ██║██╔██╗ ██║
; ██║ ╚██╔╝ ██╔═══╝ ██║██║╚██╗██║██║ ██║ ██║ ██╔══██╗██╔══██║██║╚██╗██║╚════██║██║ ██╔══██║ ██║ ██║██║ ██║██║╚██╗██║
; ██║ ██║ ██║ ██║██║ ╚████║╚██████╔╝ ██║ ██║ ██║██║ ██║██║ ╚████║███████║███████╗██║ ██║ ██║ ██║╚██████╔╝██║ ╚████║
; ╚═╝ ╚═╝ ╚═╝ ╚═╝╚═╝ ╚═══╝ ╚═════╝ ╚═╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═══╝╚══════╝╚══════╝╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═══╝
#Requires AutoHotkey v2.0
#include <log>
#include <zmq>
#include <WinHttpRequest>
CoordMode('ToolTip', 'Screen')
Persistent
logger.is_log_open := false
logger.is_use_editor := true
g_config := Map()
g_text := ''
g_last_text := ''
ctx := A_Args[1]
socket := zmq_socket(ctx, ZMQ_SUB)
rtn := zmq_connect(socket, "inproc://main")
rtn := zmq_setsockopt(socket, ZMQ_SUBSCRIBE, buf := StrBuf(''), buf.Size - 1)
SetTimer(exe_command , 50)
OnExit(exit)
return
exit(*)
{
zmq_close(socket)
zmq_ctx_shutdown(ctx)
}
exe_command(*)
{
Critical 'on'
global g_tt_hwnd
try
{
rtn := zmq_recv_string(socket, &recv_str := '', ZMQ_DONTWAIT)
if(rtn == -1)
return
if(recv_str != '')
logger.info(recv_str)
o := JSON.parse(recv_str)
x := o['x']
y := o['y']
is_ime_char := o['is_ime_char']
text := o['text']
api := o['api']
}
catch as e
{
logger.info('Parse recv error!')
return
}
if(text == '@stop' || text == '')
{
ToolTip('')
return
}
try
{
;ToolTip('[' api ']', x, y - 28, 1)
switch (api)
{
case "youdao":
{
rtn := youdaocd(text)
}
case "baidu":
{
is_hz := RegExMatch(text, "\p{Han}")
if(is_hz)
rtn := baiducd(text, g_config['baidu']['BaiduFanyiAPPID'], g_config['baidu']['BaiduFanyiAPPSEC'], 'zh', 'en')
else
rtn := baiducd(text, g_config['baidu']['BaiduFanyiAPPID'], g_config['baidu']['BaiduFanyiAPPSEC'], 'en', 'zh')
}
case "edge":
{
rtn := edgecd(text)
}
case "ali":
{
}
case 'google':
{
rtn := googlecd(text)
}
case 'sougou':
{
rtn := sougoucd(text)
}
default:
}
logger.info(rtn)
}
catch as e
{
return
}
g_tt_hwnd := ToolTip('[' api '] ' rtn, x, y - 28, 1)
A_Clipboard := rtn
Critical 'off'
}
youdaocd(text)
{
Url := 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc&sessionFrom=null'
postdata := 'type=AUTO&i=' text '&doctype=json&xmlVersion=1.4&keyfrom=fanyi.web&ue=UTF-8&typoResult=true&flag=false'
rtn := WinHttp(Url ,"POST", postdata)
str := byteToStr(rtn, 'UTF-8')
js := JSON.parse(str)
return js['translateResult'][1][1]['tgt']
}
edgecd(text)
{
is_hz := RegExMatch(text, "\p{Han}")
data := JSON.stringify([{Text: text}])
static whr := WinHttpRequest('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 Edg/122.0.0.0')
static authCode := whr.request('https://edge.microsoft.com/translate/auth')
r := whr.request('https://api-edge.cognitive.microsofttranslator.com/translate?' (is_hz ? 'from=zh&to=en' : 'from=en&to=zh') '&api-version=3.0&includeSentenceLength=true',
'POST', data, { authorization: 'Bearer ' authCode,
%'Content-Type'%: "application/json", })
r := JSON.parse(r)
return r[1]['translations'][1]['text']
}
;发送接收数据
WinHttp(Httpurl, Httpmode := "GET", Httppostdata := "")
{
Httpmode := StrUpper(Httpmode)
XMLHTTP := ComObject("Microsoft.XMLHTTP")
XMLHTTP.open(Httpmode, Httpurl, false)
XMLHTTP.setRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 5.1; rv:11.0) Gecko/20100101 Firefox/11.0")
if Httpmode == 'POST'
{
XMLHTTP.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
XMLHTTP.send(Httppostdata)
}else
XMLHTTP.send()
return XMLHTTP.ResponseBody
}
;将原始数据流以指定的编码的形式读出
byteToStr(body, charset)
{
Stream := ComObject("Adodb.Stream")
Stream.Type := 1
Stream.Mode := 3
Stream.Open()
Stream.Write(body)
Stream.Position := 0
Stream.Type := 2
Stream.Charset := charset
str := Stream.ReadText()
Stream.Close()
return str
}
; 返回包含字符串的缓冲对象.
StrBuf(str, encoding := 'UTF-8')
{
; 计算所需的大小并分配缓冲.
buf := Buffer(StrPut(str, encoding))
; 复制或转换字符串.
StrPut(str, buf, encoding)
return buf
}
baiducd(keyword, BaiduFanyiAPPID := '', BaiduFanyiAPPSEC := '', from:="en", to:="zh")
{
salt:=A_Now
MD5Sign := StrLower(MD5_str(BaiduFanyiAPPID keyword salt BaiduFanyiAPPSEC))
URL:=Format("http://api.fanyi.Baidu.com/api/trans/vip/translate?q={1}&from={2}&to={3}&appid={4}&salt={5}&sign={6}",keyword,from,to,BaiduFanyiAPPID,salt,MD5Sign )
WebRequest := ComObject("WinHttp.WinHttpRequest.5.1")
WebRequest.Open("GET", url)
WebRequest.Send()
result := WebRequest.ResponseText
try
{
o := JSON.parse(result)
result := o['trans_result'][1]['dst']
}
catch as e
{
result := ''
}
return result
}
MD5_str(str, encoding := 'UTF-8')
{
bf := StrBuf(str, encoding)
if(encoding = 'UTF-8')
size := bf.Size - 1
else if(encoding = 'UTF-16')
size := bf.Size - 2
else
return ''
return MD5(bf, size)
StrBuf(str, encoding := 'UTF-8')
{
len := StrPut(str, encoding)
buf := Buffer(StrPut(str, encoding))
StrPut(str, buf, encoding)
return buf
}
}
MD5(Data, Size:=0)
{
MD5_CTX:=Buffer(104), DllCall("advapi32\MD5Init", "Ptr", MD5_CTX)
DllCall("advapi32\MD5Update", "Ptr", MD5_CTX, "Ptr", Data, "UInt", (!Size&&Type(Data)="Buffer") ? Data.Size : Size)
DllCall("advapi32\MD5Final", "Ptr", MD5_CTX), MD5:=""
Loop 16
MD5 .= Format("{:02X}", NumGet(MD5_CTX, 87+A_Index, "UChar"))
return MD5
}
googlecd(keyword, time_out := 2)
{
;zh-TW en
from := 'en'
to := 'zh-CN'
if(RegExMatch(keyword, "\p{Han}"))
{
from := 'zh-CN'
to := 'en'
}
;https://translate.googleapis.com/translate_a/single?dt=t&client=gtx&sl=%from_lang%&tl=%to_lang%&dj=1&ie=UTF-8&oe=UTF-8&q=%text_selected%
URL := Format('https://translate.googleapis.com/translate_a/single?dt=t&client=gtx&sl={1}&tl={2}&dj=1&ie=UTF-8&oe=UTF-8&q={3}', from, to, EncodeDecodeURI(keyword))
WebRequest := ComObject("WinHttp.WinHttpRequest.5.1")
WebRequest.Open("GET", url, true)
WebRequest.SetRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36")
WebRequest.Send()
try
{
WebRequest.WaitForResponse(time_out)
result := WebRequest.ResponseText
o := JSON.parse(result)
result := o['sentences'][1]['trans']
}
catch as e
{
result := ''
}
return result
}
EncodeDecodeURI(str, encode := true, component := true) {
; Adapted from teadrinker: https://www.autohotkey.com/boards/viewtopic.php?p=372134#p372134
static Doc, JS
if !IsSet(Doc) {
Doc := ComObject("htmlfile")
Doc.write('<meta http-equiv="X-UA-Compatible" content="IE=9">')
JS := Doc.parentWindow
( Doc.documentMode < 9 && JS.execScript() )
}
Return JS.%( (encode ? "en" : "de") . "codeURI" . (component ? "Component" : "") )%(str)
}
sougoucd(keyword, time_out := 2)
{
from := 'auto'
to := 'en'
;https://fanyi.sogou.com/text?keyword=%E4%BD%A0%E5%A5%BD&transfrom=auto&transto=en&model=general
URL := Format('https://fanyi.sogou.com/text?keyword={1}&transfrom={2}&transto={3}&model=general', EncodeDecodeURI(keyword), from, to)
WebRequest := ComObject("WinHttp.WinHttpRequest.5.1")
WebRequest.Open("GET", url, true)
WebRequest.SetRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36")
WebRequest.Send()
try
{
WebRequest.WaitForResponse(time_out)
result := WebRequest.ResponseText
A_Clipboard := result
Doc := ComObject("htmlfile")
Doc.write(result)
if((rtn := Doc.querySelector('#trans-result > span.trans-sentence')) != '')
{
result := rtn.innerHTML
}
else if((rtn := Doc.querySelector('#trans-result')) != '')
{
result := rtn.innerHTML
}
else
{
result := ''
}
}
catch as e
{
result := ''
}
return result
}