support for wrapping text in a span (for later use)
This commit is contained in:
parent
f445dcb0be
commit
a69abb5b2f
1 changed files with 48 additions and 0 deletions
48
scriptedbrowser/commands/spanwrap.php
Normal file
48
scriptedbrowser/commands/spanwrap.php
Normal 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;
|
||||
}
|
Loading…
Reference in a new issue