I only recently learned that bookmarklets exist. A portmanteau of bookmark and applet, they are pieces of Javascript you can run from the address bar or be called from your bookmarks. The idea of running Javascript directly from the address bar sounds strange at first but has fun applications, literally.
I previously built an alternative Rare Bird Alert! web application whose primary feature was grouping rare eBird sightings by species into expandable <details> elements by pulling data from the public eBird API. Creating the same feature for your eBird Needs Alerts is a little trickier because it requires authentication and access to your personal eBird data. Instead of some convoluted login and CSV data upload system, how about we just modify the DOM on the actual eBird website to group sightings by species?
Here it is
How to use it
Create a random bookmark (this blog post for instance)
Open your bookmark manager
Edit the bookmark you just created, rename it to whatever you want (maybe “eBird Compress”), and replace the URL with the code above
Save your new bookmark
Now when you go to any eBird alert page you can open the bookmark that you just created and voila!
Demo
It works on both desktop and mobile. Consult your browser’s documentation if you’re not sure how to edit and save bookmarks.
So, what is the code doing?
Here is the full non-minified script for your perusal. If you are curious how it works, here is a beat by beat breakdown:
First, we store a NodeList of all the DOM elements for observations in a variable called observations. If no observations are found (like if the script is run on a non-eBird alert page) it shows an alert message and stops the execution of the script. If the script has already been run on a page, it will alert you about that as well.
The parent container of the observations is stored in another variable. We define a function to extract the species name from an element, then a function to create a set of unique species names. Sets can only store unique values so no need to check for duplicate species names.
We define a function that will create new <details> DOM elements for each species, assign the species name to a data-species attribute and a <summary> element, then mount the new DOM elements where they need to go.
We then define a function to move all of the observations into the new DOM elements we just created.
The spacer nodes are also grabbed and moved because otherwise you end up with a mess of gray bars at the top:
Next, we get the number of observations for each species and append that number to the name of each species.
All that is left to do is call all of the functions to action!
We then run the above code through esbuild’s code minifier, which basically removes whitespace and shortens variable names to keep all the same functionality in a more compressed format. The result is terse code ready for a bookmark:
Feedback
There you have it, my first bookmarklet. Let me know how it works for you.