1 <h1>Getting Started: Building a Chrome Extension</h1> 2 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 give you 10 one-click access to pictures of kittens. Kittens! 11 </p> 12 13 <p> 14 We'll do so by implementing a UI element we call a 15 <a href="browserAction.html">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 kittenish goodness, which will look 18 something like this: 19 </p> 20 21 <img src="{{static}}/images/gettingstarted-1.jpg" 22 width="600" 23 height="420" 24 alt="Chrome, with an extension's popup open and displaying many kittens."> 25 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> 31 32 <h2 id="declaration">Something to Declare</h2> 33 34 <p> 35 The very first thing we'll need to create is a <dfn>manifest file</dfn> named 36 <code>manifest.json</code>. The manifest is nothing more than a JSON-formatted 37 table of contents, containing properties like your extension's name and 38 description, its version number, and so on. At a high level, we'll 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. 41 </p> 42 43 <p> 44 In order to display kittens, we'll want to tell Chrome that we'd like to 45 create a browser action, and that we'd like free-reign to access kittens from 46 a particular source on the net. A manifest file containing those instructions 47 looks like this: 48 </p> 49 50 <pre class="lang-js" data-lang="javascript" data-filename="manifest.json"><code>{ 51 "manifest_version": 2, 52 53 "name": "One-click Kittens", 54 "description": "This extension demonstrates a browser action with kittens.", 55 "version": "1.0", 56 57 "permissions": [ 58 "https://secure.flickr.com/" 59 ], 60 "browser_action": { 61 "default_icon": "icon.png", 62 "default_popup": "popup.html" 63 } 64 }</code></pre> 65 66 <p> 67 Go ahead and save that data to a file named <code>manifest.json</code> in the 68 directory you created, or 69 <a href="examples/tutorials/getstarted/manifest.json" download="manifest.json"> 70 download a copy of <code>manifest.json</code> from our sample repository 71 </a>. 72 </p> 73 74 <h3 id="manifest">What does it mean?</h3> 75 76 <p> 77 The attribute names are fairly self-descriptive, but let's walk through the 78 manifest line-by-line to make sure we're all on the same page. 79 </p> 80 81 <p> 82 The first line, which declares that we're using version 2 of the manifest file 83 format, is mandatory (version 1 is old, deprecated, and generally not 84 awesome). 85 </p> 86 87 <p> 88 The next block defines the extension's name, description, and version. These 89 will be used both inside of Chrome to show a user which extensions you have 90 installed, and also on the Chrome Web Store to display your extension to 91 potentially new users. The name should be short and snappy, and the 92 description no longer than a sentence or so (you'll have more room for a 93 detailed description later). 94 </p> 95 96 <p> 97 The final block first requests permission to work with data on 98 <code>https://secure.flickr.com/</code>, and declares that this extension 99 implements a browser action, assigning it a default icon and popup in the 100 process. 101 </p> 102 103 <h2 id="resources">Resources</h2> 104 105 <p> 106 You probably noticed that <code>manifest.json</code> pointed at two resource 107 files when defining the browser action: <code>icon.png</code> and 108 <code>popup.html</code>. Both resources must exist inside the extension 109 package, so let's create them now: 110 </p> 111 112 <ul class="imaged"> 113 <li> 114 <p> 115 <img src="{{static}}/images/gettingstarted-icon.png" 116 width="127" 117 height="127" 118 alt="The popup's icon will be displayed right next to the Omnibox."> 119 <code>icon.png</code> will be displayed next to the Omnibox, waiting for 120 user interaction. Download a copy of icon.png from our sample repository, 121 <a href="examples/tutorials/getstarted/icon.png" download="icon.png"> 122 Download a copy of <code>icon.png</code> from our sample repository 123 </a>, and save it into the directory you're working in. You could also 124 create your own if you're so inclined; it's just a 19px-square PNG file. 125 </p> 126 </li> 127 <li> 128 <p> 129 <img src="{{static}}/images/gettingstarted-popup.jpg" 130 width="165" 131 height="200" 132 alt="The popup's HTML will be rendered directly below the icon when clicked."> 133 <code>popup.html</code> will be rendered inside the popup window that's 134 created in response to a user's click on the browser action. It's a 135 standard HTML file, just like you're used to from web development, giving 136 you more or less free reign over what the popup displays. 137 <a href="examples/tutorials/getstarted/popup.html" download="popup.html"> 138 Download a copy of <code>popup.html</code> from our sample repository 139 </a>, and save it into 140 the directory you're working in. 141 </p> 142 <p> 143 <code>popup.html</code> requires an additional JavaScript file in order to 144 do the work of grabbing kitten images from the web and loading them into 145 the popup. To save you some effort, just 146 <a href="examples/tutorials/getstarted/popup.js" download="popup.js"> 147 download a copy of <code>popup.js</code> from our sample repository 148 </a>, and save it into the directory you're working in. 149 </p> 150 </li> 151 </ul> 152 153 <p> 154 You should now have four files in your working directory: 155 <a href="examples/tutorials/getstarted/icon.png" download="icon.png"><code>icon.png</code></a>, 156 <a href="examples/tutorials/getstarted/manifest.json" download="manifest.json"><code>manifest.json</code></a>, 157 <a href="examples/tutorials/getstarted/popup.html" download="popup.html"><code>popup.html</code></a>, 158 <a href="examples/tutorials/getstarted/popup.js" download="popup.js"><code>popup.js</code></a>. 159 The next step is to load those files into Chrome. 160 </p> 161 162 <h2 id="unpacked">Load the extension</h2> 163 164 <p> 165 Extensions that you download from the Chrome Web Store are packaged up as 166 <code>.crx</code> files, which is great for distribution, but not so great for 167 development. Recognizing this, Chrome gives you a quick way of loading up your 168 working directory for testing. Let's do that now. 169 </p> 170 171 <ol> 172 <li> 173 <p> 174 Visit <code>chrome://extensions</code> in your browser (or open up the 175 Chrome menu by clicking the icon to the far right of the Omnibox: 176 <img src="{{static}}/images/hotdogmenu.png" 177 height="29" 178 width="29" 179 alt="The menu's icon is three horizontal bars.">. and 180 select <strong>Extensions</strong> under the <strong>Tools</strong> menu 181 to get to the same place). 182 </p> 183 </li> 184 <li> 185 <p> 186 Ensure that the <strong>Developer mode</strong> checkbox in the top 187 right-hand corner is checked. 188 </p> 189 </li> 190 <li> 191 <p> 192 Click <strong>Load unpacked extension…</strong> to pop up a 193 file-selection dialog. 194 </p> 195 </li> 196 <li> 197 <p> 198 Navigate to the directory in which your extension files live, and select 199 it. 200 </p> 201 </li> 202 </ol> 203 204 <p> 205 If the extension is valid, it'll be loaded up and active right away! If it's 206 invalid, an error message will be displayed at the top of the page. Correct 207 the error, and try again. 208 </p> 209 210 <h2 id="update-code">Fiddle with Code</h2> 211 212 <p> 213 Now that you've got your first extension up and running, let's fiddle with 214 things so that you have an idea what your development process might look like. 215 As a trivial example, let's change the data source to search for pictures of 216 puppies instead of kittens. 217 </p> 218 219 <p> 220 Hop into <code>popup.js</code>, and edit line 11 from 221 <code>var QUERY = 'kittens';</code> to read 222 <code>var QUERY = 'puppies';</code>, and save your changes. 223 </p> 224 225 <p> 226 If you click on your extension's browser action again, you'll note that your 227 change hasn't yet had an effect. You'll need to let Chrome know that something 228 has happened, either explicitly by going back to the extension page 229 (<strong>chrome://extensions</strong>, or 230 <strong>Tools > Extensions</strong> under the Chrome menu), and clicking 231 <strong>Reload</strong> under your extension, or by reloading the extensions 232 page itself (either via the reload button to the left of the Omnibox, or by 233 hitting F5 or Ctrl-R). 234 </p> 235 236 <p> 237 Once you've reloaded the extension, click the browser action icon again. 238 Puppies galore! 239 </p> 240 241 <h2 id="next-steps">What next?</h2> 242 243 <p> 244 You now know about the manifest file's central role in bringing things 245 together, and you've mastered the basics of declaring a browser action, and 246 rendering some kittens (or puppies!) in response to a user's click. That's a 247 great start, and has hopefully gotten you interested enough to explore 248 further. There's a lot more out there to play around with. 249 </p> 250 251 <ul> 252 <li> 253 <p> 254 The <a href="overview.html">Chrome Extension Overview</a> backs up a bit, 255 and fills in a lot of detail about extensions' architecture in general, 256 and some specific concepts you'll want to be familiar with going forward. 257 It's the best next step on your journey towards extension mastery. 258 </p> 259 </li> 260 <li> 261 <p> 262 No one writes perfect code on the first try, which means that you'll need 263 to learn about the options available for debugging your creations. Our 264 <a href="tut_debugging.html">debugging tutorial</a> is perfect for that, 265 and is well worth carefully reading. 266 </p> 267 </li> 268 <li> 269 <p> 270 Chrome extensions have access to powerful APIs above and beyond what's 271 available on the open web: browser actions are just the tip of the 272 iceburg. Our <a href="api_index.html">chrome.* APIs documentation</a> will 273 walk you through each API in turn. 274 </p> 275 </li> 276 <li> 277 <p> 278 Finally, the <a href="devguide.html">developer's guide</a> has dozens of 279 additional links to pieces of documentation you might be interested in. 280 </p> 281 </li> 282 </ul> 283