Created
October 31, 2021 16:05
-
-
Save amir-arad/12292fc396e3db3e07714a941e8017bb to your computer and use it in GitHub Desktop.
meckano monthly report clock faker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const endTime = []; | |
const WORK_DAY_TIME_RANGE = { | |
half: { | |
min: 450, | |
max: 550 | |
}, | |
full: { | |
min: 540, | |
max: 660 | |
} | |
} | |
const defaultOptions = { | |
pointerX: 0, | |
pointerY: 0, | |
button: 0, | |
ctrlKey: false, | |
altKey: false, | |
shiftKey: false, | |
metaKey: false, | |
bubbles: true, | |
cancelable: true | |
}; | |
const eventMatchers = { | |
'HTMLEvents': /^(?:load|unload|abort|error|select|change|submit|reset|focus|blur|resize|scroll)$/, | |
'MouseEvents': /^(?:click|dblclick|mouse(?:down|up|over|move|out))$/ | |
}; | |
async function w8() { | |
return await new Promise((resolve) => setTimeout(() => resolve(), 3000)); | |
} | |
function randomIntFromInterval(min, max) { | |
if (min === 1 && max === 59) { | |
let n; | |
do { | |
n = (Math.floor(Math.random() * (max - min + 1) + min)); | |
} while(n % 5 !== 0) | |
return n; | |
} | |
return Math.floor(Math.random() * (max - min + 1) + min); | |
}; | |
function randomCheckout(checkInHour, checkInMinutes, workDayType) { | |
const {min, max} = WORK_DAY_TIME_RANGE[workDayType]; | |
let workTime; | |
do { | |
workTime = Math.floor(Math.random() * (max - min + 1) + min); | |
} while(workTime % 5 !== 0) | |
const minutes = workTime % 60; | |
const hours = (workTime - minutes) / 60; | |
const time = new Date(2020, 10, 1, checkInHour, checkInMinutes); | |
time.setHours(time.getHours() + hours); | |
time.setMinutes(time.getMinutes() + minutes); | |
const timeMinutes = time.getMinutes() < 10 ? `0${time.getMinutes()}` : `${time.getMinutes()}` | |
return `${time.getHours()}:${timeMinutes}` | |
} | |
function extend(destination, source) { | |
for (let property in source) | |
destination[property] = source[property]; | |
return destination; | |
}; | |
function getElements() { | |
return [ | |
...document.querySelectorAll('.alt0'), | |
...document.querySelectorAll('.alt1') | |
].filter(a => !a.className.includes('highlightingRestDays')); | |
} | |
function simulate(element, eventName) { | |
let options = extend(defaultOptions, arguments[2] || {}); | |
let oEvent, eventType = null; | |
for (let name in eventMatchers) { | |
if (eventMatchers[name].test(eventName)) { eventType = name; break; } | |
} | |
if (!eventType) | |
throw new SyntaxError('Only HTMLEvents and MouseEvents interfaces are supported'); | |
if (document.createEvent) { | |
oEvent = document.createEvent(eventType); | |
if (eventType == 'HTMLEvents') { | |
oEvent.initEvent(eventName, options.bubbles, options.cancelable); | |
} | |
else { | |
oEvent.initMouseEvent(eventName, options.bubbles, options.cancelable, document.defaultView, | |
options.button, options.pointerX, options.pointerY, options.pointerX, options.pointerY, | |
options.ctrlKey, options.altKey, options.shiftKey, options.metaKey, options.button, element); | |
} | |
element.dispatchEvent(oEvent); | |
} | |
else { | |
options.clientX = options.pointerX; | |
options.clientY = options.pointerY; | |
let evt = document.createEventObject(); | |
oEvent = extend(evt, options); | |
element.fireEvent('on' + eventName, oEvent); | |
} | |
return element; | |
}; | |
async function startCheckin() { | |
getElements().forEach( async (e) => { | |
const [checkinTd, checkoutTd] = [...e.querySelectorAll('.center')]; | |
const [checkinDisplayMarkSpan, checkinSpan] = [...checkinTd.querySelectorAll('span')]; | |
const [checkinInput] = [...checkinTd.querySelectorAll('input')]; | |
const [standardSpan] = [...e.querySelectorAll('.standard')]; | |
const workDayType = standardSpan.textContent === '07:30' ? 'half' : 'full'; | |
const checkInHour = randomIntFromInterval(8, 10); | |
const chekInMinutes = randomIntFromInterval(1, 59); | |
const randomCheckInHour = ('0' + checkInHour).slice(-2); | |
const randomCheckInMinute = ('0' + chekInMinutes).slice(-2); | |
const randomCheckInTime = `${randomCheckInHour}:${randomCheckInMinute}`; | |
endTime.push(randomCheckout(checkInHour, chekInMinutes, workDayType)); | |
await new Promise(async (resolve) => { | |
await simulate(checkinSpan, "click"); | |
checkinInput.value = randomCheckInTime; | |
checkinInput.dispatchEvent(new KeyboardEvent('keyup', { | |
bubbles: true, | |
cancelable: true, | |
key: "enter", | |
keyCode: 13 | |
})) | |
resolve(); | |
}); | |
}) | |
} | |
async function startCheckout() { | |
getElements().forEach( async (e, i) => { | |
const [checkinTd, checkoutTd] = [...e.querySelectorAll('.center')]; | |
const [checkoutDisplayMarkSpan, checkoutSpan] = [...checkoutTd.querySelectorAll('span')]; | |
const [checkoutInput] = [...checkoutTd.querySelectorAll('input')]; | |
await new Promise(async (resolve) => { | |
await simulate(checkoutSpan, "click"); | |
checkoutInput.value = endTime[i]; | |
checkoutInput.dispatchEvent(new KeyboardEvent('keyup', { | |
bubbles: true, | |
cancelable: true, | |
key: "enter", | |
keyCode: 13 | |
})) | |
resolve(); | |
}); | |
}) | |
} | |
async function finishCheckin() { | |
const checkOut = document.querySelector(`#mainview > div > table > tbody > tr:nth-child(2) > td:nth-child(6) > div > span.checkout`); | |
const checkOutInput = document.querySelector("#mainview > div > table > tbody > tr:nth-child(2) > td:nth-child(6) > div > input"); | |
simulate(checkOut, "click"); | |
setTimeout(() => { | |
checkOutInput.dispatchEvent(new KeyboardEvent('keyup', { | |
bubbles: true, | |
cancelable: true, | |
key: "enter", | |
keyCode: 13 | |
})); | |
}, 0); | |
} | |
async function finishCheckout() { | |
const checkInLast = document.querySelector("#mainview > div > table > tbody > tr:nth-child(2) > td:nth-child(4) > div > span.checkin"); | |
const checkInInputLast = document.querySelector("#mainview > div > table > tbody > tr:nth-child(2) > td:nth-child(4) > div > input"); | |
simulate(checkInLast, "click"); | |
setTimeout(() => { | |
checkInInputLast.dispatchEvent(new KeyboardEvent('keyup', { | |
bubbles: true, | |
cancelable: true, | |
key: "enter", | |
keyCode: 13 | |
})); | |
}, 0); | |
} | |
async function start() { | |
await startCheckin(); | |
await w8(); | |
await finishCheckin(); | |
await startCheckout(); | |
await w8(); | |
await finishCheckout(); | |
} | |
start(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment