Today I used JavaScript to mass-unsubscribe from a website’s email notifications. Here’s the code, followed by what I did and why.

document
  .querySelectorAll('[role="switch"]')
  .forEach((element) => element.click());

Today I was interacting with a website that specializes in job postings. To unsubscribe to their emails, you must visit a notifications page and un-check a toggle to opt out of each kind of email you’d like to not receive.

Perusing the DOM taught me that each button has a role of “switch.” This JavaScript unsubscribed me from every mailing list in a single line of code.

How this works is going to vary based on the HTML you’re working with. It’s a bit cumbersome. But I love puzzles like this, and find it to be slightly more ergonomic than un-checking a few dozen toggles.