-
Notifications
You must be signed in to change notification settings - Fork 71
/
IELib.ahk
469 lines (411 loc) · 13.3 KB
/
IELib.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
/*
This AHK Library is intended for use when utilizing COM Methods to Internet Explorer Applications. For support please reach out to me at Ashetynw@Gmail.com
*IMPORTANT*
Before utilizing this library, the IE object must be invoked via the "IEGet" Function. Please ensure that you are making your IE object handle a global var prior to use in this library.
Version: 3
*/
IEGet(Name="") { ;Retrieve pointer to existing IE window/tab. Pass the title of the IE page to this function to initialize it as an object. example, ie:=IEGet("Google")
; Credit to Jethrow AHK forums
IfEqual, Name,, WinGetTitle, Name, ahk_class IEFrame
Name := ( Name="New Tab - Windows Internet Explorer" ) ? "about:Tabs"
: RegExReplace( Name, " - (Windows|Microsoft) Internet Explorer" )
For wb in ComObjCreate( "Shell.Application" ).Windows
If ( wb.LocationName = Name ) && InStr( wb.FullName, "iexplore.exe" )
Return wb
}
FindIE(ShowMessage, Title) { ;Pass "True/False" to the Showmessage arg and the title of your page to the Title Arg. This will attempt to make the page the IE object, if not able, the item not found tip will show.
ie := IeGet(Title)
if (IsObject(ie)=false) &&(ShowMessage=True){
TrayTip,Item List not found, Can't find the Item List screen!
return false
}
return ie
}
IeLoad(Browser, Url:="") {
;Function Details:
;Returns True or False. Will indicate if the process was successful
;Parameter 1: Send a pointer to an internet explorer browser
;See IEGet on how to get a pointer
;Function will wait for a page to load
;Credit to Jethrow AHK Forums
If !Browser
return false
;Wait up to 2 minutes
if (URL!="")
{
Loop, 480
{
sleep 250
if (Instr(Browser.LocationURL,Url))
break
}
}
loop, 50
{
if (Browser.busy || Browser.ReadyState<>4)
sleep 100
}
loop, 4
{
tagnames:=Browser.document.getelementsbytagname("*").Length
loop, 10
{
Sleep 100
if (tagnames = Browser.document.getelementsbytagname("*").Length)
return true
}
}
return false
}
CompareNewTitle(NewTitle, ieHandle) {
/*
compares the old title pull from the PostClickTitleComparison() and compares to the current title. you need to pass it the NewTitle and the same ieHandle
*/
invalidTitleCount = 0
ValidTitleCount = 0
Switch = 0
Loop
{
WinGetTitle, CurrentTitleName, ahk_class IEFrame
StringReplace, CurrentTitleName, CurrentTitleName, %ieHandle%,, all
if (NewTitle = CurrentTitleName)
ValidTitleCount +=1
else
InvalidTitleCount +=1
if (ValidTitleCount = 500)
{
Switch = 1
break
}
if (InvalidTitleCount = 500)
{
Switch = 0
break
}
ToolTip, %NewTitle%...%CurrentTitleName%...CNT func VC: %ValidTitleCount%..INVC: %InvalidtitleCount%...switch: %switch%
}
if (switch = 1)
{
return CurrentTitleName
} else {
return FALSE
}
}
PostClickTitleComparison(prevName, ieHandle) {
/*
you must pull the previous IeFrame title as an AHK_class and pass to this function under "PrevName" the ieHandle portion is dedicated to be omitted from the title since COM doesnt like " - Internet Explorer" in the Title.
*/
count = 0
Loop,
{
WinGetTitle, Name, ahk_class IEFrame
StringReplace, Name, Name, %ieHandle%,, all
if !(prevName = Name)
{
Loop
{
ToolTip, %prevname%....%Name%.....not equal
ie := FindIE(False, CompareNewTitle(Name, ieHandle))
if !(ie = False)
{
return ie
}
if (ie = False)
{
WinGetTitle, PrevName, ahk_class IEFrame
StringReplace, PrevName, PrevName, %IeHandle%,, all
}
}
}
if (prevname = Name)
{
ToolTip, %prevname%....%Name%.....equal %count%
COUNT += 1
if (count > 100)
{
ie := FindIE(False, Name)
if !(ie = False)
{
MsgBox ie not false
return ie
}
if (ie = False)
{
MsgBox, ie false
WinGetTitle, Name, ahk_class IEFrame
StringReplace, Name, Name, %IeHandle%,, all
}
}
}
}
}
GetURL() {
WinGetTitle, IeTitle, ahk_class IEFrame
ControlGetText, Url, Edit1, %IeTitle%
return Url
}
IE_DocumentComplete(ieEventParam, url, ieFinalParam) {
global ie
if (ie != ieEventParam)
s .= "First parameter is a new wrapper object.`n"
if (ie == ieFinalParam)
s .= "Final parameter is the original wrapper object.`n"
if ((disp1:=ComObjUnwrap(ieEventParam)) == (disp2:=ComObjUnwrap(ieFinalParam)))
s .= "Both wrapper objects refer to the same IDispatch instance.`n"
ObjRelease(disp1), ObjRelease(disp2)
MsgBox % s . "Finished loading " ie.Document.title " @ " url
ie.Quit()
ExitApp
}
GetWebBrowser() {
; Get a raw pointer to the document object of the top-most IE window.
static msg := DllCall("RegisterWindowMessage", "str", "WM_HTML_GETOBJECT")
SendMessage msg, 0, 0, Internet Explorer_Server1, ahk_class IEFrame
if ErrorLevel = FAIL
return ; IE not found.
lResult := ErrorLevel
DllCall("oleacc\ObjectFromLresult", "ptr", lResult
, "ptr", GUID(IID_IHTMLDocument2,"{332C4425-26CB-11D0-B483-00C04FD90119}")
, "ptr", 0, "ptr*", pdoc)
; Query for the WebBrowserApp service. In this particular case,
; the SID and IID are the same, but it isn't always this way.
static IID_IWebBrowserApp := "{0002DF05-0000-0000-C000-000000000046}"
static SID_SWebBrowserApp := IID_IWebBrowserApp
pweb := ComObjQuery(pdoc, SID_SWebBrowserApp, IID_IWebBrowserApp)
; Release the document object pointer.
ObjRelease(pdoc)
; Return the WebBrowser object, wrapped for usability:
static VT_DISPATCH := 9, F_OWNVALUE := 1
return ComObject(VT_DISPATCH, pweb, F_OWNVALUE)
}
GUID(ByRef GUID, sGUID) { ; Converts a string to a binary GUID and returns its address.
MsgBox, % guid
MsgBox, % sguid
VarSetCapacity(GUID, 16, 0)
return DllCall("ole32\CLSIDFromString", "wstr", sGUID, "ptr", &GUID) >= 0 ? &GUID : ""
}
ElementInteraction(ElementID, ElementTag, Browser, Action, ValueToSet, Type){
/*
Arguments are utilized by DOM Elements in IE
ElementID: this is the specific Identifier handle you're looking for. This can either be the "Name" or the "ID" of the element itself. Case Sensitive
ElementTag: This is the actual tag that houses the Element. So, <div>; <input>; <p> ; <a> etc...
Browser: This is the IE Object Handle. Typically made as "ie"
Action: The desired action once the object has been found. Accepted inputs are "Click", "Set", and "Get"
ValueToSet: This is only utilized in the "Set" Action. This must be not null to pass appropriately, but will not affect the "Get" and "Click" Portions
Type: Specifies the type of input you are using. Currently, this function can only be utilized with "ID" or "Name"
*/
;~ IfWinNotExist, Libraries
;~ run, explorer.exe
if (Type = "ID")
{
try
{
AllItems:=ie.Document.getElementsByTagName("*")
DomObj:=ie.Document
Length:=AllItems.Length
loop, % Length
{
ToolTip, %A_index%
if (instr(AllItems[A_Index-1].ID,ElementID))
{
my_element:=DomObj.getElementById(ElementID)
if (Action="Click")
{
WinActivate, File Explorer
WinMinimize, File Explorer
clickLast(ElementID, ie, false)
my_element.focus
my_element.click()
my_element.onClick()
my_element.FireEvent("OnClick")
Sleep 600
IeLoad(Browser)
return True
}
if (Action="Set")
{
my_element.focus
my_element.value:=ValueToSet
Sleep, 50
my_element.onChange()
my_element.FireEvent("onChange")
IeLoad(Browser)
return True
}
if (Action="Get")
{
try {
value:=my_element.value
return my_element.value
} catch e {
value:= my_element.innerText
return my_element.innerText
}
}
}
}
}
}
if (Type = "Name") {
Try
{
AllItems:=ie.Document.GetElementsByTagName(ElementTag)
DomObj:=ie.Document
Length:=AllItems.Length()
Loop % Length
{
Haystack:= AllItems[A_index-1].OuterHtml
Needle = name=`"%ElementID%`"
if (instr(AllItems[A_index-1].OuterHtml,ElementID))
{
my_element:=DomObj.getElementsByTagName(ElementTag)[A_index-1]
ClickElement:=DomObj.getElementsByTagName(ElementID)
if (Action="Click"){
WinActivate, File Explorer
WinMinimize, File Explorer
clickLast(ElementID, ie, false)
my_element.focus
my_element.click()
my_element.onClick()
my_element.FireEvent("OnClick")
Sleep 600
IeLoad(Browser)
return True
}
if (Action="Set"){
my_element.focus
my_element.value:=ValueToSet
Sleep, 50
my_element.onChange()
my_element.FireEvent("onChange")
IeLoad(Browser)
return True
}
if (Action="Get"){
try {
value:=my_element.value
return my_element.value
} catch e {
value:= my_element.innerText
return my_element.innerText
}
}
}
}
}
}
return false
}
ClickLast(ElementID, Browser, iframe){ ;Still Under Construction
if (iframe = TRUE)
{
AllItems:=Browser.Document.getElementsByTagName("iFrame")
Length:=AllItems.Length
loop, % Length
{
if (instr(AllItems[A_Index-1].ID,ElementID) && isObject(AllItems[A_Index-1].contentdocument.getElementById(ElementID))){
AllItems[A_Index-1].contentdocument.getElementById(elementID).Click()
IeLoad(Browser)
return True
}
}
} else {
AllItems:=Browser.Document.getElementsByTagName("*")
Length:=AllItems.Length
DomObj:=ie.Document
loop, % Length
{
if (instr(AllItems[A_Index-1].ID,ElementID) && isObject(DomObj.getElementById(ElementID))){
DomObj.getElementById(elementID).Click()
IeLoad(Browser)
return True
}
}
}
return False
}
FindElement(Document, ElementName, ElementType, Index:=0, TagName:="*", LikeMatch:=False){ ; Still Under Construction
/*
Function Details:
Returns an Element Object
Parameter 1: Pointer to an IE.Document
Parameter 2: The ElementName you are searching for
If you are searching for the: text, alt, value, innertext or title properties this is value you are looking for
Parameter 3: The ElementType you are looking for
Valid Parameters: ID, Name, ClassName,Text, Alt, Value, InnerText or Title. All parameters are strings.
Parameter 4 (Optional): This is the index number for getElementsByName/ClassName.
Default value is 0 (first item)
Parameter 5 (Optional): This is the TagName keyword to reduce the search space in getElementsbyTagName.
Default value is everything ("*")
*/
loop, 10
{
try
{
SubDocument:= Document.getElementsbyTagName("iFrame")[A_Index-1].contentdocument
FindElement:=FindElement(SubDocument,ElementName,ElementType,Index,TagName,LikeMatch)
if isObject(FindElement){
FindElement.Focus()
return FindElement
}
}
}
if (LikeMatch=True){
AllItems:=Document.Body.getElementsbyTagName(TagName)
Length:=AllItems.Length
Loop, %Length%
{
try
{
if (ElementType="ID" && instr(AllItems[A_Index-1].ID,ElementName))
return AllItems[A_Index-1]
if (ElementType="Name" && instr(AllItems[A_Index-1].Name,ElementName))
return AllItems[A_Index-1]
if (ElementType="ClassName" && instr(AllItems[A_Index-1].Class,ElementName))
return AllItems[A_Index-1]
if (ElementType="Value" && instr(AllItems[A_Index-1].Value,ElementName))
return AllItems[A_Index-1]
if (ElementType="Alt" && instr(AllItems[A_Index-1].Alt,ElementName))
return AllItems[A_Index-1]
if (ElementType="Text" && instr(AllItems[A_Index-1].Text,ElementName))
return AllItems[A_Index-1]
if (ElementType="Title" && instr(AllItems[A_Index-1].Title,ElementName))
return AllItems[A_Index-1]
if (ElementType="InnerText" && instr(AllItems[A_Index-1].InnerText,ElementName))
return AllItems[A_Index-1]
if (ElementType="Href" && instr(AllItems[A_Index-1].href,ElementName))
return AllItems[A_Index-1]
}
}
}
if (LikeMatch=False){
if (ElementType="Name" && isobject(Document.getElementsbyName(ElementName)[Index]))
return Document.getelementsbyName(ElementName)[Index]
if (ElementType="ClassName" && isObject(Document.getElementsbyClassName(ElementName)[Index]))
return Document.getElementsbyClassName(ElementName)[Index]
if (ElementType="ID" && isObject(Document.getElementbyID(ElementName)))
return Document.getElementbyID(ElementName)
AllItems:=Document.Body.getElementsbyTagName(TagName)
Length:=AllItems.Length
Loop, %Length%
{
try
{
if (ElementType="Text" && AllItems[A_Index-1].text=ElementName)
return AllItems[A_Index-1]
if (ElementType="Value" && AllItems[A_Index-1].value=ElementName)
return AllItems[A_Index-1]
if (ElementType="Alt" && AllItems[A_Index-1].alt=ElementName)
return AllItems[A_Index-1]
if (ElementType="Title" && AllItems[A_Index-1].title=ElementName)
return AllItems[A_Index-1]
if (ElementType="InnerText" && AllItems[A_Index-1].InnerText=ElementName)
return AllItems[A_Index-1]
if (ElementType="Href" && AllItems[A_Index-1].href=ElementName)
return AllItems[A_Index-1]
if (ElementType="TabIndex" && AllItems[A_Index-1].TabIndex=ElementName)
return AllItems[A_Index-1]
}
}
}
}