Browser extensions - Hooking into installs

I'm a full-stack developer from South Africa πΏπ¦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa πΏπ¦. I love writing about JavaScript, HTML and CSS.
Happy to hear you learned something new today Rafael π
In the previous article, we added an option to change the color of our extensions. So far, we have used our main popup view, but we can also leverage the browser options menu. This is a neat trick to clean up extension options, and in this article, I...
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

In some cases, you might want to hook into the install script of your extensions, for instance, when you want to onboard the users with some extra information.
In other cases, you might want to catch updates so that you can redirect users to the latest changelog.
And you can even use this install runtime to set a uninstall URL. This URL will be called whenever the user removes your extension.
If you would like to code along with this article, take the following GitHub branch as your starting point.
This project already has the main setup and uses a background worker, which we'll need.
Note: Read the following article for more information about the background worker.
Now open up the public/background.js file and add the following script.
chrome.runtime.onInstalled.addListener((details) => {
// Do something
});
This registers as soon as your extension is installed.
We can leverage the details, which is a OnInstalledReason type.
The reasons can be:
Let's see how we can use that for catching the install and update actions. We'll add some browser notifications in those cases, as we already have access to those.
chrome.runtime.onInstalled.addListener((details) => {
if (details.reason === 'install') {
chrome.notifications.create({
type: 'basic',
iconUrl: 'icons/icon-48.png',
title: 'Hi there π',
message: 'Welcome to the best extensions you ever installed',
buttons: [{ title: 'Thanks π
οΈ' }],
priority: 0,
});
}
if (details.reason === 'update') {
chrome.notifications.create({
type: 'basic',
iconUrl: 'icons/icon-48.png',
title: 'Thank you',
message: 'For updating this extensions',
buttons: [{ title: 'Cool' }],
priority: 0,
});
}
});
Another cool thing we can do is to set the uninstall URL. This is the URL that the user will be redirected to when they uninstall the extension. It can be helpful to ask them questions about why they are leaving, for instance.
chrome.runtime.onInstalled.addListener(details => {
chrome.runtime.setUninstallURL('https://daily-dev-tips.com');
}
Note: You can also safely add that code inside your
installreason, but this is safer if your extension was previously deployed without it.
And now, when users remove the extension, they are redirected to this link.
You can view the complete code in this GitHub branch.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter