Skip to content

Commit 9e58c3f

Browse files
committed
update abstract behaviour for autoscroll
1 parent 914a932 commit 9e58c3f

File tree

2 files changed

+198
-54
lines changed

2 files changed

+198
-54
lines changed

src/autoscroll.ts

Lines changed: 74 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
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";
212
//import { type AutoFetcher } from "./autofetcher";
313

4-
514
// ===========================================================================
6-
export class AutoScroll {
15+
export class AutoScroll implements AbstractBehavior {
716
showMoreQuery: string;
8-
state: { segments: number } = { segments: 1};
17+
state: { segments: number } = { segments: 1 };
918
lastScrollPos: number;
1019
samePosCount: number;
1120

@@ -15,7 +24,8 @@ export class AutoScroll {
1524
constructor() {
1625
//super();
1726

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')]";
1929

2030
this.lastScrollPos = -1;
2131
this.samePosCount = 0;
@@ -27,7 +37,7 @@ export class AutoScroll {
2737

2838
static init() {
2939
return {
30-
state: {}
40+
state: {},
3141
};
3242
}
3343

@@ -45,7 +55,10 @@ export class AutoScroll {
4555

4656
canScrollMore() {
4757
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+
);
4962
}
5063

5164
debug(msg: string) {
@@ -67,9 +80,11 @@ export class AutoScroll {
6780
}
6881

6982
async shouldScroll() {
70-
if (!this.hasScrollEL(self.window) &&
83+
if (
84+
!this.hasScrollEL(self.window) &&
7185
!this.hasScrollEL(self.document) &&
72-
!this.hasScrollEL(self.document.body)) {
86+
!this.hasScrollEL(self.document.body)
87+
) {
7388
return false;
7489
}
7590

@@ -82,16 +97,19 @@ export class AutoScroll {
8297
const numFetching = currentlyFetching();
8398

8499
// 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;
86102

87103
window.scrollTo({ top: scrollEnd, left: 0, behavior: "smooth" });
88104

89105
// wait for any updates
90106
await sleep(500);
91107

92108
// 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+
) {
95113
window.scrollTo({ top: 0, left: 0, behavior: "auto" });
96114
return true;
97115
}
@@ -104,14 +122,18 @@ export class AutoScroll {
104122
return false;
105123
}
106124

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+
) {
108130
return false;
109131
}
110132

111133
return true;
112134
}
113135

114-
async* run(ctx) {
136+
async *run(ctx) {
115137
const { getState } = ctx.Lib;
116138

117139
if (this.shouldScrollUp()) {
@@ -124,12 +146,18 @@ export class AutoScroll {
124146
return;
125147
}
126148

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+
);
128153
}
129154

130-
async* scrollDown(ctx) {
155+
async *scrollDown(ctx) {
131156
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+
);
133161
const interval = 75;
134162
let elapsedWait = 0;
135163

@@ -141,8 +169,11 @@ export class AutoScroll {
141169

142170
while (this.canScrollMore()) {
143171
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+
);
146177
void addLink(document.location.href);
147178
return;
148179
}
@@ -165,8 +196,11 @@ export class AutoScroll {
165196
await sleep(waitUnit);
166197

167198
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),
170204
]);
171205

172206
if (self.document.scrollingElement.scrollHeight === scrollHeight) {
@@ -182,20 +216,25 @@ export class AutoScroll {
182216

183217
if (this.state.segments === 1) {
184218
// 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+
);
186223
elapsedWait = 2.0;
187-
188224
} else {
189225
const waitSecs = elapsedWait / (this.state.segments - 1);
190226
// only add extra wait if actually changed height
191227
// 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+
);
193232

194233
const startTime = Date.now();
195234

196235
await Promise.race([
197236
waitUntil(() => this.canScrollMore(), interval),
198-
sleep(waitSecs)
237+
sleep(waitSecs),
199238
]);
200239

201240
elapsedWait += (Date.now() - startTime) * 2;
@@ -215,9 +254,12 @@ export class AutoScroll {
215254
}
216255
}
217256

218-
async* scrollUp(ctx) {
257+
async *scrollUp(ctx) {
219258
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+
);
221263
const interval = 75;
222264

223265
const scrollOpts = { top: -scrollInc, left: 0, behavior: "auto" };
@@ -238,13 +280,16 @@ export class AutoScroll {
238280

239281
if (this.state.segments === 1) {
240282
// 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+
);
242287
} else {
243288
// only add extra wait if actually changed height
244289
// check for scrolling, but allow for more time for content to appear the longer have already scrolled
245290
await Promise.race([
246291
waitUntil(() => self.scrollY > 0, interval),
247-
sleep((this.state.segments - 1) * 2000)
292+
sleep((this.state.segments - 1) * 2000),
248293
]);
249294
}
250295
}

0 commit comments

Comments
 (0)