Last active
April 23, 2025 11:18
-
-
Save devlato/1e08cadadcc2e8f367608e3c1fd471d4 to your computer and use it in GitHub Desktop.
Ticketek—purchasing tickets
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
| // Do not forget to use your own selectors | |
| const trySelecting = async () => { | |
| const delay = (timeoutMs = 100) => new Promise((resolve, _reject) => { | |
| setTimeout(resolve, timeoutMs); | |
| }); | |
| const findEl = async ({ parent, query, retries, delayBetweenRetriesMs }) => { | |
| let el = null; | |
| let attempts = 0; | |
| do { | |
| el = (parent ?? document).querySelector(query); | |
| attempts++; | |
| await delay(delayBetweenRetriesMs ?? 50); | |
| } while (el == null && attempts <= (retries ?? 10)) | |
| return el; | |
| }; | |
| const playTada = () => { | |
| const AudioContext = window.AudioContext || window.webkitAudioContext; | |
| const audioCtx = new AudioContext(); | |
| const frequencies = [523.25, 659.25, 783.99, 1046.50]; // C5, E5, G5, C6 | |
| const duration = 0.2; | |
| frequencies.forEach((frequency, index) => { | |
| const oscillator = audioCtx.createOscillator(); | |
| const gainNode = audioCtx.createGain(); | |
| oscillator.connect(gainNode); | |
| gainNode.connect(audioCtx.destination); | |
| oscillator.type = 'triangle'; | |
| oscillator.frequency.setValueAtTime(frequency, audioCtx.currentTime + index * duration); | |
| gainNode.gain.setValueAtTime(0, audioCtx.currentTime + index * duration); | |
| gainNode.gain.linearRampToValueAtTime(1, audioCtx.currentTime + index * duration + 0.05); | |
| gainNode.gain.linearRampToValueAtTime(0, audioCtx.currentTime + (index + 1) * duration); | |
| oscillator.start(audioCtx.currentTime + index * duration); | |
| oscillator.stop(audioCtx.currentTime + (index + 1) * duration); | |
| }); | |
| }; | |
| const retry = () => { | |
| setTimeout(() => trySelecting(), Math.floor(Math.random() * 4900 + 100)); | |
| }; | |
| await delay(); | |
| const ticketQuanity = await findEl({ query: '#quantity-MCARDPRE-1449038' }); | |
| console.log('ticketQuanity', ticketQuanity); | |
| // Resetting | |
| await delay(); | |
| ticketQuanity.value = '0'; | |
| ticketQuanity.dispatchEvent(new Event('change', { bubbles: true })); | |
| await delay(); | |
| ticketQuanity.value = '1'; | |
| ticketQuanity.dispatchEvent(new Event('change', { bubbles: true })); | |
| // Clicking the button | |
| await delay(); | |
| const nextButton = await findEl({ query: '#ticket-selection-page-next-button' }); | |
| console.log('nextButton', nextButton); | |
| if (nextButton == null) { | |
| retry(); | |
| return; | |
| } | |
| await delay(); | |
| nextButton.click(); | |
| await delay(); | |
| const noTickets = await findEl({ query: '#continueBar > ul.alert-error' }); | |
| console.log('noTickets', noTickets); | |
| if (noTickets != null) { | |
| retry(); | |
| return; | |
| } else { | |
| playTada(); | |
| } | |
| }; | |
| trySelecting(); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Do not forget to use your own selectors