Getting YouTube to Play on Repeat

Let's face it— as of writing this, YouTube is missing one critically important feature: automatically replaying videos. Meanwhile, within the last few years (since 2012 or thereabouts), modern web browsers have gained an incredible feature: the JavaScript Console.

The title of this article is kind of a side-effect of its real purpose. When I was starting out in the world of programming, one of the things that really turned me off about web development was the process. Either, one used a third-party editor with God-knows what rendering engine and hope that whatever you created in the editor (here's looking at you, Dreamweaver) worked the same way in "real life"... or, one used a regular text editor (Notepad back at the time) and saved their code, alt+tabbed over to their web browser and refreshed the page. Then, there was the whole question of whether what you just wrote would work in both Netscape and Internet Explorer.

Did I just say Netscape? I... uh... meant Chrome.

Today, Ctrl+Shift+K in Firefox for Windows will get you to probably the greatest tool ever invented— the interactive JavaScript console. You can muckle 'round with code in real time on the page you're currently on. Use document.createElement and appendChild to create and add content to a page— including script elements.

Right. This brings me to YouTube. Load up your jam on YouTube, and then open up the console (it exists in every major browser including Internet Explorer). This may involve right-clicking on the page background somewhere and choosing "Inspect" or "Inspect Element" and finding a tab called "Console" (aside: this is the noun, we're not comforting anybody in their time of need).

Next, just paste this code bit into the input line at the bottom of the console window:

setInterval( function() { var b = document.getElementsByTagName("button"); for ( var i = 0; i < b.length; i++ ) { if ( b[i].getAttribute("title") == "Replay" ) {b[i].click();}}}, 500);

You're a wizard!, you declare after your ninth hour of jamming to that obscure song that only you like from Gloryhammer, Unleash the Archers, Keldian, Powerwolf, The Rankin Sisters, or Eluveitie... well, it didn't start as a one-liner. It started with the getElementsByTagName, finding the index where title equals Replay (the Inspector tab came in handy here), and calling the click method. The rest was putting all the building blocks (the loop and interval, namely) together.

Or maybe you're cursing me for slowing down your computer. Reload the YouTube page (essentially undoing the magic), and replace the "500" with a bigger number (1500 would check for a replay button every second and a half) so that it's not hammering the JavaScript engine quite as hard.

Hopefully, now you're feeling good about the developer console. Definitely mess with it, then go back and read other stuff!