Recruiter Codes Chrome Extension pt. 2 — Conquering new lands! (Or websites)

Previously on “Recruiter Codes Chrome Extension”: A LinkedIn Fairy(-tale)!
Check out “Season 1”, too:
 Tech Recruiter Tries Coding, pt 1pt 2, and pt 3!

Hello again from the messy-but-exciting world of coding as a non-coder!

This time I will dig a bit deeper into the Chrome Extension I talked about in the first article, showing how I expanded it to work on other websites, and made it more useful by making it exchange more data with Google Sheets.

WARNING: I realize the series is putting more and more emphasis on coding; if you think it should be different, please don’t hesitate to be overly critical in comments, being either constructive or destructive: all welcome!

Only if it’s empty!

A brief intro/recap for those who landed here first.

I have very little coding experience, especially with modern languages: this is my first coding project in 20 years, so basically the first as an adult. 😅

In the past 3–4 months though, I experimented and learned a lot with Google Apps Script (which is JavaScript, and can even be written in TypeScript, as I did) and soon after with Chrome Extensions (also in TypeScript), coding this one straight after/during this short uDemy course.

The whole point of this extension is to send profile data from websites like LinkedIn to a Google Sheet of choice, classified/categorized as wanted.

Due to my background/context above, and the fact the main objective was to save time and increase productivity, I figured it was faster to split this in two: the extension just grabs data and sends it via simple HTTP requests, while an intermediary web app, already authorized to work as a proxy of my Google account, pushes and formats the data into Google Sheets.

This perhaps suboptimal choice was very fast to code because it didn’t require me to learn OAuth or an extra Sheets API: authorization is basically skipped (and in fact it’s ok only for internal usage: don’t try to distribute something like this to the world!), and interaction with Sheets is done via regular Google Apps Script, which I already knew, instead of another API.

Cute, but I didn’t want to to be THIS cute… 😂

As a thought experiment (or who am I kidding, just for fun) I envisioned these two components as fantasy characters associated to emojis, so we have: 🧚‍♀️Sylph, the LinkedIn fairy, and 🧜‍♂️ Lancer, the Google Web App.

I’ll try to keep the fantasy to a minimum in this second article, but if you see weird names and emojis in the code snippets, now you know why… 😄

Supporting multiple websites in V3 Chrome Extensions. A liturgy.

Why “liturgy”? Well, you will see it does indeed look like a ritualistic thing, if you just follow the official documentation, mainly because of all the places in the code one has to add the same information in, but as you will see, there are multiple ways around it.

First of all, since we’re talking about an extension that works on bookmarks created (or bookmarks.onCreated, speaking in Chromesque) we indeed make it a bit more difficult than it should be, to support multiple websites.

Normally, in V3 Manifest extensions, you would have 2 steps.
With bookmarks, we got 3.

1- Write the “matches” for your content scripts, in your manifest.json file

The asterisks are great but careful: don’t select pages you can’t process. Why jobs, you ask? Hold that thought!

In my case I ended up writing more in the Manifest, you will see why later.

2- Use declarativeContent.PageStateMatchers in the service worker script

So repetitive… Yes, you will see a refactored version of this later!

This lets you simulate the behavior of V2 extensions, with icon disabled or enabled depending on the website. Just takes more code than before…

3- If you have to react to bookmarks selectively, update your listener

You can see the rest of this listener’s code on pt. 1 of this series.

Again so much repetition… How to get rid of all this?

Enter array (flat)mapping from objects

My first solution was a cool exercise, but what it did was only avoiding repetition within the service worker, between bookmarks and awakening.

Pop culture reference, and continued usage of my fantasy metaphors, but LOOK at THAT LAST LINE..! 🤯

The object allows to avoid repetition of websites for every internal prefix we want to support, and the array created from the object can be used by both PageStateMatcher and bookmark listener, after these changes:

Now that’s a refactor! This is equivalent to the sum of the code in points and 3, above.

Cool and line-saving, especially the “some” array method, but in the end not so useful: still makes me modify both manifest and code every time I want to support a new website, or even just a new suffix in a website’s url.

Enter getManifest!! (And array mapping from the manifest)

Magical indeed!

With this single line, we replace the object and array from before, without having to change anything else used in the previous refactoring.
Bye bye to 6 lines of code, and hello maintainability: now I can change bookmarks AND extension behavior by changing the manifest only. 👍

But I wanted the extension to be more manageable than a single block that contains all the functions for all the websites, so I went a bit farther, and did the following on the manifest.

Modular content script loading: hot or not?

Not sure it’s the best solution: it adds another thing to maintain kind of separately (in the same file at least), but allows to load only the function(s) needed by each website, instead of all functions in all websites.

This won’t change the loading time by much, since nowadays this is so quick you can’t even tell, but it “compartmentalizes” stuff, which is cleaner.

If not seeing a benefit, you can still use multiple files, and just load them all on all matches, adding them to the first array of “js”, in point above.

Adding checks for doubles: a little DIY API, and caching system!

As mentioned previously, this extension is a time saver, but as it was in its first version it suffered from a glaring defect: it could let me add the same profile multiple times in my sheet/database, without even alerting me, so I had to rely on my “DBfication of sheets” to manage that.

In theory, it’s not a big deal, but in practice, it was not as efficient as I wanted this to be, because I would still be curious to go and check if it was double, or keep a tab to check before adding… Precious seconds each time!

So the solution I thought was having my sheet scripts themselves send indexing data to Lancer (the web app talking to the extension), while Sylph (the extension) would ask this data at least the first time it loads a relevant page, to then cache the data and quickly check for doubles before usage.

