From 7a8895df331fa7fbaf3160a8b6171e2b8f2d78e4 Mon Sep 17 00:00:00 2001 From: Sascha Nitsch Date: Wed, 4 Sep 2024 18:57:52 +0200 Subject: [PATCH] support for scrolling up and down --- scriptedbrowser/commands/base.php | 18 ++++++++++++++++++ scriptedbrowser/control.php | 17 +++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/scriptedbrowser/commands/base.php b/scriptedbrowser/commands/base.php index d84f65b..f6a7295 100644 --- a/scriptedbrowser/commands/base.php +++ b/scriptedbrowser/commands/base.php @@ -98,6 +98,24 @@ function mousemoverel($main, $control, $options) return true; } +/** + * scroll + * + * @param \ScriptedBrowser\Main $main + * @unused-param $main + * @param \ScriptedBrowser\Control $control control interface + * @unused-param $control + * @param array $options options + * @return bool always true + */ +function scroll($main, $control, $options) +{ + $amount = $options['parameter']; + $delay = $options['delay']; + $control->scroll(intval($amount), floatval($delay)); + return true; +} + /** * sleep * diff --git a/scriptedbrowser/control.php b/scriptedbrowser/control.php index 8b68d91..295064d 100644 --- a/scriptedbrowser/control.php +++ b/scriptedbrowser/control.php @@ -273,6 +273,23 @@ class Control @socket_close($socket); } + /** + * scroll given amount + * + * @param int $amount + * @param float $delay delay per scroll + * @return void + */ + public function scroll($amount, $delay) + { + $dir = $amount > 0 ? 5 : 4; + $count = abs($amount); + for ($i = 0; $i < $count; ++$i) { + fwrite($this->xdotool, "click $dir\n"); + usleep($delay * 1000 * 1000); + } + } + /** * find elements with given css selector *