Last active
May 30, 2019 19:49
-
-
Save peerasak-u/be923a813ec93d9e7475113447594cfd to your computer and use it in GitHub Desktop.
บริจาคให้แก่กลุ่มเพื่อน นป.
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
let zoneIndex = 0; | |
let zoneCount = 0; | |
const waitForElement = (selector, retryTimes = 400) => { | |
return new Promise((resolve, reject) => { | |
const findElement = (_selector, _retried) => { | |
const element = document.querySelector(_selector); | |
if (!element) { | |
if (_retried < retryTimes) { | |
window.requestAnimationFrame(() => findElement(_selector, _retried + 1)); | |
} else { | |
reject(new Error(`Cannot find element "${selector}"`)); | |
} | |
} else { | |
resolve(element); | |
} | |
}; | |
findElement(selector, 0); | |
}); | |
}; | |
const selectSeat = (availables) => { | |
availables[0].parentElement.click(); | |
}; | |
const selectPayment = () => { | |
const payment = document.getElementsByClassName('paymentWrapper')[0]; | |
payment.click(); | |
const checkAgree = document.getElementById('checkAgree'); | |
checkAgree.click(); | |
} | |
const booking = () => { | |
const bookingBtn = document.getElementsByClassName('btn btn-atk-primary mt-3')[0]; | |
bookingBtn.click(); | |
} | |
const getZone = () => { | |
const map = document.querySelector('#zone > div > p > map'); | |
const zones = Array.from(map.children); | |
zoneCount = zones.length; | |
const zoneBtn = zones[zoneIndex]; | |
zoneBtn.click(); | |
} | |
const findNextZone = () => { | |
if (zoneIndex < zoneCount - 1) { | |
zoneIndex += 1; | |
getZone(); | |
} | |
} | |
const getSeat = () => { | |
const seatSvg = document.getElementsByClassName('seat available'); | |
const availables = Array.from(seatSvg); | |
if (availables.length > 0) { | |
selectSeat(availables); | |
booking(); | |
} else { | |
findNextZone(); | |
} | |
} | |
const startFlow = () => { | |
let buyNowBtn = document.getElementsByClassName('btn btn-atk-primary'); | |
if (buyNowBtn) { | |
buyNowBtn[0].click(); | |
} | |
} | |
const setupObservation = () => { | |
waitForElement('.custom-control-input') | |
.then(() => { | |
const roundLabels = document.getElementsByClassName('custom-control-label'); | |
roundLabels[0].click(); | |
}) | |
.catch(error => { | |
alert(error.message); | |
}); | |
waitForElement('#zone > div > p > map > area') | |
.then(() => { | |
getZone(); | |
}) | |
.catch(error => { | |
alert(error.message); | |
}); | |
waitForElement('.seat-wrapper') | |
.then(() => { | |
getSeat(); | |
}) | |
.catch(error => { | |
alert(error.message); | |
}); | |
waitForElement('.paymentWrapper') | |
.then(() => { | |
selectPayment(); | |
}) | |
.catch(error => { | |
alert(error.message); | |
}) | |
} | |
const runTask = () => { | |
setupObservation(); | |
startFlow(); | |
} | |
runTask(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment