Skip to content

Instantly share code, notes, and snippets.

@alexpiezo
Last active November 1, 2023 12:18
Show Gist options
  • Save alexpiezo/16df3bdbebcd98bf2eb85e561173e7dc to your computer and use it in GitHub Desktop.
Save alexpiezo/16df3bdbebcd98bf2eb85e561173e7dc to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Calculator Automator
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Automate calculations and interact with a virtual calculator
// @author Piezo
// @match https://play.ttrockstars.com/*
// @grant GM_log
// ==/UserScript==
(function() {
'use strict';
let loop = false;
let currentIteration = 0;
function sleep(ms) {
let randomDeviation = Math.floor(Math.random() * 200) - 100;
return new Promise(resolve => setTimeout(resolve, ms + randomDeviation));
}
async function moveMouseToElement(element) {
const rect = element.getBoundingClientRect();
const x = rect.left + rect.width / 2;
const y = rect.top + rect.height / 2;
const mouseEvent = new MouseEvent("mousemove", {
clientX: x,
clientY: y,
bubbles: true,
cancelable: true,
view: window
});
element.dispatchEvent(mouseEvent);
await sleep(100);
}
async function clickNumber(number) {
let strNumber = number.toString();
for (let i = 0; i < strNumber.length; i++) {
let element = document.querySelector(`[aria-label="${strNumber[i]}"]`);
if (element) {
await moveMouseToElement(element);
element.click();
await sleep(300);
} else {
console.error(`Element with aria-label="${strNumber[i]}" not found`);
}
}
}
async function calculateAndClickResult() {
const selector = ".equation-container";
let element = document.querySelector(selector);
if (!element) {
console.error(`Element with selector "${selector}" not found`);
return;
}
let expression = element.textContent.replace("=", "");
let calculate = new Function('return ' + expression.replace("×", "*").replace("÷", "/"));
let result;
try {
result = calculate();
} catch (e) {
console.error('Invalid mathematical expression', e);
return;
}
await clickNumber(result);
let enterKey = document.querySelector(".key-ent");
if (enterKey) {
enterKey.click();
} else {
console.error('Enter key not found');
}
}
function toggleButtonText() {
const button = document.getElementById('toggle-button');
const iterationIndicator = document.getElementById('iteration-indicator');
if (loop) {
button.textContent = "Stop";
iterationIndicator.textContent = `Currently at iteration: ${currentIteration}`;
} else {
button.textContent = "Start";
iterationIndicator.textContent = `Paused at iteration: ${currentIteration}`;
}
}
function startCalculations() {
loop = true;
toggleButtonText();
const iterationsInput = document.getElementById('iterations-input');
const delayInput = document.getElementById('delay-input');
const times = Number(iterationsInput.value);
const delay = Number(delayInput.value);
calculateRepeatedly(times, delay);
}
function stopCalculations() {
loop = false;
toggleButtonText();
}
function resetCalculations() {
loop = false;
currentIteration = 0;
toggleButtonText();
}
async function calculateRepeatedly(times, delay) {
for (let i = 0; i < times; i++) {
if (!loop) break;
currentIteration = i + 1;
calculateAndClickResult();
document.getElementById('iteration-indicator').textContent =
`Currently at iteration: ${currentIteration}`;
await sleep(delay);
}
resetCalculations(); // Automatically reset after all calculations
}
function createControlPanel() {
const controlPanel = document.createElement('div');
controlPanel.innerHTML = `
<form id="control-form" style="position: fixed; top: 0; left: 0; background-color: white; padding: 10px; border: 1px solid black; z-index: 99999;">
<label>
Iterations:
<input type="number" id="iterations-input" value="30" />
</label>
<label>
Delay (ms):
<input type="number" id="delay-input" value="2000" />
</label>
<button type="button" id="toggle-button">Start</button>
<p id="iteration-indicator">Paused at iteration: 0</p>
</form>
`;
document.body.appendChild(controlPanel);
}
createControlPanel();
document.getElementById('toggle-button').addEventListener('click', function() {
if (loop) {
stopCalculations();
} else {
startCalculations();
}
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment