Académique Documents
Professionnel Documents
Culture Documents
2
search
For the purpose of this tutorial we will use Firefox, though most
concepts carry over to other browsers as well.
70 19 120
https://dev.to/yechielk/writing-your-first-browser-extension-part-1-d5e 1/16
06/08/2019 Writing Your First Browser Extension Tutorial - Part 1 - DEV Community 👩💻👨💻
Were you ever looking at a web page and wished you could change 2
search
something about it? Maybe there were too many ads? Perhaps you
didn't like the font? Could be the color theme was too bright?
Whatever the case is if the change you wish to see is something that
can happen in the browser (i.e., it's a change on the front-end that
doesn't involve any of the back-end) then chances are you can write a
browser extension to do it.
sample-extension
└───images
│ └───icon-48.png
│ └───icon-96.png
│ └───image1.jpg
│ └───image2.jpg
│
└───manifest.json
└───sample-extnsion.js
70 19 120
https://dev.to/yechielk/writing-your-first-browser-extension-part-1-d5e 2/16
06/08/2019 Writing Your First Browser Extension Tutorial - Part 1 - DEV Community 👩💻👨💻
Thesearch
extension lives in a folder which I called sample-extension . 2
The actual code for your extension lives in a JavaScript file that I
called sample-extension.js .
Finally, any images you wish to include, including the icons you will
use to promote your extension in the browser add-on store, can go into
a subfolder that I called images .
The Manifest.
The manifest is at the heart of your extension; it's how your browser
knows which code to run, and when and how to run it.
{
"manifest_version": 2,
"name": "<EXTENSION-NAME>",
"version": "1.0",
70 19 120
https://dev.to/yechielk/writing-your-first-browser-extension-part-1-d5e 3/16
06/08/2019 Writing Your First Browser Extension Tutorial - Part 1 - DEV Community 👩💻👨💻
"icons": {
"48": "<URL TO AN ICON>",
"96": "<URL TO AN ICON>"
},
"content_scripts": [
{
"matches": ["<URL MATCHER>"],
"js": ["<RELATIVE PATH TO A JS FILE>"]
}
]
}
That looks like a lot! Let's take a look at the relevant parts:
https://dev.to/yechielk/writing-your-first-browser-extension-part-1-d5e 4/16
06/08/2019 Writing Your First Browser Extension Tutorial - Part 1 - DEV Community 👩💻👨💻
There are other keys you can provide that represent different functions
your extension can do. They are listed here.
So now that we have all of the information we need let's get coding!
We will start with the simplest extension I could think of, the "Hello
World" of browser extensions so to speak.
mkdir first-extension
cd first-extension
touch manifest.json
70 19 120
https://dev.to/yechielk/writing-your-first-browser-extension-part-1-d5e 5/16
06/08/2019 Writing Your First Browser Extension Tutorial - Part 1 - DEV Community 👩💻👨💻
Now,
search using your favorite editor open up the manifest we just 2
created and paste in the following JSON (I'm usually not a fan of
copy/pasting code from tutorials; I think by taking the time to
type things out you build muscle memory and retain information
better, but I won't make you type out all this JSON yourself):
{
"manifest_version": 2,
"name": "first-extension",
"version": "1.0",
"content_scripts": [
{
"matches": ["*://*.dev.to/*"],
"js": ["first-extension.js"]
}
]
}
https://dev.to/yechielk/writing-your-first-browser-extension-part-1-d5e 6/16
06/08/2019 Writing Your First Browser Extension Tutorial - Part 1 - DEV Community 👩💻👨💻
touch first-extension.js
Near the top right corner click on the button that says "Load
Temporary Add On..."
70 19 120
https://dev.to/yechielk/writing-your-first-browser-extension-part-1-d5e 7/16
06/08/2019 Writing Your First Browser Extension Tutorial - Part 1 - DEV Community 👩💻👨💻
Navigate
search to the folder we created for our extension and select 2
alert("Hello DEV!")
https://dev.to/yechielk/writing-your-first-browser-extension-part-1-d5e 8/16
06/08/2019 Writing Your First Browser Extension Tutorial - Part 1 - DEV Community 👩💻👨💻
Go back
search to "about:debugging" and look at the temporary 2
Troubleshooting
If you have trouble getting the alert to pop up, or even getting the
extension to load double check that your manifest is valid JSON with
no syntax errors (missing commas, open brackets, etc.). Make sure
there are no typos in the "js" filename and that the name in the
manifest matches the actual name of the file.
https://dev.to/yechielk/writing-your-first-browser-extension-part-1-d5e 9/16
06/08/2019 Writing Your First Browser Extension Tutorial - Part 1 - DEV Community 👩💻👨💻
That was cool! But let's write an extension that actually does
something useful. How about an extension that will ping you
after being on Twitter for 10 minutes and remind you to take a
mental health break.
Let's head back to our manifest and change the value of our
"matches" key from the DEV website to Twitter:
"content_scripts": [
{
- "matches": ["*://*.dev.to/*"],
+ "matches": ["*://*.twitter.com/*"],
"js": ["first-extension.js"]
}
]
70 19 120
const interval = 600000 // 600,000 milliseconds = 10 minutes
https://dev.to/yechielk/writing-your-first-browser-extension-part-1-d5e 10/16
06/08/2019 Writing Your First Browser Extension Tutorial - Part 1 - DEV Community 👩💻👨💻
2
search
But I find it more readable to break up the interval into its constituent
parts. This will make it easier to understand when you come back to
the code in a few weeks:
Next let's write the function that will run every ten minutes. We
want a function that pops up an alert to tell us to get off Twitter.
It should look something like this:
function reminder() {
alert("Get off Twitter!")
}
Now we have all the parts we need. The only thing left is to put
it all together and call our setInterval function:
setInterval(reminder, interval)
70 19 120
https://dev.to/yechielk/writing-your-first-browser-extension-part-1-d5e 11/16
06/08/2019 Writing Your First Browser Extension Tutorial - Part 1 - DEV Community 👩💻👨💻
2
search
https://dev.to/yechielk/writing-your-first-browser-extension-part-1-d5e 12/16
06/08/2019 Writing Your First Browser Extension Tutorial - Part 1 - DEV Community 👩💻👨💻
So search
now we have an actual, useful browser extension, but chances are 2
that when you think of interesting browser extensions the ones that
come to mind are extensions that actually change things on web pages.
In part two of this series we will build a fun extension that does just
that while attempting to fix the broken hiring system in tech and, as a
bonus, includes cats!
PREVIEW SUBMIT
Aug 5
Sandro Volery
This is honestly great, browser extensions are about the only thing i havent looked into yet to
work on for a side project, simply because i didn't have any ideas :D this article kind of got me
interested so now i got to find ideas.. does anyone have a cool idea?
2 REPLY
70 19 120
https://dev.to/yechielk/writing-your-first-browser-extension-part-1-d5e 13/16
06/08/2019 Writing Your First Browser Extension Tutorial - Part 1 - DEV Community 👩💻👨💻
25
search Aug
Yechiel Kalmenson
1 REPLY
Aug 5
KristijanFištrek
2 REPLY
Aug 5
Yechiel Kalmenson
2 REPLY
Aug 5
KristijanFištrek
2 REPLY
Aug 5
Muhammad
2 REPLY
Aug 5
Yechiel Kalmenson
2 REPLY
70 19 120
https://dev.to/yechielk/writing-your-first-browser-extension-part-1-d5e 14/16
06/08/2019 Writing Your First Browser Extension Tutorial - Part 1 - DEV Community 👩💻👨💻
25
Aug
search
Eugen Cazacu
great article! thanks for clarifying the concept of browser extensions and how easy they are to
write
2 REPLY
Aug 5
Yechiel Kalmenson
1 REPLY
70 19 120
Another Post You Might Like
https://dev.to/yechielk/writing-your-first-browser-extension-part-1-d5e 15/16
06/08/2019 Writing Your First Browser Extension Tutorial - Part 1 - DEV Community 👩💻👨💻
2
search
A Developers Guide to Getting Fit
Eric Bishard
How I lost 30 lbs and kept if off while juggling a career as an engineer and
developer advocate.
371 47
70 19 120
https://dev.to/yechielk/writing-your-first-browser-extension-part-1-d5e 16/16