-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathsourcegrab.ahk
146 lines (133 loc) · 3.1 KB
/
sourcegrab.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
/*
A class to grab the source code of a web page
By Clive Galway - [email protected]
License: WTFPL
ToDo:
* Test with Edge
*/
class SourceGrab {
GuiWidth := 200
__New(hotkey, duration := 2000){
this.Duration := duration
; Bind Hotkeys to class methods
fn := this.DoGrab.Bind(this)
hotkey, % hotkey, % fn
; Set up confirmation dialog
Gui, New, HwndHwnd -Border
this.ConfirmationHwnd := hwnd
Gui, Add, Text, % "hwndhwnd Center w" this.GuiWidth
this.TitleHwnd := hwnd
}
DoGrab(){
this.Source := ""
; Save clipboard state
this.Clipboard := Clipboard
; Clear Clipboard
clipboard := ""
grabbed := 0
; Get process name of active window
WinGet, win, ProcessName, A
; Check if active window is a browser
if (win = "chrome.exe"){
SetTitleMatchMode, 1
; ============== CHROME ==============
; Open ViewSource
Send ^u
; Wait for new window
WinWait, ahk_exe chrome.exe,, 3
WinGetTitle, new_win
if (substr(new_win, 1, 12) = "view-source:"){
; Saw source window pop up
time := A_TickCount + 2000
; Wait for source window to populate
while (this.Source = "" && A_TickCount < time){
; Select all
Send ^a
Sleep 100
; Copy to clipboard
Send ^c
Sleep 100
this.Source := Clipboard
}
; Close source window
Send ^{f4}
} else {
return
}
} else if (win = "firefox.exe") {
SetTitleMatchMode, 1
; ============== FIREFOX ==============
; Open ViewSource
Send ^u
; Wait for new window
WinWait, Source of:,, 3
if (ErrorLevel){
; WinWait timed out
return
}
; Saw source window pop up
time := A_TickCount + 2000
; Wait for source window to populate
while (this.Source = "" && A_TickCount < time){
; Select all
Send ^a
Sleep 100
; Copy to clipboard
Send ^c
Sleep 100
this.Source := Clipboard
}
; Close source window
WinClose
} else if (win = "iexplore.exe"){
; ============== INTERNET EXPLORER ==============
SetTitleMatchMode, RegEx
; Open ViewSource
Send ^u
; Wait for new window
WinWait, .* - Original Source$,, 3
if (ErrorLevel){
; WinWait timed out
return
}
; Saw source window pop up
time := A_TickCount + 2000
; Wait for source window to populate
while (this.Source = "" && A_TickCount < time){
; Select all
Send ^a
Sleep 100
; Copy to clipboard
Send ^c
Sleep 100
this.Source := Clipboard
}
; Close source window
WinClose
}
; Restore clipboard
Clipboard := this.Clipboard
this.ShowConfirmation()
}
; Sets the title of the Confirmation Dialog
SetTitle(title){
GuiControl,,% this.TitleHwnd, % title
}
; Prepends hwnd of Gui to Gui commands
GuiCmd(cmd){
return this.ConfirmationHwnd ":" cmd
}
; Shows the confirmation Gui
ShowConfirmation(){
; Show the Confirmation Dialog
Gui, % this.GuiCmd("Show")
fn := this.HideConfirmation.Bind(this)
; Hide Confirmation Dialog after delay
SetTimer, % fn, % "-" this.Duration
}
; Hides the confirmation Gui
HideConfirmation(){
; Hide the Confirmation Dialog
Gui, % this.GuiCmd("Hide")
}
}