Get Computers To Solve Your Problems

The world is in a bit of a state at the moment. People are panicking and they're scared. Far be it for me to suggest that poor leadership might be at least partly to blame. I never learn lessons from other people's mistakes so how can I expect the people who run the country to do it?

This isn't a post about politics. It's about solving a really specific problem. Really specific.

A supermarket in this country is using a queueing feature to let people have access to their site. There are so many ways that this is broken, but assuming that this is trying to solve every problem presented by 100,000 people trying to panic buy alcohol and toilet paper, is short-sighted. This allows said supermarket to keep their core service alive so that at least some people can use it. Ideal? Not really, but some people being able to use a thing is better than no people being able to use a thing.

This queueing system uses a "There are currently 99,000 people in front of you" style system so that you can hazard some sort of a guess as to how many years you're going to need to wait to get drunk and wipe your ass. This presents an opportunity. This number also updates asynchronously. Another opportunity! We can run some JavaScript on this page to watch that number (without impacting an already over-stretched service), then hit a URL when it drops below a certain level, to send me an SMS so that I don't have to sit and watch a countdown timer go from 100,000 to 0. Like I have anything else to do.

Disclaimer: this approach does not mean you're going to get a delivery slot. These queueing systems are designed to stop people hammering the core service. They're not designed to let people in one at a time so that everyone gets a slot. This is a demonstration of an idea that solves a very specific problem and nothing more. You should not use this approach if you need food, as it will not work.

Firstly we need some JavaScript to watch the page. There's a few things going on here, but broadly we grab the current user's phone number, password (so that people can't hammer your Twilio account), CSS selector for the counter, and the threshold you want an alert at. We then poll on an interval to watch that number change, and when it's below the configured level we hit a URL that triggers a call to Twilio, and that's all she wrote.

loading...

This gets minified, then set as a bookmark in your browser so it can easily be triggered on the page. You could also run it in your console, but that's pretty nerdy. It's not pretty, but quick things tend not to be.

The backend is fairly self-explanatory. We boot an Express server (because that's what I had running), mount an endpoint, check a password, and trigger a call to Twilio. We don't need a handling of the response. We're going to assume it runs fine since this is just a proof of concept.

loading...

And that's it. A pretty simple demonstration of how you can watch a page for changes then do something when you detect some. What I'd really like is to make this a bit more robust and run this in a scriptable browser so that it can run on sites that don't update asynchronously, but fortunately for me the one I wanted to use did! I don't really like JavaScript. I'm not sure why I use it for everything.