-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebScrapping.ahk
1121 lines (1016 loc) · 46.5 KB
/
WebScrapping.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
#Requires AutoHotkey v2.0
class WebScrapping {
__New(chrome_path := '') {
this.chrome_path := chrome_path
this.working := true
this.page := false
this.working := false
; if !WinExist("ahk_exe chrome.exe")
; WinWait("ahk_exe chrome.exe")
}
CheckChrome() {
if !this.CheckPage()
if !WinExist("ahk_exe chrome.exe") {
path := this.chrome_path ? this.chrome_path : "C:\Program Files\Google\Chrome\Application\chrome.exe"
args := " --remote-debugging-port=9222 --remote-allow-origins=*"
Run path args, , , &chrome_pid
ProcessWait(chrome_pid)
}
WinWait("ahk_exe chrome.exe")
this.page := this.SetAnyPage()
}
Close() {
if !IsObject(this.page)
return false
this.page.Close()
this.page := false
return true
}
WaitForLoad() {
if !IsObject(this.page)
return false
this.page.WaitForLoad()
return true
}
CheckPage() {
if !IsObject(this.page) {
this.page := false
return false
}
return true
}
SetAnyPage() {
this.page := Chrome().GetPage()
if !this.CheckPage()
return false
return true
}
SetPageByTitle(title) {
this.CheckChrome()
this.page := Chrome().GetPageByTitle(title, 'contains')
return this.CheckPage()
}
SetPageByURL(url) {
this.CheckChrome()
this.page := Chrome().GetPageByURL(url, 'contains')
return this.CheckPage()
}
Activate(){
if !this.CheckPage()
return false
this.page.Activate()
return true
}
Kill(){
if !this.CheckPage()
return false
this.page.Kill()
return true
}
Navigate(url, wait_for_load := false) {
this.page.Call("Page.navigate", {
url: url
})
if wait_for_load
this.page.WaitForLoad()
return this.CheckPage()
}
SendJS(js) {
if !this.CheckPage()
return false
return this.page.Evaluate(js)
}
GetElement(selector) {
if !this.CheckPage()
return false
js :=
(
'(function(){
const element = ' selector ';
if (element) {
return element.outerHTML;
} else {
return false;
}
})()'
)
return this.page.Evaluate(js)["value"]
}
Scroll(pos := "bottom", amount := 0) {
if (pos = "bottom") {
; Scroll hasta el final de la página
this.page.Call("Runtime.evaluate", {
expression: "window.scrollTo({top: Math.max(document.documentElement.scrollHeight, document.body.scrollHeight, document.documentElement.clientHeight), behavior: 'smooth'})"
})
} else if (pos = "top") {
; Scroll hasta el inicio de la página
this.page.Call("Runtime.evaluate", {
expression: "window.scrollTo({top: 0, behavior: 'smooth'})"
})
} else if (pos = "specific") {
; Scroll a una posición específica
this.page.Call("Runtime.evaluate", {
expression: "window.scrollTo({top: " amount ", behavior: 'smooth'})"
})
} else if (pos = "relative") {
; Scroll relativo a la posición actual
this.page.Call("Runtime.evaluate", {
expression: "window.scrollBy({top: " amount ", behavior: 'smooth'})"
})
}
}
GetElementPosition(selector) {
if !this.GetElement(selector)
return false
js :=
(
'(() => {
const element = ' selector ';
if (!element) return "null|null|false|null|null";
const rect = element.getBoundingClientRect();
const viewportWidth = window.innerWidth || document.documentElement.clientWidth;
const viewportHeight = window.innerHeight || document.documentElement.clientHeight;
const centerX = Math.round(rect.left + rect.width / 2);
const centerY = Math.round(rect.top + rect.height / 2);
const isVisible = (
rect.top >= 0 &&
rect.left >= 0 &&
rect.bottom <= viewportHeight &&
rect.right <= viewportWidth);
return ``${centerX}|${centerY}|${isVisible}|${Math.round(rect.width)}|${Math.round(rect.height)}``;
})();'
)
arr := StrSplit(this.page.Evaluate(js)["value"], "|")
return {
centerX: arr[1],
centerY: arr[2],
isVisible: arr[3],
width: arr[4],
height: arr[5]
}
}
GetValue(js) {
if !this.CheckPage()
return false
return this.page.Evaluate(js)["value"]
}
ClickElement(selector, button := "left", clickCount := 1) {
pos := this.GetElementPosition(selector)
if !pos
return false
loop clickCount
this.page.Evaluate(selector ".click()")
return true
}
ClickElementByPosition(selector, button := "left", clickCount := 1) {
; Obtiene la posición del elemento
try {
pos := this.GetElementPosition(selector)
if !pos {
return false
}
; Verifica que las coordenadas sean números válidos
if !IsNumber(pos.centerX) || !IsNumber(pos.centerY) {
throw Error("Coordenadas inválidas")
}
; Convertir coordenadas a números
x := Number(pos.centerX)
y := Number(pos.centerY)
; Normalizar el botón del mouse
button := StrLower(button)
if !InStr("left|middle|right", button) {
button := "left"
}
; Asegurar que clickCount sea un número positivo
clickCount := Max(1, Integer(clickCount))
; Simular el movimiento del mouse
this.page.Call("Input.dispatchMouseEvent", {
type: "mouseMoved",
x: x,
y: y
})
; Simular presionar el botón
this.page.Call("Input.dispatchMouseEvent", {
type: "mousePressed",
x: x,
y: y,
button: button,
clickCount: clickCount
})
; Simular soltar el botón
this.page.Call("Input.dispatchMouseEvent", {
type: "mouseReleased",
x: x,
y: y,
button: button,
clickCount: clickCount
})
return true
} catch as err {
; Manejo de errores
MsgBox("Error al hacer click: " err.Message)
return false
}
}
SimulateTyping(selector, text, options := "") {
try {
; Opciones por defecto
defaultOptions := {
focusFirst: true,
pressEnter: false
}
; Combinar opciones
options := options ? options : defaultOptions
; Focus en el elemento si es requerido
if (options.focusFirst) {
; Usar el método existente que sabemos que funciona
if !this.ClickElementByPosition(selector) {
throw Error("No se pudo encontrar el elemento: " selector)
}
Sleep(50) ; Pequeña pausa para asegurar el focus
}
; Mapa de teclas especiales
specialKeys := Map(
"{ENTER}", { key: "Enter", code: "Enter" },
"{TAB}", { key: "Tab", code: "Tab" },
"{SPACE}", { key: " ", code: "Space" },
"{BACKSPACE}", { key: "Backspace", code: "Backspace" },
"{DELETE}", { key: "Delete", code: "Delete" },
"{ESC}", { key: "Escape", code: "Escape" }
)
; Procesar el texto
pos := 1
while (pos <= StrLen(text)) {
isSpecialKey := false
; Revisar teclas especiales
for special, keyInfo in specialKeys {
if (SubStr(text, pos, StrLen(special)) = special) {
; Enviar eventos keyDown y keyUp para teclas especiales
this.page.Call("Input.dispatchKeyEvent", {
type: "keyDown",
key: keyInfo.key,
code: keyInfo.code
})
this.page.Call("Input.dispatchKeyEvent", {
type: "keyUp",
key: keyInfo.key,
code: keyInfo.code
})
pos += StrLen(special)
isSpecialKey := true
break
}
}
if (!isSpecialKey) {
; Procesar carácter normal
char := SubStr(text, pos, 1)
; Enviar eventos keyDown y keyUp para caracteres normales
this.page.Call("Input.dispatchKeyEvent", {
type: "keyDown",
key: char,
text: char,
windowsVirtualKeyCode: Ord(char)
})
this.page.Call("Input.dispatchKeyEvent", {
type: "keyUp",
key: char,
text: char,
windowsVirtualKeyCode: Ord(char)
})
pos++
}
Sleep(10) ; Pequeña pausa entre caracteres
}
; Presionar Enter al final si está configurado
if (options.pressEnter) {
this.page.Call("Input.dispatchKeyEvent", {
type: "keyDown",
key: "Enter",
code: "Enter"
})
this.page.Call("Input.dispatchKeyEvent", {
type: "keyUp",
key: "Enter",
code: "Enter"
})
}
return true
} catch as err {
MsgBox("Error en SimulateTyping: " err.Message)
return false
}
}
SimulatePaste(selector, text, options := "") {
try {
; Opciones por defecto
defaultOptions := { focusFirst: true }
; Combinar opciones
options := options ? options : defaultOptions
; Focus en el elemento si es requerido
if (options.focusFirst) {
; Usar el método existente que sabemos que funciona
if !this.ClickElementByPosition(selector) {
throw Error("No se pudo encontrar el elemento: " selector)
}
Sleep(50) ; Pequeña pausa para asegurar el focus
}
; Construir el código JavaScript para pegar texto
js :=
(
'(() => {
const element = ' selector ';
if (element) {
element.value = "' text '";
element.dispatchEvent(new Event(`'input`', { bubbles: true }));
return true;
} else {
return false;
}
})()'
)
; Evaluar el JavaScript en la página
result := this.page.Evaluate(js)["value"]
if !result {
throw Error("No se pudo establecer el valor del elemento: " selector)
}
return true
} catch as err {
MsgBox("Error en SimulatePaste: " err.Message)
return false
}
}
}
/************************************************************************
* @description: Modify from G33kDude's Chrome.ahk v1
* @author thqby
* @date 2023/05/10
* @version 1.0.4
***********************************************************************/
class Chrome {
static _http := ComObject('WinHttp.WinHttpRequest.5.1'), Prototype.NewTab := this.Prototype.NewPage
static FindInstance(exename := 'Chrome.exe', debugport := 0) {
items := Map(), filter_items := Map()
for item in ComObjGet('winmgmts:').ExecQuery("SELECT CommandLine, ProcessID FROM Win32_Process WHERE Name = '" exename "' AND CommandLine LIKE '% --remote-debugging-port=%'"
)
(!items.Has(parentPID := ProcessGetParent(item.ProcessID)) && items[item.ProcessID] := [parentPID, item.CommandLine])
for pid, item in items
if !items.Has(item[1]) && (!debugport || InStr(item[2], ' --remote-debugging-port=' debugport))
filter_items[pid] := item[2]
for pid, cmd in filter_items
if RegExMatch(cmd, 'i) --remote-debugging-port=(\d+)', &m)
return { Base: this.Prototype, DebugPort: m[1], PID: pid }
}
/**
* @param ProfilePath - Path to the user profile directory to use. Will use the standard if left blank.
* @param URLs - The page or array of pages for Chrome to load when it opens
* @param Flags - Additional flags for Chrome when launching
* @param ChromePath - Path to Chrome or Edge, will detect from start menu when left blank
* @param DebugPort - What port should Chrome's remote debugging server run on
*/
__New(URLs := '', Flags := '', ChromePath := '', DebugPort := 9222, ProfilePath := '') {
; Verify ChromePath
if !ChromePath
try FileGetShortcut A_StartMenuCommon '\Programs\Chrome.lnk', &ChromePath
catch
ChromePath := RegRead(
'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\Chrome.exe', ,
'C:\Program Files (x86)\Google\Chrome\Application\Chrome.exe')
if !FileExist(ChromePath) && !FileExist(ChromePath :=
'C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe')
throw Error('Chrome/Edge could not be found')
; Verify DebugPort
if !IsInteger(DebugPort) || (DebugPort <= 0)
throw Error('DebugPort must be a positive integer')
this.DebugPort := DebugPort, URLString := ''
SplitPath(ChromePath, &exename)
URLs := URLs is Array ? URLs : URLs && URLs is String ? [URLs] : []
if instance := Chrome.FindInstance(exename, DebugPort) {
this.PID := instance.PID, http := Chrome._http
for url in URLs
http.Open('PUT', 'http://127.0.0.1:' this.DebugPort '/json/new?' url), http.Send()
return
}
; Verify ProfilePath
if (ProfilePath && !FileExist(ProfilePath))
DirCreate(ProfilePath)
; Escape the URL(s)
for url in URLs
URLString .= ' ' CliEscape(url)
hasother := ProcessExist(exename)
Run(CliEscape(ChromePath) ' --remote-debugging-port=' this.DebugPort ' --remote-allow-origins=*'
(ProfilePath ? ' --user-data-dir=' CliEscape(ProfilePath) : '')
(Flags ? ' ' Flags : '') URLString, , , &PID)
if (hasother && Sleep(600) || !instance := Chrome.FindInstance(exename, this.DebugPort))
throw Error(Format('{1:} is not running in debug mode. Try closing all {1:} processes and try again',
exename))
this.PID := PID
CliEscape(Param) => '"' RegExReplace(Param, '(\\*)"', '$1$1\"') '"'
}
/**
* End Chrome by terminating the process.
*/
Kill() {
ProcessClose(this.PID)
}
/**
* Queries Chrome for a list of pages that expose a debug interface.
* In addition to standard tabs, these include pages such as extension
* configuration pages.
*/
GetPageList() {
http := Chrome._http
try {
http.Open('GET', 'http://127.0.0.1:' this.DebugPort '/json')
http.Send()
return JSON.parse(http.responseText)
} catch
return []
}
FindPages(opts, MatchMode := 'exact') {
Pages := []
for PageData in this.GetPageList() {
fg := true
for k, v in (opts is Map ? opts : opts.OwnProps())
if !((MatchMode = 'exact' && PageData[k] = v) || (MatchMode = 'contains' && InStr(PageData[k], v))
|| (MatchMode = 'startswith' && InStr(PageData[k], v) == 1) || (MatchMode = 'regex' && PageData[k] ~= v
)) {
fg := false
break
}
if (fg)
Pages.Push(PageData)
}
return Pages
}
NewPage(url := 'about:blank', fnCallback?) {
http := Chrome._http
http.Open('PUT', 'http://127.0.0.1:' this.DebugPort '/json/new?' url), http.Send()
if ((PageData := JSON.parse(http.responseText)).Has('webSocketDebuggerUrl'))
return Chrome.Page(StrReplace(PageData['webSocketDebuggerUrl'], 'localhost', '127.0.0.1'), fnCallback?)
}
ClosePage(opts, MatchMode := 'exact') {
http := Chrome._http
switch Type(opts) {
case 'String':
return (http.Open('GET', 'http://127.0.0.1:' this.DebugPort '/json/close/' opts), http.Send())
case 'Map':
if opts.Has('id')
return (http.Open('GET', 'http://127.0.0.1:' this.DebugPort '/json/close/' opts['id']), http.Send())
case 'Object':
if opts.HasProp('id')
return (http.Open('GET', 'http://127.0.0.1:' this.DebugPort '/json/close/' opts.id), http.Send())
}
for page in this.FindPages(opts, MatchMode)
http.Open('GET', 'http://127.0.0.1:' this.DebugPort '/json/close/' page['id']), http.Send()
}
ActivatePage(opts, MatchMode := 'exact') {
http := Chrome._http
for page in this.FindPages(opts, MatchMode)
return (http.Open('GET', 'http://127.0.0.1:' this.DebugPort '/json/activate/' page['id']), http.Send())
}
/**
* Returns a connection to the debug interface of a page that matches the
* provided criteria. When multiple pages match the criteria, they appear
* ordered by how recently the pages were opened.
*
* Key - The key from the page list to search for, such as 'url' or 'title'
* Value - The value to search for in the provided key
* MatchMode - What kind of search to use, such as 'exact', 'contains', 'startswith', or 'regex'
* Index - If multiple pages match the given criteria, which one of them to return
* fnCallback - A function to be called whenever message is received from the page, `msg => void`
*/
GetPageBy(Key, Value, MatchMode := 'exact', Index := 1, fnCallback?) {
static match_fn := {
contains: InStr,
exact: (a, b) => a = b,
regex: (a, b) => a ~= b,
startswith: (a, b) => InStr(a, b) == 1
}
Count := 0, Fn := match_fn.%MatchMode%
for PageData in this.GetPageList()
if Fn(PageData[Key], Value) && ++Count == Index
return Chrome.Page(PageData['webSocketDebuggerUrl'], fnCallback?)
}
; Shorthand for GetPageBy('url', Value, 'startswith')
GetPageByURL(Value, MatchMode := 'startswith', Index := 1, fnCallback?) {
return this.GetPageBy('url', Value, MatchMode, Index, fnCallback?)
}
; Shorthand for GetPageBy('title', Value, 'startswith')
GetPageByTitle(Value, MatchMode := 'startswith', Index := 1, fnCallback?) {
return this.GetPageBy('title', Value, MatchMode, Index, fnCallback?)
}
/**
* Shorthand for GetPageBy('type', Type, 'exact')
*
* The default type to search for is 'page', which is the visible area of
* a normal Chrome tab.
*/
GetPage(Index := 1, Type := 'page', fnCallback?) {
return this.GetPageBy('type', Type, 'exact', Index, fnCallback?)
}
; Connects to the debug interface of a page given its WebSocket URL.
class Page extends WebSocket {
_index := 0, _responses := Map(), _callback := 0
/**
* @param url the url of webscoket
* @param events callback function, `(msg) => void`
*/
__New(url, events := 0) {
super.__New(url)
this._callback := events
pthis := ObjPtr(this)
SetTimer(this.KeepAlive := () => ObjFromPtrAddRef(pthis)('Browser.getVersion', , false), 25000)
}
__Delete() {
if !this.KeepAlive
return
SetTimer(this.KeepAlive, 0), this.KeepAlive := 0
super.__Delete()
}
Call(DomainAndMethod, Params?, WaitForResponse := true) {
if (this.readyState != 1)
throw Error('Not connected to tab')
; Use a temporary variable for ID in case more calls are made
; before we receive a response.
if !ID := this._index += 1
ID := this._index += 1
this.sendText(JSON.stringify(Map('id', ID, 'params', Params ?? {}, 'method', DomainAndMethod), 0))
if (!WaitForResponse)
return
; Wait for the response
this._responses[ID] := false
while (this.readyState = 1 && !this._responses[ID])
Sleep(20)
; Get the response, check if it's an error
if !response := this._responses.Delete(ID)
throw Error('Not connected to tab')
if !(response is Map)
return response
if (response.Has('error'))
throw Error('Chrome indicated error in response', , JSON.stringify(response['error']))
try return response['result']
}
Evaluate(JS) {
response := this('Runtime.evaluate', {
expression: JS,
objectGroup: 'console',
includeCommandLineAPI: JSON.true,
silent: JSON.false,
returnByValue: JSON.false,
userGesture: JSON.true,
awaitPromise: JSON.false
})
if (response is Map) {
if (response.Has('exceptionDetails'))
throw Error(response['result']['description'], , JSON.stringify(response['exceptionDetails']))
return response['result']
}
}
Close() {
RegExMatch(this.url, 'ws://[\d\.]+:(\d+)/devtools/page/(.+)$', &m)
http := Chrome._http, http.Open('GET', 'http://127.0.0.1:' m[1] '/json/close/' m[2]), http.Send()
this.__Delete()
}
Activate() {
http := Chrome._http, RegExMatch(this.url, 'ws://[\d\.]+:(\d+)/devtools/page/(.+)$', &m)
http.Open('GET', 'http://127.0.0.1:' m[1] '/json/activate/' m[2]), http.Send()
}
WaitForLoad(DesiredState := 'complete', Interval := 100) {
while this.Evaluate('document.readyState')['value'] != DesiredState
Sleep Interval
}
onClose(*) {
try this.reconnect()
catch WebSocket.Error
this.__Delete()
}
onMessage(msg) {
data := JSON.parse(msg)
if this._responses.Has(id := data.Get('id', 0))
this._responses[id] := data
try (this._callback)(data)
}
}
}
/************************************************************************
* @description The websocket client implemented through winhttp,
* requires that the system version be no less than win8.
* @author thqby
* @date 2024/01/27
* @version 1.0.7
***********************************************************************/
#DllLoad winhttp.dll
class JSON {
static null := ComValue(1, 0), true := ComValue(0xB, 1), false := ComValue(0xB, 0)
/**
* Converts a AutoHotkey Object Notation JSON string into an object.
* @param text A valid JSON string.
* @param keepbooltype convert true/false/null to JSON.true / JSON.false / JSON.null where it's true, otherwise 1 / 0 / ''
* @param as_map object literals are converted to map, otherwise to object
*/
static parse(text, keepbooltype := false, as_map := true) {
keepbooltype ? (_true := this.true, _false := this.false, _null := this.null) : (_true := true, _false := false,
_null := "")
as_map ? (map_set := (maptype := Map).Prototype.Set) : (map_set := (obj, key, val) => obj.%key% := val, maptype :=
Object)
NQ := "", LF := "", LP := 0, P := "", R := ""
D := [C := (A := InStr(text := LTrim(text, " `t`r`n"), "[") = 1) ? [] : maptype()], text := LTrim(SubStr(text,
2), " `t`r`n"), L := 1, N := 0, V := K := "", J := C, !(Q := InStr(text, '"') != 1) ? text := LTrim(text,
'"') : ""
loop parse text, '"' {
Q := NQ ? 1 : !Q
NQ := Q && RegExMatch(A_LoopField, '(^|[^\\])(\\\\)*\\$')
if !Q {
if (t := Trim(A_LoopField, " `t`r`n")) = "," || (t = ":" && V := 1)
continue
else if t && (InStr("{[]},:", SubStr(t, 1, 1)) || A && RegExMatch(t,
"m)^(null|false|true|-?\d+(\.\d*(e[-+]\d+)?)?)\s*[,}\]\r\n]")) {
loop parse t {
if N && N--
continue
if InStr("`n`r `t", A_LoopField)
continue
else if InStr("{[", A_LoopField) {
if !A && !V
throw Error("Malformed JSON - missing key.", 0, t)
C := A_LoopField = "[" ? [] : maptype(), A ? D[L].Push(C) : map_set(D[L], K, C), D.Has(++L) ?
D[L] := C : D.Push(C), V := "", A := Type(C) = "Array"
continue
} else if InStr("]}", A_LoopField) {
if !A && V
throw Error("Malformed JSON - missing value.", 0, t)
else if L = 0
throw Error("Malformed JSON - to many closing brackets.", 0, t)
else C := --L = 0 ? "" : D[L], A := Type(C) = "Array"
} else if !(InStr(" `t`r,", A_LoopField) || (A_LoopField = ":" && V := 1)) {
if RegExMatch(SubStr(t, A_Index),
"m)^(null|false|true|-?\d+(\.\d*(e[-+]\d+)?)?)\s*[,}\]\r\n]", &R) && (N := R.Len(0) - 2, R :=
R.1, 1) {
if A
C.Push(R = "null" ? _null : R = "true" ? _true : R = "false" ? _false : IsNumber(R) ?
R + 0 : R)
else if V
map_set(C, K, R = "null" ? _null : R = "true" ? _true : R = "false" ? _false :
IsNumber(R) ? R + 0 : R), K := V := ""
else throw Error("Malformed JSON - missing key.", 0, t)
} else {
; Added support for comments without '"'
if A_LoopField == '/' {
nt := SubStr(t, A_Index + 1, 1), N := 0
if nt == '/' {
if nt := InStr(t, '`n', , A_Index + 2)
N := nt - A_Index - 1
} else if nt == '*' {
if nt := InStr(t, '*/', , A_Index + 2)
N := nt + 1 - A_Index
} else nt := 0
if N
continue
}
throw Error("Malformed JSON - unrecognized character.", 0, A_LoopField " in " t)
}
}
}
} else if A || InStr(t, ':') > 1
throw Error("Malformed JSON - unrecognized character.", 0, SubStr(t, 1, 1) " in " t)
} else if NQ && (P .= A_LoopField '"', 1)
continue
else if A
LF := P A_LoopField, C.Push(InStr(LF, "\") ? UC(LF) : LF), P := ""
else if V
LF := P A_LoopField, map_set(C, K, InStr(LF, "\") ? UC(LF) : LF), K := V := P := ""
else
LF := P A_LoopField, K := InStr(LF, "\") ? UC(LF) : LF, P := ""
}
return J
UC(S, e := 1) {
static m := Map('"', '"', "a", "`a", "b", "`b", "t", "`t", "n", "`n", "v", "`v", "f", "`f", "r", "`r")
local v := ""
loop parse S, "\"
if !((e := !e) && A_LoopField = "" ? v .= "\" : !e ? (v .= A_LoopField, 1) : 0)
v .= (t := m.Get(SubStr(A_LoopField, 1, 1), 0)) ? t SubStr(A_LoopField, 2) :
(t := RegExMatch(A_LoopField, "i)^(u[\da-f]{4}|x[\da-f]{2})\K")) ?
Chr("0x" SubStr(A_LoopField, 2, t - 2)) SubStr(A_LoopField, t) : "\" A_LoopField,
e := A_LoopField = "" ? e : !e
return v
}
}
/**
* Converts a AutoHotkey Array/Map/Object to a Object Notation JSON string.
* @param obj A AutoHotkey value, usually an object or array or map, to be converted.
* @param expandlevel The level of JSON string need to expand, by default expand all.
* @param space Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.
*/
static stringify(obj, expandlevel := unset, space := " ") {
expandlevel := IsSet(expandlevel) ? Abs(expandlevel) : 10000000
return Trim(CO(obj, expandlevel))
CO(O, J := 0, R := 0, Q := 0) {
static M1 := "{", M2 := "}", S1 := "[", S2 := "]", N := "`n", C := ",", S := "- ", E := "", K := ":"
if (OT := Type(O)) = "Array" {
D := !R ? S1 : ""
for key, value in O {
F := (VT := Type(value)) = "Array" ? "S" : InStr("Map,Object", VT) ? "M" : E
Z := VT = "Array" && value.Length = 0 ? "[]" : ((VT = "Map" && value.count = 0) || (VT = "Object" &&
ObjOwnPropCount(value) = 0)) ? "{}" : ""
D .= (J > R ? "`n" CL(R + 2) : "") (F ? (%F%1 (Z ? "" : CO(value, J, R + 1, F)) %F%2) : ES(value)) (
OT = "Array" && O.Length = A_Index ? E : C)
}
} else {
D := !R ? M1 : ""
for key, value in (OT := Type(O)) = "Map" ? (Y := 1, O) : (Y := 0, O.OwnProps()) {
F := (VT := Type(value)) = "Array" ? "S" : InStr("Map,Object", VT) ? "M" : E
Z := VT = "Array" && value.Length = 0 ? "[]" : ((VT = "Map" && value.count = 0) || (VT = "Object" &&
ObjOwnPropCount(value) = 0)) ? "{}" : ""
D .= (J > R ? "`n" CL(R + 2) : "") (Q = "S" && A_Index = 1 ? M1 : E) ES(key) K (F ? (%F%1 (Z ? "" :
CO(value, J, R + 1, F)) %F%2) : ES(value)) (Q = "S" && A_Index = (Y ? O.count : ObjOwnPropCount(
O)) ? M2 : E) (J != 0 || R ? (A_Index = (Y ? O.count : ObjOwnPropCount(O)) ? E : C) : E)
if J = 0 && !R
D .= (A_Index < (Y ? O.count : ObjOwnPropCount(O)) ? C : E)
}
}
if J > R
D .= "`n" CL(R + 1)
if R = 0
D := RegExReplace(D, "^\R+") (OT = "Array" ? S2 : M2)
return D
}
ES(S) {
switch Type(S) {
case "Float":
if (v := '', d := InStr(S, 'e'))
v := SubStr(S, d), S := SubStr(S, 1, d - 1)
if ((StrLen(S) > 17) && (d := RegExMatch(S, "(99999+|00000+)\d{0,3}$")))
S := Round(S, Max(1, d - InStr(S, ".") - 1))
return S v
case "Integer":
return S
case "String":
S := StrReplace(S, "\", "\\")
S := StrReplace(S, "`t", "\t")
S := StrReplace(S, "`r", "\r")
S := StrReplace(S, "`n", "\n")
S := StrReplace(S, "`b", "\b")
S := StrReplace(S, "`f", "\f")
S := StrReplace(S, "`v", "\v")
S := StrReplace(S, '"', '\"')
return '"' S '"'
default:
return S == this.true ? "true" : S == this.false ? "false" : "null"
}
}
CL(i) {
loop (s := "", space ? i - 1 : 0)
s .= space
return s
}
}
}
class WebSocket {
Ptr := 0, async := 0, readyState := 0, url := ''
; The array of HINTERNET handles, [hSession, hConnect, hRequest(onOpen) | hWebSocket?]
HINTERNETs := []
; when request is opened
onOpen() => 0
; when server sent a close frame
onClose(status, reason) => 0
; when server sent binary message
onData(data, size) => 0
; when server sent UTF-8 message
onMessage(msg) => 0
reconnect() => 0
/**
* @param {String} Url the url of websocket
* @param {Object} Events an object of `{open:(this)=>void,data:(this, data, size)=>bool,message:(this, msg)=>bool,close:(this, status, reason)=>void}`
* @param {Integer} Async Use asynchronous mode
* @param {Object|Map|String} Headers Additional request headers to use when creating connections
* @param {Integer} TimeOut Set resolve, connect, send and receive timeout
*/
__New(Url, Events := 0, Async := true, Headers := '', TimeOut := 0, InitialSize := 8192) {
static contexts := Map()
if (!RegExMatch(Url,
'i)^((?<SCHEME>wss?)://)?((?<USERNAME>[^:]+):(?<PASSWORD>.+)@)?(?<HOST>[^/:\s]+)(:(?<PORT>\d+))?(?<PATH>/\S*)?$', &
m))
throw WebSocket.Error('Invalid websocket url')
if !hSession := DllCall('Winhttp\WinHttpOpen', 'ptr', 0, 'uint', 0, 'ptr', 0, 'ptr', 0, 'uint', Async ?
0x10000000 : 0, 'ptr')
throw WebSocket.Error()
this.async := Async := !!Async, this.url := Url
this.HINTERNETs.Push(hSession)
port := m.PORT ? Integer(m.PORT) : m.SCHEME = 'ws' ? 80 : 443
dwFlags := m.SCHEME = 'wss' ? 0x800000 : 0
if TimeOut
DllCall('Winhttp\WinHttpSetTimeouts', 'ptr', hSession, 'int', TimeOut, 'int', TimeOut, 'int', TimeOut,
'int', TimeOut, 'int')
if !hConnect := DllCall('Winhttp\WinHttpConnect', 'ptr', hSession, 'wstr', m.HOST, 'ushort', port, 'uint', 0,
'ptr')
throw WebSocket.Error()
this.HINTERNETs.Push(hConnect)
switch Type(Headers) {
case 'Object', 'Map':
s := ''
for k, v in Headers is Map ? Headers : Headers.OwnProps()
s .= '`r`n' k ': ' v
Headers := LTrim(s, '`r`n')
case 'String':
default:
Headers := ''
}
if (Events) {
for k, v in Events.OwnProps()
if (k ~= 'i)^(open|data|message|close)$')
this.DefineProp('on' k, { call: v })
}
if (Async) {
this.DefineProp('shutdown', { call: async_shutdown })
.DefineProp('receive', { call: receive })
.DefineProp('_send', { call: async_send })
} else this.__cache_size := InitialSize
connect(this), this.DefineProp('reconnect', { call: connect })
connect(self) {
if !self.HINTERNETs.Length
throw WebSocket.Error('The connection is closed')
self.shutdown()
if !hRequest := DllCall('Winhttp\WinHttpOpenRequest', 'ptr', hConnect, 'wstr', 'GET', 'wstr', m.PATH, 'ptr',
0, 'ptr', 0, 'ptr', 0, 'uint', dwFlags, 'ptr')
throw WebSocket.Error()
self.HINTERNETs.Push(hRequest), self.onOpen()
if (Headers)
DllCall('Winhttp\WinHttpAddRequestHeaders', 'ptr', hRequest, 'wstr', Headers, 'uint', -1, 'uint',
0x20000000, 'int')
if (!DllCall('Winhttp\WinHttpSetOption', 'ptr', hRequest, 'uint', 114, 'ptr', 0, 'uint', 0, 'int')
|| !DllCall('Winhttp\WinHttpSendRequest', 'ptr', hRequest, 'ptr', 0, 'uint', 0, 'ptr', 0, 'uint', 0, 'uint',
0, 'uptr', 0, 'int')
|| !DllCall('Winhttp\WinHttpReceiveResponse', 'ptr', hRequest, 'ptr', 0)
|| !DllCall('Winhttp\WinHttpQueryHeaders', 'ptr', hRequest, 'uint', 19, 'ptr', 0, 'wstr', status := '00000',
'uint*', 10, 'ptr', 0, 'int')
|| status != '101')
throw IsSet(status) ? WebSocket.Error('Invalid status: ' status) : WebSocket.Error()
if !self.Ptr := DllCall('Winhttp\WinHttpWebSocketCompleteUpgrade', 'ptr', hRequest, 'ptr', 0)
throw WebSocket.Error()
DllCall('Winhttp\WinHttpCloseHandle', 'ptr', self.HINTERNETs.Pop())
self.HINTERNETs.Push(self.Ptr), self.readyState := 1
(Async && async_receive(self))
}
async_receive(self) {
static on_read_complete := get_sync_callback(), hHeap := DllCall('GetProcessHeap', 'ptr')
static msg_gui := Gui(), wm_ahkmsg := DllCall('RegisterWindowMessage', 'str', 'AHK_WEBSOCKET_STATUSCHANGE',
'uint')
static pHeapReAlloc := DllCall('GetProcAddress', 'ptr', DllCall('GetModuleHandle', 'str', 'kernel32', 'ptr'
), 'astr', 'HeapReAlloc', 'ptr')
static pSendMessageW := DllCall('GetProcAddress', 'ptr', DllCall('GetModuleHandle', 'str', 'user32', 'ptr'),
'astr', 'SendMessageW', 'ptr')
static pWinHttpWebSocketReceive := DllCall('GetProcAddress', 'ptr', DllCall('GetModuleHandle', 'str',
'winhttp', 'ptr'), 'astr', 'WinHttpWebSocketReceive', 'ptr')
static _ := (OnMessage(wm_ahkmsg, WEBSOCKET_READ_WRITE_COMPLETE, 0xff), DllCall('SetParent', 'ptr', msg_gui
.Hwnd, 'ptr', -3))
; #DllLoad E:\projects\test\test\x64\Debug\test.dll
; on_read_complete := DllCall('GetProcAddress', 'ptr', DllCall('GetModuleHandle', 'str', 'test', 'ptr'), 'astr', 'WINHTTP_STATUS_READ_COMPLETE', 'ptr')
NumPut('ptr', pws := ObjPtr(self), 'ptr', msg_gui.Hwnd, 'uint', wm_ahkmsg, 'uint', InitialSize, 'ptr',
hHeap,
'ptr', cache := DllCall('HeapAlloc', 'ptr', hHeap, 'uint', 0, 'uptr', InitialSize, 'ptr'), 'uptr', 0,
'uptr', InitialSize,
'ptr', pHeapReAlloc, 'ptr', pSendMessageW, 'ptr', pWinHttpWebSocketReceive,
contexts[pws] := context := Buffer(11 * A_PtrSize)), self.__send_queue := []
context.DefineProp('__Delete', { call: self => DllCall('HeapFree', 'ptr', hHeap, 'uint', 0, 'ptr', NumGet(
self, 3 * A_PtrSize + 8, 'ptr')) })
DllCall('Winhttp\WinHttpSetOption', 'ptr', self, 'uint', 45, 'ptr*', context.Ptr, 'uint', A_PtrSize)
DllCall('Winhttp\WinHttpSetStatusCallback', 'ptr', self, 'ptr', on_read_complete, 'uint', 0x80000, 'uptr',
0, 'ptr')
if err := DllCall('Winhttp\WinHttpWebSocketReceive', 'ptr', self, 'ptr', cache, 'uint', InitialSize,
'uint*', 0, 'uint*', 0)
self.onError(err)
}
static WEBSOCKET_READ_WRITE_COMPLETE(wp, lp, msg, hwnd) {
static map_has := Map.Prototype.Has
if !map_has(contexts, ws := NumGet(wp, 'ptr')) || (ws := ObjFromPtrAddRef(ws)).readyState != 1
return
switch lp {
case 5: ; WRITE_COMPLETE
try ws.__send_queue.Pop()
case 4: ; WINHTTP_WEB_SOCKET_CLOSE_BUFFER_TYPE
if err := NumGet(wp, A_PtrSize, 'uint')
return ws.onError(err)
rea := ws.QueryCloseStatus(), ws.shutdown()
return ws.onClose(rea.status, rea.reason)
default: ; WINHTTP_WEB_SOCKET_BINARY_MESSAGE_BUFFER_TYPE, WINHTTP_WEB_SOCKET_UTF8_MESSAGE_BUFFER_TYPE
data := NumGet(wp, A_PtrSize, 'ptr')
size := NumGet(wp, 2 * A_PtrSize, 'uptr')
if lp == 2
return ws.onMessage(StrGet(data, size, 'utf-8'))
else return ws.onData(data, size)
}
}
static async_send(self, type, buf, size) {
if (self.readyState != 1)
throw WebSocket.Error('websocket is disconnected')
(q := self.__send_queue).InsertAt(1, buf)
while (err := DllCall('Winhttp\WinHttpWebSocketSend', 'ptr', self, 'uint', type, 'ptr', buf, 'uint', size,