Google Chrome is the most popular browser on the planet. You use it, your mom uses it, your cat uses it. Sure, it’s got quirks and problems. But if you want to reach a large audience, designing a Google Chrome extension isn’t a bad way to do it. Fortunately, it’s pretty easy to build a Chrome extension.
Chrome extensions are basically web pages. Primarily, they’re Javascript, with some HTML and CSS thrown in and a teaspoonful of JSON. We’ll go through the basic steps to build a Chrome extension, but you can extend this knowledge to just about any of the many powerful functions Google offers. We’ll review the bones that build an extension, and you can flesh it out from there. If you want to check out the documentation to build a Chrome extension, you can find it at Chrome’s Developer Extension Guide.
Build a Chrome Extension: Hello World
A Chrome extension is made of up a couple of files. For our extension, you’ll need a JSON file to tell the extension what to do (manifest.json
), an HTML file that shows what the extension loads (popup.html
), and an icon to represent the extension in the toolbar (icon.png
).
manifest.json
is the heart of any Chrome extension. It tells the extension what resources to load and what kinds of actions it can take. Our simple manifest.json
looks like so:
{ "manifest_version": 2, "name": "Hello World!", "version": "0.1", "description": "My first Chrome extension.", "browser_action": { "default_icon": "icon.png", "default_popup": "popup.html" }, "permissions": [ "activeTab" ] }
Some fields are fairly self-descriptive. Let’s review the others:
manifest_version
is required by Google’s extension framework. It must be set to2
to indicate the current manifest framework.browser action
identifies the type of extension you’re building. A browser action extension places a clickable icon in Chrome’s menu bar, allowing the user to interact with your extension and run its contents.default_icon
shows the path to the icon. It starts from the extensions directory.default_popup
shows the path to the file that will run when the extension is clicked. It will be shown beneath the extension in a popup box.permissions
tells the extension where it’s allowed to operate.activeTab
is the most common, allowing the extension to access information about the front-most tab.
For our extension, we’ll need to drop that manifest into a folder alongside a file named andicon.png
an HTML file named popup.html
. If your extension’s HTML file calls any scripts, you’ll need to include a script file in your extension’s directory. Google’s security rules won’t allow you to embed Javascript (or any other script) in the HTML file itself.
Doing the Bare Minimum
We’ll focus on making the most basic possible extension here. Once you’ve got a handle on the basic structure of a Chrome extension, you can use your web developer chops to code up something awesome.
We’ll use the following for our popup.html
file:
<!doctype html> <html> <head> <title>Hello World</title> </head> <style type="text/css"> body { margin: 10px; } h1 { font-size: 15px; text-align: center; } </style> <body> <h1>Hello World!</h1> </body> </html>
All this does is say hello to the user, nothing more. If you want to do anything more complicated involving user interaction, you’ll need to get your Javascript out. Thanks to Google’s extension security rules, if you want to load some Javascript alongside your HTML, you’ll need to present that in a separate file. You can load it in head
with the script src
tag.
Load Your Extension
Once you’ve finished writing your extension, you can load it into Chrome.
Navigate to chrome://extensions
and turn on Developer Mode by ticking the checkbox in the upper right.
Then click the “Load unpacked extension…” button and select the extension’s directory. Chrome will run a basic debug on your manifest.json
file to make sure it’s up to snuff. If you make a syntax error or leave out important information, you’ll need to fix it before you can successfully load the extension.
Once the extension is loaded, you’ll see its icon in the menu bar. Now we’re getting places!
Click on the extension to see its (admittedly meager) effect.
As you debug and develop your extension, you can click the “Reload” link below the extension (or press Command-R on Mac and Control-R on Windows) to update its files.
Publish Your Extension to the Chrome Store
Once you’ve finished your extension, you’ll need to upload the finished version to the Chrome store. You’ll need to zip all your files together and then process it through your Chrome developer account. Unfortunately, that can be a little complicated. Google has quite a bit of security on their end. Check out the full instructions for publishing your Google extension here.
Conclusion
Remeber, this is only a very basic guide to creating a Chrome extension. As the Chrome Store shows, you can get far more creative with your content. Once you’ve digested all of this, learn more about Chrome extensions at Google’s Chrome’s Developer Extension Guide.
You might be interested in the following posts as well: