Skip to content

Instantly share code, notes, and snippets.

@diego-mi
Last active March 18, 2022 21:31
Show Gist options
  • Save diego-mi/f5cd257e07f025a57ae4121f6fdcbc2d to your computer and use it in GitHub Desktop.
Save diego-mi/f5cd257e07f025a57ae4121f6fdcbc2d to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Zodiacs Auto Refuel
// @namespace https://gist.github.com/diego-mi/f5cd257e07f025a57ae4121f6fdcbc2d
// @version 0.1
// @description Zodiacs Auto Refuel
// @author DiegoMi
// @match https://v2.zodiacs.me
// @grant none
// @run-at document-start
// ==/UserScript==
(function() {
'use strict';
const QuerySelector_GasStationModal = `.station-info-modal`;
const QuerySelector_GasStationModal_CarItems = `${QuerySelector_GasStationModal} .list .ant-col`;
const QuerySelector_GasStationModal_RefuelMaxButton = `.btn-red`;
const QuerySelector_GasStationModal_ConfirmRefuelMaxButton = `body > div:nth-child(8) > div > div.ant-modal-wrap.ant-modal-confirm-centered.ant-modal-centered > div > div.ant-modal-content > div > div > div.ant-modal-confirm-btns > button.ant-btn.ant-btn-success`;
const timer = 10000;
let carListItems = null;
const delayDefault = 50;
/**
* HELPERS
*/
async function WaitForElement(selector) {
while (document.querySelector(selector) === null) {
await new Promise( resolve => requestAnimationFrame(resolve) )
}
await new Promise(resolve => setTimeout(resolve, delayDefault));
return document.querySelector(selector);
}
async function WaitForElementClose(selector) {
while (document.querySelector(selector)) {
await new Promise( resolve => requestAnimationFrame(resolve) )
}
}
async function Delay(time) {
return new Promise(resolve => setTimeout(resolve, time));
}
/**
* END HELPERS
*/
async function GetCarsToRefuel () {
await WaitForElement(QuerySelector_GasStationModal_CarItems);
carListItems = document.querySelectorAll(QuerySelector_GasStationModal_CarItems);
if (carListItems.length === 0) return console.log("Car Items not found");
return console.log(`Car Items found: ${carListItems.length}`);
}
async function DoRefuels() {
for(let carIndex = 0; carIndex < carListItems.length; carIndex++) {
if (await CarNeedRefuelByIndex(carIndex)) {
carListItems[carIndex].querySelector(QuerySelector_GasStationModal_RefuelMaxButton).click();
await Delay(delayDefault);
await ConfirmRefuel(carIndex);
await Delay(600);
}
}
}
async function CarNeedRefuelByIndex(carIndex) {
const carsFuelCount = carListItems[carIndex].querySelector(`.fuel-wrapper .full`);
try {
return carsFuelCount.length === 0;
} catch (e) {
return true;
}
}
async function ConfirmRefuel() {
await WaitForElement(QuerySelector_GasStationModal_ConfirmRefuelMaxButton);
document.querySelector(QuerySelector_GasStationModal_ConfirmRefuelMaxButton).click();
await WaitForElementClose(QuerySelector_GasStationModal_ConfirmRefuelMaxButton)
}
/**
* INITIALIZATION
**/
async function WaitForGasStationModal() {
await WaitForElement(QuerySelector_GasStationModal);
await GetCarsToRefuel();
await DoRefuels();
}
setTimeout(WaitForGasStationModal, timer);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment