Here's a fun challenge for ya: Add Giphy support to GitHub comments. How hard can it be?
Indeed, how hard can it be?
45min speedcoding challenge! That's how hard.
All you need is a Chrome extension that injects a little input field next to each comment. Type search into input field, press Enter, talk to Giphy's API, add markdown to the textarea.
Piece of cake.
So on Monday I gave it a shot ...
... and failed miserably.
Chrome extensions are easy in theory, but I wanted to use React for reasons. A different project where it makes more sense and this was a good excuse to figure it out.
Well, it didn't work. It is nigh impossible to convince create-react-app to work as a Chrome Extension.
Until I discovered react-chrome-extension. A boilerplate that comes with everything configured. Build steps, manifest files, full React support. It's wonderful 👌
React problem resolve, surely this is gonna be a piece of cake now. Let's do this!
Friday I tried again
We wasted time drinking beer and shooting the shit about San Francisco, Silicon Valley, and some recent career news on my end (more on that later). Would you say SF is fancy? It's covered in poop, but also seeing a Porsche is so common you don't even notice anymore 🤔
But giphs-for-github worked!
Well ... the operation was a success but the patient died. Oops.
Finding a giph was easy. Giphy's API is great and React Hooks make building small forms a breeze.
- Render a form with an input element
- Use a
setSearchon change events
findGiphyon submit and prevent default
- Use giphy-api to run a search query
- Take the first result
- Insert into textarea
Rendering this next to every textarea is a matter of some classic DOM manipulation. Find all textareas, inject an element in their parent.
Works great 👌
So why does it break the textarea?
target is our textarea node. Its value resides in the
.value property and the DOM spec allows us to change it.
Value is just a string so we
+= another string. Works.
Which brings us to Sunday when I tried to figure this out once and for all.
That was ... fun? Confusing? I learned more than I ever wanted to know about keyboard events? 🤨
The problem is two-fold:
- Pressing Enter causes an error. GitHub's code picks up on the event and gets confused
- GitHub's framework is data-driven. Our shenanigans put reality out of sync with internal state and GitHub JS gets confused
I don't know what they use, but you can imagine why it wouldn't like outsiders messing with things.
Nothing a little change event can't fix.
That's strange. How could a change event not make it work? Most frameworks use the
onChange event ...
But then you look at the list of events GitHub's textarea is listening for and wow that's a lot.
Maybe we need to replay the full keyboard interaction to make this work? Something crazy like this StackOverflow comment lists?
Focus, keydown, beforeinput, keypress, input, change, keyup ... essentially replaying the whole sequence of events as if this were a real user interacting with the website.
It didn't work.
Not only didn't it work, the content didn't even show up. You still have to call
We even tried faking the
input event to make it look like a copy paste. Didn't work. Not even gonna show you the code.
The craziest idea was replacing the whole textarea with a textarea of our own. If you could unrender that one and make it listen to all the same DOM events as the original, that should be seamless, right?
Unfortunately you can't get a list of event listeners for a DOM node.
Yes Chrome lists them in dev tools. You can even use
getEventListeners in Chrome console.
There's no way to get them in real code.
Out of options, out of ideas, I asked the internet for help.
And the internet delivered! Thanks @_tarekdj
change event was the right idea. I was just doing it wrong.
Yay it works!
Sure, the framework error is still there but we can fix that with a submit button instead of pressing Enter. I've tried, it worked.
As long as it doesn't break functionality I'm okay with firing an error.
What's more curious is why @_tarekdj's change event works and mine doesn't.
Let's see, we've got two change events. One works, one doesn't
We're creating a new Event as per MDN docs. It's the most basic of event objects that all the others build off of.
The Event interface represents an event which takes place in the DOM.
The Event Constructor creates an event object of a certain type.
dispatchEvent fires it.
Second argument is an
EventInit dictionary with options. By default events are non-bubbling.
That feels important 🤔
Event bubbling is how events propagate up the hierarchy of DOM elements by the way. That's how a form knows to submit when you press Enter inside an input field 5 levels down.
The working version also creates an event and dispatches it. Except it's using a more old school approach.
Calls createEvent which has a shiny red banner saying we should be using event constructors instead. But it still creates an uninitialized event.
Creates an event of the type specified. The returned object should be first initialized and can then be passed to EventTarget.dispatchEvent.
initEvent call initializes it.
That's not a good sign 😅
What do those booleans we're passing in do? Here's the function signature
So it was all about bubbling! Let's try
It works! 😱
Well that was fun. Thanks for reading.
Happy Monday ✌️,
Here's how it works 👇
And get thoughtful letters 💌 on mindsets, tactics, and technical skills for your career. Real lessons from building production software. No bullshit.
"Man, love your simple writing! Yours is the only newsletter I open and only blog that I give a fuck to read & scroll till the end. And wow always take away lessons with me. Inspiring! And very relatable. 👌"
Have a burning question that you think I can answer? Hit me up on twitter and I'll do my best.
Who am I and who do I help? I'm Swizec Teller and I turn coders into engineers with "Raw and honest from the heart!" writing. No bullshit. Real insights into the career and skills of a modern software engineer.
Want to become a true senior engineer? Take ownership, have autonomy, and be a force multiplier on your team. The Senior Engineer Mindset ebook can help 👉 swizec.com/senior-mindset. These are the shifts in mindset that unlocked my career.
Curious about Serverless and the modern backend? Check out Serverless Handbook, for frontend engineers 👉 ServerlessHandbook.dev
Want to Stop copy pasting D3 examples and create data visualizations of your own? Learn how to build scalable dataviz React components your whole team can understand with React for Data Visualization
Did someone amazing share this letter with you? Wonderful! You can sign up for my weekly letters for software engineers on their path to greatness, here: swizec.com/blog
By the way, just in case no one has told you it yet today: I love and appreciate you for who you are ❤️