steamutils 1.3.99 → 1.4.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (238) hide show
  1. package/.idea/codeStyles/Project.xml +1 -0
  2. package/.idea/deployment.xml +14 -0
  3. package/.idea/jsLibraryMappings.xml +6 -0
  4. package/.prettierrc.json +3 -3
  5. package/SteamClient.js +2418 -2418
  6. package/bufferHelpers.js +115 -115
  7. package/cheerio.js +103 -103
  8. package/const.js +336 -336
  9. package/create_remote_file.js +66 -66
  10. package/index.js +7 -2
  11. package/package.json +1 -1
  12. package/protos/csgo/base_gcmessages.proto +553 -553
  13. package/protos/csgo/base_gcmessages_csgo.proto +547 -547
  14. package/protos/csgo/c_peer2peer_netmessages.proto +55 -55
  15. package/protos/csgo/clientmessages.proto +48 -48
  16. package/protos/csgo/connectionless_netmessages.proto +17 -17
  17. package/protos/csgo/cs_gameevents.proto +35 -35
  18. package/protos/csgo/cs_usercmd.proto +34 -34
  19. package/protos/csgo/cstrike15_gcmessages.proto +1431 -1431
  20. package/protos/csgo/cstrike15_usermessages.proto +592 -592
  21. package/protos/csgo/demo.proto +165 -165
  22. package/protos/csgo/econ_gcmessages.proto +219 -219
  23. package/protos/csgo/engine_gcmessages.proto +14 -14
  24. package/protos/csgo/enums_clientserver.proto +1529 -1529
  25. package/protos/csgo/fatdemo.proto +125 -125
  26. package/protos/csgo/gameevents.proto +120 -120
  27. package/protos/csgo/gcsdk_gcmessages.proto +323 -323
  28. package/protos/csgo/gcsystemmsgs.proto +243 -243
  29. package/protos/csgo/netmessages.proto +618 -618
  30. package/protos/csgo/network_connection.proto +126 -126
  31. package/protos/csgo/networkbasetypes.proto +246 -246
  32. package/protos/csgo/networksystem_protomessages.proto +17 -17
  33. package/protos/csgo/steamdatagram_messages_auth.proto +65 -65
  34. package/protos/csgo/steamdatagram_messages_sdr.proto +534 -534
  35. package/protos/csgo/steammessages.proto +620 -620
  36. package/protos/csgo/steammessages_base.proto +301 -301
  37. package/protos/csgo/steammessages_cloud.steamworkssdk.proto +68 -68
  38. package/protos/csgo/steammessages_gamenetworkingui.proto +61 -61
  39. package/protos/csgo/steammessages_helprequest.steamworkssdk.proto +22 -22
  40. package/protos/csgo/steammessages_oauth.steamworkssdk.proto +18 -18
  41. package/protos/csgo/steammessages_player.steamworkssdk.proto +254 -254
  42. package/protos/csgo/steammessages_publishedfile.steamworkssdk.proto +233 -233
  43. package/protos/csgo/steammessages_unified_base.steamworkssdk.proto +30 -30
  44. package/protos/csgo/steamnetworkingsockets_messages.proto +205 -205
  45. package/protos/csgo/steamnetworkingsockets_messages_certs.proto +39 -39
  46. package/protos/csgo/steamnetworkingsockets_messages_udp.proto +75 -75
  47. package/protos/csgo/te.proto +259 -259
  48. package/protos/csgo/uifontfile_format.proto +13 -13
  49. package/protos/csgo/usercmd.proto +39 -39
  50. package/protos/csgo/usermessages.proto +752 -752
  51. package/protos/csgo/valveextensions.proto +17 -17
  52. package/protos/google/protobuf/descriptor.proto +281 -281
  53. package/protos/steam/clientmetrics.proto +45 -45
  54. package/protos/steam/content_manifest.proto +62 -62
  55. package/protos/steam/contenthubs.proto +41 -41
  56. package/protos/steam/encrypted_app_ticket.proto +10 -10
  57. package/protos/steam/enums.proto +501 -501
  58. package/protos/steam/enums_clientserver.proto +1508 -1508
  59. package/protos/steam/enums_productinfo.proto +14 -14
  60. package/protos/steam/htmlmessages.proto +1039 -1039
  61. package/protos/steam/offline_ticket.proto +8 -8
  62. package/protos/steam/steamdatagram_messages_auth.proto +65 -65
  63. package/protos/steam/steamdatagram_messages_sdr.proto +533 -533
  64. package/protos/steam/steammessages_accounthardware.steamclient.proto +167 -167
  65. package/protos/steam/steammessages_appoverview.proto +204 -204
  66. package/protos/steam/steammessages_auth.steamclient.proto +375 -375
  67. package/protos/steam/steammessages_base.proto +335 -335
  68. package/protos/steam/steammessages_broadcast.steamclient.proto +618 -618
  69. package/protos/steam/steammessages_chat.steamclient.proto +1070 -1070
  70. package/protos/steam/steammessages_client_objects.proto +649 -649
  71. package/protos/steam/steammessages_clientlanp2p.proto +43 -43
  72. package/protos/steam/steammessages_clientmetrics.steamclient.proto +192 -192
  73. package/protos/steam/steammessages_clientnotificationtypes.proto +250 -250
  74. package/protos/steam/steammessages_clientserver.proto +392 -392
  75. package/protos/steam/steammessages_clientserver_2.proto +771 -771
  76. package/protos/steam/steammessages_clientserver_appinfo.proto +131 -131
  77. package/protos/steam/steammessages_clientserver_friends.proto +260 -260
  78. package/protos/steam/steammessages_clientserver_gameservers.proto +159 -159
  79. package/protos/steam/steammessages_clientserver_lbs.proto +70 -70
  80. package/protos/steam/steammessages_clientserver_login.proto +167 -167
  81. package/protos/steam/steammessages_clientserver_mms.proto +233 -233
  82. package/protos/steam/steammessages_clientserver_ucm.proto +207 -207
  83. package/protos/steam/steammessages_clientserver_uds.proto +125 -125
  84. package/protos/steam/steammessages_clientserver_ufs.proto +45 -45
  85. package/protos/steam/steammessages_clientserver_userstats.proto +80 -80
  86. package/protos/steam/steammessages_clientsettings.proto +208 -208
  87. package/protos/steam/steammessages_cloud.steamclient.proto +422 -422
  88. package/protos/steam/steammessages_contentsystem.steamclient.proto +123 -123
  89. package/protos/steam/steammessages_credentials.steamclient.proto +96 -96
  90. package/protos/steam/steammessages_datapublisher.steamclient.proto +102 -102
  91. package/protos/steam/steammessages_depotbuilder.steamclient.proto +99 -99
  92. package/protos/steam/steammessages_deviceauth.steamclient.proto +169 -169
  93. package/protos/steam/steammessages_econ.steamclient.proto +137 -137
  94. package/protos/steam/steammessages_familygroups.steamclient.proto +441 -441
  95. package/protos/steam/steammessages_friendmessages.steamclient.proto +141 -141
  96. package/protos/steam/steammessages_gamenetworking.steamclient.proto +25 -25
  97. package/protos/steam/steammessages_gamenetworkingui.proto +61 -61
  98. package/protos/steam/steammessages_gamenotifications.steamclient.proto +131 -131
  99. package/protos/steam/steammessages_gameservers.steamclient.proto +136 -136
  100. package/protos/steam/steammessages_hiddevices.proto +157 -157
  101. package/protos/steam/steammessages_inventory.steamclient.proto +172 -172
  102. package/protos/steam/steammessages_linkfilter.steamclient.proto +39 -39
  103. package/protos/steam/steammessages_lobbymatchmaking.steamclient.proto +29 -29
  104. package/protos/steam/steammessages_market.steamclient.proto +22 -22
  105. package/protos/steam/steammessages_marketingmessages.steamclient.proto +273 -273
  106. package/protos/steam/steammessages_offline.steamclient.proto +33 -33
  107. package/protos/steam/steammessages_parental.steamclient.proto +262 -262
  108. package/protos/steam/steammessages_parties.steamclient.proto +75 -75
  109. package/protos/steam/steammessages_partnerapps.steamclient.proto +106 -106
  110. package/protos/steam/steammessages_player.steamclient.proto +915 -915
  111. package/protos/steam/steammessages_publishedfile.steamclient.proto +737 -737
  112. package/protos/steam/steammessages_qms.steamclient.proto +111 -111
  113. package/protos/steam/steammessages_remoteclient.proto +100 -100
  114. package/protos/steam/steammessages_remoteclient_discovery.proto +244 -244
  115. package/protos/steam/steammessages_remoteclient_service.steamclient.proto +31 -31
  116. package/protos/steam/steammessages_remoteclient_service_messages.proto +217 -217
  117. package/protos/steam/steammessages_remoteplay.proto +975 -975
  118. package/protos/steam/steammessages_secrets.steamclient.proto +35 -35
  119. package/protos/steam/steammessages_shader.steamclient.proto +89 -89
  120. package/protos/steam/steammessages_site_license.steamclient.proto +103 -103
  121. package/protos/steam/steammessages_sitelicenseclient.proto +38 -38
  122. package/protos/steam/steammessages_siteserverui.proto +130 -130
  123. package/protos/steam/steammessages_steamtv.steamclient.proto +533 -533
  124. package/protos/steam/steammessages_store.steamclient.proto +404 -404
  125. package/protos/steam/steammessages_storebrowse.steamclient.proto +457 -457
  126. package/protos/steam/steammessages_timedtrial.steamclient.proto +40 -40
  127. package/protos/steam/steammessages_twofactor.steamclient.proto +157 -157
  128. package/protos/steam/steammessages_unified_base.steamclient.proto +45 -45
  129. package/protos/steam/steammessages_unified_test.steamclient.proto +51 -51
  130. package/protos/steam/steammessages_useraccount.steamclient.proto +211 -211
  131. package/protos/steam/steammessages_vac.steamclient.proto +37 -37
  132. package/protos/steam/steammessages_video.steamclient.proto +68 -68
  133. package/protos/steam/steammessages_virtualcontroller.proto +138 -138
  134. package/protos/steam/steammessages_workshop.steamclient.proto +19 -19
  135. package/protos/steam/steamnetworkingsockets_messages.proto +205 -205
  136. package/protos/steam/steamnetworkingsockets_messages_certs.proto +39 -39
  137. package/protos/steam/steamnetworkingsockets_messages_udp.proto +75 -75
  138. package/protos/steam/webuimessages_achievements.proto +29 -29
  139. package/protos/steam/webuimessages_base.proto +24 -24
  140. package/protos/steam/webuimessages_gamenotes.proto +21 -21
  141. package/protos/steam/webuimessages_gamescope.proto +57 -57
  142. package/protos/steam/webuimessages_steamengine.proto +41 -41
  143. package/protos/steam/webuimessages_steamos.proto +114 -114
  144. package/protos/steam/webuimessages_storagedevicemanager.proto +110 -110
  145. package/protos/steam/webuimessages_systemmanager.proto +17 -17
  146. package/protos/steam/webuimessages_transport.proto +22 -22
  147. package/protos/steam/webuimessages_transportvalidation.proto +111 -111
  148. package/protos/webui/common.proto +4024 -4024
  149. package/protos/webui/common_base.proto +27 -27
  150. package/protos/webui/service_accountcart.proto +153 -153
  151. package/protos/webui/service_accountlinking.proto +27 -27
  152. package/protos/webui/service_accountprivacy.proto +42 -42
  153. package/protos/webui/service_accountprivateapps.proto +34 -34
  154. package/protos/webui/service_auction.proto +97 -97
  155. package/protos/webui/service_authentication.proto +222 -222
  156. package/protos/webui/service_authenticationsupport.proto +74 -74
  157. package/protos/webui/service_broadcast.proto +562 -562
  158. package/protos/webui/service_chat.proto +11 -11
  159. package/protos/webui/service_chatroom.proto +810 -810
  160. package/protos/webui/service_chatusability.proto +107 -107
  161. package/protos/webui/service_checkout.proto +84 -84
  162. package/protos/webui/service_clan.proto +41 -41
  163. package/protos/webui/service_clanchatrooms.proto +25 -25
  164. package/protos/webui/service_clanfaqs.proto +202 -202
  165. package/protos/webui/service_clientcomm.proto +151 -151
  166. package/protos/webui/service_clientmetrics.proto +22 -22
  167. package/protos/webui/service_cloud.proto +222 -222
  168. package/protos/webui/service_cloudconfigstore.proto +51 -51
  169. package/protos/webui/service_cloudgaming.proto +30 -30
  170. package/protos/webui/service_community.proto +365 -365
  171. package/protos/webui/service_dailydeal.proto +89 -89
  172. package/protos/webui/service_econ.proto +134 -134
  173. package/protos/webui/service_embedded.proto +24 -24
  174. package/protos/webui/service_experimentservice.proto +6 -6
  175. package/protos/webui/service_familygroups.proto +355 -355
  176. package/protos/webui/service_fovasvideo.proto +15 -15
  177. package/protos/webui/service_friendmessages.proto +133 -133
  178. package/protos/webui/service_friendslist.proto +70 -70
  179. package/protos/webui/service_gamenotes.proto +15 -15
  180. package/protos/webui/service_gamerecording.proto +265 -265
  181. package/protos/webui/service_gamerecordingclip.proto +85 -85
  182. package/protos/webui/service_gamerecordingdebug.proto +52 -52
  183. package/protos/webui/service_helprequestlogs.proto +16 -16
  184. package/protos/webui/service_loyaltyrewards.proto +252 -252
  185. package/protos/webui/service_marketingmessages.proto +215 -215
  186. package/protos/webui/service_mobileapp.proto +18 -18
  187. package/protos/webui/service_mobileauth.proto +24 -24
  188. package/protos/webui/service_mobiledevice.proto +26 -26
  189. package/protos/webui/service_mobileperaccount.proto +31 -31
  190. package/protos/webui/service_news.proto +110 -110
  191. package/protos/webui/service_parental.proto +258 -258
  192. package/protos/webui/service_partnermembershipinvite.proto +34 -34
  193. package/protos/webui/service_partnerstorebrowse.proto +28 -28
  194. package/protos/webui/service_phone.proto +51 -51
  195. package/protos/webui/service_physicalgoods.proto +15 -15
  196. package/protos/webui/service_player.proto +918 -918
  197. package/protos/webui/service_promotioneventinvites.proto +109 -109
  198. package/protos/webui/service_promotionplanning.proto +186 -186
  199. package/protos/webui/service_promotionstats.proto +23 -23
  200. package/protos/webui/service_publishedfile.proto +714 -714
  201. package/protos/webui/service_publishing.proto +200 -200
  202. package/protos/webui/service_quest.proto +127 -127
  203. package/protos/webui/service_salefeature.proto +373 -373
  204. package/protos/webui/service_saleitemrewards.proto +54 -54
  205. package/protos/webui/service_shoppingcart.proto +152 -152
  206. package/protos/webui/service_steamawards.proto +76 -76
  207. package/protos/webui/service_steamcharts.proto +75 -75
  208. package/protos/webui/service_steamengine.proto +36 -36
  209. package/protos/webui/service_steamlearn.proto +876 -876
  210. package/protos/webui/service_steamnotification.proto +73 -73
  211. package/protos/webui/service_steamtv.proto +495 -495
  212. package/protos/webui/service_steamvrvoicechat.proto +67 -67
  213. package/protos/webui/service_steamvrwebrtc.proto +48 -48
  214. package/protos/webui/service_storagedevicemanager.proto +104 -104
  215. package/protos/webui/service_store.proto +359 -359
  216. package/protos/webui/service_storeappsimilarity.proto +76 -76
  217. package/protos/webui/service_storebrowse.proto +103 -103
  218. package/protos/webui/service_storemarketing.proto +44 -44
  219. package/protos/webui/service_storequery.proto +103 -103
  220. package/protos/webui/service_storesales.proto +87 -87
  221. package/protos/webui/service_storetopsellers.proto +44 -44
  222. package/protos/webui/service_systemmanager.proto +11 -11
  223. package/protos/webui/service_test_transporterror.proto +6 -6
  224. package/protos/webui/service_transportauth.proto +17 -17
  225. package/protos/webui/service_transportvalidation.proto +69 -69
  226. package/protos/webui/service_twofactor.proto +170 -170
  227. package/protos/webui/service_useraccount.proto +142 -142
  228. package/protos/webui/service_usergameactivity.proto +34 -34
  229. package/protos/webui/service_usergamenotes.proto +70 -70
  230. package/protos/webui/service_usernews.proto +68 -68
  231. package/protos/webui/service_userreviews.proto +97 -97
  232. package/protos/webui/service_video.proto +50 -50
  233. package/protos/webui/service_videoclip.proto +62 -62
  234. package/protos/webui/service_voicechat.proto +139 -139
  235. package/protos/webui/service_webrtc.proto +52 -52
  236. package/remote.js +6259 -6259
  237. package/test_steamclient.js +20 -0
  238. package/utils.js +155 -155
