/
EndlessScrolling.js
846 lines (824 loc) · 31.6 KB
/
EndlessScrolling.js
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
import { Module } from '../../class/Module';
import { utils } from '../../lib/jsUtils';
import { common } from '../Common';
import IntersectionObserver from 'intersection-observer-polyfill';
const
parseHtml = utils.parseHtml.bind(utils),
animateScroll = common.animateScroll.bind(common),
checkMissingDiscussions = common.checkMissingDiscussions.bind(common),
createElements = common.createElements.bind(common),
createHeadingButton = common.createHeadingButton.bind(common),
endless_load = common.endless_load.bind(common),
request = common.request.bind(common),
reverseComments = common.reverseComments.bind(common),
setSetting = common.setSetting.bind(common)
;
class GeneralEndlessScrolling extends Module {
constructor() {
super();
this.info = {
description: [
[`ul`, [
[`li`, `Loads the next page when you scroll down to the end of any page, allowing you to endlessly scroll through pages.`],
[`li`, `Adds multiple buttons to the main page heading of the page:`],
[`ul`, [
[`li`, [
[`i`, { class: `fa fa-play` }],
` if the endless scrolling is paused and `,
[`i`, { class: `fa fa-pause` }],
` if it is not, which allows you to pause/resume the endless scrolling.`
]],
[`li`, [
[`i`, { class: `fa fa-step-forward` }],
`, which allows you to load the next page without having to scroll down.`
]],
[`li`, [
[`i`, { class: `fa fa-fast-forward` }],
`, which allows you continuously load the next pages until either the last page is reached or you pause the endless scrolling.`
]],
[`li`, [
[`i`, { class: `fa fa-refresh` }],
` `,
[`i`, { class: `fa fa-map-marker` }],
`, which allows you to refresh the page currently visible in the window.`
]],
[`li`, [
[`i`, { class: `fa fa-refresh` }],
`, which allows you to refresh all of the pages that have been loaded.`
]]
]],
[`li`, `You can choose whether or not to show page divisors (page headings separating each loaded page).`],
[`li`, `As you scroll through the page, the pagination navigation of the page changes according to the page currently visible in the window.`],
[`li`, `If you use the pagination navigation of the page to try to go to a page that has been loaded, it scrolls to the page instead of opening it.`],
[`li`, `There is a reverse scrolling option for discussions that loads the pages in descending order and loads the last page instead of the first one when visiting a discussion from the main/inbox page.`]
]]
],
features: {
es_ch: {
name: `Enable for Comment History.`,
sg: true
},
es_df: {
name: `Enable for Discussion Filters.`,
sg: true
},
es_tf: {
name: `Enable for Trade Filters.`,
st: true
},
es_dh: {
name: `Enable for Discussion Highlighter.`,
sg: true
},
es_gb: {
name: `Enable for Giveaway Bookmarks.`,
sg: true
},
es_ged: {
name: `Enable for Giveaway Encrypter/Decrypter.`,
sg: true
},
es_ge: {
name: `Enable for Giveaway Extractor.`,
sg: true
},
es_gf: {
name: `Enable for Giveaway Filters.`,
sg: true
},
es_cl: {
inputItems: [
{
attributes: {
max: 10,
min: 0,
type: `number`
},
id: `es_pages`,
prefix: `Pages (Max 10): `
}
],
name: `Continuously load X more pages automatically when visiting any page.`,
sg: true
},
es_r: {
description: [
[`ul`, [
[`li`, `Loads the pages of a discussion in descending order.`],
[`li`, `Loads the last page instead of the first one when visiting a discussion from the main/inbox page.`]
]]
],
name: `Enable reverse scrolling.`,
sg: true
},
es_rd: {
name: `Refresh active discussions/deals when refreshing the main page.`,
sg: true
},
es_pd: {
description: [
[`ul`, [
[`li`, `With this option enabled, each loaded page is separated by a page heading, which makes it very clear where a page ends and another begins. With it disabled, there is no such distinction, so it looks like the entire page is a single page, giving a true endless feeling.`]
]]
],
name: `Show page divisors.`,
sg: true,
st: true
}
},
id: `es`,
load: this.es,
name: `Endless Scrolling`,
sg: true,
st: true,
includeOptions: [{
id: `pause`,
name: `Paused`
}],
type: `general`
};
}
es() {
if (!this.esgst.mainPageHeading || !this.esgst.pagination) return;
this.esgst.itemsPerPage = parseInt(this.esgst.pagination.firstElementChild.firstElementChild.nextElementSibling.textContent.replace(/,/g, ``)) - parseInt(this.esgst.pagination.firstElementChild.firstElementChild.textContent.replace(/,/g, ``)) + 1;
let es = {};
this.esgst.es = es;
es.divisors = this.esgst.es_pd;
es.mainContext = this.esgst.pagination.previousElementSibling;
if (this.esgst.commentsPath && !es.mainContext.classList.contains(`comments`)) {
es.mainContext = common.createElements_v2(es.mainContext, `afterEnd`, [
[`div`, { class: `comments` }]
]);
}
let rows = es.mainContext.getElementsByClassName(`table__rows`)[0];
if (rows) {
es.mainContext = rows;
}
es.paginations = [this.esgst.paginationNavigation ? this.esgst.paginationNavigation.innerHTML : ``];
es.reverseScrolling = this.esgst.es_r && this.esgst.discussionPath;
if (es.reverseScrolling) {
if (this.esgst.currentPage === 1 && this.esgst.paginationNavigation && !this.esgst.parameters.page) {
for (let i = 0, n = es.mainContext.children.length; i < n; ++i) {
es.mainContext.children[0].remove();
}
this.esgst.scopes.main.reset(`comments`);
this.esgst.pagination.firstElementChild.firstElementChild.nextElementSibling.textContent = `0`;
if (this.esgst.paginationNavigation) {
let lastLink = this.esgst.paginationNavigation.lastElementChild;
if (lastLink.classList.contains(`is-selected`) && lastLink.querySelector(`.fa-angle-double-right`) && !this.esgst.lastPageLink) {
es.currentPage = parseInt(lastLink.getAttribute(`data-page-number`));
} else {
es.currentPage = 999999999;
}
} else {
es.currentPage = 999999999;
}
es.nextPage = es.currentPage;
es.reversePages = true;
es.ended = false;
} else {
es.currentPage = this.esgst.currentPage;
es.nextPage = es.currentPage - 1;
es.pageBase = es.currentPage + 1;
es.ended = es.nextPage === 0;
}
} else {
es.currentPage = this.esgst.currentPage;
es.nextPage = es.currentPage + 1;
es.pageBase = es.currentPage - 1;
es.ended = (!this.esgst.paginationNavigation || this.esgst.paginationNavigation.lastElementChild.classList.contains(this.esgst.selectedClass));
}
const options = {
rootMargin: `-${this.esgst.commentsTop + 1}px 0px 0px 0px`
};
es.observer = new IntersectionObserver(this.es_observe.bind(this, es), options);
// noinspection JSIgnoredPromiseFromCall
this.es_activate(es);
}
es_observe(es, entries) {
for (const entry of entries) {
if (!entry.boundingClientRect && !entry.rootBounds) {
continue;
}
if (!entry.target.getAttribute(`data-esgst-intersection`)) {
// So it doesn't get fired when starting to observe an element.
entry.target.setAttribute(`data-esgst-intersection`, true);
if (!entry.isIntersecting) {
continue;
}
}
if (entry.target.classList.contains(`pagination`)) {
if (entry.isIntersecting) {
this.esgst.pagination.setAttribute(`data-esgst-intersecting`, `true`);
this.esgst.es_loadNext(null, true);
} else {
this.esgst.pagination.removeAttribute(`data-esgst-intersecting`);
}
} else {
const index = parseInt(entry.target.className.match(/es-page-(\d+)/)[1]);
if (entry.isIntersecting) {
this.es_changePagination(es, index);
} else if (entry.boundingClientRect.y <= entry.rootBounds.y) {
// The intersection element is no longer visible, but was scrolled upwards,
// so we can now change the pagination.
this.es_changePagination(es, es.reverseScrolling ? index - 1 : index + 1);
}
}
}
}
async es_activate(es) {
for (let i = 0, n = es.mainContext.children.length; i < n; ++i) {
if (i === n - 1) {
es.observer.observe(es.mainContext.children[i]);
}
es.mainContext.children[i].classList.add(`esgst-es-page-${es.currentPage}`);
}
es.nextButton = createHeadingButton({
featureId: `es`,
id: `esNext`,
icons: [`fa-step-forward`],
title: `Load next page`
});
es.continuousButton = createHeadingButton({
featureId: `es`,
id: `esContinuous`,
icons: [`fa-fast-forward`],
title: `Continuously load pages`
});
if (es.ended) {
es.continuousButton.classList.add(`esgst-hidden`);
}
es.pauseButton = createHeadingButton({
featureId: `es`,
id: `esPause`,
icons: [`fa-pause`],
title: `Pause the endless scrolling`
});
es.resumeButton = createHeadingButton({
featureId: `es`,
id: `esResume`,
orderId: `esPause`,
icons: [`fa-play`],
title: `Resume the endless scrolling`
});
es.refreshButton = createHeadingButton({
featureId: `es`,
id: `esRefresh`,
icons: [`fa-refresh`, `fa-map-marker`],
title: `Refresh current page`
});
es.refreshAllButton = createHeadingButton({
featureId: `es`,
id: `esRefreshAll`,
icons: [`fa-refresh`],
title: `Refresh all pages`
});
this.esgst.es_refresh = this.es_refresh.bind(this, es);
es.refreshButton.addEventListener(`click`, this.esgst.es_refresh.bind(this));
this.esgst.es_refreshAll = this.es_refreshAll.bind(this, es);
es.refreshAllButton.addEventListener(`click`, this.esgst.es_refreshAll.bind(this));
es.continuousButton.addEventListener(`click`, this.es_continuouslyLoad.bind(this, es));
es.nextButton.addEventListener(`click`, this.es_stepNext.bind(this, es));
es.pauseButton.addEventListener(`click`, this.es_pause.bind(this, es, false));
es.resumeButton.addEventListener(`click`, this.es_resume.bind(this, es, false));
if (this.esgst.paginationNavigation) {
this.esgst.modules.generalPaginationNavigationOnTop.pnot_simplify();
this.es_fixFirstPageLinks();
let lastLink = this.esgst.paginationNavigation.lastElementChild;
if (this.esgst.lastPageLink && this.esgst.lastPage !== es.pageIndex && !lastLink.classList.contains(`is-selected`) && !lastLink.querySelector(`.fa-angle-double-right`)) {
createElements(this.esgst.paginationNavigation, `beforeEnd`, this.esgst.lastPageLink);
}
this.es_setPagination(es);
}
es.isLimited = false;
es.limitCount = 0;
es.busy = false;
es.paused = this.esgst.currentSettings.es.current.options && this.esgst.currentSettings.es.current.options.pause;
this.esgst.es_loadNext = this.es_loadNext.bind(this, es);
if (es.paused) {
// noinspection JSIgnoredPromiseFromCall
this.es_pause(es, true);
} else {
// noinspection JSIgnoredPromiseFromCall
this.es_resume(es, true);
}
es.pageIndex = es.currentPage;
const options = {
rootMargin: `0px 0px ${window.innerHeight}px 0px`
};
const observer = new IntersectionObserver(this.es_observe.bind(this, es), options);
observer.observe(this.esgst.pagination);
if (es.paused && es.reversePages) {
this.esgst.es_loadNext();
} else if (this.esgst.es_cl) {
// noinspection JSIgnoredPromiseFromCall
this.es_continuouslyLoad(es);
}
}
async es_loadNext(es, callback, force) {
if (!this.esgst.stopEs && !es.busy && (!es.paused || es.reversePages) && !es.ended && ((force && !es.continuous && !es.step) || (!force && (es.continuous || es.step))) && (!es.isLimited || es.limitCount > 0)) {
es.limitCount -= 1;
es.busy = true;
es.progress = createElements(this.esgst.pagination.firstElementChild, `beforeEnd`, [{
attributes: {
class: `esgst-bold`
},
type: `span`,
children: [{
attributes: {
class: `fa fa-circle-o-notch fa-spin`
},
type: `i`
}, {
text: ` Loading next page...`,
type: `node`
}]
}]);
// noinspection JSIgnoredPromiseFromCall
this.es_getNext(es, false, false, callback, await request({
method: `GET`,
url: `${this.esgst.searchUrl}${es.nextPage}`
}));
} else if (callback && typeof callback === `function`) {
callback();
}
}
async es_getNext(es, refresh, refreshAll, callback, response) {
let pagination = parseHtml(response.responseText).getElementsByClassName(`pagination`)[0],
context = pagination.previousElementSibling,
rows = context.getElementsByClassName(`table__rows`)[0];
if (this.esgst.commentsPath && !context.classList.contains(`comments`)) {
if (!refreshAll) {
es.refreshButton.addEventListener(`click`, this.esgst.es_refresh.bind(this));
createElements(es.refreshButton, `inner`, [{
attributes: {
class: `fa fa-refresh`
},
type: `i`
}, {
attributes: {
class: `fa fa-map-marker`
},
type: `i`
}]);
}
return;
}
if (rows) {
context = rows;
}
let paginationNavigation = pagination.getElementsByClassName(this.esgst.paginationNavigationClass)[0];
if (es.reversePages) {
es.paginations[0] = paginationNavigation.innerHTML;
createElements(this.esgst.paginationNavigation, `inner`, [...(Array.from(parseHtml(es.paginations[0]).body.childNodes).map(x => {
return {
context: x
};
}))]);
if (this.esgst.paginationNavigation) {
this.esgst.modules.generalPaginationNavigationOnTop.pnot_simplify();
this.es_fixFirstPageLinks();
let lastLink = this.esgst.paginationNavigation.lastElementChild;
if (this.esgst.lastPageLink && this.esgst.lastPage !== es.pageIndex && !lastLink.classList.contains(`is-selected`) && !lastLink.querySelector(`.fa-angle-double-right`)) {
createElements(this.esgst.paginationNavigation, `beforeEnd`, this.esgst.lastPageLink);
}
this.es_setPagination(es);
}
es.reversePages = false;
if (es.currentPage === 999999999) {
es.currentPage = parseInt(paginationNavigation.lastElementChild.getAttribute(`data-page-number`));
es.nextPage = es.currentPage;
es.pageBase = es.currentPage + 1;
es.pageIndex = es.currentPage;
}
this.esgst.pagination.firstElementChild.firstElementChild.textContent = (parseInt(this.esgst.pagination.firstElementChild.firstElementChild.nextElementSibling.nextElementSibling.textContent.replace(/,/g, ``)) + 1).toString().replace(/\B(?=(\d{3})+(?!\d))/g, `,`);
this.esgst.pagination.firstElementChild.firstElementChild.nextElementSibling.textContent = this.esgst.pagination.firstElementChild.firstElementChild.nextElementSibling.nextElementSibling.textContent;
} else if (refresh) {
pagination = es.paginations[(es.reverseScrolling ? es.pageBase - (refreshAll || es.pageIndex) : (refreshAll || es.pageIndex) - es.pageBase) - 1];
if (paginationNavigation && pagination !== paginationNavigation.innerHTML) {
es.paginations[(es.reverseScrolling ? es.pageBase - (refreshAll || es.pageIndex) : (refreshAll || es.pageIndex) - es.pageBase) - 1] = paginationNavigation.innerHTML;
es.ended = false;
}
} else {
es.paginations.push(paginationNavigation.innerHTML);
}
let fragment = document.createDocumentFragment();
if (this.esgst.cr && this.esgst.discussionPath) {
reverseComments(context);
}
let n = context.children.length;
const currentPage = refresh ? refreshAll || es.pageIndex : es.nextPage;
for (let i = 0; i < n; ++i) {
let child = context.children[0];
child.classList.add(`esgst-es-page-${currentPage}`);
fragment.appendChild(child);
}
let oldN = 0;
if (refresh) {
let elements = document.getElementsByClassName(`esgst-es-page-${currentPage}`);
oldN = elements.length;
for (let i = 1; i < oldN; ++i) {
elements[0].remove();
}
let element = elements[0];
if (element) {
es.mainContext.insertBefore(fragment, element);
es.observer.observe(element.previousElementSibling);
element.remove();
} else {
es.mainContext.appendChild(fragment);
es.observer.observe(es.mainContext.lastElementChild);
}
if (!refreshAll) {
await endless_load(es.mainContext, true, null, currentPage);
this.es_setRemoveEntry(es.mainContext);
if (this.esgst.ts && !this.esgst.us) {
this.esgst.modules.generalTableSorter.ts_sortTables();
}
es.refreshButton.addEventListener(`click`, this.esgst.es_refresh.bind(this));
createElements(es.refreshButton, `inner`, [{
attributes: {
class: `fa fa-refresh`
},
type: `i`
}, {
attributes: {
class: `fa fa-map-marker`
},
type: `i`
}]);
}
} else {
if (es.divisors) {
createElements(es.mainContext, `beforeEnd`, [{
attributes: {
class: `esgst-page-heading esgst-es-page-divisor`
},
type: `div`,
children: [{
attributes: {
class: `page__heading__breadcrumbs page_heading_breadcrumbs`
},
type: `div`,
children: [{
attributes: {
href: `${this.esgst.searchUrl}${es.nextPage}`
},
text: `Page ${es.nextPage}`,
type: `a`
}]
}]
}]);
}
es.mainContext.appendChild(fragment);
es.observer.observe(es.mainContext.lastElementChild);
await endless_load(es.mainContext, true, null, currentPage);
this.es_setRemoveEntry(es.mainContext);
if (this.esgst.ts && !this.esgst.us) {
this.esgst.modules.generalTableSorter.ts_sortTables();
}
es.progress.remove();
if (es.reverseScrolling) {
--es.nextPage;
es.busy = false;
if (es.nextPage <= 0) {
es.ended = true;
if (callback && typeof callback === `function`) {
callback();
}
} else if (!es.paused && !es.step) {
if (es.continuous) {
this.esgst.es_loadNext(callback);
} else if (callback && typeof callback === `function`) {
callback();
} else if (this.esgst.pagination.getAttribute(`data-esgst-intersecting`)) {
this.esgst.es_loadNext(null, true);
}
} else if (callback && typeof callback === `function`) {
callback();
}
} else {
++es.nextPage;
es.busy = false;
if (paginationNavigation.lastElementChild.classList.contains(this.esgst.selectedClass)) {
es.ended = true;
if (callback && typeof callback === `function`) {
callback();
}
} else if (!es.paused && !es.step) {
if (es.continuous) {
this.esgst.es_loadNext(callback);
} else if (callback && typeof callback === `function`) {
callback();
} else if (this.esgst.pagination.getAttribute(`data-esgst-intersecting`)) {
this.esgst.es_loadNext(null, true);
}
} else if (callback && typeof callback === `function`) {
callback();
}
}
}
let paginationCount = null;
if (this.esgst.pagination.textContent.match(/No\sresults\swere\sfound\./)) {
this.esgst.pagination.firstElementChild.firstChild.remove();
common.createElements_v2(this.esgst.pagination.firstElementChild, `afterBegin`, [
`Displaying `,
[`strong`, 1],
` to `,
[`strong`, n],
` of `,
[`strong`, n],
` result${n > 1 ? `s` : ``}`
]);
} else {
if (es.reverseScrolling && !refresh) {
paginationCount = this.esgst.pagination.firstElementChild.firstElementChild;
} else {
paginationCount = this.esgst.pagination.firstElementChild.firstElementChild.nextElementSibling;
}
paginationCount.textContent = (parseInt(paginationCount.textContent.replace(/,/g, ``)) - oldN + (es.reverseScrolling && !refresh ? (-n) : n)).toString().replace(/\B(?=(\d{3})+(?!\d))/g, `,`);
}
}
es_changePagination(es, index) {
const correctedIndex = es.reverseScrolling ? (es.pageBase - index) : (index - es.pageBase);
const pagination = es.paginations[correctedIndex - 1];
if (pagination && this.esgst.paginationNavigation.innerHTML !== pagination) {
createElements(this.esgst.paginationNavigation, `inner`, [...(Array.from(parseHtml(pagination).body.childNodes).map(x => {
return {
context: x
};
}))]);
this.esgst.modules.generalPaginationNavigationOnTop.pnot_simplify();
this.es_fixFirstPageLinks();
let lastLink = this.esgst.paginationNavigation.lastElementChild;
if (this.esgst.lastPageLink && this.esgst.lastPage !== es.pageIndex && !lastLink.classList.contains(`is-selected`) && !lastLink.querySelector(`.fa-angle-double-right`)) {
createElements(this.esgst.paginationNavigation, `beforeEnd`, this.esgst.lastPageLink);
}
this.es_setPagination(es);
}
}
es_fixFirstPageLinks() {
const firstPageLinks = this.esgst.paginationNavigation.querySelectorAll(`[data-page-number="1"]`);
for (const firstPageLink of firstPageLinks) {
firstPageLink.setAttribute(`href`, `${firstPageLink.getAttribute(`href`)}/search?page=1`);
}
}
async es_stepNext(es) {
if (es.step) return;
createElements(es.nextButton, `inner`, [{
attributes: {
class: `fa fa-circle-o-notch fa-spin`
},
type: `i`
}]);
es.step = true;
const wasPaused = es.paused;
await this.es_resume(es);
this.esgst.es_loadNext(async () => {
es.step = false;
if (wasPaused) {
await this.es_pause(es);
} else {
await this.es_resume(es);
}
createElements(es.nextButton, `inner`, [{
attributes: {
class: `fa fa-step-forward`
},
type: `i`
}]);
});
}
async es_continuouslyLoad(es) {
if (es.continuous) return;
createElements(es.continuousButton, `inner`, [{
attributes: {
class: `fa fa-circle-o-notch fa-spin`
},
type: `i`
}]);
es.continuous = true;
const wasPaused = es.paused;
await this.es_resume(es);
if (this.esgst.es_cl) {
es.isLimited = true;
es.limitCount = Math.min(10, parseInt(this.esgst.es_pages));
}
this.esgst.es_loadNext(async () => {
es.isLimited = false;
es.limitCount = 0;
es.continuous = false;
if (wasPaused) {
await this.es_pause(es);
} else {
await this.es_resume(es);
}
createElements(es.continuousButton, `inner`, [{
attributes: {
class: `fa fa-fast-forward`
},
type: `i`
}]);
});
}
async es_pause(es, firstRun) {
es.paused = true;
es.pauseButton.classList.add(`esgst-hidden`);
es.resumeButton.classList.remove(`esgst-hidden`);
if (!firstRun) {
const setting = this.esgst.currentSettings.es.setting;
setting.include = setting.include.map(item => {
if (item !== this.esgst.currentSettings.es.current) {
return item;
}
if (!item.options) {
item.options = {};
}
item.options.pause = es.paused;
this.esgst.currentSettings.es.current = item;
return item;
});
await setSetting(`es`, setting, true);
}
es.continuous = false;
createElements(es.continuousButton, `inner`, [{
attributes: {
class: `fa fa-fast-forward`
},
type: `i`
}]);
}
async es_resume(es, firstRun) {
es.paused = false;
es.resumeButton.classList.add(`esgst-hidden`);
es.pauseButton.classList.remove(`esgst-hidden`);
if (!firstRun) {
const setting = this.esgst.currentSettings.es.setting;
setting.include = setting.include.map(item => {
if (item !== this.esgst.currentSettings.es.current) {
return item;
}
if (!item.options) {
item.options = {};
}
item.options.pause = es.paused;
this.esgst.currentSettings.es.current = item;
return item;
});
await setSetting(`es`, setting, true);
}
if (this.esgst.pagination.getAttribute(`data-esgst-intersecting`)) {
this.esgst.es_loadNext(null, true);
}
}
async es_refresh(es) {
es.refreshButton.removeEventListener(`click`, this.esgst.es_refresh);
createElements(es.refreshButton, `inner`, [{
attributes: {
class: `fa fa-circle-o-notch fa-spin`
},
type: `i`
}]);
let response = await request({ method: `GET`, url: `${this.esgst.searchUrl}${es.pageIndex}` });
// noinspection JSIgnoredPromiseFromCall
this.es_getNext(es, true, false, null, response);
if (this.esgst.giveawaysPath && this.esgst.es_rd) {
if (this.esgst.oadd) {
// noinspection JSIgnoredPromiseFromCall
this.esgst.modules.discussionsOldActiveDiscussionsDesign.oadd_load(true);
} else {
checkMissingDiscussions(true);
}
}
if (this.esgst.pinnedGiveaways) {
createElements(this.esgst.pinnedGiveaways, `inner`, [...(Array.from(parseHtml(response.responseText).getElementsByClassName(`pinned-giveaways__outer-wrap`)[0].childNodes).map(x => {
return {
context: x
};
}))]);
await endless_load(this.esgst.pinnedGiveaways, true);
this.esgst.modules.giveawaysPinnedGiveawaysButton.pgb();
}
if (!this.esgst.hr) {
await this.esgst.modules.generalHeaderRefresher.hr_refreshHeaderElements(parseHtml((await request({
method: `GET`,
url: this.esgst.sg ? `/giveaways/search?type=wishlist` : `/`
})).responseText));
// noinspection JSIgnoredPromiseFromCall
this.esgst.modules.generalHeaderRefresher.hr_refreshHeader(this.esgst.modules.generalHeaderRefresher.hr_getCache());
}
}
async es_refreshAll(es) {
es.refreshAllButton.removeEventListener(`click`, this.esgst.es_refreshAll);
createElements(es.refreshAllButton, `inner`, [{
attributes: {
class: `fa fa-circle-o-notch fa-spin`
},
type: `i`
}]);
let page = es.reverseScrolling ? es.pageBase - 1 : es.pageBase + 1,
response = await request({ method: `GET`, url: `${this.esgst.searchUrl}${page}` });
// noinspection JSIgnoredPromiseFromCall
this.es_getNext(es, true, page, null, response);
const promises = [];
for (let i = 1, n = es.paginations.length; i < n; ++i) {
page = es.reverseScrolling ? es.pageBase - (i + 1) : es.pageBase + (i + 1);
// noinspection JSIgnoredPromiseFromCall
promises.push(this.es_getNext(es, true, page, null, await request({ method: `GET`, url: `${this.esgst.searchUrl}${page}` })));
}
if (!this.esgst.hr) {
await this.esgst.modules.generalHeaderRefresher.hr_refreshHeaderElements(parseHtml((await request({
method: `GET`,
url: this.esgst.sg ? `/giveaways/search?type=wishlist` : `/`
})).responseText));
// noinspection JSIgnoredPromiseFromCall
this.esgst.modules.generalHeaderRefresher.hr_refreshHeader(this.esgst.modules.generalHeaderRefresher.hr_getCache());
}
await Promise.all(promises);
await endless_load(es.mainContext, true);
this.es_setRemoveEntry(es.mainContext);
es.refreshAllButton.addEventListener(`click`, this.esgst.es_refreshAll.bind(this));
createElements(es.refreshAllButton, `inner`, [{
attributes: {
class: `fa fa-refresh`
},
type: `i`
}]);
if (this.esgst.ts && !this.esgst.us) {
this.esgst.modules.generalTableSorter.ts_sortTables();
}
if (this.esgst.giveawaysPath && this.esgst.es_rd) {
if (this.esgst.oadd) {
// noinspection JSIgnoredPromiseFromCall
this.esgst.modules.discussionsOldActiveDiscussionsDesign.oadd_load(true);
} else {
checkMissingDiscussions(true);
}
}
if (this.esgst.pinnedGiveaways) {
createElements(this.esgst.pinnedGiveaways, `inner`, [...(Array.from(parseHtml(response.responseText).getElementsByClassName(`pinned-giveaways__outer-wrap`)[0].childNodes).map(x => {
return {
context: x
};
}))]);
await endless_load(this.esgst.pinnedGiveaways, true);
this.esgst.modules.giveawaysPinnedGiveawaysButton.pgb();
}
}
es_setPagination(es) {
let matches = this.esgst.paginationNavigation.children;
for (let i = 0, n = matches.length; i < n; ++i) {
matches[i].addEventListener(`click`, this.es_setPaginationItem.bind(this, es));
}
}
es_setPaginationItem(es, event) {
event.preventDefault();
let page = parseInt(event.currentTarget.getAttribute(`data-page-number`)),
element = document.querySelector(`.esgst-es-page-${page}:not(.esgst-hidden)`);
if (element) {
animateScroll(element.offsetTop, () => {
this.es_changePagination(es, page);
});
} else {
window.location.href = event.currentTarget.getAttribute(`href`);
}
}
es_setRemoveEntry(Context) {
let Matches = Context.getElementsByClassName(`table__row-inner-wrap`);
for (let I = 0, N = Matches.length; I < N; ++I) {
this.es_removeEntry(Matches[I]);
}
}
es_removeEntry(Context) {
let Default, Loading, Complete, Data;
Default = Context.getElementsByClassName(`table__remove-default`)[0];
if (Default) {
Loading = Default.nextElementSibling;
Complete = Loading.nextElementSibling;
Default.addEventListener(`click`, async () => {
let Values, I, N;
Default.classList.toggle(`is-hidden`);
Loading.classList.toggle(`is-hidden`);
Values = Context.getElementsByTagName(`input`);
Data = ``;
for (I = 0, N = Values.length; I < N; ++I) {
Data += `${Values[I].getAttribute(`name`)}=${Values[I].value}${I < (N - 1) ? `&` : ``}`;
}
Loading.classList.toggle(`is-hidden`);
let responseJson = JSON.parse((await request({ data: Data, method: `POST`, url: `/ajax.php` })).responseText);
if (responseJson.type === `success`) {
Context.classList.add(`is-faded`);
Complete.classList.toggle(`is-hidden`);
if (responseJson.points) {
this.esgst.pointsContainer.textContent = responseJson.points;
this.esgst.points = parseInt(this.esgst.pointsContainer.textContent.replace(/,/g, ``).match(/\d+/)[0]);
}
} else {
Default.classList.toggle(`is-hidden`);
}
});
}
}
}
const generalEndlessScrolling = new GeneralEndlessScrolling();
export { generalEndlessScrolling };