I'm working on a Chrome extension and am trying to use JavaScript to click on an element on the page when a certain condition is met, which works just fine.
The problem is, if the element to be clicked on is off screen, the browser will automatically scroll so that the element is then on the screen. I'd like to have my code click on the element without having the screen scroll automatically.
Here is the code that triggers the click event.
function isHidden(users, comment, sticky) {
let userElement = comment.querySelector(`[data-testid="comment_author_link"]`);
let currName = userElement.getAttribute("href");
let shouldHide = users.includes(currName);
let isStickied = comment.querySelector("._2wd-K5Djdc9TGPRGDgmkpX") !== null;
if(shouldHide || (isStickied && sticky !== null)) {
blockIndex = users.indexOf(currName);
let level = comment.querySelector("._1RIl585IYPW6cmNXwgRz0J").innerHTML;
level = parseInt(level.replace(/^\D+/g, ""), 10) - 1;
let threadlines = comment.querySelectorAll(".threadline");
threadlines[level]?.click();
return true;
}
return false;
}
I can provide other code snippets if needed, or explain more about what I'm trying to do.