forked from RaptorX/Rufaydium-Webdriver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRufaydium.ahk
1074 lines (960 loc) · 25.9 KB
/
Rufaydium.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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
; Rufaydium v1.7.0
;
; Rufaydium : AutoHotkey WebDriver Library to interact with browsers.
; Requirement : WebDriver version needs to be compatible with the Browser version.
; Rufaydium will automatically try to download the correct version.
; Supported browsers : Chrome, MS Edge, Firefox, Opera
;
; Rufaydium utilizes Rest API of W3C from https://www.w3.org/TR/webdriver2/
; and also supports Chrome Devtools Protocols same as chrome.ahk
;
; Note : no need to install / setup Selenium, Rufaydium is AHK's Selenium
; Link : https://www.autohotkey.com/boards/viewtopic.php?f=6&t=102616
; Git : https://github.com/Xeo786/Rufaydium-Webdriver
; By Xeo786 - GPL-3.0 license, see LICENSE
#Requires Autohotkey v1.1.33+
#include %A_LineFile%\..\
#Include WDM.ahk
#Include CDP.ahk
#Include JSON.ahk
#include WDElements.ahk
#Include Capabilities.ahk
#include actions.ahk
Class Rufaydium
{
static WebRequest := ComObjCreate("WinHttp.WinHttpRequest.5.1")
__new(DriverName:="chromedriver.exe",Parameters:="--port=0")
{
this.Driver := new RunDriver(DriverName,Parameters)
this.DriverUrl := "http://127.0.0.1:" This.Driver.Port
Switch this.Driver.Name
{
case "chromedriver" :
this.capabilities := new ChromeCapabilities(this.Driver.browser,this.Driver.Options)
case "msedgedriver" :
this.capabilities := new EdgeCapabilities(this.Driver.browser,this.Driver.Options)
case "geckodriver" :
this.capabilities := new FireFoxCapabilities(this.Driver.browser,this.Driver.Options)
case "operadriver" :
this.capabilities := new OperaCapabilities(this.Driver.browser,this.Driver.Options)
case "BraveDriver" :
this.capabilities := new BraveCapabilities(this.Driver.browser,this.Driver.Options)
this.capabilities.Setbinary("C:\Program Files\BraveSoftware\Brave-Browser\Application\brave.exe")
; drive might crash for 32 Brave on 64 bit OS there for we can load binary while new session,
; i.e. >> NewSession("32bit brave browser exe location")
}
this.Driver.Location := this.Driver.GetPath() ;this.Driver.Dir "\" this.Driver.exe
if !isobject(cap := this.capabilities.cap)
this.capabilities := capabilities.Simple
}
__Delete()
{
;this.QuitAllSessions()
;this.Exit()
}
Exit()
{
this.Driver.Exit()
}
Send(url,Method,Payload:= 0,WaitForResponse:=1)
{
if !instr(url,"HTTP")
url := this.address "/" url
if !Payload and (Method = "POST")
Payload := Json.null
try r := Json.load(this.Request(url,Method,Payload,WaitForResponse)).value ; Thanks to GeekDude for his awesome cJson.ahk
if(r.error = "chrome not reachable") ; incase someone close browser manually but session is not closed for driver
this.quit() ; so we close session for driver at cost of one time response wait lag
if r
return r
}
Request(url,Method,p:=0,w:=0)
{
Rufaydium.WebRequest.Open(Method, url, false)
Rufaydium.WebRequest.SetRequestHeader("Content-Type","application/json")
if p
{
p := StrReplace(json.dump(p),"[[]]","[{}]") ; why using StrReplace() >> https://www.autohotkey.com/boards/viewtopic.php?f=6&p=450824#p450824
p := RegExReplace(p,"\\\\uE(\d+)","\uE$1") ; fixing Keys turn '\\uE000' into '\uE000'
Rufaydium.WebRequest.Send(p)
}
else
Rufaydium.WebRequest.Send()
if w
Rufaydium.WebRequest.WaitForResponse()
return Rufaydium.WebRequest.responseText
}
; The SetTimeouts method specifies the individual time-out components of a send/receive operation, in milliseconds.
SetTimeouts(ResolveTimeout:=3000,ConnectTimeout:=3000,SendTimeout:=3000,ReceiveTimeout:=3000)
{
Rufaydium.WebRequest.SetTimeouts(ResolveTimeout,ConnectTimeout,SendTimeout,ReceiveTimeout)
}
; To create New Rufaydium Session
NewSession(Binary:="")
{
if !this.capabilities.options
{
Msgbox,64,Rufaydium WebDriver Support, % "Unknown Driver Loaded`n.Please read readme and manually set capabilities for " this.Driver.Name ".exe"
return
}
if Binary
this.capabilities.Setbinary(Binary)
this.Driver.Options := this.capabilities.options ; in case someone uses a custom driver and want to change capabilities manually
k := this.Send( this.DriverUrl "/session","POST",this.capabilities.cap,1)
if !k
{
msgbox,48,Rufaydium WebDriver Error, % This.driver.Name " Error`nRufaydium is unable to access Driver Session`n as No response received against New Session request`n`nMake sure you have driver version supported with browser version"
return
}
if k.error
{
if(k.message = "binary is not a Firefox executable")
{
; its all in my mind not tested, 32/64ahk 64OS 32/64ff broken down in simple three step logic
ffbinary := A_ProgramFiles "\Mozilla Firefox\firefox.exe" ; check ff in default location, cover all 32AHKFFOS, 64AHKFFOS
if !FileExist(ffbinary)
ffbinary := RegExReplace(ffbinary, " (x86)") ; in case 64OS 32AHK 64FF checking 64ff loc
else if !FileExist(ffbinary)
ffbinary := A_ProgramFiles " (x86)\Mozilla Firefox\firefox.exe" ; in case 64OS has 64ahk checking 32ff loc
else
{
msgbox,48,Rufaydium WebDriver Support,% k.message "`n`nDriver is unable to locate Firefox binary and, Rufaydium is also unable to detect Firefox default location.`n`nIf you see this message repeatedly please file a bug report."
return
}
this.capabilities.Setbinary(ffbinary)
return This.NewSession()
}
else if RegExMatch(k.message,"version ([\d.]+).*\n.*version is (\d+.\d+.\d+)")
{
MsgBox, 52,Rufaydium WebDriver Support,% k.message "`n`nPlease press Yes to download latest driver"
IfMsgBox Yes
{
this.driver.exit()
i := this.driver.GetDriver(k.message)
if !FileExist(i)
{
Msgbox,64,Rufaydium WebDriver Support,Unable to download driver`nRufaydium exiting.
ExitApp
}
This.Driver := new RunDriver(i,This.Driver.Param)
return This.NewSession()
}
}
else
{
msgbox, 48,Rufaydium WebDriver Support Error,% k.error "`n`n" k.message
return k
}
}
window := []
window.Name := This.driver.Name
window.DriverPID := This.driver.PID
if window.Name = "geckodriver"
{
window.debuggerAddress := "http://" k.capabilities["moz:debuggerAddress"]
IniWrite, % window.debuggerAddress, % this.driver.dir "/ActiveSessions.ini", % This.driver.Name, % k.SessionId
}
else
window.debuggerAddress := StrReplace(k.capabilities[This.driver.options].debuggerAddress,"localhost","http://127.0.0.1")
window.address := this.DriverUrl "/session/" k.SessionId
window.id := k.SessionId
if k.capabilities.websocketurl
window.websocketurl := k.capabilities.websocketurl
return new Session(window)
}
Sessions() ; get all Sessions Details
{
return this.send(this.DriverUrl "/sessions","GET")
}
getSessions() ; get all Sessions for Rufaydium
{
if !this.capabilities.options
{
Msgbox,64,Rufaydium WebDriver Support, % "Unknown Driver Loaded.`nPlease read readme and manually set capabilities for " this.Driver.Name ".exe"
return
}
this.Driver.Options := this.capabilities.options
i := 0
if This.driver.Name = "geckodriver"
{
IniRead, SessionList, % this.driver.dir "/ActiveSessions.ini", % This.driver.Name
Windows := []
for k, se in StrSplit(SessionList,"`n")
{
if !RegExMatch(se, "(.*)=(.*)", $)
{
IniDelete, % this.driver.dir "/ActiveSessions.ini", % This.driver.Name, % se
continue
}
r := this.Send(this.DriverUrl "/session/" $1 "/url","GET")
if r.error
IniDelete, % this.driver.dir "/ActiveSessions.ini", % This.driver.Name, % $1
else
{
s := []
s.id := $1
s.DriverPID := This.driver.PID
s.Name := This.driver.Name
s.address := this.DriverUrl "/session/" s.id
s.debuggerAddress := $2
windows[++i] := new Session(s)
}
}
return windows
}
windows := []
for k, se in this.Sessions()
{
chromeOptions := Se["capabilities",This.driver.options]
s := []
s.id := Se.id
s.DriverPID := This.driver.PID
s.Name := This.driver.Name
s.debuggerAddress := StrReplace(chromeOptions.debuggerAddress,"localhost","http://127.0.0.1")
s.address := this.DriverUrl "/session/" s.id
if se.capabilities.websocketurl
s.websocketurl := se.capabilities.websocketurl
windows[k] := new Session(s)
}
return windows
}
; Get existing Session by number 'i' and Tab 't'
getSession(i:=0,t:=0)
{
if i
{
S := this.getSessions()[i]
if t
{
S.SwitchTab(t)
}
else
S.ActiveTab()
return S
}
}
; get Existing Session by URL, it will look into all sessions and return with first match
getSessionByUrl(URL)
{
for k, w in this.getSessions()
{
w.SwitchbyURL(URL)
if instr(w.URL,URL)
return w
}
}
; get Existing Session by Title, will look for title in all sessions and return with first match
getSessionByTitle(Title)
{
for k, s in this.getSessions()
{
s.SwitchbyTitle(Title)
if instr(s.title,Title)
return s
}
}
; Get all session Quit one by one
QuitAllSessions()
{
for k, s in this.getSessions()
s.Quit()
}
; To verify driver is there working
Status()
{
return Rufaydium.Request( this.DriverUrl "/status","GET")
}
__Get(n)
{
return this.Send( this.DriverUrl "/status","GET")[n]
}
}
Class Session
{
__new(i)
{
this.id := i.id
this.Address := i.address
this.debuggerAddress := i.debuggerAddress
this.name := i.name
if i.websocketurl
this.websocketurl := i.websocketurl
this.currentTab := this.Send("window","GET")
switch i.name
{
case "chromedriver" :
this.CDP := new CDP(this.Address)
case "msedgedriver" :
this.CDP := new CDP(this.Address)
case "operadriver" :
this.CDP := new CDP(this.Address)
case "geckodriver" :
for proc in ComObjGet("winmgmts:").ExecQuery("Select * from Win32_Process WHERE Name = 'firefox.exe'")
if ( proc.ParentProcessId = i.DriverPID)
for proc2 in ComObjGet("winmgmts:").ExecQuery("Select * from Win32_Process WHERE Name = 'firefox.exe'")
if ( proc2.ParentProcessId = proc.processid)
{
this.BroswerPID := proc2.processid
break
}
}
}
__Delete()
{
;this.Quit()
}
; To quit Session
Quit()
{
this.Send(this.address ,"DELETE")
}
; To quit tab or window
close()
{
Tabs := this.Send("window","DELETE")
this.Switch(this.currentTab := tabs[tabs.Length()])
}
send(url,Method,Payload:= 0,WaitForResponse:=1)
{
if !instr(url,"HTTP")
url := this.address "/" url
if (Payload = 0) and (Method = "POST")
Payload := Json.null
try r := Json.load(Rufaydium.Request(url,Method,Payload,WaitForResponse)).value ; Thanks to GeekDude for his awesome cJson.ahk
if(r.error = "chrome not reachable") ; incase someone close browser manually but session is not closed for driver
this.quit() ; so we close session for driver at cost of one time response wait lag
if r
return r
}
; to create New tab, NewTab(0) will create new tab without switching
NewTab(i:=1)
{
This.currentTab := this.Send("window/new","POST",{"type":"tab"}).handle
if i
This.Switch(This.currentTab)
}
; To create New window, NewWindow(0) will create new window without switching
NewWindow(i:=1) ; by https://github.com/hotcheesesoup
{
This.currentTab := this.Send("window/new","POST",{"type":"window"}).handle
if i
This.Switch(This.currentTab)
}
; Traditional Browser Json/List
Detail()
{
return Json.load(Rufaydium.Request(this.debuggerAddress "/json/list","GET"))
}
GetTabs()
{
return this.Send("window/handles","GET")
}
Switch(Tabid)
{
this.currentTab := Tabid
this.Send("window","POST",{"handle":Tabid})
}
Title
{
get
{
return this.Send("title","GET")
}
}
; Switch to active tab, incase manually switch tab, working good for chromium but not for firefox
ActiveTab()
{
if( this.name != "geckodriver" )
this.Switch(this.Detail()[1].id ) ; First id always Current Handle
else
{
wingettitle, title, % "ahk_pid " this.BroswerPID
this.SwitchbyTitle(strreplace(title," — Mozilla Firefox"))
}
}
; switch tab/window by number 'i'
SwitchTab(i:=0)
{
return this.Switch(This.currentTab := this.GetTabs()[i])
}
; Switch tab/window by Title
SwitchbyTitle(Title:="")
{
; Rufaydium will soon use CDP Target's methods to re-access sessions and pages
; might able to access pages even after restarting webdriver
; Targets := this.CDP.GetTargets()
handles := this.GetTabs()
try pages := this.Detail() ; if Browser closed by user this will closed the session
if !pages
this.quit()
for k , handle in handles
{
for i, t in pages ;Targets.targetInfos
{
if instr(Handle,t.id)
{
if !t.HasKey("Title")
{
phandle := This.currentTab
this.Switch(handle)
if instr(this.title,Title)
return
else
continue
}
if instr(t.Title, Title)
{
This.currentTab := handle ; "CDwindow-" t.targetid
this.Switch(This.currentTab )
;this.CDP.Switch(t.targetid)
return
}
}
}
}
if pHandle
this.Switch(handle)
}
; Switch tab/window by URL
SwitchbyURL(url:="",Silent:=1)
{
; Rufaydium will soon use CDP Target's methods to re-access sessions and pages
; might able to access pages even after restarting webdriver
;Targets := this.CDP.GetTargets()
handles := this.GetTabs()
try pages := this.Detail() ; if Browser closed by user this will closed the session
if !pages
this.quit()
for k , handle in handles
{
for i, t in pages ;Targets.targetInfos
{
if instr(Handle,t.id)
{
if instr(t.url, url)
{
This.currentTab := Handle ;"CDwindow-" t.targetid
this.Switch(This.currentTab )
;this.CDP.Switch(t.targetid)
return
}
}
}
}
}
url
{
get
{
return this.Send("url","GET")
}
set
{
return this.Send("url","POST",{"url":RegExReplace(Value,"^(?!\w+[:\/])(.*)","https://$1",,1)})
}
}
; to refresh active tab/window
Refresh()
{
return this.Send("refresh","POST")
}
IsLoading
{
get
{
return this.Send("is_loading","GET")
}
}
timeouts()
{
return this.Send("timeouts","GET")
}
; to navigate to 1 or multiple urls Navigate(url1,url2,url3)
Navigate(urls*)
{
for i, url in urls
{
if a_index != 1
this.NewTab(i:=1)
this.URL := url
}
}
Forward()
{
return this.Send("forward","POST") ; not tested
}
Back()
{
return this.Send("back","POST") ; not tested
}
GetRect()
{
return this.Send("window/rect","GET")
}
SetRect(x:=1,y:=1,w:=0,h:=0)
{
if !w
w := A_ScreenWidth - 0
if !h
h := A_ScreenHeight - (A_ScreenHeight * 5 / 100)
return this.Send("window/rect","POST",{"x":x,"y":y,"width":w,"height":h})
}
X
{
get
{
rect := this.GetRect()
return rect.x
}
Set
{
msgbox, % value
return this.Send("window/rect","POST",{"x":value})
}
}
Y
{
get
{
rect := this.GetRect()
return rect.y
}
Set
{
return this.Send("window/rect","POST",{"y":value})
}
}
width
{
get
{
rect := this.GetRect()
return rect.width
}
Set
{
return this.Send("window/rect","POST",{"width":value})
}
}
height
{
get
{
rect := this.GetRect()
return rect.height
}
Set
{
return this.Send("window/rect","POST",{"height":value})
}
}
Maximize()
{
return this.Send("window/maximize","POST",json.null)
}
Minimize()
{
return this.Send("window/minimize","POST",json.null)
}
FullScreen()
{
return this.Send("window/fullscreen","POST",json.null)
}
FramesLength()
{
return this.ExecuteSync("return window.length")
}
Frame(i)
{
return this.Send("frame","POST",{"id":i})
}
ParentFrame()
{
return this.Send("frame/parent","POST",json.null)
}
HTML
{
get
{
return this.Send("source","GET",0,1)
}
}
ActiveElement()
{
for i, elementid in this.Send("element/active","GET")
{
address := RegExReplace(this.address "/element/" elementid,"(\/shadow\/.*)\/element","/element")
address := RegExReplace(address "/element/" elementid,"(\/element\/.*)\/element","/element")
return New WDElement(address,i)
}
}
findelement(u,v)
{
r := this.Send("element","POST",{"using":u,"value":v},1)
for i, elementid in r
{
if instr(elementid,"no such")
return 0
address := RegExReplace(this.address "/element/" elementid,"(\/shadow\/.*)\/element","/element")
address := RegExReplace(address "/element/" elementid,"(\/element\/.*)\/element","/element")
return New WDElement(address,i)
}
}
findelements(u,v)
{
e := []
for k, element in this.Send("elements","POST",{"using":u,"value":v},1)
{
for i, elementid in element
{
address := RegExReplace(this.address "/element/" elementid,"(\/shadow\/.*)\/element","/element")
address := RegExReplace(address "/element/" elementid,"(\/element\/.*)\/element","/element")
e[k-1] := New WDElement(address,i)
}
}
if e.count() > 0
return e
return 0
}
shadow()
{
for i, elementid in this.Send("shadow","GET")
{
address := RegExReplace(this.address "/element/" elementid,"(\/element\/.*)\/element","/shadow")
return new ShadowElement(address)
}
}
getElementByID(id)
{
return this.findelement(by.selector,"#" id)
}
QuerySelector(Path)
{
return this.findelement(by.selector,Path)
}
QuerySelectorAll(Path)
{
return this.findelements(by.selector,Path)
}
getElementsbyClassName(Class)
{
return this.findelements(by.selector,"[class='" Class "']")
}
getElementsbyTagName(Name)
{
return this.findelements(by.TagName,Name)
}
getElementsbyName(Name)
{
return this.findelements(by.selector,"[Name='" Name "']")
}
getElementsbyXpath(xPath)
{
return this.findelements(by.xPath,xPath)
}
ExecuteSync(Script,Args*)
{
return this.Send("execute/sync","POST", { "script":Script,"args":[Args*]},1)
}
ExecuteAsync(Script,Args*)
{
return this.Send("execute/async","POST", { "script":Script,"args":Args*},1)
}
GetCookies()
{
return this.Send("cookie","GET")
}
GetCookieName(Name)
{
return this.Send("cookie/" Name,"GET")
}
AddCookie(CookieObj)
{
return this.Send("cookie","POST",CookieObj)
}
Alert(Action,Text:=0)
{
switch Action
{
case "accept": i := "/alert/accept", m := "POST"
case "dismiss": i := "/alert/dismiss", m := "POST"
case "GET": i := "/alert/text", m := "GET"
case "Send": i := "/alert/text", m := "POST"
}
if Text
return this.Send(this.address i,m,{"text":Text})
else
return this.Send(this.address i,m)
}
Screenshot(location:=0)
{
Base64Canvas := this.Send("screenshot","GET")
if Base64Canvas
{
nBytes := Base64Dec( Base64Canvas, Bin ) ; thank you Skan :)
File := FileOpen(location, "w")
File.RawWrite(Bin, nBytes)
File.Close()
}
}
CaptureFullSizeScreenShot(location)
{
if !isobject(this.CDP)
{
msgbox, ,Rufaydium, Unable to Capture Full Size ScreenShot`nThis Chromium limited method
return
}
JSOP := {"width":this.Getrect().width,"height":this.ExecuteSync("return document.documentElement.scrollHeight")+0,"deviceScaleFactor":1,"mobile":json.false}
this.CDP.Call("Emulation.setDeviceMetricsOverride",JSOP)
this.Screenshot(location)
this.CDP.Call("Emulation.clearDeviceMetricsOverride")
}
Print(PDFLocation,Options:=0)
{
if !instr(PDFLocation,".pdf")
{
msgbox, ,Rufaydium, error: File location be ".pdf"
return
}
if this.Capabilities.HeadlessMode
{
Base64pdfData := this.Send("print","POST",Options) ; does not work
if !Base64pdfData.error
{
nBytes := Base64Dec( Base64pdfData, Bin ) ; thank you Skan :)
File := FileOpen(PDFLocation, "w")
File.RawWrite(Bin, nBytes)
File.Close()
}
else
msgbox, ,Rufaydium, % "Fail to save PDF`nError : " json.Dump(Base64pdfData) "`nPlease define Print Options or use print profiles from PrintOptions.class`nSince Chrome Printing is not available in Headful mode you can try 'wkhtmltopdf' printing"
}
else
{
if isProgInstalled("wkhtmltox") or isProgInstalled("wkhtmltopdf")
{
wkhtmltopdf(this.HtML,PDFLocation,options)
}
else
{
MsgBox,36,Rufaydium, User is required to install "wkhtmltopdf" In order to enable pdf printing without Headless mode`n`nPress Yes to navigate to download page of "wkhtmltox" tool
IfMsgBox Yes
{
this.NewTab()
this.url := "https://wkhtmltopdf.org/downloads.html"
MsgBox,64,Rufaydium,Please Download and install "wkhtmltox" now, according to Windows Version then Restart Rufaydium.
}
}
}
}
click(i:=0) ; [button: 0(left) | 1(middle) | 2(right)]
{
MouseEvent := new mouse()
MouseEvent.Release(i)
MouseEvent.Pause(100)
MouseEvent.Release(i)
return this.Actions(MouseEvent)
}
DoubleClick(i:=0) ; [button: 0(left) | 1(middle) | 2(right)]
{
MouseEvent := new mouse()
; click 1
MouseEvent.Release(i)
MouseEvent.Pause(100)
MouseEvent.Release(i)
; delay
MouseEvent.Pause(500)
; click 2
MouseEvent.Release(i)
MouseEvent.Pause(100)
MouseEvent.Release(i)
return this.Actions(MouseEvent)
}
MBDown(i:=0) ; [button: 0(left) | 1(middle) | 2(right)]
{
MouseEvent := new mouse()
MouseEvent.Press(i)
return this.Actions(MouseEvent)
}
MBup(i:=0) ; [button: 0(left) | 1(middle) | 2(right)]
{
MouseEvent := new mouse()
MouseEvent.Release(i)
return this.Actions(MouseEvent)
}
Move(x,y)
{
MouseEvent := new mouse()
MouseEvent.move(x,y,0)
return this.Actions(MouseEvent)
}
ScrollUP(s:=50)
{
WheelEvent := new Scroll()
WheelEvent.ScrollUP(s)
r := this.Actions(WheelEvent)
WheelEvent := ""
return r
}
ScrollDown(s:=50)
{
WheelEvent := new Scroll()
WheelEvent.ScrollDown(s)
return this.Actions(WheelEvent)
}
ScrollLeft(s:=50)
{
WheelEvent := new Scroll()
WheelEvent.ScrollLeft(s)
return this.Actions(WheelEvent)
}
ScrollRight(s:=50)
{
WheelEvent := new Scroll()
WheelEvent.ScrollRight(s)
return this.Actions(WheelEvent)
}
SendKey(Chars)
{
KeyboardEvent := new Keyboard()
KeyboardEvent.SendKey(Chars) ; right now it does not support Key.Class()
return this.Actions(KeyboardEvent)
}
Actions(Interactions*)
{
if Interactions.count()
{
ActionArray := []
for i, interaction in Interactions
{
ActionArray.push(interaction.perform())
Interactions.clear()
Interaction := ""
}
return this.Send("actions","POST",{"actions":ActionArray})
}
else
return this.Send("actions","DELETE")
}
execute_sql()
{
return this.Send("execute_sql","POST",{"":""}) ; idk about sql
}
}
Class by
{
static selector := "css selector"
static Linktext := "link text"
static Plinktext := "partial link text"
static TagName := "tag name"
static XPath := "xpath"
}
Class PrintOptions ; https://www.w3.org/TR/webdriver2/#print
{
static A4_Default =
( LTrim Join
{
"page":{
"width": 50,
"height": 60
},
"margin":{
"top": 2,
"bottom": 2,
"left": 2,
"right": 2
},
"scale": 1,
"orientation":"portrait",
"shrinkToFit": json.true,
"background": json.true
}
)
}
; https://www.autohotkey.com/boards/viewtopic.php?t=35964
Base64Dec( ByRef B64, ByRef Bin ) { ; By SKAN / 18-Aug-2017
Local Rqd := 0, BLen := StrLen(B64) ; CRYPT_STRING_BASE64 := 0x1
DllCall( "Crypt32.dll\CryptStringToBinary", "Str",B64, "UInt",BLen, "UInt",0x1
, "UInt",0, "UIntP",Rqd, "Int",0, "Int",0 )
VarSetCapacity( Bin, 128 ), VarSetCapacity( Bin, 0 ), VarSetCapacity( Bin, Rqd, 0 )
DllCall( "Crypt32.dll\CryptStringToBinary", "Str",B64, "UInt",BLen, "UInt",0x1
, "Ptr",&Bin, "UIntP",Rqd, "Int",0, "Int",0 )
Return Rqd
}
Base64Enc( ByRef Bin, nBytes, LineLength := 64, LeadingSpaces := 0 ) { ; By SKAN / 18-Aug-2017
Local Rqd := 0, B64, B := "", N := 0 - LineLength + 1 ; CRYPT_STRING_BASE64 := 0x1
DllCall( "Crypt32.dll\CryptBinaryToString", "Ptr",&Bin ,"UInt",nBytes, "UInt",0x1, "Ptr",0, "UIntP",Rqd )
VarSetCapacity( B64, Rqd * ( A_Isunicode ? 2 : 1 ), 0 )
DllCall( "Crypt32.dll\CryptBinaryToString", "Ptr",&Bin, "UInt",nBytes, "UInt",0x1, "Str",B64, "UIntP",Rqd )
If ( LineLength = 64 and ! LeadingSpaces )
Return B64
B64 := StrReplace( B64, "`r`n" )
Loop % Ceil( StrLen(B64) / LineLength )
B .= Format("{1:" LeadingSpaces "s}","" ) . SubStr( B64, N += LineLength, LineLength ) . "`n"
Return RTrim( B,"`n" )
}
isProgInstalled(Prog)
{
shell := ComObjCreate("Shell.Application")
programsFolder := shell.NameSpace("::{26EE0668-A00A-44D7-9371-BEB064C98683}\8\::{7B81BE6A-CE2B-4676-A29E-EB907A5126C5}")
items := programsFolder.Items()