How to Write and Create a Basic Chrome Extension

How to Write and Create a Basic Chrome Extension Featured Image

Chrome is easily the most popular web browser on the planet. According to Statcounter’s desktop browser market share survey, Chrome takes home a whopping 65% of the market in desktop browsers as of late 2017.

With that sort of market-defining power, designing for Chrome has become a priority. The same goes for extensions: Chrome offers the largest user base for extensions by far, with tens of thousands of extensions, themes and apps populating the Chrome Web Store.

If you want to cash in on the trend, you can build your own basic Chrome extension. You’ll just need some basic web development skills (HTML, CSS and Javascript) as well as a teaspoonful of JSON to tie it all together.

We’ll be reviewing the most basic structure required to build a basic Chrome extension in this post. To get an in-depth view of the possibilities available, check out Chrome’s Developer Extension Guide.

Writing a Basic Chrome Extension: Manifest Destiny

For this tutorial we will build a basic Chrome extension that displays a simple popup message when clicked. We’ll need a couple of important files: an icon (“icon.png”), an HTML file (“popup.html”) and the all-important manifest (“manifest.json”). All these files will live inside of a directory with the name of your extension. In this case that’s called “Hello World.”

build-a-chrome-extension-hello-world-files

A Chrome extension is defined by its manifest. This snippet of JSON shows Chrome how to interpret the extension, what files to load, and how to interact with the user.

The manifest file for our very basic extension looks like this:

{
    "manifest_version": 2,

    "name": "Hello World!", 
    "description": "My first Chrome extension.",
    "browser_action": {
        "default_icon": "icon.png",
        "default_popup": "popup.html"
    },
    "permissions": [
        "activeTab"
     ]
}

This manifest file will put an icon in the user’s toolbar that, when clicked, loads the contents of the file named “popup.html.” The following is the nitty-gritty on the rest of the contents:

  • manifest_version tells Chrome what version of the manifest markup you’re working with. For modern extensions, you’ll need to set this to 2.
  • name displays the name the extension will show in Chrome store and “chrome://extensions.”
  • description shows the descriptive text displayed on “chrome://extensions.”
  • browser action loads the icon into the toolbar. It also allows the extension to respond to user input by displaying a tooltip, popup or badge. Check out a full list of everything “browser_action” can do.
  • default_icon shows the path to the icon from the extension’s directory.
  • default_popup shows the path to the file that will load when the extension’s icon is clicked.
  • permissions limits the extension’s functional region. activeTab is the most common, allowing the extension to access the front-most tab. Google provides a list of all the permissions an extension can request.

If you want a deep dive into everything that a Chrome extension’s manifest can declare, check out Google’s docs on extension manifests.

Writing a Basic Chrome Extension: Popups

Now that we’ve written our manifest, we can figure out what our extension will display. That’s up to our “popup.html” file, which will display when the extension loads. Here’s what we’ll be using for this project:


    
        Hello World
    
    
    
        

Hello World!

As you can see, this will render some text styled with CSS. If you want to add Javascript or external CSS to your extension, that requires declaring the scripts in the manifest, under the content_scripts key. Once you have that referenced in the manifest, you can load those scripts as you normally would.

Writing a Basic Chrome Extension: Loading into Chrome

Once we’ve written the basic code for our extension and found a suitable icon, we can load it into Chrome.

1. Navigate to “chrome://extensions” and turn on “Developer Mode” by ticking the checkbox in the upper-right.

build-a-chrome-extension-load-extension-1

2. Click the “Load unpacked extension…” button and select the extension’s directory.

build-a-chrome-extension-load-extension-2

3. Once the extension is loaded, you’ll see its icon in the menu bar.

build-a-chrome-extension-load-extension-3

4. Click on the extension to see its (very simple) effect.

build-a-chrome-extension-load-extension-4

Conclusion: Expanding Your Chrome Extension

Once you’re finished with your extension and are ready to publish, you’ll need to create a Chrome developer account. That’s not an exactly straightforward process, but Google provides complete instructions for publishing your Chrome extension here.

There is obviously so much more you can do with your Chrome extension, but that’s somewhat beyond the scope of this post. If you want to learn more about everything Chrome extensions can do, check out Google’s Chrome Developer Extension Guide.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

Alexander Fox Avatar

Read next

Suzanne Simard sealed paper birch and Douglas fir seedlings inside plastic bags, fed them carbon-14 and carbon-13 dioxide, and nine days later found carbon had crossed between species through fungal threads in the British Columbia soil beneath her boots
A species of jellyfish called Turritopsis dohrnii can revert its adult cells back to a juvenile polyp stage when injured or starving, effectively restarting its life cycle, and biologists have so far failed to identify any natural limit to how many times it can do this.
A Japanese man named Jiroemon Kimura, who lived to 116, was born in 1897 when Queen Victoria still ruled and died in 2013, meaning a single human life personally overlapped with the invention of the airplane, the atomic bomb, the internet, and Instagram
The Hollywood sign originally read HOLLYWOODLAND when it was built in 1923 as a real estate advertisement for a housing development, and it was only meant to stand for 18 months, but nobody ever got around to taking it down and the city eventually adopted it as a landmark
Almost all of the world’s internet traffic does not travel by satellite but through fibre-optic cables lying on the ocean floor, a hidden web of wires crossing the deepest parts of the sea to connect the continents.
People who flip their phone face down on every table aren’t being secretive. They figured out that staying interruptible meant handing their time to whoever rang first
Twitch vs. Facebook Gaming vs. YouTube Gaming: What’s the Best Live Game Streaming Platform?
Chrome Extensions Ownership Transfer is a Direct Threat to You: How to Stay Safe