package/bufferHelpers.js CHANGED
@@ -1,115 +1,115 @@
1
- import ByteBuffer from 'bytebuffer';
2
- import SteamID from 'steamid';
3
-
4
- const Type = {
5
- None : 0,
6
- String : 1,
7
- Int32 : 2,
8
- Float32 : 3,
9
- Pointer : 4,
10
- WideString : 5,
11
- Color : 6,
12
- UInt64 : 7,
13
- End : 8
14
- }
15
-
16
- // Encodes an object into a ByteBuffer
17
- export const encode = (
18
- _object,
19
- prefix = [0x00, 0x00],
20
- suffix = [],
21
- customEncodeFields = {}
22
- ) => {
23
- // Create a copy of the input object to avoid modifying it
24
- const object = { ..._object };
25
- const buffer = new ByteBuffer();
26
-
27
- // Write the prefix bytes to the buffer
28
- for (let pre of prefix) {
29
- buffer.writeByte(pre);
30
- }
31
-
32
- // Iterate through the object's properties and encode them
33
- for (let item in object) {
34
- if (object.hasOwnProperty(item) === false) {
35
- continue;
36
- }
37
-
38
- // If a custom encoder function exists for the property, use it
39
- if (typeof customEncodeFields[item] === 'function') {
40
- object[item] = customEncodeFields[item](object[item]);
41
- }
42
- _encode(object[item], buffer, item);
43
- }
44
-
45
- // Write the suffix bytes to the buffer
46
- for (let suf of suffix) {
47
- buffer.writeByte(suf);
48
- }
49
-
50
- // Write an End marker to the buffer
51
- buffer.writeByte(Type.End);
52
- buffer.flip(); // Prepare the buffer for reading
53
- return buffer;
54
- };
55
-
56
- // Encodes individual properties into the ByteBuffer
57
- const _encode = (object, buffer, name) => {
58
- if (object instanceof Buffer) {
59
- // If the object is a Buffer
60
- buffer.writeByte(Type.String);
61
- buffer.writeCString(name);
62
-
63
- // Convert the Buffer to a hexadecimal string, split into 2-character parts, and write each part as a byte
64
- let parts = object
65
- .toString('hex')
66
- .toUpperCase()
67
- .match(/.{1,2}/g);
68
-
69
- for (let part of parts) {
70
- buffer.writeByte(parseInt('0x' + part));
71
- }
72
- } else {
73
- // If the object is not a Buffer, check its type
74
- switch (typeof object) {
75
- case 'object': {
76
- buffer.writeByte(Type.None);
77
- buffer.writeCString(name);
78
-
79
- for (let index in object) {
80
- _encode(object[index], buffer, index); // Recursively encode each property
81
- }
82
-
83
- buffer.writeByte(Type.End);
84
- break;
85
- }
86
- case 'string': {
87
- buffer.writeByte(Type.String);
88
- buffer.writeCString(name);
89
- buffer.writeCString(object ? object : '');
90
- break;
91
- }
92
- case 'number': {
93
- buffer.writeByte(Type.String);
94
- buffer.writeCString(name);
95
- buffer.writeCString(object.toString());
96
- break;
97
- }
98
- }
99
- }
100
- };
101
-
102
- // Encodes SteamIDs into a Buffer
103
- export const encodeUids = (steamids) => {
104
- const outputBuffer = [];
105
-
106
- for (let id of steamids.map((s) => s.accountid)) {
107
- while (id > 0x7f) {
108
- outputBuffer.push((id | 0x80) & 0xff);
109
- id >>= 7;
110
- }
111
- outputBuffer.push(id);
112
- }
113
- outputBuffer.push(0x00); // Null terminator
114
- return Buffer.from(outputBuffer);
115
- };
1
+ import ByteBuffer from 'bytebuffer';
2
+ import SteamID from 'steamid';
3
+
4
+ const Type = {
5
+ None : 0,
6
+ String : 1,
7
+ Int32 : 2,
8
+ Float32 : 3,
9
+ Pointer : 4,
10
+ WideString : 5,
11
+ Color : 6,
12
+ UInt64 : 7,
13
+ End : 8
14
+ }
15
+
16
+ // Encodes an object into a ByteBuffer
17
+ export const encode = (
18
+ _object,
19
+ prefix = [0x00, 0x00],
20
+ suffix = [],
21
+ customEncodeFields = {}
22
+ ) => {
23
+ // Create a copy of the input object to avoid modifying it
24
+ const object = { ..._object };
25
+ const buffer = new ByteBuffer();
26
+
27
+ // Write the prefix bytes to the buffer
28
+ for (let pre of prefix) {
29
+ buffer.writeByte(pre);
30
+ }
31
+
32
+ // Iterate through the object's properties and encode them
33
+ for (let item in object) {
34
+ if (object.hasOwnProperty(item) === false) {
35
+ continue;
36
+ }
37
+
38
+ // If a custom encoder function exists for the property, use it
39
+ if (typeof customEncodeFields[item] === 'function') {
40
+ object[item] = customEncodeFields[item](object[item]);
41
+ }
42
+ _encode(object[item], buffer, item);
43
+ }
44
+
45
+ // Write the suffix bytes to the buffer
46
+ for (let suf of suffix) {
47
+ buffer.writeByte(suf);
48
+ }
49
+
50
+ // Write an End marker to the buffer
51
+ buffer.writeByte(Type.End);
52
+ buffer.flip(); // Prepare the buffer for reading
53
+ return buffer;
54
+ };
55
+
56
+ // Encodes individual properties into the ByteBuffer
57
+ const _encode = (object, buffer, name) => {
58
+ if (object instanceof Buffer) {
59
+ // If the object is a Buffer
60
+ buffer.writeByte(Type.String);
61
+ buffer.writeCString(name);
62
+
63
+ // Convert the Buffer to a hexadecimal string, split into 2-character parts, and write each part as a byte
64
+ let parts = object
65
+ .toString('hex')
66
+ .toUpperCase()
67
+ .match(/.{1,2}/g);
68
+
69
+ for (let part of parts) {
70
+ buffer.writeByte(parseInt('0x' + part));
71
+ }
72
+ } else {
73
+ // If the object is not a Buffer, check its type
74
+ switch (typeof object) {
75
+ case 'object': {
76
+ buffer.writeByte(Type.None);
77
+ buffer.writeCString(name);
78
+
79
+ for (let index in object) {
80
+ _encode(object[index], buffer, index); // Recursively encode each property
81
+ }
82
+
83
+ buffer.writeByte(Type.End);
84
+ break;
85
+ }
86
+ case 'string': {
87
+ buffer.writeByte(Type.String);
88
+ buffer.writeCString(name);
89
+ buffer.writeCString(object ? object : '');
90
+ break;
91
+ }
92
+ case 'number': {
93
+ buffer.writeByte(Type.String);
94
+ buffer.writeCString(name);
95
+ buffer.writeCString(object.toString());
96
+ break;
97
+ }
98
+ }
99
+ }
100
+ };
101
+
102
+ // Encodes SteamIDs into a Buffer
103
+ export const encodeUids = (steamids) => {
104
+ const outputBuffer = [];
105
+
106
+ for (let id of steamids.map((s) => s.accountid)) {
107
+ while (id > 0x7f) {
108
+ outputBuffer.push((id | 0x80) & 0xff);
109
+ id >>= 7;
110
+ }
111
+ outputBuffer.push(id);
112
+ }
113
+ outputBuffer.push(0x00); // Null terminator
114
+ return Buffer.from(outputBuffer);
115
+ };
package/cheerio.js CHANGED
@@ -1,103 +1,103 @@
1
- import * as cheerio from "cheerio";
2
-
3
- export function tables2json($, tables, getValue) {
4
- const tablesData = [];
5
- $(tables).each(function (index, el) {
6
- tablesData.push(table2json($, el, getValue));
7
- });
8
- return tablesData;
9
- }
10
-
11
- export function table2json(
12
- $,
13
- table,
14
- getValue = function ($, el) {
15
- return $(el).text().trim();
16
- },
17
- ) {
18
- const data = [];
19
- const header = [];
20
- $($(table)[0])
21
- .find("tr")
22
- .each(function (trIndex, trEl) {
23
- const tr = $(trEl);
24
- const row = [];
25
- tr.children().each(function (tdIndex, tdEl) {
26
- const isHeader = tdEl.name.toLowerCase() === "th";
27
- tdEl = $(tdEl);
28
- const tdContent = getValue($, tdEl, isHeader);
29
- (isHeader ? header : row).push(tdContent);
30
- });
31
- if (row.length) {
32
- data.push(row);
33
- }
34
- });
35
-
36
- if (!header.length) return data;
37
-
38
- for (let i = 0; i < data.length; i++) {
39
- const obj = {};
40
- for (let j = 0; j < header.length; j++) {
41
- obj[header[j]] = data[i][j];
42
- }
43
- data[i] = obj;
44
- }
45
-
46
- return data;
47
- }
48
-
49
- export function getTableHasHeaders($, $tables, headers = []) {
50
- headers = headers.slice();
51
- const tables = [];
52
- $($tables).each((tableIndex, tableEl) => {
53
- const _headers = headers.slice();
54
- let table;
55
- if (table) {
56
- return;
57
- }
58
- const $tableEl = $(tableEl);
59
- $tableEl
60
- .find("tr")
61
- .children()
62
- .each(function (trIndex, trEl) {
63
- if (table) {
64
- return;
65
- }
66
- const $trEl = $(trEl);
67
- let text = $trEl.text().trim();
68
-
69
- if (_headers.includes(text)) {
70
- _headers.splice(_headers.indexOf(text), 1);
71
-
72
- if (!_headers.length) {
73
- table = $tableEl;
74
- }
75
- }
76
- });
77
- if (table) {
78
- tables.push(table);
79
- }
80
- });
81
- return tables;
82
- }
83
-
84
- export function querySelectorAll($, selector, root) {
85
- try {
86
- if (root) {
87
- return [...root.find(selector)].map((el) => $(el));
88
- } else {
89
- return [...$(selector)].map((el) => $(el));
90
- }
91
- } catch (e) {
92
- console.error(e);
93
- return [];
94
- }
95
- }
96
-
97
- export function getDocumentTitle(html) {
98
- if (!html || typeof html !== "string") return;
99
- return cheerio
100
- .load(html.replaceAll(/[\t\n\r]/gi, "").trim())("head > title")
101
- .text()
102
- .trim();
103
- }
1
+ import * as cheerio from "cheerio";
2
+
3
+ export function tables2json($, tables, getValue) {
4
+ const tablesData = [];
5
+ $(tables).each(function (index, el) {
6
+ tablesData.push(table2json($, el, getValue));
7
+ });
8
+ return tablesData;
9
+ }
10
+
11
+ export function table2json(
12
+ $,
13
+ table,
14
+ getValue = function ($, el) {
15
+ return $(el).text().trim();
16
+ },
17
+ ) {
18
+ const data = [];
19
+ const header = [];
20
+ $($(table)[0])
21
+ .find("tr")
22
+ .each(function (trIndex, trEl) {
23
+ const tr = $(trEl);
24
+ const row = [];
25
+ tr.children().each(function (tdIndex, tdEl) {
26
+ const isHeader = tdEl.name.toLowerCase() === "th";
27
+ tdEl = $(tdEl);
28
+ const tdContent = getValue($, tdEl, isHeader);
29
+ (isHeader ? header : row).push(tdContent);
30
+ });
31
+ if (row.length) {
32
+ data.push(row);
33
+ }
34
+ });
35
+
36
+ if (!header.length) return data;
37
+
38
+ for (let i = 0; i < data.length; i++) {
39
+ const obj = {};
40
+ for (let j = 0; j < header.length; j++) {
41
+ obj[header[j]] = data[i][j];
42
+ }
43
+ data[i] = obj;
44
+ }
45
+
46
+ return data;
47
+ }
48
+
49
+ export function getTableHasHeaders($, $tables, headers = []) {
50
+ headers = headers.slice();
51
+ const tables = [];
52
+ $($tables).each((tableIndex, tableEl) => {
53
+ const _headers = headers.slice();
54
+ let table;
55
+ if (table) {
56
+ return;
57
+ }
58
+ const $tableEl = $(tableEl);
59
+ $tableEl
60
+ .find("tr")
61
+ .children()
62
+ .each(function (trIndex, trEl) {
63
+ if (table) {
64
+ return;
65
+ }
66
+ const $trEl = $(trEl);
67
+ let text = $trEl.text().trim();
68
+
69
+ if (_headers.includes(text)) {
70
+ _headers.splice(_headers.indexOf(text), 1);
71
+
72
+ if (!_headers.length) {
73
+ table = $tableEl;
74
+ }
75
+ }
76
+ });
77
+ if (table) {
78
+ tables.push(table);
79
+ }
80
+ });
81
+ return tables;
82
+ }
83
+
84
+ export function querySelectorAll($, selector, root) {
85
+ try {
86
+ if (root) {
87
+ return [...root.find(selector)].map((el) => $(el));
88
+ } else {
89
+ return [...$(selector)].map((el) => $(el));
90
+ }
91
+ } catch (e) {
92
+ console.error(e);
93
+ return [];
94
+ }
95
+ }
96
+
97
+ export function getDocumentTitle(html) {
98
+ if (!html || typeof html !== "string") return;
99
+ return cheerio
100
+ .load(html.replaceAll(/[\t\n\r]/gi, "").trim())("head > title")
101
+ .text()
102
+ .trim();
103
+ }