Rocket League Gallery - How I Did That Bad Thing
I recently put together a little gallery page for Rocket League clips, and figuring out how as a completely incapable web-dev person was enjoyable! I'm writing it up here to share the hacks and probably so I can laugh at it later.
The page in question is here: Some Recent Rocket League Clips (be warned, it's a bunch of iframes holding a 720p video each, it's not trivial to render.)
I got most of the solutions to my problem through googling, but the solution can be broken up into the 3 usual webdev parts - HTML, CSS, JS. I could have skipped the JS part easily if I was happy with updating this page being a pain, but I'm not! So the bulk of this code is JS, I'm afraid.
HTML
There's nothing to it really.
<div class="gyg-gallery"><ul id="gyg-list"></ul></div>
<script>
/* The JS Part */
</script>
<style>
/* The CSS Part */
<style>
We make a div that we're going to style later with CSS, and put a list in it with a specific ID. Through-out this I'm calling things gyg-*
, this is just because the Rocket League clip provider I'm using is called GYG.
CSS
The CSS is also pretty simple, but I absolutely had to steal it from elsewhere.
/* gyg embedding list */
.gyg-embed{ left: 0; width: 100%; height: 0; position: relative; padding-bottom: 56.25% }
.gyg-iframe{ left: 0; width: 100%; height: 100%; position: absolute; border: 0;}
.gyg-gallery ul {
display: grid;
grid-template-columns: repeat(2, 1fr);
grid-gap: 12px;
padding: 20px 0;
list-style: none;
}
.gyg-gallery li {
position: relative;
flex-basis: calc(50% - 20px);
}
gyg-embed
and gyg-iframe
just steal the style
portion of the embed HTML provided by this iframe helper website. All this is doing is setting some positional parameters, and I didn't see the point in messing with them.
gyg-gallery ul
and gyg-gallery li
are stolen from the fantastic add-ons put together by Robert Birming for his Bear theme Bearming, and tweaked to manage the gallery layout I want. This really does a ton of work in very little code, and cleans things up beautifully.
Similar to how I would add his gallery to my blog, I added this CSS to my Theme at https://bearblog.dev/[account]/dashboard/styles/
exactly as written in the codeblock.
Javascript
Ok here's where the bullshit happens.
var gyg_list = [
"StalwartUnclaimedDestindoraguille",
"UnthawedPointingKolorado",
"InsaneGlairyGordon",
"GratisSturdiedBlooey",
"ArtyPubicAzura",
"MoneyedRhomboidBigboss",
"InphaseCursedCardqueen",
"SevereNicerFreyacrescent"
];
var target_ul = document.getElementById("gyg-list");
for (let i = 0; i < 8; i++) {
var gyg_code = gyg_list[i];
var src_string = "https://www.gifyourgame.com/ifr/" + gyg_code
var makeIframe = document.createElement("iframe");
makeIframe.setAttribute("src", src_string);
makeIframe.setAttribute("class", "gyg-iframe");
makeIframe.setAttribute("scrolling", "no");
makeIframe.setAttribute("allow", "encrypted-media");
makeIframe.setAttribute("allowfullscreen", true);
var makediv = document.createElement("div");
makediv.setAttribute("class", "gyg-embed");
makediv.appendChild(makeIframe);
target_ul.appendChild(makediv);
}
Originally I was going to implement this the quick and dirty way by just hand-making every single <li>
each time, or god forbid do it in Excel, but then I saw this post by Ruby that got my terrible little script-kiddy brain ticking over.
All of this is just because I'd like to be able to copy-paste in a list of new ids with minimum faff. It's a pretty quick bit of formatting to get the ids out manually, bash them about in notepad++, then paste them in. I'd like to smooth this process out a bit more in future though.
We then grab our empty gyg-list
from the page so we can tinker with it.
We loop over the list of GYG ids, paste each together with the GYG embed link, and get started on the ugly stuff. We make an iframe (basically a window into another webpage), and set some attributes:
"src"
- the url we want, thesrc_string
we made earlier."class"
- this corresponds to the CSS styling we made, so we want"gyg-iframe"
- A bunch of other attributes also taken directly from the embed link I nabbed - I have no idea how much of this is necessary, I haven't tested it.
This gives us the iframe object, but if we just jammed it in the list it'd be... horrific. Lovecraftian in how it'd encompass the page. We want to trap it in a div
- as far as I understand, in HTML that's the crutch that lets you do cool things easily. It just means "a contained bit of the page", and the styling on this div will help set bounds on our iframes.
We make our div with document.createElement("div")
, set it as a "gyg-embed"
class, put the iframe in it, then use target_ul.appendChild(makediv)
to add it to the end of the list we're displaying on the page.
The CSS takes care of the rest and we're... Kind of done?
The Finished Item
You can just go over to the page and look, but in case it breaks here's what it's supposed to look like:
I'm pretty satisfied with the look of it, so won't be looking to improve that at all in the future.
Improvements
There are a few things that are obvious stumbling blocks here:
- The JS is all embedded in the page - it'd be nice to host it elsewhere, just maintain the one file if I use this multiple times. I'm fairly sure I can set this up, but it's another thing to learn so I'm not there yet.
- Getting ids together is still a manual process - I've asked the GYG people if I can get an export of all my clip IDs, but it doesn't seem like that'll happen. I might have to do some more script-kiddy shit to get them, but it'd be super tidy as an end-product - I could also then host that set of ids externally (like the script itself) and maintain the list as I make clips, without human intervention.
- I should definitely do some testing to see how much I actually need those attributes in the JS.
- It looks pretty bad on mobile - I think I'd be able to do some reactive formatting? To have a single column below some screen-width or something. Another thing that's probably simple, but I don't know it!