We'll do so by implementing a UI element we call a browser action, which allows us to place a clickable icon right next to Chrome's Omnibox for easy access. Clicking that icon will open a popup window filled with an image derived from the current page, which will look something like this:
If you'd like to follow along at home (and you should!), create a shiny new directory on your computer, and pop open your favourite text editor. Let's get going!
Something to Declare
The very first thing we'll need to create is a manifest file namedmanifest.json
. This manifest is nothing more than a metadata file
in JSON format that contains properties like your extension's name,
description, version number and so on. At a high level, we will use it to
declare to Chrome what the extension is going to do, and what permissions it
requires in order to do those things. To learn more about the manifest, read
the Manifest File Format documentation.In our example's manifest, we will declare a browser action, the activeTab permission to see the URL of the current tab, and the host permission to access the external Google Image search API.
{ "manifest_version": 2, "name": "Getting started example", "description": "This extension shows a Google Image search result for
the current page", "version": "1.0", "browser_action": { "default_icon": "icon.png", "default_popup": "popup.html" }, "permissions": [ "activeTab", "https://ajax.googleapis.com/" ] }
Go ahead and save that data to a file named
manifest.json
in the
directory you created, or download a copy of manifest.json from our sample repository .
Resources
You probably noticed thatmanifest.json
pointed at two resource
files when defining the browser action: icon.png
and
popup.html
. Both resources must exist inside the extension
package, so let's create them now:
-
icon.png
will be displayed next to the Omnibox, waiting for user interaction. Download a copy of icon.png from our sample repository , and save it into the directory you're working in. You could also create your own if you're so inclined; it's just a 19px-square PNG file.
-
popup.html
will be rendered inside the popup window that's created in response to a user's click on the browser action. It's a standard HTML file, just like you're used to from web development, giving you more or less free reign over what the popup displays. Download a copy of popup.html from our sample repository , and save it into the directory you're working in.
The actual logic of rendering the content of the popup is implemented by popup.js. You are encouraged to read the elaborate comments in this file to learn more about the logic.
Download a copy of popup.js from our sample repository , and save it into the directory you're working in.
icon.png
,
manifest.json
,
popup.html, popup.js.
The next step is to load those files into Chrome.
Load the extension
Extensions that you download from the Chrome Web Store are packaged up as.crx
files, which is great for distribution, but not so great for
development. Recognizing this, Chrome gives you a quick way of loading up your
working directory for testing. Let's do that now.
-
Visit
chrome://extensions
in your browser (or open up the Chrome menu by clicking the icon to the far right of the Omnibox:and select Extensions under the Tools menu to get to the same place).
-
Ensure that the Developer mode checkbox in the top
right-hand corner is checked.
-
Click Load unpacked extension… to pop up a
file-selection dialog.
-
Navigate to the directory in which your extension files live, and select
it.
chrome://extensions
in your browser to load it.
If the extension is valid, it'll be loaded up and active right away! If it's invalid, an error message will be displayed at the top of the page. Correct the error, and try again.
Fiddle with Code
Now that you've got your first extension up and running, let's fiddle with things so that you have an idea what your development process might look like. For example, let's set a tooltip on the browser action button.According to the browserAction documentation, tooltips can be set by specifying the
default_title
key in the manifest file. Open
manifest.json
, and add the default_title
key to the
browser_action
.
Make sure that the JSON is valid, so quote the key and add a comma where necessary.
{ ... "browser_action": { "default_icon": "icon.png", "default_popup": "popup.html", "default_title": "Click here!" }, ... }
The manifest file is only parsed when the extension is loaded. If you want to see the previous changes in action, the extension has to be reloaded. Visit the extensions page (go to chrome://extensions, or Tools > Extensions under the Chrome menu), and click Reload under your extension. All extensions are also reloaded when the extensions page is reloaded, e.g. after hitting F5 or Ctrl-R.
Once you've reloaded the extension, hover over the browser action badge to see the new tooltip!


What next?
You now know about the manifest file's central role in bringing things together, and you've mastered the basics of declaring a browser action. That's a great start, and has hopefully gotten you interested enough to explore further. There's a lot more out there to play around with.-
The Chrome Extension Overview backs up a bit,
and fills in a lot of detail about extensions' architecture in general,
and some specific concepts you'll want to be familiar with going forward.
It's the best next step on your journey towards extension mastery.
-
No one writes perfect code on the first try, which means that you'll need
to learn about the options available for debugging your creations. Our
debugging tutorial is perfect for that,
and is well worth carefully reading.
-
Chrome extensions have access to powerful APIs above and beyond what's
available on the open web: browser actions are just the tip of the
iceburg. Our chrome.* APIs documentation will
walk you through each API in turn.
-
Finally, the developer's guide has dozens of
additional links to pieces of documentation you might be interested in.
0 comments: