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 👌
Attempt no. 2
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.
The Giphy part
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?
Attempt no. 3
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? 🤨
Why is this so hard?
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.
What about faking the textarea itself?
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.
Wisdom of the crowds to the rescue
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.
A change event is not a change event
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 ✌️,
Learned something new? Want to improve your skills?
Join over 10,000 engineers just like you already improving their skills!
Here's how it works 👇
PS: You should also follow me on twitter 👉 here.
It's where I go to shoot the shit about programming.