It’s been years since I started using userscripts, at first as a hobby, to automate things I wanted to do in the browser every day. The goal was to make them as simple as a single click or add a feature I thought was essential. I soon realized this tool is useful in professional settings, too, so I think everyone who uses a browser can benefit from this technology.
Before explaining the technology in detail, I’d like to show you a real example implemented in the project I’m part of at Seat Code. The project consists of more than fifteen microservices hosted in an organization with about 1.7k repositories. GitHub’s interface is good, but it wasn’t ideal for what we needed to do every day.
I decided to create a new dashboard summarizing our projects and providing shortcuts to the sections we used most. Here’s how it turned out:
It’s hard to calculate exactly how much time this new dashboard saves me, but I estimate it cuts the time in half every time I need to access it—and I use it several times a day.
The first thing you need to know is that the language used is JavaScript, so these files use the .user.js extension.
The second is that you need to install an extension to inject their content. Here’s a list of the most popular
browsers:
The idea behind the extension is to inject the script into the pages where it’s needed and run it after they load.
Once it’s installed, create your first userscript by right-clicking the extension’s icon. A context menu will appear with the option “Add new script…”
A new window will open with a text editor and a blank script template.
// ==UserScript==
// @name New Userscript
// @namespace https://sergiosusa.com
// @version 0.1
// @description try to take over the world!
// @author You
// @match https://sergiosusa.com
// @grant none
// ==/UserScript==
(function() {
'use strict';
// Your code here...
})();
I’ll explain the two parts of this template in detail:
@name: the script’s name.@namespace: a unique identifier.@version: the script version, required for releasing updates.@description: a short description of the script’s functionality.@author: the script’s author.@match: specifies which sites the script will run on.@grant: indicates whether the script needs any special permissions.(function() { ... })();: this is an IIFE (Immediately Invoked Function Expression), meaning a function that
runs immediately after it is defined. In this case, it runs after the page finishes loading, although that can
be changed if necessary.'use strict': enables JavaScript’s strict mode to prevent common errors and bad practices.// Your code here...: our code goes here.To illustrate, here’s a very simple example replacing the comment described above:
const navbarBrand = document.querySelector(".navbar-brand img");
if (navbarBrand) {
navbarBrand.style.transition = "transform 0.8s ease";
navbarBrand.style.transform = "rotate(180deg)";
}
If you save it and then visit the URL specified in the @match tag, the extension will display a red number showing
how many scripts have been injected into the current page.
If you try this example, you’ll see that I’ve changed the appearance of one of the elements on the website.
From here on, the only limit is your imagination. To show you what these scripts can do, here’s another real-world example:
This script adds buttons, charts, and some behavior to the world’s most famous video game marketplace: Steam.
I won’t repeat myself by explaining the code or what it does; you can find that in the description and in the project’s public code.
What I do want to share and explain is a skeleton I’ve created for organizing script code based on the URLs where elements or behaviors are injected. I recommend using it when:
On the other hand, for simple, very focused scripts, I don’t think this extra complexity is necessary.
(function () {
'use strict';
try {
let siteImproved = new SiteImproved();
siteImproved.render();
} catch (exception) {
alert(exception);
}
})();
It creates an instance of the SiteImproved class/function, which inherits from Renderer, and calls the render
method to start injecting code for that particular site.
SiteImproved class has two attributes inherited from Renderer. They are where new instances of classes that
inherit from Renderable (something that can be rendered) should be declared:
rendererList: a list of classes that modify specific pages.globalRenderList: a list of functions that are applied globally.RenderableExampleFunction class inherits from Renderable. Its handlePage attribute must be completed with
a regular expression matching the page(s) where it will run, and its render function must be implemented with the
code responsible for making the changes.Renderer class contains all the rendering logic configured by the class that inherits from it—in this example,
SiteImproved.Renderable class implements the logic for checking whether a class can be rendered on a specific page using a
regular expression.This design lets us add multiple rendering functions without changing the base code, in a modular and organized way.
In short, the script is structured as a rendering framework that looks for specific functions to modify the page. Its purpose is to organize how visual and logical elements are executed according to certain rules.