-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
659 lines (607 loc) · 15.6 KB
/
main.ts
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
import { parseRT } from "./logic/parse-rt.ts"
import { AsyncComments } from "./types/AsyncComments.ts"
import { DefaultComments } from "./types/DefaultComments.ts"
import { PostComment } from "./types/PostComment.ts"
import { RemoveComment } from "./types/RemoveComment.ts"
export interface FBCommentPluginConfig {
app_id?: string
href: string
limit?: number
locale: string
order_by?: "time" | "reverse_time"
pluginUrl?: string
dialogUrl?: string
/** @description {string} url require starts with "https://" or "http://" */
app: string // https://app.animevsub.eu.org
sdk?: string
version?: string
fetch?: (
url: string,
options?: {
method: string
headers: Record<string, string>
body: URLSearchParams
}
) => Promise<string>
headersCustom?: Record<string, string>
}
export class FBCommentPlugin {
static readonly default_config: Partial<FBCommentPluginConfig> = {
app_id: "",
limit: 10,
order_by: "reverse_time",
pluginUrl: "https://www.facebook.com/plugins",
dialogUrl: "https://www.facebook.com/dialog",
sdk: "joey",
version: "v15.0",
fetch: (url, options) => fetch(url, options).then((res) => res.text())
}
readonly #config: Required<FBCommentPluginConfig>
constructor(config: FBCommentPluginConfig) {
this.#config = Object.assign(
{},
FBCommentPlugin.default_config,
config
) as Required<FBCommentPluginConfig>
}
#setup_return?: Awaited<ReturnType<typeof this._setup>>
private async _setup() {
const url = `${this.#config.pluginUrl}/feedback.php`
const queries = {
app_id: this.#config.app_id,
channel: `https://staticxx.facebook.com/x/connect/xd_arbiter/?version=46#cb=fc971c42bd70c0003&domain=${
new URL(this.#config.app).hostname
}&is_canvas=false&origin=https%3A%2F%2F${
this.#config.app
}%2Ffbaf0c74572724fba&relation=parent.parent`,
color_scheme: "light",
container_width: "973",
height: "100",
href: this.#config.href,
lazy: "false",
locale: this.#config.locale,
numposts: this.#config.limit + "",
order_by: this.#config.order_by,
sdk: this.#config.sdk,
version: this.#config.version,
width: ""
}
const mainUrl = `${url}?${new URLSearchParams(queries)}`
const html = await this.#config.fetch(mainUrl)
// ============= parse =============
const targetID = html.match(/"targetID":"(\d+)"/)?.[1]
const app_id = html.match(/"appID":"(\d+)"/)?.[1]
const limit = html.match(/numposts=(\d+)/)?.[1] ?? this.#config.limit + ""
// TODO: custom
// const __user = 0; //100024588752208//0;
const __a = "1"
const __req = "1" // is index request
const __hs = html.match(/"haste_session"\:"([^"]+)"/)?.[1]
const dpr = "1"
const __ccg = "GOOD"
const __rev = html.match(/"client_revision":(\d+)/)?.[1]
// TODO: ::8rhvxd
const __s = ""
const __hsi = html.match(/"hsi":"(\d+)"/)?.[1]
const __dyn = ""
const __csr = ""
// TODO: custom
const locale = html.match(/"locale":"(\w+)"/)?.[1] ?? this.#config.locale
const lsd = html.match(/"LSD",\[\],{"token":"([^"]+)"/)?.[1]
const jazoest = ""
const __sp = "1"
const after_cursor = html.match(/"afterCursor":"([^"]+)"/)?.[1]
const fb_dtsg = html.match(/"DTSGInitData",\[\],{"token":"([^"]+)"/)?.[1]
const comments = JSON.parse(
html.slice(
(html.lastIndexOf('"props"') >>> 0) + 8,
(html.lastIndexOf('"placeholderElement"') >>> 0) - 1
)
) as DefaultComments
if (
targetID === undefined ||
app_id === undefined ||
__hs === undefined ||
__rev === undefined ||
__hsi === undefined ||
lsd === undefined
) {
throw new Error("Can't resolve setup because lost params.")
}
const headers = {
"content-type": "application/x-www-form-urlencoded",
dpr: "1",
pragma: "no-cache",
"sec-ch-prefers-color-scheme": "dark",
"sec-ch-ua":
'"Chromium";v="122", "Not(A:Brand";v="24", "Google Chrome";v="122"',
"sec-ch-ua-full-version-list":
'"Chromium";v="122.0.6261.112", "Not(A:Brand";v="24.0.0.0", "Google Chrome";v="122.0.6261.112"',
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": '"Windows"',
"sec-ch-ua-platform-version": '"15.0.0"',
"sec-gpc": "1",
"viewport-width": "727",
"x-asbd-id": "129477",
"x-fb-lsd": lsd,
origin: new URL(this.#config.pluginUrl).origin,
// "cookie": "ps_n=0; datr=LU3cZSRNCYxxCm3eYkMh7iNr; sb=-NziZdme__APuhzWxH_97M-g; locale=vi_VN; wd=1366x708; c_user=100024588752208; xs=36%3AMZY_PwWr2vVraw%3A2%3A1709738073%3A-1%3A7867; fr=0boOOKHOSrwgP7dTy.AWX7fKQ_lLJsv4f1XDkgHzo3eYM.Bl4tz4..AAA.0.0.Bl6Iha.AWWUC1Wpi5s; SL_G_WPT_TO=vi",
// "cookie": "ps_n=0; wd=1366x679; datr=LU3cZSRNCYxxCm3eYkMh7iNr; sb=-NziZdme__APuhzWxH_97M-g; fr=0boOOKHOSrwgP7dTy..Bl4tz4..AAA.0.0.Bl5zwq.AWU6WFSUyeM; SL_G_WPT_TO=vi",
Referer: mainUrl,
"Referrer-Policy": "origin-when-cross-origin",
...this.#config.headersCustom
}
const data = {
headers,
targetID,
app_id,
limit,
// __user,
__a,
__req,
__hs,
dpr,
__ccg,
__rev,
__s,
__hsi,
__dyn,
__csr,
locale,
lsd,
jazoest,
__sp,
after_cursor,
fb_dtsg,
comments
}
return data
}
async setup() {
if (this.#setup_return) return this.#setup_return
this.#setup_return = await this._setup()
return this.#setup_return
}
signInPopup() {
return new Promise<void>((resolve, reject) => {
const queries = {
channel: `https://staticxx.facebook.com/x/connect/xd_arbiter/?version=46#cb=fc971c42bd70c0003&domain=${
new URL(this.#config.app).hostname
}&is_canvas=false&origin=https%3A%2F%2F${
this.#config.app
}%2Ffbaf0c74572724fba&relation=parent.parent`,
color_scheme: "light",
container_width: "973",
height: "100",
href: this.#config.href,
lazy: "false",
locale: this.#config.locale,
numposts: this.#config.limit + "",
order_by: this.#config.order_by,
sdk: this.#config.sdk,
version: this.#config.version,
width: ""
}
const popup = window.open(
`${this.#config.dialogUrl}/plugin.optin?app_id=${this.#config.app_id}&secure=true&social_plugin=comments&return_params=${encodeURIComponent(JSON.stringify(queries))}&login_params=${encodeURIComponent(JSON.stringify({ referrer: "" }))}&display=popup`,
"_blank",
"noopener,noreferrer,resizable"
)
popup!.opener = null
const popupTick = setInterval(() => {
if (!popup || popup.closed) {
clearTimeout(timeoutInterval)
resolve()
}
}, 500)
const timeoutInterval = setTimeout(
() => {
reject(new Error("Time out"))
clearInterval(popupTick)
},
5 * 60 * 1000
)
})
}
async getComments(after_cursor?: string): Promise<AsyncComments> {
const setup = await this.setup()
after_cursor ??= setup.comments.meta.afterCursor
const {
app_id,
limit,
__a,
__req,
__hs,
dpr,
__rev,
__s,
__hsi,
__dyn,
__csr,
locale,
lsd,
jazoest,
__sp,
fb_dtsg
} = setup
const __ccg = "GOOD"
return this.#config
.fetch(
`${this.#config.pluginUrl}/comments/async/${setup.targetID}/pager/${this.#config.order_by}/`,
{
headers: setup.headers,
body: new URLSearchParams({
app_id,
limit,
__user: setup.comments.meta.userID,
__a,
__req,
__hs,
dpr,
__ccg,
__rev,
__s,
__hsi,
__dyn,
__csr,
locale,
lsd,
jazoest,
__sp,
fb_dtsg,
...(after_cursor ? { after_cursor } : {})
}),
method: "POST"
}
)
.then((data) => parseRT(data) as AsyncComments)
}
async getMoreComments(
commentID: string,
after_cursor?: string
): Promise<AsyncComments> {
const setup = await this.setup()
const {
app_id,
limit,
__a,
__req,
__hs,
dpr,
__rev,
__s,
__hsi,
__dyn,
__csr,
locale,
lsd,
jazoest,
__sp,
fb_dtsg
} = setup
const __ccg = "MODERATE"
return this.#config
.fetch(
`${this.#config.pluginUrl}/comments/async/comment/${commentID}/pager/`,
{
headers: setup.headers,
body: new URLSearchParams({
app_id,
limit,
__user: setup.comments.meta.userID,
__a,
__req,
__hs,
dpr,
__ccg,
__rev,
__s,
__hsi,
__dyn,
__csr,
locale,
lsd,
jazoest,
__sp,
fb_dtsg,
...(after_cursor ? { after_cursor } : {})
}),
method: "POST"
}
)
.then((data) => parseRT(data) as AsyncComments)
}
async postComment(text: string): Promise<PostComment> {
const setup = await this.setup()
if (!setup.fb_dtsg) {
throw new Error("fb_dtsg not found")
}
const {
targetID,
app_id,
__a,
__req,
__hs,
dpr,
__rev,
__s,
__hsi,
__dyn,
__csr,
locale,
lsd,
jazoest,
__sp,
fb_dtsg
} = setup
const __user = setup.comments.meta.userID
const attached_photo_fbid = "0"
const attached_sticker_fbid = "0"
const post_to_feed = "false"
const privacy_option = "everyone"
const __ccg = "EXCELLENT"
return this.#config
.fetch(
`${this.#config.pluginUrl}/comments/async/createComment/${targetID}/?av=${__user}`,
{
headers: setup.headers,
method: "POST",
body: new URLSearchParams({
app_id,
text,
attached_photo_fbid,
attached_sticker_fbid,
post_to_feed,
privacy_option,
__user,
__a,
__req,
__hs,
dpr,
__ccg,
__rev,
__s,
__hsi,
__dyn,
__csr,
locale,
fb_dtsg,
jazoest,
lsd,
__sp
})
}
)
.then((data) => parseRT(data) as PostComment)
}
async replyComment(commentID: string, text: string): Promise<PostComment> {
const setup = await this.setup()
if (!setup.fb_dtsg) {
throw new Error("fb_dtsg not found")
}
const {
app_id,
__a,
__req,
__hs,
dpr,
__rev,
__s,
__hsi,
__dyn,
__csr,
locale,
lsd,
jazoest,
__sp,
fb_dtsg
} = setup
const __user = setup.comments.meta.userID
const attached_photo_fbid = "0"
const attached_sticker_fbid = "0"
const post_to_feed = "false"
const privacy_option = "everyone"
const __ccg = "EXCELLENT"
return this.#config
.fetch(
`${this.#config.pluginUrl}/comments/async/createReply/${commentID}/?av=${__user}`,
{
headers: setup.headers,
method: "POST",
body: new URLSearchParams({
app_id,
text,
attached_photo_fbid,
attached_sticker_fbid,
post_to_feed,
privacy_option,
__a,
__req,
__hs,
dpr,
__ccg,
__rev,
__s,
__hsi,
__dyn,
__csr,
locale,
fb_dtsg,
jazoest,
lsd,
__sp
})
}
)
.then((data) => parseRT(data) as PostComment)
}
async likeComment(commentID: string, isLike: boolean): Promise<PostComment> {
const setup = await this.setup()
if (!setup.fb_dtsg) {
throw new Error("fb_dtsg not found")
}
const {
app_id,
__a,
__req,
__hs,
dpr,
__rev,
__s,
__hsi,
__dyn,
__csr,
locale,
lsd,
jazoest,
__sp,
fb_dtsg
} = setup
const __user = setup.comments.meta.userID
const __ccg = "EXCELLENT"
return this.#config
.fetch(
`${this.#config.pluginUrl}/comments/async/like/?action_like=${isLike}&av=${__user}`,
{
headers: setup.headers,
method: "POST",
body: new URLSearchParams({
app_id,
comment_id: commentID,
__user,
__a,
__req,
__hs,
dpr,
__ccg,
__rev,
__s,
__hsi,
__dyn,
__csr,
locale,
fb_dtsg,
jazoest,
lsd,
__sp
})
}
)
.then((data) => parseRT(data) as PostComment)
}
async editComment(commentID: string, text: string): Promise<PostComment> {
const setup = await this.setup()
if (!setup.fb_dtsg) {
throw new Error("fb_dtsg not found")
}
const {
app_id,
__a,
__req,
__hs,
dpr,
__rev,
__s,
__hsi,
__dyn,
__csr,
locale,
lsd,
jazoest,
__sp,
fb_dtsg
} = setup
const __user = setup.comments.meta.userID
const attached_photo_fbid = "0"
const attached_sticker_fbid = "0"
const post_to_feed = "false"
const privacy_option = "everyone"
const __ccg = "EXCELLENT"
return this.#config
.fetch(`${this.#config.pluginUrl}/comments/async/edit/?av=${__user}`, {
headers: setup.headers,
method: "POST",
body: new URLSearchParams({
app_id,
text,
comment_id: commentID,
attached_photo_fbid,
attached_sticker_fbid,
post_to_feed,
privacy_option,
__user,
__a,
__req,
__hs,
dpr,
__ccg,
__rev,
__s,
__hsi,
__dyn,
__csr,
locale,
fb_dtsg,
jazoest,
lsd,
__sp
})
})
.then((data) => parseRT(data) as PostComment)
}
async removeComment(commentID: string): Promise<RemoveComment> {
const setup = await this.setup()
if (!setup.fb_dtsg) {
throw new Error("fb_dtsg not found")
}
const {
app_id,
__a,
__req,
__hs,
dpr,
__rev,
__s,
__hsi,
__dyn,
__csr,
locale,
lsd,
jazoest,
__sp,
fb_dtsg
} = setup
const __user = setup.comments.meta.userID
const __ccg = "EXCELLENT"
return this.#config
.fetch(`${this.#config.pluginUrl}/comments/async/delete/?av=${__user}`, {
headers: setup.headers,
method: "POST",
body: new URLSearchParams({
app_id,
comment_id: commentID,
__user,
__a,
__req,
__hs,
dpr,
__ccg,
__rev,
__s,
__hsi,
__dyn,
__csr,
locale,
fb_dtsg,
jazoest,
lsd,
__sp
})
})
.then((data) => parseRT(data) as PostComment)
}
}
export * from "./types/AsyncComments.ts"
export * from "./types/DefaultComments.ts"
export * from "./types/PostComment.ts"
export * from "./types/RemoveComment.ts"