OLD | NEW |
---|---|
1 <h1>Getting Started: Building a Chrome Extension</h1> | 1 <h1>Getting Started: Building a Chrome Extension</h1> |
2 | 2 |
3 <p> | 3 <p> |
4 Extensions allow you to add functionality to Chrome without diving deeply | 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 | 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, | 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 | 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 | 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 | 9 walking through the construction of a simple extension that will fetch an |
10 one-click access to pictures of kittens. Kittens! | 10 image from Google using the current page's URL as search term. |
mkearney1
2015/01/07 22:30:23
"as search term" should be "as a search term".
robwu
2015/01/07 23:40:54
Done.
| |
11 </p> | 11 </p> |
12 | 12 |
13 <p> | 13 <p> |
14 We'll do so by implementing a UI element we call a | 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 | 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 | 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 | 17 icon will open a popup window filled with an image derived from the current |
18 something like this: | 18 page, which will look something like this: |
19 </p> | 19 </p> |
20 | 20 |
21 <img src="{{static}}/images/gettingstarted-1.jpg" | 21 <img src="{{static}}/images/gettingstarted-preview" |
22 width="600" | 22 width="558" |
23 height="420" | 23 height="264" |
24 alt="Chrome, with an extension's popup open and displaying many kittens."> | 24 alt="Chrome with an extension's popup open."> |
25 | 25 |
26 <p> | 26 <p> |
27 If you'd like to follow along at home (and you should!), create a shiny new | 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 | 28 directory on your computer, and pop open your favourite text editor. Let's get |
29 going! | 29 going! |
30 </p> | 30 </p> |
31 | 31 |
32 <h2 id="declaration">Something to Declare</h2> | 32 <h2 id="declaration">Something to Declare</h2> |
33 | 33 |
34 <p> | 34 <p> |
35 The very first thing we'll need to create is a <dfn>manifest file</dfn> named | 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 | 36 <code>manifest.json</code>. This manifest is nothing more than a metadata file |
37 table of contents, containing properties like your extension's name and | 37 in JSON format that contains properties like your extension's name, |
38 description, its version number, and so on. At a high level, we'll use it to | 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 | 39 declare to Chrome what the extension is going to do, and what permissions it |
40 requires in order to do those things. | 40 requires in order to do those things. To learn more about the manifest, read |
41 the <a href="manifest">documentation of manifest.json</a>. | |
mkearney1
2015/01/07 22:30:23
Instead of "documentation of manifest.json", repla
robwu
2015/01/07 23:40:54
Done.
| |
41 </p> | 42 </p> |
42 | 43 |
43 <p> | 44 <p> |
44 In order to display kittens, we'll want to tell Chrome that we'd like to | 45 For our example, we will declare a <a href="browserAction">browser action</a>, |
mkearney1
2015/01/07 22:30:23
Replace "For our example" with "In our example's m
robwu
2015/01/07 23:40:54
Done.
| |
45 create a browser action, and that we'd like free-reign to access kittens from | 46 the <a href="activeTab">activeTab permission</a> to see the URL of the current |
46 a particular source on the net. A manifest file containing those instructions | 47 tab, and the host <a href="declare_permissions">permission</a> to access the |
47 looks like this: | 48 external Google Image search API. |
48 </p> | 49 </p> |
49 | 50 |
50 <pre data-filename="manifest.json"> | 51 <pre data-filename="manifest.json"> |
51 { | 52 { |
52 "manifest_version": 2, | 53 "manifest_version": 2, |
53 | 54 |
54 "name": "One-click Kittens", | 55 "name": "Getting started example", |
55 "description": "This extension demonstrates a browser action with kittens.", | 56 "description": "This extension shows a Google Image search result for the curr ent page", |
56 "version": "1.0", | 57 "version": "1.0", |
57 | 58 |
58 "permissions": [ | |
59 "https://secure.flickr.com/" | |
60 ], | |
61 "browser_action": { | 59 "browser_action": { |
62 "default_icon": "icon.png", | 60 "default_icon": "icon.png", |
63 "default_popup": "popup.html" | 61 "default_popup": "popup.html" |
64 } | 62 }, |
63 "permissions": [ | |
mkearney1
2015/01/07 22:30:23
Add comments in the sample code that call out the
robwu
2015/01/07 23:40:54
Done.
mkearney1
2015/01/08 23:00:34
I still don't see the comments. Weird.
On 2015/01/
robwu
2015/01/08 23:09:03
I added a comment to popup.html that explained the
robwu
2015/01/14 16:09:19
I just tested again (14 jan 2015), and the Chrome
| |
64 "activeTab", | |
65 "https://ajax.googleapis.com/" | |
66 ] | |
65 } | 67 } |
66 </pre> | 68 </pre> |
67 | 69 |
68 <p> | 70 <p> |
69 Go ahead and save that data to a file named <code>manifest.json</code> in the | 71 Go ahead and save that data to a file named <code>manifest.json</code> in the |
70 directory you created, or | 72 directory you created, or |
71 <a href="examples/tutorials/getstarted/manifest.json" download="manifest.json" > | 73 <a href="examples/tutorials/getstarted/manifest.json" download="manifest.json" > |
72 download a copy of <code>manifest.json</code> from our sample repository | 74 download a copy of <code>manifest.json</code> from our sample repository |
73 </a>. | 75 </a>. |
74 </p> | 76 </p> |
75 | 77 |
mkearney1
2015/01/07 22:30:23
Is there a reason you dropped line-by-line breakdo
robwu
2015/01/07 23:40:54
name/description/version are obvious. The first th
| |
76 <h3 id="manifest">What does it mean?</h3> | |
77 | |
78 <p> | |
79 The attribute names are fairly self-descriptive, but let's walk through the | |
80 manifest line-by-line to make sure we're all on the same page. | |
81 </p> | |
82 | |
83 <p> | |
84 The first line, which declares that we're using version 2 of the manifest file | |
85 format, is mandatory (version 1 is old, deprecated, and generally not | |
86 awesome). | |
87 </p> | |
88 | |
89 <p> | |
90 The next block defines the extension's name, description, and version. These | |
91 will be used both inside of Chrome to show a user which extensions you have | |
92 installed, and also on the Chrome Web Store to display your extension to | |
93 potentially new users. The name should be short and snappy, and the | |
94 description no longer than a sentence or so (you'll have more room for a | |
95 detailed description later). | |
96 </p> | |
97 | |
98 <p> | |
99 The final block first requests permission to work with data on | |
100 <code>https://secure.flickr.com/</code>, and declares that this extension | |
101 implements a browser action, assigning it a default icon and popup in the | |
102 process. | |
103 </p> | |
104 | |
105 <h2 id="resources">Resources</h2> | 78 <h2 id="resources">Resources</h2> |
106 | 79 |
107 <p> | 80 <p> |
108 You probably noticed that <code>manifest.json</code> pointed at two resource | 81 You probably noticed that <code>manifest.json</code> pointed at two resource |
109 files when defining the browser action: <code>icon.png</code> and | 82 files when defining the browser action: <code>icon.png</code> and |
110 <code>popup.html</code>. Both resources must exist inside the extension | 83 <code>popup.html</code>. Both resources must exist inside the extension |
111 package, so let's create them now: | 84 package, so let's create them now: |
112 </p> | 85 </p> |
113 | 86 |
114 <ul class="imaged"> | 87 <ul class="imaged"> |
115 <li> | 88 <li> |
116 <p> | 89 <p> |
117 <img src="{{static}}/images/gettingstarted-icon.png" | 90 <img src="{{static}}/images/gettingstarted-icon.png" |
118 width="127" | 91 width="127" |
119 height="127" | 92 height="127" |
93 style="float:right" | |
120 alt="The popup's icon will be displayed right next to the Omnibox."> | 94 alt="The popup's icon will be displayed right next to the Omnibox."> |
121 <code>icon.png</code> will be displayed next to the Omnibox, waiting for | 95 <code>icon.png</code> will be displayed next to the Omnibox, waiting for |
122 user interaction. Download a copy of icon.png from our sample repository, | 96 user interaction. |
123 <a href="examples/tutorials/getstarted/icon.png" download="icon.png"> | 97 <a href="examples/tutorials/getstarted/icon.png" download="icon.png"> |
124 Download a copy of <code>icon.png</code> from our sample repository | 98 Download a copy of <code>icon.png</code> from our sample repository |
125 </a>, and save it into the directory you're working in. You could also | 99 </a>, and save it into the directory you're working in. You could also |
126 create your own if you're so inclined; it's just a 19px-square PNG file. | 100 create your own if you're so inclined; it's just a 19px-square PNG file. |
127 </p> | 101 </p> |
128 </li> | 102 </li> |
129 <li> | 103 <li> |
130 <p> | 104 <p> |
131 <img src="{{static}}/images/gettingstarted-popup.jpg" | |
132 width="165" | |
133 height="200" | |
134 alt="The popup's HTML will be rendered directly below the icon when c licked."> | |
135 <code>popup.html</code> will be rendered inside the popup window that's | 105 <code>popup.html</code> will be rendered inside the popup window that's |
136 created in response to a user's click on the browser action. It's a | 106 created in response to a user's click on the browser action. It's a |
137 standard HTML file, just like you're used to from web development, giving | 107 standard HTML file, just like you're used to from web development, giving |
138 you more or less free reign over what the popup displays. | 108 you more or less free reign over what the popup displays. |
139 <a href="examples/tutorials/getstarted/popup.html" download="popup.html"> | 109 <a href="examples/tutorials/getstarted/popup.html" download="popup.html"> |
140 Download a copy of <code>popup.html</code> from our sample repository | 110 Download a copy of <code>popup.html</code> from our sample repository |
141 </a>, and save it into | 111 </a>, and save it into the directory you're working in. |
142 the directory you're working in. | |
143 </p> | 112 </p> |
144 <p> | 113 <p> |
145 <code>popup.html</code> requires an additional JavaScript file in order to | 114 The actual logic of rendering the content of the popup is implemented by |
146 do the work of grabbing kitten images from the web and loading them into | 115 <a href="examples/tutorials/getstarted/popup.js">popup.js</a>. You are |
147 the popup. To save you some effort, just | 116 encouraged to read the elaborate comments in this file to learn more |
117 about the logic.<br> | |
148 <a href="examples/tutorials/getstarted/popup.js" download="popup.js"> | 118 <a href="examples/tutorials/getstarted/popup.js" download="popup.js"> |
149 download a copy of <code>popup.js</code> from our sample repository | 119 Download a copy of <code>popup.js</code> from our sample repository |
150 </a>, and save it into the directory you're working in. | 120 </a>, and save it into the directory you're working in. |
151 </p> | 121 </p> |
152 </li> | 122 </li> |
153 </ul> | 123 </ul> |
154 | 124 |
155 <p> | 125 <p> |
156 You should now have four files in your working directory: | 126 You should now have four files in your working directory: |
157 <a href="examples/tutorials/getstarted/icon.png" download="icon.png"><code>ico n.png</code></a>, | 127 <a href="examples/tutorials/getstarted/icon.png" download="icon.png"><code>ico n.png</code></a>, |
158 <a href="examples/tutorials/getstarted/manifest.json" download="manifest.json" ><code>manifest.json</code></a>, | 128 <a href="examples/tutorials/getstarted/manifest.json" download="manifest.json" ><code>manifest.json</code></a>, |
159 <a href="examples/tutorials/getstarted/popup.html" download="popup.html"><code >popup.html</code></a>, | 129 <a href="examples/tutorials/getstarted/popup.html" download="popup.html"><code >popup.html</code></a>, |
(...skipping 11 matching lines...) Expand all Loading... | |
171 </p> | 141 </p> |
172 | 142 |
173 <ol> | 143 <ol> |
174 <li> | 144 <li> |
175 <p> | 145 <p> |
176 Visit <code>chrome://extensions</code> in your browser (or open up the | 146 Visit <code>chrome://extensions</code> in your browser (or open up the |
177 Chrome menu by clicking the icon to the far right of the Omnibox: | 147 Chrome menu by clicking the icon to the far right of the Omnibox: |
178 <img src="{{static}}/images/hotdogmenu.png" | 148 <img src="{{static}}/images/hotdogmenu.png" |
179 height="29" | 149 height="29" |
180 width="29" | 150 width="29" |
181 alt="The menu's icon is three horizontal bars.">. and | 151 alt="The menu's icon is three horizontal bars.">. and |
mkearney1
2015/01/07 22:30:24
Remove unnecessary period after "> and before and.
robwu
2015/01/07 23:40:54
Done.
| |
182 select <strong>Extensions</strong> under the <strong>Tools</strong> menu | 152 select <strong>Extensions</strong> under the <strong>Tools</strong> menu |
183 to get to the same place). | 153 to get to the same place). |
184 </p> | 154 </p> |
185 </li> | 155 </li> |
186 <li> | 156 <li> |
187 <p> | 157 <p> |
188 Ensure that the <strong>Developer mode</strong> checkbox in the top | 158 Ensure that the <strong>Developer mode</strong> checkbox in the top |
189 right-hand corner is checked. | 159 right-hand corner is checked. |
190 </p> | 160 </p> |
191 </li> | 161 </li> |
(...skipping 20 matching lines...) Expand all Loading... | |
212 If the extension is valid, it'll be loaded up and active right away! If it's | 182 If the extension is valid, it'll be loaded up and active right away! If it's |
213 invalid, an error message will be displayed at the top of the page. Correct | 183 invalid, an error message will be displayed at the top of the page. Correct |
214 the error, and try again. | 184 the error, and try again. |
215 </p> | 185 </p> |
216 | 186 |
217 <h2 id="update-code">Fiddle with Code</h2> | 187 <h2 id="update-code">Fiddle with Code</h2> |
218 | 188 |
219 <p> | 189 <p> |
220 Now that you've got your first extension up and running, let's fiddle with | 190 Now that you've got your first extension up and running, let's fiddle with |
221 things so that you have an idea what your development process might look like. | 191 things so that you have an idea what your development process might look like. |
222 As a trivial example, let's change the data source to search for pictures of | 192 <br> |
mkearney1
2015/01/07 22:30:23
Remove this <br>.
robwu
2015/01/07 23:40:54
Done.
| |
223 puppies instead of kittens. | 193 For example, let's set a tooltip on the browser action button. According to |
mkearney1
2015/01/07 22:30:23
End paragraph after "For example, let's set a tool
robwu
2015/01/07 23:40:54
Done.
| |
194 the browserAction documentation, tooltips can be set by specifying the | |
195 <code>default_title</code> key in the manifest file. So open | |
mkearney1
2015/01/07 22:30:23
Remove 'So' and start sentence with 'Open'.
robwu
2015/01/07 23:40:54
Done.
| |
196 <code>manifest.json</code>, and add this property to <code>browser_action</cod e>. | |
mkearney1
2015/01/07 22:30:23
Change 'this property' to 'the default_title to th
robwu
2015/01/07 23:40:54
Done.
| |
197 Make sure that the JSON is valid, so quote the key and add a comma where neces sary. | |
198 </p> | |
199 | |
200 <pre data-filename="manifest.json"> | |
201 { | |
202 ... | |
203 "browser_action": { | |
204 "default_icon": "icon.png", | |
205 "default_popup": "popup.html", | |
206 "default_title": "Click here!" | |
207 }, | |
208 ... | |
209 } | |
210 </pre> | |
211 | |
212 <p> | |
213 The manifest file is only parsed when the extension is loaded. If you want to | |
214 see the previous changes in action, the extension has to be reloaded. This can | |
mkearney1
2015/01/07 22:30:23
The last two sentences in this paragraph are wordy
robwu
2015/01/07 23:40:54
At the left of the omnibox.
| |
215 be done by visiting the extension page | |
216 (<strong>chrome://extensions</strong>, or | |
217 <strong>Tools > Extensions</strong> under the Chrome menu), and clicking | |
218 <strong>Reload</strong> under your extension. The extension can can also be | |
219 reload by reloading the extension page, e.g. by clicking on the reload button, | |
220 or by hitting <kbd>F5<kbd> or <kbd>Ctrl</kbd>-<kbd>R</kbd>. | |
224 </p> | 221 </p> |
225 | 222 |
226 <p> | 223 <p> |
227 Hop into <code>popup.js</code>, and edit line 11 from | 224 Once you've reloaded the extension, hover over the browser action badge to see |
228 <code>var QUERY = 'kittens';</code> to read | 225 the new tooltip!<br> |
229 <code>var QUERY = 'puppies';</code>, and save your changes. | 226 <img src="{{static}}/images/gettingstarted-tooltip-before.png" |
230 </p> | 227 width="160" |
228 height="120" | |
229 alt=""Getting started example" tooltip."> | |
231 | 230 |
232 <p> | 231 <img src="{{static}}/images/gettingstarted-tooltip-after.png" |
233 If you click on your extension's browser action again, you'll note that your | 232 width="160" |
234 change hasn't yet had an effect. You'll need to let Chrome know that something | 233 height="120" |
235 has happened, either explicitly by going back to the extension page | 234 alt=""Click here!" tooltip, after modifying manifest.json and r eloading the extension."> |
236 (<strong>chrome://extensions</strong>, or | |
237 <strong>Tools > Extensions</strong> under the Chrome menu), and clicking | |
238 <strong>Reload</strong> under your extension, or by reloading the extensions | |
239 page itself (either via the reload button to the left of the Omnibox, or by | |
240 hitting F5 or Ctrl-R). | |
241 </p> | |
242 | |
243 <p> | |
244 Once you've reloaded the extension, click the browser action icon again. | |
245 Puppies galore! | |
246 </p> | 235 </p> |
247 | 236 |
248 <h2 id="next-steps">What next?</h2> | 237 <h2 id="next-steps">What next?</h2> |
249 | 238 |
250 <p> | 239 <p> |
251 You now know about the manifest file's central role in bringing things | 240 You now know about the manifest file's central role in bringing things |
252 together, and you've mastered the basics of declaring a browser action, and | 241 together, and you've mastered the basics of declaring a browser action. |
253 rendering some kittens (or puppies!) in response to a user's click. That's a | 242 That's a great start, and has hopefully gotten you interested enough to |
254 great start, and has hopefully gotten you interested enough to explore | 243 explore further. There's a lot more out there to play around with. |
255 further. There's a lot more out there to play around with. | |
256 </p> | 244 </p> |
257 | 245 |
258 <ul> | 246 <ul> |
259 <li> | 247 <li> |
260 <p> | 248 <p> |
261 The <a href="overview">Chrome Extension Overview</a> backs up a bit, | 249 The <a href="overview">Chrome Extension Overview</a> backs up a bit, |
262 and fills in a lot of detail about extensions' architecture in general, | 250 and fills in a lot of detail about extensions' architecture in general, |
263 and some specific concepts you'll want to be familiar with going forward. | 251 and some specific concepts you'll want to be familiar with going forward. |
264 It's the best next step on your journey towards extension mastery. | 252 It's the best next step on your journey towards extension mastery. |
265 </p> | 253 </p> |
(...skipping 14 matching lines...) Expand all Loading... | |
280 walk you through each API in turn. | 268 walk you through each API in turn. |
281 </p> | 269 </p> |
282 </li> | 270 </li> |
283 <li> | 271 <li> |
284 <p> | 272 <p> |
285 Finally, the <a href="devguide">developer's guide</a> has dozens of | 273 Finally, the <a href="devguide">developer's guide</a> has dozens of |
286 additional links to pieces of documentation you might be interested in. | 274 additional links to pieces of documentation you might be interested in. |
287 </p> | 275 </p> |
288 </li> | 276 </li> |
289 </ul> | 277 </ul> |
OLD | NEW |