-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcloudlink.js
More file actions
1704 lines (1565 loc) · 44.7 KB
/
cloudlink.js
File metadata and controls
1704 lines (1565 loc) · 44.7 KB
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
(function(Scratch) {
var servers = {}; ; // Server list
let mWS = null;
// Get the server URL list
try {
fetch('https://mikedev101.github.io/cloudlink/serverlist.json').then(response => {
return response.text();
}).then(data => {
servers = JSON.parse(data);
}).catch(err => {
console.log(err);
servers = {};
});
} catch(err) {
console.log(err);
servers = {};
};
function find_id(ID, ulist) {
// Thanks StackOverflow!
if (jsonCheck(ID) && (!intCheck(ID))) {
return ulist.some(o => ((o.username === JSON.parse(ID).username) && (o.id == JSON.parse(ID).id)));
} else {
return ulist.some(o => ((o.username === String(ID)) || (o.id == ID)));
};
}
function jsonCheck(JSON_STRING) {
try {
JSON.parse(JSON_STRING);
return true;
} catch (err) {
return false;
}
}
function intCheck(value) {
return !isNaN(value);
}
function autoConvert(value) {
// Check if the value is JSON / Dict first
try {
JSON.parse(value);
return JSON.parse(value);
} catch (err) {};
// Check if the value is an array
try {
tmp = value;
tmp = tmp.replace(/'/g, '"');
JSON.parse(tmp);
return JSON.parse(tmp);
} catch (err) {};
// Check if an int/float
if (!isNaN(value)) {
return Number(value);
};
// Leave as the original value if none of the above work
return value;
}
class CloudLink {
constructor (runtime, extensionId) {
// Extension stuff
this.runtime = runtime;
this.cl_icon = 'data:image/svg+xml;base64,PHN2ZyB2ZXJzaW9uPSIxLjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6eGxpbms9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsiIHdpZHRoPSIyMjUuMzU0OCIgaGVpZ2h0PSIyMjUuMzU0OCIgdmlld0JveD0iMCwwLDIyNS4zNTQ4LDIyNS4zNTQ4Ij48ZyB0cmFuc2Zvcm09InRyYW5zbGF0ZSgtMTI3LjMyMjYsLTY3LjMyMjYpIj48ZyBkYXRhLXBhcGVyLWRhdGE9InsmcXVvdDtpc1BhaW50aW5nTGF5ZXImcX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mZmZmYiLz48cGF0aCBkPSJNMjg5LjA4NjU1LDIxNi45NjA3NHY5LjA0NjY3aC0yNi45MTY2M2gtOS4wNDY2N3YtOS4wNDY2N3YtNTQuNTAzMzloOS4wNDY2N3Y1NC41MDMzOXoiIGZpbGw9IiMwMGMyOGMiLz48cGF0aCBkPSJNMjIyLjQwOTI1LDIyNi4wMDc0MWMtOC4zNTMyLDAgLTE2LjM2NDMxLC0zLjMxODM0IC0yMi4yNzA5LC05LjIyNDkyYy01LjkwNjYxLC01LjkwNjU4IC05LjIyNDkxLC0xMy45MTc2OCAtOS4yMjQ5MSwtMjIuMjcwODljMCwtOC4zNTMyIDMuMzE4MjksLTE2LjM2NDMxIDkuMjI0OTEsLTIyLjI3MDljNS45MDY1OSwtNS45MDY2MSAxMy45MTc3LC05LjIyNDkxIDIyLjI3MDksLTkuMjI0OTFoMjEuMTA4OXY4LjkzNDk4aC0yMS4xMDg5djAuMTAyNTdjLTUuOTU2MjgsMCAtMTEuNjY4NjQsMi4zNjYxNiAtMTUuODgwMzcsNi41Nzc4OWMtNC4yMTE3Myw0LjIxMTczIC02LjU3Nzg5LDkuOTI0MDggLTYuNTc3ODksMTUuODgwMzdjMCw1Ljk1NjI4IDIuMzY2MTYsMTEuNjY4NjQgNi41Nzc4OSwxNS44ODAzN2M0LjIxMTczLDQuMjExNzMgOS45MjQwOCw2LjU3NzkzIDE1Ljg4MDM3LDYuNTc3OTN2MC4xMDI1M2gyMS4xMDg5djguOTM0OTh6IiBmaWxsPSIjMDBjMjhjIi8+PC9nPjwvZz48L2c+PC9zdmc+PCEtLXJvdGF0aW9uQ2VudGVyOjg4LjE5OTI2OTk5OTk5OTk4OjYxLjMzNTM0NDk5OTk5OTk5LS0+';
// Socket data
this.socketData = {
"gmsg": [],
"pmsg": [],
"direct": [],
"statuscode": [],
"gvar": [],
"pvar": [],
"motd": "",
"client_ip": "",
"ulist": [],
"server_version": ""
};
this.varData = {
"gvar": {},
"pvar": {}
};
this.queueableCmds = ["gmsg", "pmsg", "gvar", "pvar", "direct", "statuscode"];
this.varCmds = ["gvar", "pvar"];
// Listeners
this.socketListeners = {};
this.socketListenersData = {};
this.newSocketData = {
"gmsg": false,
"pmsg": false,
"direct": false,
"statuscode": false,
"gvar": false,
"pvar": false
};
// Edge-triggered hat blocks
this.connect_hat = 0;
this.packet_hat = 0;
this.close_hat = 0;
// Status stuff
this.isRunning = false;
this.isLinked = false;
this.version = "S4.0";
this.link_status = 0;
this.username = "";
this.tmp_username = "";
this.isUsernameSyncing = false;
this.isUsernameSet = false;
this.disconnectWasClean = false;
this.wasConnectionDropped = false;
this.didConnectionFail = false;
this.protocolOk = false;
// Listeners stuff
this.enableListener = false;
this.setListener = "";
// Rooms stuff
this.enableRoom = false;
this.isRoomSetting = false;
this.selectRoom = "";
// Remapping stuff
this.menuRemap = {
"Global data": "gmsg",
"Private data": "pmsg",
"Global variables": "gvar",
"Private variables": "pvar",
"Direct data": "direct",
"Status code": "statuscode",
"All data": "all"
};
}
getInfo () {
return {
"id": 'cloudlink',
"name": 'CloudLink',
"blockIconURI": this.cl_block,
"menuIconURI": this.cl_icon,
"blocks": [
{
"opcode": 'returnGlobalData',
"blockType": "reporter",
"text": "全局数据"
},
{
"opcode": 'returnPrivateData',
"blockType": "reporter",
"text": "私有数据"
},
{
"opcode": 'returnDirectData',
"blockType": "reporter",
"text": "直接数据"
},
{
"opcode": 'returnLinkData',
"blockType": "reporter",
"text": "链接状态"
},
{
"opcode": 'returnStatusCode',
"blockType": "reporter",
"text": "状态码"
},
{
"opcode": 'returnUserListData',
"blockType": "reporter",
"text": "用户名"
},
{
"opcode": "returnUsernameData",
"blockType": "reporter",
"text": "我的用户名"
},
{
"opcode": "returnVersionData",
"blockType": "reporter",
"text": "扩展版本"
},
{
"opcode": "returnServerVersion",
"blockType": "reporter",
"text": "服务器版本"
},
{
"opcode": "returnServerList",
"blockType": "reporter",
"text": "服务器列表"
},
{
"opcode": "returnMOTD",
"blockType": "reporter",
"text": "服务器MOTD"
},
{
"opcode": "returnClientIP",
"blockType": "reporter",
"text": "我的IP地址"
},
{
"opcode": 'returnListenerData',
"blockType": "reporter",
"text": "监听器[ID]的响应",
"arguments": {
"ID": {
"type": "string",
"defaultValue": "example-listener",
},
},
},
{
"opcode": "readQueueSize",
"blockType": "reporter",
"text": "队列大小 [TYPE]",
"arguments": {
"TYPE": {
"type": "string",
"menu": "allmenu",
"defaultValue": "所有数据",
},
},
},
{
"opcode": "readQueueData",
"blockType": "reporter",
"text": "数据队列 [TYPE]",
"arguments": {
"TYPE": {
"type": "string",
"menu": "allmenu",
"defaultValue": "All data",
},
},
},
{
"opcode": 'returnVarData',
"blockType": "reporter",
"text": "[TYPE] [VAR] 数据",
"arguments": {
"VAR": {
"type": "string",
"defaultValue": "Apple",
},
"TYPE": {
"type": "string",
"menu": "varmenu",
"defaultValue": "Global variables",
},
},
},
{
"opcode": 'parseJSON',
"blockType": "reporter",
"text": '[JSON_STRING] 中的 [PATH]',
"arguments": {
"PATH": {
"type": "string",
"defaultValue": 'fruit/apples',
},
"JSON_STRING": {
"type": "string",
"defaultValue": '{"fruit": {"apples": 2, "bananas": 3}, "total_fruit": 5}',
},
},
},
{
"opcode": 'getFromJSONArray',
"blockType": "reporter",
"text": '从 JSON 数组 [ARRAY] 中获取 [NUM]',
"arguments": {
"NUM": {
"type": "number",
"defaultValue": 0,
},
"ARRAY": {
"type": "string",
"defaultValue": '["foo","bar"]',
}
}
},
{
"opcode": 'fetchURL',
"blockType": "reporter",
"blockAllThreads": "true",
"text": "从URL [url]获取数据",
"arguments": {
"url": {
"type": "string",
"defaultValue": "https://mikedev101.github.io/cloudlink/fetch_test",
},
},
},
{
"opcode": 'requestURL',
"blockType": "reporter",
"blockAllThreads": "true",
"text": '使用方法[method]向URL[url]发送数据[data]和头部[headers]的请求',
"arguments": {
"method": {
"type": "string",
"defaultValue": 'GET',
},
"url": {
"type": "string",
"defaultValue": 'https://mikedev101.github.io/cloudlink/fetch_test',
},
"data": {
"type": "string",
"defaultValue": '{}'
},
"headers": {
"type": "string",
"defaultValue": '{}'
},
}
},
{
"opcode": 'makeJSON',
"blockType": "reporter",
"text": '将[toBeJSONified]转换为JSON',
"arguments": {
"toBeJSONified": {
"type": "string",
"defaultValue": '{"test": true}',
},
}
},
{
"opcode": 'onConnect',
"blockType": "hat",
"text": '当连接成功时',
"blockAllThreads": "true"
},
{
"opcode": 'onClose',
"blockType": "hat",
"text": '当连接断开时',
"blockAllThreads": "true"
},
{
"opcode": 'onListener',
"blockType": "hat",
"text": '当我收到具有侦听器[ID]的新数据包时',
"blockAllThreads": "true",
"arguments": {
"ID": {
"type": "string",
"defaultValue": "example-listener",
},
},
},
{
"opcode": 'onNewPacket',
"blockType": "hat",
"text": '当我收到新的[TYPE]数据包时',
"blockAllThreads": "true",
"arguments": {
"TYPE": {
"type": "string",
"menu": "almostallmenu",
"defaultValue": 'Global data'
},
},
},
{
"opcode": 'onNewVar',
"blockType": "hat",
"text": '当我收到[VAR]的新[TYPE]数据时',
"blockAllThreads": "true",
"arguments": {
"TYPE": {
"type": "string",
"menu": "varmenu",
"defaultValue": 'Global variables',
},
"VAR": {
"type": "string",
"defaultValue": 'Apple',
},
},
},
{
"opcode": 'getComState',
"blockType": "Boolean",
"text": '已连接?',
},
{
"opcode": 'getRoomState',
"blockType": "Boolean",
"text": '已连接到房间?',
},
{
"opcode": 'getComLostConnectionState',
"blockType": "Boolean",
"text": '失去连接?',
},
{
"opcode": 'getComFailedConnectionState',
"blockType": "Boolean",
"text": '连接失败?',
},
{
"opcode": 'getUsernameState',
"blockType": "Boolean",
"text": '用户名同步?',
},
{
"opcode": 'returnIsNewData',
"blockType": "Boolean",
"text": '收到新的[TYPE]?',
"arguments": {
"TYPE": {
"type": "string",
"menu": "datamenu",
"defaultValue": '全局数据',
},
},
},
{
"opcode": 'returnIsNewVarData',
"blockType": "Boolean",
"text": '收到变量[VAR]的新[TYPE]数据?',
"arguments": {
"TYPE": {
"type": "string",
"menu": "varmenu",
"defaultValue": 'Global variables',
},
"VAR": {
"type": "string",
"defaultValue": 'Apple',
},
},
},
{
"opcode": 'returnIsNewListener',
"blockType": "Boolean",
"text": '是否收到具有侦听器[ID]的新数据包?',
"blockAllThreads": "true",
"arguments": {
"ID": {
"type": "string",
"defaultValue": "example-listener",
},
},
},
{
"opcode": 'checkForID',
"blockType": "Boolean",
"text": 'ID [ID]是否已连接?',
"arguments": {
"ID": {
"type": "string",
"defaultValue": 'Another name',
},
},
},
{
"opcode": 'isValidJSON',
"blockType": "Boolean",
"text": '[JSON_STRING]是否为有效的JSON?',
"arguments": {
"JSON_STRING": {
"type": "string",
"defaultValue": '{"fruit": {"apples": 2, "bananas": 3}, "total_fruit": 5}',
},
},
},
{
"opcode": 'openSocket',
"blockType": "command",
"text": '连接到[IP]',
"blockAllThreads": "true",
"arguments": {
"IP": {
"type": "string",
"defaultValue": 'ws://127.0.0.1:3000/',
},
},
},
{
"opcode": 'openSocketPublicServers',
"blockType": "command",
"text": '连接到服务器[ID]',
"blockAllThreads": "true",
"arguments": {
"ID": {
"type": "number",
"defaultValue": '',
},
},
},
{
"opcode": 'closeSocket',
"blockType": "command",
"blockAllThreads": "true",
"text": '断开连接',
},
{
"opcode": 'setMyName',
"blockType": "command",
"text": '将[NAME]设置为用户名',
"blockAllThreads": "true",
"arguments": {
"NAME": {
"type": "string",
"defaultValue": "A name",
},
},
},
{
"opcode": 'createListener',
"blockType": "command",
"text": '将侦听器[ID]附加到下一个数据包',
"blockAllThreads": "true",
"arguments": {
"ID": {
"type": "string",
"defaultValue": "example-listener",
},
},
},
{
"opcode": 'linkToRooms',
"blockType": "command",
"text": '连接到房间[ROOMS]',
"blockAllThreads": "true",
"arguments": {
"ROOMS": {
"type": "string",
"defaultValue": '["test"]',
},
}
},
{
"opcode": 'selectRoomsInNextPacket',
"blockType": "command",
"text": '选择房间[ROOMS]以发送下一个数据包',
"blockAllThreads": "true",
"arguments": {
"ROOMS": {
"type": "string",
"defaultValue": '["test"]',
},
},
},
{
"opcode": 'unlinkFromRooms',
"blockType": "command",
"text": '取消连接所有房间',
"blockAllThreads": "true"
},
{
"opcode": 'sendGData',
"blockType": "command",
"text": '发送[DATA]',
"blockAllThreads": "true",
"arguments": {
"DATA": {
"type": "string",
"defaultValue": 'Apple'
}
}
},
{
"opcode": 'sendPData',
"blockType": "command",
"text": '发送[DATA]到[ID]',
"blockAllThreads": "true",
"arguments": {
"DATA": {
"type": "string",
"defaultValue": 'Apple'
},
"ID": {
"type": "string",
"defaultValue": 'Another name'
}
}
},
{
"opcode": 'sendGDataAsVar',
"blockType": "command",
"text": '发送变量[VAR]和数据[DATA]',
"blockAllThreads": "true",
"arguments": {
"DATA": {
"type": "string",
"defaultValue": 'Banana'
},
"VAR": {
"type": "string",
"defaultValue": 'Apple'
}
}
},
{
"opcode": 'sendPDataAsVar',
"blockType": "command",
"text": '发送变量[VAR]和数据[DATA]到[ID]',
"blockAllThreads": "true",
"arguments": {
"DATA": {
"type": "string",
"defaultValue": 'Banana'
},
"ID": {
"type": "string",
"defaultValue": 'Another name'
},
"VAR": {
"type": "string",
"defaultValue": 'Apple'
}
}
},
{
"opcode": 'runCMDnoID',
"blockType": "command",
"text": '发送命令[CMD]和数据[DATA],不带ID',
"blockAllThreads": "true",
"arguments": {
"CMD": {
"type": "string",
"defaultValue": 'direct'
},
"DATA": {
"type": "string",
"defaultValue": 'val'
}
}
},
{
"opcode": 'runCMD',
"blockType": "command",
"text": '发送命令[CMD]和数据[DATA]到[ID]',
"blockAllThreads": "true",
"arguments": {
"CMD": {
"type": "string",
"defaultValue": 'direct'
},
"ID": {
"type": "string",
"defaultValue": 'id'
},
"DATA": {
"type": "string",
"defaultValue": 'val'
}
}
},
{
"opcode": 'resetNewData',
"blockType": "command",
"text": '重置[TYPE]状态',
"blockAllThreads": "true",
"arguments": {
"TYPE": {
"type": "string",
"menu": "datamenu",
"defaultValue": 'Global data'
}
}
},
{
"opcode": 'resetNewVarData',
"blockType": "command",
"text": '重置新的[TYPE] [VAR]状态',
"blockAllThreads": "true",
"arguments": {
"TYPE": {
"type": "string",
"menu": "varmenu",
"defaultValue": 'Global variables'
},
"VAR": {
"type": "string",
"defaultValue": 'Apple'
}
}
},
{
"opcode": 'resetNewListener',
"blockType": "command",
"text": '重置新的[ID]监听状态',
"blockAllThreads": "true",
"arguments": {
"ID": {
"type": "string",
"defaultValue": 'example-listener'
}
}
},
{
"opcode": 'clearAllPackets',
"blockType": "command",
"text": "清除[TYPE]的所有数据包",
"arguments": {
"TYPE": {
"type": "string",
"menu": "allmenu",
"defaultValue": "All data"
},
},
}
],
"menus": {
"coms": {
"items": [{text:"连接",value:"Connected"},{text:"已同步用户名",value: "Username synced"}]
},
"datamenu": {
"items": [{text:"全局数据",value:'Global data'},{text:"私人数据",value: 'Private data'}, {text:"直接数据",value: 'Direct data'},{text:"状态码",value: 'Status code'}]
},
"varmenu": {
"items": [{text:"全局变量",value:'Global variables'},{text:"局部变量",value: 'Private variables'}]
},
"allmenu": {
"items": [{text:"全局数据",value:'Global data'},{text:"私人数据",value: 'Private data'}, {text:"直接数据",value: 'Direct data'},{text:"状态码",value: 'Status code'}, {text:"全局变量",value:'Global variables'},{text:"局部变量",value: 'Private variables'}, {text:"所有数据",value: "All data"}]
},
"almostallmenu": {
"items": [{text:"全局数据",value:'Global data'},{text:"私人数据",value: 'Private data'}, {text:"直接数据",value: 'Direct data'},{text:"状态码",value: 'Status code'}, {text:"全局变量",value:'Global variables'},{text:"局部变量",value: 'Private variables'}]
},
},
};
};
// Code for blocks go here
returnGlobalData() {
if (this.socketData.gmsg.length != 0) {
let data = (this.socketData.gmsg[this.socketData.gmsg.length - 1].val);
if (typeof(data) == "object") {
data = JSON.stringify(data); // Make the JSON safe for Scratch
}
return data;
} else {
return "";
};
};
returnPrivateData() {
if (this.socketData.pmsg.length != 0) {
let data = (this.socketData.pmsg[this.socketData.pmsg.length - 1].val);
if (typeof (data) == "object") {
data = JSON.stringify(data); // Make the JSON safe for Scratch
}
return data;
} else {
return "";
};
};
returnDirectData() {
if (this.socketData.direct.length != 0) {
let data = (this.socketData.direct[this.socketData.direct.length - 1].val);
if (typeof (data) == "object") {
data = JSON.stringify(data); // Make the JSON safe for Scratch
}
return data;
} else {
return "";
};
};
returnLinkData() {
return String(this.link_status);
};
returnStatusCode() {
if (this.socketData.statuscode.length != 0) {
let data = (this.socketData.statuscode[this.socketData.statuscode.length - 1].code);
if (typeof (data) == "object") {
data = JSON.stringify(data); // Make the JSON safe for Scratch
}
return data;
} else {
return "";
};
};
returnUserListData() {
return JSON.stringify(this.socketData.ulist);
};
returnUsernameData() {
let data = this.username;
if (typeof (data) == "object") {
data = JSON.stringify(data); // Make the JSON safe for Scratch
}
return data;
};
returnVersionData() {
return String(this.version);
};
returnServerVersion() {
return String(this.socketData.server_version);
};
returnServerList() {
return JSON.stringify(servers);
};
returnMOTD() {
return String(this.socketData.motd);
};
returnClientIP() {
return String(this.socketData.client_ip);
};
returnListenerData({ID}) {
const self = this;
if ((this.isRunning) && (this.socketListeners.hasOwnProperty(String(ID)))) {
return JSON.stringify(this.socketListenersData[ID]);
} else {
return "{}";
};
};
readQueueSize({TYPE}) {
if (this.menuRemap[String(TYPE)] == "all") {
let tmp_size = 0;
tmp_size = tmp_size + this.socketData.gmsg.length;
tmp_size = tmp_size + this.socketData.pmsg.length;
tmp_size = tmp_size + this.socketData.direct.length;
tmp_size = tmp_size + this.socketData.statuscode.length;
tmp_size = tmp_size + this.socketData.gvar.length;
tmp_size = tmp_size + this.socketData.pvar.length;
return tmp_size;
} else {
return this.socketData[this.menuRemap[String(TYPE)]].length;
};
};
readQueueData({TYPE}) {
if (this.menuRemap[String(TYPE)] == "all") {
let tmp_socketData = JSON.parse(JSON.stringify(this.socketData)); // Deep copy
delete tmp_socketData.motd;
delete tmp_socketData.client_ip;
delete tmp_socketData.ulist;
delete tmp_socketData.server_version;
return JSON.stringify(tmp_socketData);
} else {
return JSON.stringify(this.socketData[this.menuRemap[String(TYPE)]]);
};
};
returnVarData({ TYPE, VAR }) {
if (this.isRunning) {
if (this.varData.hasOwnProperty(this.menuRemap[TYPE])) {
if (this.varData[this.menuRemap[TYPE]].hasOwnProperty(VAR)) {
return this.varData[this.menuRemap[TYPE]][VAR].value;
} else {
return "";
};
} else {
return "";
};
} else {
return "";
};
};
parseJSON({PATH, JSON_STRING}) {
try {
const path = PATH.toString().split('/').map(prop => decodeURIComponent(prop));
if (path[0] === '') path.splice(0, 1);
if (path[path.length - 1] === '') path.splice(-1, 1);
let json;
try {
json = JSON.parse(' ' + JSON_STRING);
} catch (e) {
return e.message;
};
path.forEach(prop => json = json[prop]);
if (json === null) return 'null';
else if (json === undefined) return '';
else if (typeof json === 'object') return JSON.stringify(json);
else return json.toString();
} catch (err) {
return '';
};
};
getFromJSONArray({NUM, ARRAY}) {
var json_array = JSON.parse(ARRAY);
if (json_array[NUM] == "undefined") {
return "";
} else {
let data = json_array[NUM];
if (typeof (data) == "object") {
data = JSON.stringify(data); // Make the JSON safe for Scratch
}
return data;
}
};
fetchURL(args) {
return fetch(args.url, {
method: "GET"
}).then(response => response.text());
};
requestURL(args) {
if (args.method == "GET" || args.method == "HEAD") {
return fetch(args.url, {
method: args.method,
headers: JSON.parse(args.headers)
}).then(response => response.text());
} else {
return fetch(args.url, {
method: args.method,
headers: JSON.parse(args.headers),
body: JSON.parse(args.data)
}).then(response => response.text());
}
};
isValidJSON({JSON_STRING}) {
return jsonCheck(JSON_STRING);
};
makeJSON({toBeJSONified}) {
if (typeof(toBeJSONified) == "string") {
try {
JSON.parse(toBeJSONified);
return String(toBeJSONified);
} catch(err) {
return "Not JSON!";
}
} else if (typeof(toBeJSONified) == "object") {
return JSON.stringify(toBeJSONified);
} else {
return "Not JSON!";
};
};
onConnect() {
const self = this;
if (self.connect_hat == 0 && self.isRunning && self.protocolOk) {
self.connect_hat = 1;
return true;
} else {
return false;
};
};
onClose() {
const self = this;
if (self.close_hat == 0 && !self.isRunning) {
self.close_hat = 1;
return true;
} else {
return false;
};
};
onListener({ ID }) {
const self = this;
if ((this.isRunning) && (this.socketListeners.hasOwnProperty(String(ID)))) {
if (self.socketListeners[String(ID)]) {
self.socketListeners[String(ID)] = false;
return true;
} else {
return false;
};
} else {
return false;