How to develop a chrome extension to stop those annoying mailto link popups
Background to the problem
I graduated from the Copenhagen Institute of Interaction Design in December, and since January 2019, I am designing my portfolio website and going through job boards to find available opportunities for me to work. While going through all these job posts, in most of them if not every, they ask you to send an email to the recruiter.
And as soon as you click that link, BAAM! A new window opens up your email client app. It annoys me so much as, on my MacBook, I never use the default mail app. I usually use google chrome, to log in to mail accounts, be it personal or work email.
It can get really annoying when every 4th link you visit opens up the mail client again and you have to discard the email written or else it will be saved in the drafts automatically.
My intent always is to copy that email so that I can use it in a relevant email client. And I think this is a bad user experience that has been carried out for long, that we direct a user to a default mail client when we don't use a single email ID for all purposes. A person can have personal or work email separate and through different email services.
So, I decided to make a chrome extension for personal use, which will do the following:
- Stop the default behaviour of opening the links in an app.
- Copy the link instead to the clipboard and notify me that the link has been copied.
- Save the link for future usage.
This medium article by Jake Prins also helped me gain a better understanding. If you are a complete newbie, I will recommend you to go through both of them.
What is Chrome Extension usually made up of?
- Manifest.json file
- Background script:
Runs in the extension process, exist for the lifetime of the extension, and only one instance of it at a time is active
- Content scripts:
The HTML page that opens when you click the icon of the extension in your browser.
For our extension, the setup looks something like this:
Let’s start by creating a manifest JSON file which will work as the backbone for our project and we will declare all the images, pages, permissions, background and content-script here.
As you can see in the manifest code below, we have declared the following things:
We need permissions from chrome to access the active tabs, chrome storage (to save emails), notifications (to notify the user when email is saved) and to have access on all URLs.
We have one script background.js which we declared here.
all_frames true- is used to make sure that our extension works in iFrames too and we will stop links from there also.
js- is used to declare all the content scripts which we will need access to.
This refers to the extension button in the chrome address bar and what HTML page should be opened when you click it, where are the icons are declared here.
This file is used to stop all the link opening the email client, and instead copy the email to the clipboard. This file also sends the email to the background.js to store the email into chrome storage.
Link click function gets the email using regex and then passes that email to a function copytoClipboard which in turn, copies the link to clipboard and passes the link to background.js by using :
Here we are passing two things:
- type = saveEmail
For background.js to know where the request came from (in-case, in future we need to pass multiple messages to the background.)
- msg = str
The email ID to save
You can send a message to background script from content script in order to achieve some result.
We use Request.type in order to differentiate between requests from different pages. Here, in this case, we get the ‘saveEmail’ from popup.js which tells us to save the email. We save the email to the chrome storage using the saveChanges method. We also create a notification for that email is saved.
Now let us create an HTML page that will be loaded when you click the extension icon.
Note: we have used jQuery in the project. So make sure to download that in the root folder.
Now last step:
It took me less than a day to learn and develop this extension. I am sure I might not have used the best practices in some places but this solves one of my biggest annoyance for now.
The code is available on Github :-)
A chrome extension to stop the annoying behaviour of Mailto Links pop-ups of email clients. - raunaqpatel/mailtoStop
Also, I published the extension if you are not a developer and want to use this :-)