Compare commits

...

8 Commits

Author SHA1 Message Date
Sascha Nitsch a69abb5b2f support for wrapping text in a span (for later use) 2024-09-09 17:29:05 +02:00
Sascha Nitsch f445dcb0be support for calculations 2024-09-09 17:28:42 +02:00
Sascha Nitsch c47142ea5c function to move mouse to defined coordinates 2024-09-09 17:19:28 +02:00
Sascha Nitsch 184b3e8848 fixed error detection 2024-09-09 17:18:38 +02:00
Sascha Nitsch 7ac8a2f7c2 support for mouse up and down functions 2024-09-09 17:17:06 +02:00
Sascha Nitsch d8b2f05767 fixed handling of white space in text to type 2024-09-09 16:39:17 +02:00
Sascha Nitsch fb5054c7f7 added functionality for key presses 2024-09-09 16:38:49 +02:00
Sascha Nitsch 76e7826b60 fixed description 2024-09-09 16:35:17 +02:00
5 changed files with 169 additions and 6 deletions

View File

@ -12,5 +12,7 @@
"email": "grumpydeveloper@contentnation.net"
}
],
"require": {}
"require": {
"arokettu/arithmetic-parser": "*"
}
}

View File

@ -7,6 +7,28 @@
**/
namespace ScriptedBrowser\Commands;
/**
* calculate given formula
*
* @param \ScriptedBrowser\Main $main main instance
* @param \ScriptedBrowser\Control $control control interface
* @param array<string,mixed> $options options
* @return bool always true
*/
function calc($main, $control, $options)
{
require_once 'vendor/autoload.php';
$input = $main->vars($options['parameter']);
$result = $options['result'];
try {
$calc = \Arokettu\ArithmeticParser\Calculator::evaluate($input);
$main->setVar($result, $calc);
return true;
} catch (\Exception) {
return false;
}
}
/**
* inject and execute given javascript
*
@ -23,6 +45,21 @@ function injectjscript($main, $control, $options)
return true;
}
/**
* press given key
*
* @param \ScriptedBrowser\Main $main
* @param \ScriptedBrowser\Control $control
* @param array<string,mixed> $options options
* @return bool always true
*/
function key($main, $control, $options)
{
$key = $main->vars($options['parameter']);
$control->key($key);
return true;
}
/**
* do mouse left click
*
@ -38,6 +75,18 @@ function leftclick($main, $control, $options) : bool
return true;
}
function leftpress($main, $control, $options) : bool
{
$control->mouseDown(\ScriptedBrowser\MouseButton::Left);
return true;
}
function leftrelease($main, $control, $options) : bool
{
$control->mouseUp(\ScriptedBrowser\MouseButton::Left);
return true;
}
/**
* move mouse to given element
*
@ -49,8 +98,7 @@ function leftclick($main, $control, $options) : bool
function mousemove($main, $control, $options) : bool
{
$element = $control->selectCSS($main->vars($options['parameter']));
$mouseOffset = $main->getMouseOffset();
if ($element === []) {
if ($element === [] || array_key_exists('error', $element)) {
return false;
}
$element = array_values($element)[0];
@ -61,6 +109,39 @@ function mousemove($main, $control, $options) : bool
$targetX -= $scroll[0];
$targetY -= $scroll[1];
// echo "move $element - $tokens[1] $targetX $targetY\n";
_mousemoveaction($main, $control, $options, $targetX, $targetY);
return true;
}
/**
* move mouse to given target position from variables
*
* @param \ScriptedBrowser\Main $main main instance
* @param \ScriptedBrowser\Control $control control interface
* @param array<string,mixed> $options options
* @return bool true if mouse move, false on error
*/
function mousemovetarget($main, $control, $options) : bool
{
$targetX = $main->vars($options['x']);
$targetY = $main->vars($options['y']);
$scroll = $control->getScrollPosition();
$targetX -= $scroll[0];
$targetY -= $scroll[1];
_mousemoveaction($main, $control, $options, $targetX, $targetY);
return true;
}
/**
* move mouse to given target position
*
* @param \ScriptedBrowser\Main $main main instance
* @param \ScriptedBrowser\Control $control control interface
* @param array<string,mixed> $options options
*/
function _mousemoveaction($main, $control, $options, $targetX, $targetY)
{
$mouseOffset = $main->getMouseOffset();
$mouseX = $main->getMouseX();
$mouseY = $main->getMouseY();
$delta = [$targetX - $mouseX, $targetY - $mouseY];
@ -82,7 +163,6 @@ function mousemove($main, $control, $options) : bool
$main->setMouseX($targetX);
$main->setMouseY($targetY);
$control->mousemove($targetX + $mouseOffset[0], $targetY + $mouseOffset[1]);
return true;
}
/**

View File

@ -9,7 +9,7 @@
namespace ScriptedBrowser\Commands;
/**
* inject and execute given javascript
* run database query and provide result in variable
*
* @param \ScriptedBrowser\Main $main main instance
* @param \ScriptedBrowser\Control $control control interface

View File

@ -0,0 +1,48 @@
<?php
/**
* SPDX-FileCopyrightText: 2024 Sascha Nitsch (grumpydeveloper) https://contentnation.net/@grumpydevelop
* SPDX-License-Identifier: GPL-3.0-or-later
*
* @author Sascha Nitsch (grumpydeveloper)
**/
namespace ScriptedBrowser\Commands;
/**
* wrap given element in a <span id="">
*
* @param \ScriptedBrowser\Main $main main instance
* @param \ScriptedBrowser\Control $control control interface
* @unused-param $control
* @param array<string,mixed> $options options
* @return bool true on success
*/
function spanwrap($main, $control, $options)
{
$start = $options['start'];
$end = $options['end'];
$query = $options['parameter'];
$id = $options['id'];
$js = '
const start = ' . $start .';
const end = ' . $end .';
const node = document.querySelector("' . $query . '");
const text = node.innerHTML;
const frontText = (start !== 0) ? text.substring(0, start) : \'\';
const middleText = text.substring(start, end !== 0 ? end : undefined);
const endText = end !== 0 ? text.substring(end) : \'\';
let finalText = frontText + \'<span data-id="' . $id . '">\';
finalText += middleText + \'</span>\' + endText;
node.innerHTML = finalText;
';
$control->executeScript($js);
$e = $control->selectCSS(('span[data-id="'. $id .'"]'));
$element = array_values($e)[0];
$r = $control->getElementRect($element);
$main->setVar($id . '_x', $r['x']);
$main->setVar($id . '_y', $r['y']);
$main->setVar($id . '_width', $r['width']);
$main->setVar($id . '_height', $r['height']);
return true;
}

View File

@ -184,6 +184,28 @@ class Control
return $this->executeScript('return [window.pageXOffset, window.pageYOffset];');
}
/**
* press given key (combination)
*
* @param string $key key to press
* @return void
*/
public function key($key)
{
fwrite($this->xdotool, "key $key\n");
}
/**
* press mouse button
*
* @param MouseButton $button button to press
* @return void
*/
public function mouseDown($button)
{
fwrite($this->xdotool, 'mousedown ' . $button->value . "\n");
}
/**
* move mouse to absolute position
*
@ -207,6 +229,17 @@ class Control
fwrite($this->xdotool, "mousemove_relative $x $y\n");
}
/**
* release mouse button
*
* @param MouseButton $button button to press
* @return void
*/
public function mouseUp($button)
{
fwrite($this->xdotool, 'mouseup ' . $button->value . "\n");
}
/**
* open geckodriver if not already running
*
@ -346,6 +379,6 @@ class Control
public function type($typeSpeed, $text)
{
$delay = 1000 / $typeSpeed;
fwrite($this->xdotool, "type --delay $delay $text\n");
fwrite($this->xdotool, "type --delay $delay '$text'\n");
}
}