Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / common / extensions / docs / templates / articles / getstarted.html
blobe1227a0dec1e61188a15bd0a665f159d3fbee536
1 <h1>Getting Started: Building a Chrome Extension</h1>
3 <p>
4 Extensions allow you to add functionality to Chrome without diving deeply
5 into native code. You can create new extensions for Chrome with those core
6 technologies that you're already familiar with from web development: HTML,
7 CSS, and JavaScript. If you've ever built a web page, you should feel right at
8 home with extensions pretty quickly; we'll put that to the test right now by
9 walking through the construction of a simple extension that will fetch an
10 image from Google using the current page's URL as a search term.
11 </p>
13 <p>
14 We'll do so by implementing a UI element we call a
15 <a href="browserAction">browser action</a>, which allows us to place a
16 clickable icon right next to Chrome's Omnibox for easy access. Clicking that
17 icon will open a popup window filled with an image derived from the current
18 page, which will look something like this:
19 </p>
21 <img src="{{static}}/images/gettingstarted-preview"
22 width="558"
23 height="264"
24 alt="Chrome with an extension's popup open.">
26 <p>
27 If you'd like to follow along at home (and you should!), create a shiny new
28 directory on your computer, and pop open your favourite text editor. Let's get
29 going!
30 </p>
32 <h2 id="declaration">Something to Declare</h2>
34 <p>
35 The very first thing we'll need to create is a <dfn>manifest file</dfn> named
36 <code>manifest.json</code>. This manifest is nothing more than a metadata file
37 in JSON format that contains properties like your extension's name,
38 description, version number and so on. At a high level, we will use it to
39 declare to Chrome what the extension is going to do, and what permissions it
40 requires in order to do those things. To learn more about the manifest, read
41 the <a href="manifest">Manifest File Format documentation</a>.
42 </p>
44 <p>
45 In our example's manifest,
46 we will declare a <a href="browserAction">browser action</a>,
47 the <a href="activeTab">activeTab permission</a> to see the URL of the current
48 tab, and the host <a href="declare_permissions">permission</a> to access the
49 external Google Image search API.
50 </p>
52 <pre data-filename="manifest.json">
54 "manifest_version": 2,
56 "name": "Getting started example",
57 "description": "This extension shows a Google Image search result for the current page",
58 "version": "1.0",
60 "browser_action": {
61 "default_icon": "icon.png",
62 "default_popup": "popup.html"
64 "permissions": [
65 "activeTab",
66 "https://ajax.googleapis.com/"
69 </pre>
71 <p>
72 Go ahead and save that data to a file named <code>manifest.json</code> in the
73 directory you created, or
74 <a href="examples/tutorials/getstarted/manifest.json" download="manifest.json">
75 download a copy of <code>manifest.json</code> from our sample repository
76 </a>.
77 </p>
79 <h2 id="resources">Resources</h2>
81 <p>
82 You probably noticed that <code>manifest.json</code> pointed at two resource
83 files when defining the browser action: <code>icon.png</code> and
84 <code>popup.html</code>. Both resources must exist inside the extension
85 package, so let's create them now:
86 </p>
88 <ul class="imaged">
89 <li>
90 <p>
91 <img src="{{static}}/images/gettingstarted-icon.png"
92 width="127"
93 height="127"
94 style="float:right"
95 alt="The popup's icon will be displayed right next to the Omnibox.">
96 <code>icon.png</code> will be displayed next to the Omnibox, waiting for
97 user interaction.
98 <a href="examples/tutorials/getstarted/icon.png" download="icon.png">
99 Download a copy of <code>icon.png</code> from our sample repository
100 </a>, and save it into the directory you're working in. You could also
101 create your own if you're so inclined; it's just a 19px-square PNG file.
102 </p>
103 </li>
104 <li>
106 <code>popup.html</code> will be rendered inside the popup window that's
107 created in response to a user's click on the browser action. It's a
108 standard HTML file, just like you're used to from web development, giving
109 you more or less free reign over what the popup displays.
110 <a href="examples/tutorials/getstarted/popup.html" download="popup.html">
111 Download a copy of <code>popup.html</code> from our sample repository
112 </a>, and save it into the directory you're working in.
113 </p>
115 The actual logic of rendering the content of the popup is implemented by
116 <a href="examples/tutorials/getstarted/popup.js">popup.js</a>. You are
117 encouraged to read the elaborate comments in this file to learn more
118 about the logic.<br>
119 <a href="examples/tutorials/getstarted/popup.js" download="popup.js">
120 Download a copy of <code>popup.js</code> from our sample repository
121 </a>, and save it into the directory you're working in.
122 </p>
123 </li>
124 </ul>
127 You should now have four files in your working directory:
128 <a href="examples/tutorials/getstarted/icon.png" download="icon.png"><code>icon.png</code></a>,
129 <a href="examples/tutorials/getstarted/manifest.json" download="manifest.json"><code>manifest.json</code></a>,
130 <a href="examples/tutorials/getstarted/popup.html" download="popup.html"><code>popup.html</code></a>,
131 <a href="examples/tutorials/getstarted/popup.js" download="popup.js"><code>popup.js</code></a>.
132 The next step is to load those files into Chrome.
133 </p>
135 <h2 id="unpacked">Load the extension</h2>
138 Extensions that you download from the Chrome Web Store are packaged up as
139 <code>.crx</code> files, which is great for distribution, but not so great for
140 development. Recognizing this, Chrome gives you a quick way of loading up your
141 working directory for testing. Let's do that now.
142 </p>
144 <ol>
145 <li>
147 Visit <code>chrome://extensions</code> in your browser (or open up the
148 Chrome menu by clicking the icon to the far right of the Omnibox:
149 <img src="{{static}}/images/hotdogmenu.png"
150 height="29"
151 width="29"
152 alt="The menu's icon is three horizontal bars."> and
153 select <strong>Extensions</strong> under the <strong>Tools</strong> menu
154 to get to the same place).
155 </p>
156 </li>
157 <li>
159 Ensure that the <strong>Developer mode</strong> checkbox in the top
160 right-hand corner is checked.
161 </p>
162 </li>
163 <li>
165 Click <strong>Load unpacked extension&hellip;</strong> to pop up a
166 file-selection dialog.
167 </p>
168 </li>
169 <li>
171 Navigate to the directory in which your extension files live, and select
173 </p>
174 </li>
175 </ol>
178 Alternatively, you can drag and drop the directory where your extension files
179 live onto <code>chrome://extensions</code> in your browser to load it.
180 </p>
183 If the extension is valid, it'll be loaded up and active right away! If it's
184 invalid, an error message will be displayed at the top of the page. Correct
185 the error, and try again.
186 </p>
188 <h2 id="update-code">Fiddle with Code</h2>
191 Now that you've got your first extension up and running, let's fiddle with
192 things so that you have an idea what your development process might look like.
193 For example, let's set a tooltip on the browser action button.
194 </p>
196 According to the browserAction documentation, tooltips can be set by
197 specifying the <code>default_title</code> key in the manifest file. Open
198 <code>manifest.json</code>, and add the <code>default_title</code> key to the
199 <code>browser_action</code>.
200 Make sure that the JSON is valid, so quote the key and add a comma where necessary.
201 </p>
203 <pre data-filename="manifest.json">
206 "browser_action": {
207 "default_icon": "icon.png",
208 "default_popup": "popup.html",
209 "default_title": "Click here!"
213 </pre>
216 The manifest file is only parsed when the extension is loaded. If you want to
217 see the previous changes in action, the extension has to be reloaded.
218 Visit the extensions page (go to <strong>chrome://extensions</strong>, or
219 <strong>Tools &gt; Extensions</strong> under the Chrome menu), and click
220 <strong>Reload</strong> under your extension.
221 All extensions are also reloaded when the extensions page is reloaded, e.g.
222 after hitting <kbd>F5<kbd> or <kbd>Ctrl</kbd>-<kbd>R</kbd>.
223 </p>
226 Once you've reloaded the extension, hover over the browser action badge to see
227 the new tooltip!<br>
228 <img src="{{static}}/images/gettingstarted-tooltip-before.png"
229 width="160"
230 height="120"
231 alt="&quot;Getting started example&quot; tooltip.">
233 <img src="{{static}}/images/gettingstarted-tooltip-after.png"
234 width="160"
235 height="120"
236 alt="&quot;Click here!&quot; tooltip, after modifying manifest.json and reloading the extension.">
237 </p>
239 <h2 id="next-steps">What next?</h2>
242 You now know about the manifest file's central role in bringing things
243 together, and you've mastered the basics of declaring a browser action.
244 That's a great start, and has hopefully gotten you interested enough to
245 explore further. There's a lot more out there to play around with.
246 </p>
248 <ul>
249 <li>
251 The <a href="overview">Chrome Extension Overview</a> backs up a bit,
252 and fills in a lot of detail about extensions' architecture in general,
253 and some specific concepts you'll want to be familiar with going forward.
254 It's the best next step on your journey towards extension mastery.
255 </p>
256 </li>
257 <li>
259 No one writes perfect code on the first try, which means that you'll need
260 to learn about the options available for debugging your creations. Our
261 <a href="tut_debugging">debugging tutorial</a> is perfect for that,
262 and is well worth carefully reading.
263 </p>
264 </li>
265 <li>
267 Chrome extensions have access to powerful APIs above and beyond what's
268 available on the open web: browser actions are just the tip of the
269 iceberg. Our <a href="api_index">chrome.* APIs documentation</a> will
270 walk you through each API in turn.
271 </p>
272 </li>
273 <li>
275 Finally, the <a href="devguide">developer's guide</a> has dozens of
276 additional links to pieces of documentation you might be interested in.
277 </p>
278 </li>
279 </ul>