It was a nice exercise, but involves too many steps/components to describe here without boring you to death, so I’ll talk about the extension side only.

And in fact I don’t think I did. Maybe. Not sure… Anyway, that’s a topic for the next article! 😅

Content script changes: one line! (Although a bit compressed…)

First of all, am I the only one who compresses functions like this?

Yes, I use emojis as object keys, and I think it’s awesome… Change my mind! 😋

This just tells the content script to send a message to the service worker on page load, including the URL of our web app (which is on a separate file in order to easily keep it out of the GitHub repository, and only content scripts can be in multiple files) and the URL of the page visited.

Background script changes: messaging, caching, checking, UI. Oh my!

A segment of the onMessage listener, at the end of the service worker code. It reuses the message it receives.

Here we have to find out the tab that “called in”, since the content script doesn’t know it, and then we do the following:
– Start the animation (in a new way compared to last time, I’ll get to that!)
– Console.log for good measure.
– Check the “Stash” (AKA cache): if ready, fire a checkID function using the cached data. Otherwise, grab the data from Lancer (“whose” URL is in Msg[‘🧜‍♂️’], of course), and check using that.

Here is how the cache/Stash looks like:

I wanted a treasure chest emoji, had to make do with a file cabinet!

It’s more type declaration than anything else, because what this will contain is also an association of tab number/id, and entry id, which is determined by the checkID function, below.

I also made it with less indentation, but at the cost of one more condition checking: that’s my red line! 🚩

Here a cool thing is it can both initialize the cache and use it, depending on the argument type. If it doesn’t receive an array, it means it’s the first time, so it will parse the data into an array, store it in the Stash/cache, and put “✅” to true, so we know it’s good to be used. (Can’t resist emojis in keys!)

Then it will determine if we have the current entry in the database, and report accordingly with the custom “Shout” function.

If it finds it, it adds to the Stash the index of the entry in the database (directly linked to rows on Google Sheet), with the (unique) tabID as key.

Finally, this is the Shout function used for our minimal “UX”:

I love the smell of ternary operators in the morning…

This whole slew of ternary conditionals (I discovered this syntax rather recently, and LOVED it ever since) basically does the same things but in different ways, depending on the success parameter:

1- Console logging or warning, depending on the case.
2- Setting the extension title, which is actually what appears in tooltip, and my replacement for an actual HTML popup (it even changes in real time!)
 Stopping the flying fairy animation, since whether this comes as a result of ID checking or sending data, it will come after an animation started.
4- Setting the icon either back to normal, or to the “warning” one, which is now also used to show we have that person in the DB already, and there’s no need to use the extension at all.

BONUS: Refactoring the icon animation code to be less TERRIBLE

Although nobody called me out on the craziness of using a separate object to keep track of the animation’s state, I started thinking it was a really lazy amateurish solution, and didn’t this associated to FusionWorks in any way…

Although that was already an improvement upon my first solution, to avoid war declarations…😆

So here’s a refactor that at least encapsulates everything:

Here the emojis REALLY make sense. Right? Right…??

Apart from emojis now being used for functions too, because Play and Stop “buttons” for the animation were irresistible to me, this is cool because from the same object I keep track of the state (from the Tabs member, which replicates the old tabId: animation-frame object) and I can start and stop the animation: no more modifying an object AND calling a function.

In the end, the code is a bit longer, but I guess it’s for a good reason: although technically we still have a global object, now it engulfs all the functions that use it, conceptually and practically.

Also, I don’t know how I didn’t think of this before, but it’s much better to return on a negative condition rather than do everything on a positive one: it avoided a whole level of indentation! ✌️

Is this the end? Kinda! — The lessons learned moment is here

This time around I kept things more technical perhaps, but I think there are still some “philosophical” lessons to be learned here.

First: even a small project can scale fast, so thinking about maintainability is not the waste of time I always thought it was!
Adding support for a website or even a new type of usage altogether takes very little effort now: a separate file of 15–25 lines of code to parse a new source (around one data field every 2 lines, or 3 if complex) and 1 place to modify in the rest of the codebase (the manifest.json), instead of messing around in all files, having to edit 3–4 functions all over the place.

Second: communication between components is key! Whether it’s components of the extension itself (content scripts <-> service worker) or the external ones (my web app, and/or the Google Sheet it talks to), communication is essential. As with my articles, I might be a bit verbose in it, but that’s where it’s warranted, to avoid forgetting what does what.

And finally: coding your tools means a lot of extra productivity! Sure, it takes time at first, but to be honest now it’s nearly second nature, and adding a function or changing it can take as little as a couple of minutes.
A trick that worked for me is writing “pseudo-code” on physical paper first, and then on screen. It might not work for everyone, but new things come out faster to me this way. That’s how I did my first “big” program 20 years ago, and how I did the best parts of this, which is not big now, but growing!

BONUS: emojis are hyper-descriptive names in one character! I’m really glad the language allows them for object keys/methods, plus any strings.

In the next and last article of this series, I’ll dig a bit more into the Lancer web app, how I made it a bit more secure and “API-like”, and how I extended its functionality to be a bit more than just an intermediary.

Then I’ll also touch upon a new functionality for the Sylph extension, which made it useful outside of Recruitment as well…

Happy coding until then! 🦾

Previously on “Recruiter Codes Chrome Extension”: A LinkedIn Fairy(-tale)!

Check out “Season 1”, too: Tech Recruiter Tries Coding, pt 1pt 2, and pt 3!