1
- import { sleep , waitUnit , xpathNode , isInViewport , waitUntil , behaviorLog , addLink , currentlyFetching } from "./lib/utils" ;
1
+ import {
2
+ sleep ,
3
+ waitUnit ,
4
+ xpathNode ,
5
+ isInViewport ,
6
+ waitUntil ,
7
+ behaviorLog ,
8
+ addLink ,
9
+ currentlyFetching ,
10
+ } from "./lib/utils" ;
11
+ import type { AbstractBehavior } from "./lib/behavior" ;
2
12
//import { type AutoFetcher } from "./autofetcher";
3
13
4
-
5
14
// ===========================================================================
6
- export class AutoScroll {
15
+ export class AutoScroll implements AbstractBehavior {
7
16
showMoreQuery : string ;
8
- state : { segments : number } = { segments : 1 } ;
17
+ state : { segments : number } = { segments : 1 } ;
9
18
lastScrollPos : number ;
10
19
samePosCount : number ;
11
20
@@ -15,7 +24,8 @@ export class AutoScroll {
15
24
constructor ( ) {
16
25
//super();
17
26
18
- this . showMoreQuery = "//*[contains(text(), 'show more') or contains(text(), 'Show more')]" ;
27
+ this . showMoreQuery =
28
+ "//*[contains(text(), 'show more') or contains(text(), 'Show more')]" ;
19
29
20
30
this . lastScrollPos = - 1 ;
21
31
this . samePosCount = 0 ;
@@ -27,7 +37,7 @@ export class AutoScroll {
27
37
28
38
static init ( ) {
29
39
return {
30
- state : { }
40
+ state : { } ,
31
41
} ;
32
42
}
33
43
@@ -45,7 +55,10 @@ export class AutoScroll {
45
55
46
56
canScrollMore ( ) {
47
57
const scrollElem = self . document . scrollingElement || self . document . body ;
48
- return this . currScrollPos ( ) < Math . max ( scrollElem . clientHeight , scrollElem . scrollHeight ) ;
58
+ return (
59
+ this . currScrollPos ( ) <
60
+ Math . max ( scrollElem . clientHeight , scrollElem . scrollHeight )
61
+ ) ;
49
62
}
50
63
51
64
debug ( msg : string ) {
@@ -67,9 +80,11 @@ export class AutoScroll {
67
80
}
68
81
69
82
async shouldScroll ( ) {
70
- if ( ! this . hasScrollEL ( self . window ) &&
83
+ if (
84
+ ! this . hasScrollEL ( self . window ) &&
71
85
! this . hasScrollEL ( self . document ) &&
72
- ! this . hasScrollEL ( self . document . body ) ) {
86
+ ! this . hasScrollEL ( self . document . body )
87
+ ) {
73
88
return false ;
74
89
}
75
90
@@ -82,16 +97,19 @@ export class AutoScroll {
82
97
const numFetching = currentlyFetching ( ) ;
83
98
84
99
// scroll to almost end of page
85
- const scrollEnd = ( document . scrollingElement . scrollHeight * 0.98 ) - self . innerHeight ;
100
+ const scrollEnd =
101
+ document . scrollingElement . scrollHeight * 0.98 - self . innerHeight ;
86
102
87
103
window . scrollTo ( { top : scrollEnd , left : 0 , behavior : "smooth" } ) ;
88
104
89
105
// wait for any updates
90
106
await sleep ( 500 ) ;
91
107
92
108
// scroll height changed, should scroll
93
- if ( lastScrollHeight !== self . document . scrollingElement . scrollHeight ||
94
- numFetching < currentlyFetching ( ) ) {
109
+ if (
110
+ lastScrollHeight !== self . document . scrollingElement . scrollHeight ||
111
+ numFetching < currentlyFetching ( )
112
+ ) {
95
113
window . scrollTo ( { top : 0 , left : 0 , behavior : "auto" } ) ;
96
114
return true ;
97
115
}
@@ -104,14 +122,18 @@ export class AutoScroll {
104
122
return false ;
105
123
}
106
124
107
- if ( ( self . window . scrollY + self [ "scrollHeight" ] ) / self . document . scrollingElement . scrollHeight < 0.90 ) {
125
+ if (
126
+ ( self . window . scrollY + self [ "scrollHeight" ] ) /
127
+ self . document . scrollingElement . scrollHeight <
128
+ 0.9
129
+ ) {
108
130
return false ;
109
131
}
110
132
111
133
return true ;
112
134
}
113
135
114
- async * run ( ctx ) {
136
+ async * run ( ctx ) {
115
137
const { getState } = ctx . Lib ;
116
138
117
139
if ( this . shouldScrollUp ( ) ) {
@@ -124,12 +146,18 @@ export class AutoScroll {
124
146
return ;
125
147
}
126
148
127
- yield getState ( ctx , "Skipping autoscroll, page seems to not be responsive to scrolling events" ) ;
149
+ yield getState (
150
+ ctx ,
151
+ "Skipping autoscroll, page seems to not be responsive to scrolling events" ,
152
+ ) ;
128
153
}
129
154
130
- async * scrollDown ( ctx ) {
155
+ async * scrollDown ( ctx ) {
131
156
const { getState } = ctx . Lib ;
132
- const scrollInc = Math . min ( self . document . scrollingElement . clientHeight * 0.10 , 30 ) ;
157
+ const scrollInc = Math . min (
158
+ self . document . scrollingElement . clientHeight * 0.1 ,
159
+ 30 ,
160
+ ) ;
133
161
const interval = 75 ;
134
162
let elapsedWait = 0 ;
135
163
@@ -141,8 +169,11 @@ export class AutoScroll {
141
169
142
170
while ( this . canScrollMore ( ) ) {
143
171
if ( document . location . pathname !== this . origPath ) {
144
- void behaviorLog ( "Location Changed, stopping scroll: " +
145
- `${ document . location . pathname } != ${ this . origPath } ` , "info" ) ;
172
+ void behaviorLog (
173
+ "Location Changed, stopping scroll: " +
174
+ `${ document . location . pathname } != ${ this . origPath } ` ,
175
+ "info" ,
176
+ ) ;
146
177
void addLink ( document . location . href ) ;
147
178
return ;
148
179
}
@@ -165,8 +196,11 @@ export class AutoScroll {
165
196
await sleep ( waitUnit ) ;
166
197
167
198
await Promise . race ( [
168
- waitUntil ( ( ) => self . document . scrollingElement . scrollHeight > scrollHeight , 500 ) ,
169
- sleep ( 30000 )
199
+ waitUntil (
200
+ ( ) => self . document . scrollingElement . scrollHeight > scrollHeight ,
201
+ 500 ,
202
+ ) ,
203
+ sleep ( 30000 ) ,
170
204
] ) ;
171
205
172
206
if ( self . document . scrollingElement . scrollHeight === scrollHeight ) {
@@ -182,20 +216,25 @@ export class AutoScroll {
182
216
183
217
if ( this . state . segments === 1 ) {
184
218
// only print this the first time
185
- yield getState ( ctx , `Scrolling down by ${ scrollOpts . top } pixels every ${ interval / 1000.0 } seconds` ) ;
219
+ yield getState (
220
+ ctx ,
221
+ `Scrolling down by ${ scrollOpts . top } pixels every ${ interval / 1000.0 } seconds` ,
222
+ ) ;
186
223
elapsedWait = 2.0 ;
187
-
188
224
} else {
189
225
const waitSecs = elapsedWait / ( this . state . segments - 1 ) ;
190
226
// only add extra wait if actually changed height
191
227
// check for scrolling, but allow for more time for content to appear the longer have already scrolled
192
- void behaviorLog ( `Waiting up to ${ waitSecs } seconds for more scroll segments` , "debug" ) ;
228
+ void behaviorLog (
229
+ `Waiting up to ${ waitSecs } seconds for more scroll segments` ,
230
+ "debug" ,
231
+ ) ;
193
232
194
233
const startTime = Date . now ( ) ;
195
234
196
235
await Promise . race ( [
197
236
waitUntil ( ( ) => this . canScrollMore ( ) , interval ) ,
198
- sleep ( waitSecs )
237
+ sleep ( waitSecs ) ,
199
238
] ) ;
200
239
201
240
elapsedWait += ( Date . now ( ) - startTime ) * 2 ;
@@ -215,9 +254,12 @@ export class AutoScroll {
215
254
}
216
255
}
217
256
218
- async * scrollUp ( ctx ) {
257
+ async * scrollUp ( ctx ) {
219
258
const { getState } = ctx . Lib ;
220
- const scrollInc = Math . min ( self . document . scrollingElement . clientHeight * 0.10 , 30 ) ;
259
+ const scrollInc = Math . min (
260
+ self . document . scrollingElement . clientHeight * 0.1 ,
261
+ 30 ,
262
+ ) ;
221
263
const interval = 75 ;
222
264
223
265
const scrollOpts = { top : - scrollInc , left : 0 , behavior : "auto" } ;
@@ -238,13 +280,16 @@ export class AutoScroll {
238
280
239
281
if ( this . state . segments === 1 ) {
240
282
// only print this the first time
241
- yield getState ( ctx , `Scrolling up by ${ scrollOpts . top } pixels every ${ interval / 1000.0 } seconds` ) ;
283
+ yield getState (
284
+ ctx ,
285
+ `Scrolling up by ${ scrollOpts . top } pixels every ${ interval / 1000.0 } seconds` ,
286
+ ) ;
242
287
} else {
243
288
// only add extra wait if actually changed height
244
289
// check for scrolling, but allow for more time for content to appear the longer have already scrolled
245
290
await Promise . race ( [
246
291
waitUntil ( ( ) => self . scrollY > 0 , interval ) ,
247
- sleep ( ( this . state . segments - 1 ) * 2000 )
292
+ sleep ( ( this . state . segments - 1 ) * 2000 ) ,
248
293
] ) ;
249
294
}
250
295
}
0 commit comments