Skip to content

Instantly share code, notes, and snippets.

@DennisdeBest
Last active October 15, 2023 12:51
Show Gist options
  • Save DennisdeBest/47363d09a53807b717de63fa6d59bd36 to your computer and use it in GitHub Desktop.
Save DennisdeBest/47363d09a53807b717de63fa6d59bd36 to your computer and use it in GitHub Desktop.
Human Benchmark Number Test GreaseMonkey Script
// ==UserScript==
// @name HumanBenchmark number
// @version 1
// @grant none
// @match https://humanbenchmark.com/tests/number-memory
// ==/UserScript==
const getParentDiv = () => {
const $parentDivs = document.getElementsByClassName('number-memory-test');
if ($parentDivs.length === 0) {
console.log("No parent divs found.");
return null;
}
return $parentDivs[0];
};
const waitForElement = (parent, className, tagName, timeout = 25000) => { // Increased to 15 seconds
return new Promise((resolve, reject) => {
let elapsed = 0;
const interval = 100;
const poll = () => {
let element = null
if(className) {
element = parent.getElementsByClassName(className)[0];
} else {
element = parent.getElementsByTagName(tagName)[0];
}
if (element) {
resolve(element);
return;
}
elapsed += interval;
if (elapsed >= timeout) {
reject(new Error("Timed out waiting for element"));
return;
}
setTimeout(poll, interval);
};
poll();
});
};
const executeNTimes = async (n) => {
let $parentDiv = getParentDiv();
if (!$parentDiv) return;
const $buttons = $parentDiv.getElementsByTagName('button');
const $startButton = $buttons[0];
$startButton.click();
for (let i = 0; i < n; i++) {
$parentDiv = getParentDiv();
const $dataElement = await waitForElement($parentDiv, 'big-number', null);
const number = $dataElement.outerText;
// Wait for the form
const $form = await waitForElement($parentDiv, null, 'form');
const $input = $form.getElementsByTagName('input')[0];
const $submit = $form.getElementsByTagName('button')[0];
// Set the value and simulate user input event
$input.value = number;
const event = new Event('input', {
'bubbles': true,
'cancelable': true
});
$input.dispatchEvent(event);
$submit.click();
const $showAnswer = await waitForElement($parentDiv, 'showanswer', null);
const $nextButton = $showAnswer.getElementsByTagName('button')[0];
$nextButton.click();
// Optional: Wait a little bit before starting the next cycle
await new Promise(r => setTimeout(r, 2000));
}
};
executeNTimes(100); // Runs the sequence 100 times, it won't reach as much because of the time limit set in the waitForElement
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment