Skip to content

Commit

Permalink
feat: allow scrolling directly to top or bottom
Browse files Browse the repository at this point in the history
  • Loading branch information
mondaychen committed Mar 13, 2024
1 parent d13699f commit d6e25dd
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
6 changes: 4 additions & 2 deletions src/helpers/availableActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ export const availableActionsVision = [
},
{
name: "scroll",
description: 'Scroll the page up or down. Value can be "up" or "down"',
description:
'Scroll the page to see the other parts. Use "up" or "down" to scroll half the height of the window. Use "top" or "bottom" to quickly scroll to the top or bottom of the page.',
args: [
{
name: "value",
Expand Down Expand Up @@ -75,7 +76,8 @@ export const availableActions = [
},
{
name: "scroll",
description: 'Scroll the page up or down. Value can be "up" or "down"',
description:
'Scroll the page to see the other parts. Use "up" or "down" to scroll half the height of the window. Use "top" or "bottom" to scroll to the top or bottom of the page.',
args: [
{
name: "value",
Expand Down
14 changes: 14 additions & 0 deletions src/helpers/rpc/domActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,20 @@ export class DomActions {
await sleep(300);
}

public async scrollToTop() {
await this.sendCommand("Runtime.evaluate", {
expression: "window.scroll({left: 0, top: 0})",
});
await sleep(300);
}

public async scrollToBottom() {
await this.sendCommand("Runtime.evaluate", {
expression: "window.scroll({left: 0, top: document.body.offsetHeight})",
});
await sleep(300);
}

public async setValueWithElementId(payload: {
elementId: number;
value: string;
Expand Down
19 changes: 15 additions & 4 deletions src/helpers/rpc/performAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,21 @@ async function setValueWithLabel(
}

async function scroll(domActions: DomActions, action: Action) {
if (action.args.value === "up") {
await domActions.scrollUp();
} else {
await domActions.scrollDown();
switch (action.args.value) {
case "up":
await domActions.scrollUp();
break;
case "down":
await domActions.scrollDown();
break;
case "top":
await domActions.scrollToTop();
break;
case "bottom":
await domActions.scrollToBottom();
break;
default:
console.error("Invalid scroll value", action.args.value);
}
}

Expand Down

0 comments on commit d6e25dd

Please sign in to comment.