The scrollClient
function is an asynchronous function that scrolls a webpage to a specific position based on a given CSS selector, or to the top or bottom of the page if no selector is provided. It uses various functions such as selectDom
and evaluateDom
to dynamically import and evaluate code on the client-side to achieve the desired scroll position.
npm run import -- "Scroll a specific element"
const {selectDom, evaluateDom} = importer.import("select tree")
const {walkTree} = importer.import("walk tree")
async function scrollClient(driver, selector, up = false) {
// scroll to bottom of messages
return await driver
.executeScript((evaluateDomString, walkTreeString, selectDomString, selector, up = false) => {
if (selector === '' || selector === false) {
window.scroll(window.scrollX, window.scrollY + (up ? -100000 : 100000));
return;
}
if(!window.evaluateDom)
window.evaluateDom = eval('(' + evaluateDomString + ')')
if(!window.walkTree)
window.walkTree = eval('(' + walkTreeString + ')')
if(!window.selectDom)
window.selectDom = eval('(' + selectDomString + ')')
var people = selectDom(selector, document)
people.scrollTop = people.scrollTop + (up ? -100000 : 100000);
}, evaluateDom, walkTree, selectDom, selector, up)
}
module.exports = scrollClient
const { selectDom, evaluateDom } = require('select-tree');
const { walkTree } = require('walk-tree');
/**
* Scrolls the client to the top or bottom of the element specified by the selector.
*
* @param {object} driver The driver instance
* @param {string} selector The CSS selector of the element to scroll to
* @param {boolean} up Whether to scroll to the top (default false)
* @returns {Promise<void>} A promise that resolves when the scrolling is complete
*/
async function scrollClient(driver, selector, up = false) {
// scroll to bottom of messages
if (!selector) {
// If no selector is provided, scroll to the bottom of the document
await driver.executeScript('window.scroll(window.scrollX, window.scrollY + (arguments[3]? -100000 : 100000))', up);
return;
}
// Evaluate the given functions and store them in the global object
await driver.executeScript((evaluateDomString, walkTreeString, selectDomString, selector, up) => {
if (!window.evaluateDom) {
// Use Function constructor to safely evaluate the function string
window.evaluateDom = new Function('window', `return ${evaluateDomString};`)({ window });
}
if (!window.walkTree) {
window.walkTree = new Function('window', `return ${walkTreeString};`)({ window });
}
if (!window.selectDom) {
window.selectDom = new Function('window', `return ${selectDomString};`)({ window });
}
// Select the element and scroll to the specified position
const people = window.selectDom(selector, document);
if (people) {
people.scrollTop = people.scrollTop + (up? -100000 : 100000);
} else {
console.warn(`Element not found: ${selector}`);
}
}, evaluateDom.toString(), walkTree.toString(), selectDom.toString(), selector, up);
}
module.exports = scrollClient;
scrollClient Function Breakdown
The scrollClient
function is an asynchronous function that scrolls a webpage to a specific position based on a given selector.
driver
: An object likely representing a browser driver (e.g. Selenium).selector
: A string representing the CSS selector for the element to scroll to.up
: A boolean indicating whether to scroll up or down (default: false
).selector
is empty or false
, the function scrolls the window to the top or bottom (depending on the up
parameter) by 100,000 pixels.evaluateDom
, walkTree
, or selectDom
functions are not defined on the window
object, they are dynamically imported and evaluated on the client-side using eval
.selectDom
function is called with the selector
and document
as arguments, and the resulting element is assigned to the people
variable.scrollTop
property of the people
element is adjusted by 100,000 pixels (or -100,000 if up
is true
) to achieve the desired scroll position.The scrollClient
function is exported as a module.