Skip to content

Instantly share code, notes, and snippets.

@diego-mi
Created August 8, 2022 22:01
Show Gist options
  • Save diego-mi/d21b66a8c947b755d193b439c5eda990 to your computer and use it in GitHub Desktop.
Save diego-mi/d21b66a8c947b755d193b439c5eda990 to your computer and use it in GitHub Desktop.
TurtRacing - Auto Open Egg
// ==UserScript==
// @name TurtRacing - Auto Open Egg
// @namespace https://gist.github.com/diego-mi
// @version 0.1
// @description TurtRacing - Auto Open Egg
// @author DiegoMi
// @match https://play.turtleracing.io
// @grant none
// @run-at document-start
// ==/UserScript==
(function() {
'use strict';
var eggSelectorXpath = "//a[text()='OPEN']";
const initElementSelector = `.btnfosQuick`;
const eggSelector = `div.contentBx:nth-child`;
const openConfirmed = `.buttonCloseOpened`;
/**
* HELPERS
*/
async function WaitForElement(selector) {
while (document.querySelector(selector) === null) {
await new Promise( resolve => requestAnimationFrame(resolve) )
}
await new Promise(resolve => setTimeout(resolve, 100));
return document.querySelector(selector);
}
async function delay(time) {
return new Promise(resolve => setTimeout(resolve, time));
}
const clickEggToOpen = () => {
var matchingElement = document.evaluate(eggSelectorXpath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
matchingElement.click();
}
const waitForModalNewTurtle = async () => {
await WaitForElement(openConfirmed)
}
const confirmNewTurtle = async () => {
document.querySelector(openConfirmed).click();
}
/**
* INICIALIZACAO
*/
async function Init() {
await WaitForElement(initElementSelector);
const eggsCount = document.evaluate(eggSelectorXpath, document, null, XPathResult.length, null);
console.log('init open, egg count:', eggsCount);
while(document.evaluate(eggSelectorXpath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue) {
clickEggToOpen();
await waitForModalNewTurtle();
confirmNewTurtle();
await delay(500);
}
}
Init();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment