-
Notifications
You must be signed in to change notification settings - Fork 19
/
WinPopClip.ahk
343 lines (294 loc) · 9.26 KB
/
WinPopClip.ahk
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
#SingleInstance, force
#InstallMouseHook
AutoTrim, Off
CoordMode, Mouse, Screen
DetectHiddenWindows, On
ListLines Off
SendMode, Input ; Recommended for new scripts due to its superior speed and reliability.
SetBatchLines -1
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
full_command_line := DllCall("GetCommandLine", "str")
if not (A_IsAdmin or RegExMatch(full_command_line, " /restart(?!\S)"))
{
try
{
if A_IsCompiled
Run *RunAs "%A_ScriptFullPath%" /restart
else
Run *RunAs "%A_AhkPath%" /restart "%A_ScriptFullPath%"
}
ExitApp
}
; MsgBox A_IsAdmin: %A_IsAdmin%`nCommand line: %full_command_line%
winTitle:="WinClipTitle"
dpiRatio:=A_ScreenDPI/96
controlHight:=25
winHeightPx:=controlHight*dpiRatio
bGColor:="000000"
fontColor:="ffffff"
ver:="0.91"
fontSize:=12
fontFamily:="微软雅黑"
userLanguage:="zh-CN"
SyncPath:="E:\Dropbox"
SysGet, VirtualWidth, 78
SysGet, VirtualHeight, 79
Loop, read, %A_ScriptDir%/White List.txt
{
GroupAdd, whiteList, ahk_exe %A_LoopReadLine%
}
Menu, tray, NoStandard
Menu, tray, add, 更新 | Ver %ver%, UpdateScrit
Menu, tray, add, 反馈 | Issues, Issues
;Menu, tray, add, 暂停 | Pause, PauseScrit
Menu, tray, add
Menu, tray, add, 重置 | Reload, ReloadScrit
Menu, tray, add, 退出 | Exit, ExitScrit
Return
ReloadScrit:
Reload
Return
PauseScrit:
Pause, Toggle, 1
Return
UpdateScrit:
Run, https://github.com/millionart/WinPopclip/releases
Return
Issues:
Run, https://github.com/millionart/WinPopclip/issues
Return
ExitScrit:
^#p::
ExitApp
Return
#IfWinNotActive, ahk_group whiteList
~LButton::
Gui,Destroy
Return
#IfWinNotActive
#IfWinActive, ahk_group whiteList
; 如果不在脚本界面状态下
$LButton::
; ToolTip, %win% %A_TickCount%, 0,0
; 获得鼠标当前坐标
MouseGetPos, perPosX, perPosY
; 获得当前时间
preTime:=A_TickCount
If (A_Cursor="IBeam")
winClipToggle:=1
Send, {LButton Down}
KeyWait, LButton
Send, {LButton Up}
If (A_Cursor="IBeam")
winClipToggle:=1
If !WinActive(winTitle)
{
win:= WinExist("A")
ShowMainGui(perPosX,perPosY,preTime)
}
Return
#IfWinActive
ShowMainGui(perPosX,perPosY,preTime)
{
global
; 获得当前时间
curTime:=A_TickCount
; 当前时间减去之前时间
lButtonDownDelay:=curTime-preTime
; 获得鼠标当前坐标
MouseGetPos, curPosX, curPosY
guiShowX:=curPosX
guiShowY:=curPosY-winHeightPx*2 ;*dpiRatio
If (A_TimeSincePriorHotkey < 410) && (A_Cursor="IBeam")
{
GetSelectText()
ShowWinclip()
}
Else if (lButtonDownDelay > 250 && winClipToggle=1) || (lButtonDownDelay > 350)
{
; 当前坐标剪去先前坐标
moveX:=abs(curPosX-perPosX)
moveY:=abs(curPosY-perPosY)
; 如果X大于10,Y大于10, 在当前坐标弹出界面
If (moveX>10) || (moveY>10)
{
GetSelectText()
ShowWinclip()
}
}
Else
{
Gui, Destroy
}
winClipToggle:=0
}
GetSelectText()
{
global
ClipSaved:=ClipBoardAll
ClipBoard:=""
; PostMessage, 0x301, , , , ahk_id %win%
; PostMessage WM_COPY not work for some windows (even steam or notepad and more), why?
Send, {CtrlDown}c
ClipWait 0.1, 1
Send, {CtrlUp}
selectText:=ClipBoard
ClipBoard:=""
ClipBoard:=ClipSaved
ClipSaved:=""
; 处理协议地址
linkText:=""
linkButton:="🔗"
; https://gist.github.com/dperini/729294
; https://mathiasbynens.be/demo/url-regex
urlRegEx:="(?:(?:https?|ftp|file|ed2k|steam|thunder)://)(?:\S+(?::\S*)?@)?(?:(?!10(?:\.\d{1,3}){3})(?!127(?:\.\d{1,3}){3})(?!169\.254(?:\.\d{1,3}){2})(?!192\.168(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\x{00a1}-\x{ffff}0-9]+-?)*[a-z\x{00a1}-\x{ffff}0-9]+)(?:\.(?:[a-z\x{00a1}-\x{ffff}0-9]+-?)*[a-z\x{00a1}-\x{ffff}0-9]+)*(?:\.(?:[a-z\x{00a1}-\x{ffff}]{2,})))(?::\d{2,5})?(?:/[^\s]*)?"
RegExMatch(selectText, urlRegEx, linkText)
urlRegEx:="(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])"
RegExMatch(selectText, urlRegEx, ipText)
If (linkText="")
{
; https://daringfireball.net/2010/07/improved_regex_for_matching_urls
urlRegEx:="(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'.,<>?«»“”‘’]))"
RegExMatch(selectText, urlRegEx, linkText)
urlRegEx:="av\d+"
RegExMatch(selectText, urlRegEx, bilibili)
If (linkText!="")
linkText:="http://" . linkText
Else If (bilibili!="")
{
linkText:="https://www.bilibili.com/video/" . bilibili
linkButton:="` BiliBili` "
}
}
}
ShowWinclip()
{
global
local x,y,w,h,winMoveX,winMoveY
;ToolTip, %selectText%
Gui, Destroy
Gui, +ToolWindow -Caption +AlwaysOnTop ; -DPIScale
Gui, Color, %bGColor%
Gui, font, s%fontSize% c%fontColor%, %fontFamily%
Gui, Add, Text, x0 y0 w0 h%controlHight% -Wrap, ; 初始定位
If selectText in ,%A_Space%,%A_Tab%,`r`n,`r,`n
{
If (winClipToggle=1)
{
Gui, Add, Button, x+0 yp hp -Wrap vselectAll gSelectAll, ` ` 全选` ` `
Gui, Add, Button, x+0 yp hp -Wrap vpaste gPaste, ` ` 粘贴` ` `
}
}
Else
{
Gui, Add, Button, x+0 yp hp -Wrap vsearch gGoogleSearch, ` 🔍` `
If (linkText!="")
Gui, Add, Button, x+0 yp hp -Wrap gLink, ` %linkButton%` `
Gui, Add, Button, x+0 yp hp -Wrap vselectAll gSelectAll, ` ` 全选` ` `
If (winClipToggle=1)
{
Gui, Add, Button, x+0 yp hp -Wrap vcut gCut, ` ` 剪切` ` `
Gui, Add, Button, x+0 yp hp -Wrap vcopy gCopy, ` ` 复制` ` `
Gui, Add, Button, x+0 yp hp -Wrap vpaste gPaste, ` ` 粘贴` ` `
}
Else
{
Gui, Add, Button, x+0 yp hp -Wrap vcopy gCopy, ` ` 复制` ` `
}
Gui, Add, Button, x+0 yp hp -Wrap vgTranslate gGoogleTranslate, ` ` G 翻译` ` `
Gui, Add, Button, x+0 yp hp -Wrap vdTranslate gDeepLTranslate, ` ` D 翻译` ` `
}
Gui, font
Gui, Show, NA AutoSize x%guiShowX% y%guiShowY%, %winTitle%
WinGetPos , x, y, w, h, %winTitle%
winMoveX:=x-w/2,0
If (winMoveX > VirtualWidth-w+15*dpiRatio)
winMoveX:=VirtualWidth-w+15*dpiRatio
winMoveY:=Max(y,0)
WinMove, %winTitle%, , winMoveX, winMoveY, w-15*dpiRatio, %winHeightPx%
}
GoogleSearch:
Gui, Destroy
urlEncodedText:=UriEncode(selectText)
Run, https://www.google.com/search?ie=utf-8&oe=utf-8&q=%urlEncodedText%
Return
SelectAll:
Gui, Destroy
WinActivate, ahk_id %win%
WinWaitActive, ahk_id %win%
Send, {CtrlDown}a
Sleep, 100
Send, {CtrlUp}
GetSelectText()
ShowWinclip()
Return
Copy:
Gui, Destroy
WinActivate, ahk_id %win%
WinWaitActive, ahk_id %win%
ClipBoard:=""
ClipBoard:=selectText
If (FileExist(SyncPath))
{
FileSetAttrib, -R, %SyncPath%\WinPopclip
FileDelete, %SyncPath%\WinPopclip
FileAppend,
(
%selectText%
), %SyncPath%\WinPopclip
}
Return
Cut:
Gosub, Copy
Send, {Del}
Return
Paste:
Gui, Destroy
WinActivate, ahk_id %win%
WinWaitActive, ahk_id %win%
Send, {CtrlDown}v
Sleep, 100
Send, {CtrlUp}
Return
Link:
Gui, Destroy
Try
Run, %linkText%
Return
GoogleTranslate:
Gui, Destroy
selectText:=UriEncode(selectText)
Run, https://translate.google.com/#view=home&op=translate&sl=auto&tl=%userLanguage%&text=%selectText%
Return
DeepLTranslate:
Gui, Destroy
selectText:=UriEncode(selectText,1)
Run, https://www.deepl.com/translator#auto/%userLanguage%/%selectText%
Return
; from http://the-automator.com/parse-url-parameters/
UriEncode(Uri, Mode := 0, RE="[0-9A-Za-z]"){
VarSetCapacity(Var,StrPut(Uri,"UTF-8"),0),StrPut(Uri,&Var,"UTF-8")
While Code:=NumGet(Var,A_Index-1,"UChar")
Res.=(Chr:=Chr(Code))~=RE?Chr:Format("%{:02X}",Code)
Res:=StrReplace(Res, "&", "%26")
Res:=StrReplace(Res, "`n", "%0A")
If (Mode==1)
Res:=StrReplace(Res, "%2F", "%5C%2F")
Return,Res
}
TransBox(text,originalLang,tragetLang) {
pwb := ComObjCreate("InternetExplorer.Application")
pwb.Visible := False
pwb.Navigate("https://translate.google.com/#view=home&op=translate&sl=" . originalLang . "&tl=" . tragetLang . "&text=" text)
Loop {
IfWinExist, ahk_exe iexplorer.exe
Process, Close, iexplorer.exe
else
break
} While pwb.readyState != 4 || pwb.document.readyState != "complete" || pwb.busy
Sleep, 1
result := pwb.document.all.result_box.InnerText
pwb.Quit
return, result
}