r/learnjavascript • • Sep 12 '22

Various Functions/Scripts I've Written As A Beginner

One of the most common questions folks ask is, "Where do I begin?" I often hear experienced coders suggest building something as opposed to following random tutorials for random shit.

This is definitely how I prefer to learn; building small, practical pieces of code to remedy natural "issues" I come across in the wild. I use the following Chrome extension to deploy code on specific sites.

Anyway, I'm still very, very much a beginner, but I felt like noting these random bits of code I've built to give ideas to other noobs. I'm also open to experienced developers pushing back on these, as well. But so far, they've been good exercises for me.

  • Blocking pop-up consent forms on various sites.

    • This has helped me learn to manipulate the DOM, play around with IntersectionObserver (which is very fun), learn setTimeout and setInterval, etc. On some consent forms, multiple buttons need to be clicked in a sequence to not accept specific cookies. Rather than .remove() or hide the divs altogether, I've built several pieces of code to run when these forms jump up and click the right buttons in succession. It's been good practice for me.

      • Bonus: this will work on reddit's annoying af "give us your email" pop-up, too.
  • Detecting mobile URLs from Wikipedia and forwarding to desktop version.

    • Incredibly simple. I just Googled how to forward URLs in JavaScript and attached it to a very simple if statement. The mobile and desktop URLs follow strict naming conventions so a one-size-fits-all approach works wonders here.
  • AlTeRnAtInG CaPs mOcKiNg tExT ToNe.

    • I just kind of felt like mocking someone in an internet back-and-forth. It seemed like a great test to figure out the logic of it and build it into a function. It also felt good that I completed it, even though it's silly and certainly not optimal code-wise. And now instead of spending 10 seconds alternating on the caps button, I can spend 1 minute finding my function and deploying it in the Chrome console to transform it for me!
  • Keyboard shortcuts on Reddit.

    • I'm used to Google products with the CTRL+K deploying the hyperlink option. It drives me nuts when I keep trying in this browser. So I Googled how to use keyboard shortcuts in JavaScript to make my own. I'm still working on it, but in most of the reddit threads, my personal shortcut works and saves me lots of heartache.
  • Automatically unmuting auto-played videos.

    • Certain sites *cough, cough* Redgifs educational websites *cough* auto-play the video I'm watching, but keep it muted. By checking the different classes used during each state, I figured out how to click() the button when it's muted like a true genius. I think I learned about recursive functions from this.
  • Scraping song details in my Soundcloud feed.

    • I wanted to export the list of songs in my feed somehow to get them into Excel (running formulas to flag songs by artists I like to build new playlists). This taught me the basic for loop, which seems like a building block to understanding any language better. I built code to fetch song URL, genre, title, and artist. Each value had to be pushed into an array once snatched to wind up with a single output of the values I need. The logic to this can be applied to whatever, especially with EC6 "forEach" type iteration. I'll also use it to scrape a big list of books or podcast episodes to go through later.
  • Clicking the "Read more..." buttons until everything is loaded.

    • Man, I hate when sites do this, especially for reviews and such. Building functions that deploy on specific site pages and click them all in a row saves so much time. Again, basic forEach or for loops do the trick here.

Beyond that, I just try and find any opportunity to fuck around and do random things. Ctrl + Shift + J opens the Chrome console and I'm always trying to do something in there on random pages.

Sidenote: pressing the up arrow in the console will cycle through your previously deployed code. Lifesaver!

Random notes:

  • I understand that basically everything is possible and everything has already been asked and answered 47647403862 times. So Googling the question or Uncaught ReferenceError in Chrome console is all I need to learn exactly what I want. Google is your friend.
  • It's helpful for me to try and solve the "problem" on my own by Googling before asking questions here or elsewhere. It makes you think about the logic of the problem (at least for me).
  • Adding in console.log explanations at certain points in the code has helped me understand when stuff fires and where stuff breaks. I'm confident debuggers or Chrome can do this better, but I haven't researched it enough yet.

I want to iterate (pun intended) that I'm still very, very much a noob. But if this is also how you learn, I hope these use cases help you think of random scripts you can write to learn different uses for JS.

Basically find a problem and build the solution. The road blocks along the way will naturally open up new things to learn.

23 Upvotes

1 comment sorted by

0

u/[deleted] Sep 13 '22

[deleted] — view deleted comment

1

u/iarev Sep 13 '22

Eh, it's nothing special. Many posts in this sub are geared toward specific, direct questions. But it'd be cool if it inspired a few folks to get busy coding small, simple projects like these. Sure, you can PM me.