Waiting in line

Let’s start a series of little DevTools console lifehacks, shall we?

We’re currently in quarantine, and waiting for the one nurse who’s been assigned to help thousands of people self-administer a specimen collection.

Here’s what the page says:

The nurse will be with you shortly
There are currently 2719 people ahead of you.
This is an estimate as there are many nurses seeing patients at once.

OMG, that’s a lot. I could stay here all day checking it every few minutes, or … I could write a little script to execute in the console!

So what do I need? A sound to alert me! User’s choice here, but why not something from freesound.org!

Ok, so we’ve picked a funky sound, now we need to know when to play it. For some background, always hit up the invaluable Mozilla web docs.

const audioElement = new Audio("https://freesound.org/data/previews/569/569377_5674468-lq.mp3")

So now audioElement.play() and audioElement.pause() will play a funky tune. Volume up!

Next, we should grab the number we are in line:

let theNumber = parseInt(document.getElementsByTagName('h3')[0].innerText.replace( /^\D+/g, ''), 10);

Note the stuff we’re using here for extra safety. Making sure we have an integer and not a text string, for one. Also, parseInt defaults to base 10, but it’s always a good idea to make sure we’re using that. Also, note, there happens to only be one h3 element on this page, but additional specificity is usually a good idea also.

Next, let’s use setInterval to check periodically, say every 5 seconds. And a conditional statement around the play method:

let checkInterval = setInterval(function(){
    let theNumber = parseInt(document.getElementsByTagName('h3')[0].innerText.replace( /^\D+/g, ''), 10);
    console.log('the number is: ', thenum);
    if (theNumber < 200) {
        audioElement.play()
    }
}, 5000);

That’s it! I can now go make some coffee, clean up the mess the kids made in the living room, etc. If you want to check to make sure it works, you can always clear your interval like this:

clearInterval(checkInterval); 

… then make an adjustment to theNumber, to just a little above what you currently see, execute that, nod your head to that funky beat, clear again, adjust again to a lower number, and execute again.

Yes, we could have also checked the Network tab of DevTools for outgoing requests for that number (indeed, it’s returning just one simple JSON object), dug into the minified app scripts, etc. You do you, friendo.

Leave a comment

Your email address will not be published. Required fields are marked *