OLD | NEW |
1 <div id="pageData-title" class="pageData">Overview</div> | 1 <div id="pageData-title" class="pageData">Overview</div> |
2 <div id="pageData-showTOC" class="pageData">true</div> | 2 <div id="pageData-showTOC" class="pageData">true</div> |
3 | 3 |
4 <p> | 4 <p> |
5 Please read this page; | 5 Read this page! |
6 it has vital information about the extension architecture. | 6 It has vital information about the extension architecture. |
7 If you get bored or restless, | 7 Once you've finished this page |
8 take a break! | 8 and the |
9 We suggest bouncing between this page | 9 <a href="getstarted.html">Getting Started</a> tutorial, |
10 and tutorials | 10 you'll be all set to start writing extensions. |
11 such as <a href="getstarted.html">Getting Started</a> | |
12 and <a href="tut_debugging.html">Debugging</a>. | |
13 </p> | 11 </p> |
14 | 12 |
15 | 13 |
16 <h2 id="what">The basics</h2> | 14 <h2 id="what">The basics</h2> |
17 | 15 |
18 <p> | 16 <p> |
19 An extension is a zipped bundle of files — | 17 An extension is a zipped bundle of files — |
20 HTML, CSS, JavaScript, images, and anything else you need — | 18 HTML, CSS, JavaScript, images, and anything else you need — |
21 that adds functionality to the Google Chrome browser. | 19 that adds functionality to the Google Chrome browser. |
22 Because an extension is just a special kind of web page, | 20 Extensions are essentially web pages, |
23 it can use all the APIs | 21 and they can use all the APIs |
24 that the browser provides | 22 that the browser provides |
25 to web pages and apps, | 23 to web pages, |
26 from XMLHttpRequest to JSON to localStorage. | 24 from XMLHttpRequest to JSON to HTML5 local storage. |
27 </p> | 25 </p> |
28 | 26 |
29 <p> | 27 <p> |
30 Extensions can add UI to Google Chrome, | 28 Many extensions add UI to Google Chrome, |
31 in the form of | 29 in the form of |
32 <a href="toolstrip.html">toolstrips</a> (toolbar additions) | 30 <a href="toolstrip.html">toolstrips</a> (toolbar additions) |
33 and <a href="pageActions.html">page actions</a> | 31 or <a href="pageActions.html">page actions</a> |
34 (clickable badges in the address bar). | 32 (clickable badges in the address bar). |
35 Extensions can also interact programmatically | 33 Extensions can also interact programmatically |
36 with browser features such as | 34 with browser features such as |
37 <a href="bookmarks.html">bookmarks</a> | 35 <a href="bookmarks.html">bookmarks</a> |
38 and <a href="tabs.html">tabs</a>. | 36 and <a href="tabs.html">tabs</a>. |
| 37 To interact with web pages or servers, |
| 38 extensions can use |
| 39 <a href="content_scripts.html">content scripts</a> or |
| 40 <a href="xhr.html">cross-origin XMLHttpRequests</a>. |
39 </p> | 41 </p> |
40 | 42 |
41 <p> | 43 <p> |
42 To find a complete list of extension features, | 44 See the <a href="devguide.html">Developer's Guide</a> |
| 45 for a complete list of extension features, |
43 with implementation details | 46 with implementation details |
44 for each one, | 47 for each one. |
45 see the <a href="devguide.html">Developer's Guide</a>. | |
46 </p> | 48 </p> |
47 | 49 |
48 | 50 |
49 <h2 id="files">Files</h2> | 51 <h2 id="files">Files</h2> |
50 <p> | 52 <p> |
51 Each extension has the following files: | 53 Each extension has the following files: |
52 <!-- PENDING: This could use a picture --> | 54 <!-- PENDING: This could use a picture --> |
53 </p> | 55 </p> |
54 | 56 |
55 <ul> | 57 <ul> |
56 <li>A <b>manifest file</b></li> | 58 <li>A <b>manifest file</b></li> |
57 <li>One or more <b>HTML files</b> (unless the extension is a theme)</li> | 59 <li>One or more <b>HTML files</b> (unless the extension is a theme)</li> |
58 <li><em>Optional:</em> One or more <b>JavaScript files</b></li> | 60 <li><em>Optional:</em> One or more <b>JavaScript files</b></li> |
59 <li><em>Optional:</em> Any other files your extension needs — for exampl
e, image files</li> | 61 <li><em>Optional:</em> Any other files your extension needs — |
| 62 for example, image files</li> |
60 </ul> | 63 </ul> |
61 | 64 |
62 <p> | 65 <p> |
63 While you're working on your extension, | 66 While you're working on your extension, |
64 you put all these files into a single folder. | 67 you put all these files into a single folder. |
65 When you distribute your extension, | 68 When you distribute your extension, |
66 the contents of the folder are packaged into a special zipfile | 69 the contents of the folder are packaged into a special ZIP file |
67 that has a <code>.crx</code> suffix, | 70 that has a <code>.crx</code> suffix, |
68 as described in <a href="packaging.html">Packaging</a>. | 71 as described in <a href="packaging.html">Packaging</a>. |
69 </p> | 72 </p> |
70 | 73 |
71 | 74 |
72 <h3>Referring to files</h3> | 75 <h3>Referring to files</h3> |
73 | 76 |
74 <p> | 77 <p> |
75 You can put any file you like into an extension, | 78 You can put any file you like into an extension, |
76 but how do you use it? | 79 but how do you use it? |
77 Usually, | 80 Usually, |
78 you can refer to the file using a relative URL, | 81 you can refer to the file using a relative URL, |
79 just as you would in an ordinary HTML page. | 82 just as you would in an ordinary HTML page. |
80 Here's an example of referring to | 83 Here's an example of referring to |
81 a file named <code>myimage.png</code> | 84 a file named <code>myimage.png</code> |
82 that's in a subfolder named <code>images</code>. | 85 that's in a subfolder named <code>images</code>. |
83 </p> | 86 </p> |
84 | 87 |
85 <pre> | 88 <pre> |
86 <img <b>src="images/myimage.png"</b> style="width:auto; height:auto"> | 89 <img <b>src="images/myimage.png"</b>> |
87 </pre> | 90 </pre> |
88 | 91 |
89 <p> | 92 <p> |
90 As you might notice while you use the Google Chrome debugger, | 93 As you might notice while you use the Google Chrome debugger, |
91 every file in an extension is also accessible by an absolute URL like this: | 94 every file in an extension is also accessible by an absolute URL like this: |
92 </p> | 95 </p> |
93 | 96 |
94 <blockquote> | 97 <blockquote> |
95 <b>chrome-extension://</b><em><extensionID></em><b>/</b><em><pathToFile></
em> | 98 <b>chrome-extension://</b><em><extensionID></em><b>/</b><em><pathToFile></
em> |
96 </blockquote> | 99 </blockquote> |
97 | 100 |
98 <p> | 101 <p> |
99 In that URL, the <em><extensionID></em> is a unique identifier | 102 In that URL, the <em><extensionID></em> is a unique identifier |
100 that the extension system generates for each extension. | 103 that the extension system generates for each extension. |
101 You can see the IDs for all your loaded extensions | 104 You can see the IDs for all your loaded extensions |
102 by going to the URL <b>chrome://extensions/</b>. | 105 by going to the URL <b>chrome://extensions/</b>. |
103 The <em><pathToFile></em> is the location of the file | 106 The <em><pathToFile></em> is the location of the file |
104 under the extension's top folder; | 107 under the extension's top folder; |
105 it's the same as the relative URL. | 108 it's the same as the relative URL. |
106 </p> | 109 </p> |
107 | 110 |
108 <p> | 111 <p class="comment"> |
109 For example, assume your extension has | 112 [PENDING: Should mention/reflect/link to |
110 the ID <b>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</b> | 113 <a href="http://dev.chromium.org/developers/design-documents/extensions/i18n">in
ternationalization</a> |
111 and the files shown in the following figure: | 114 when it's ready.] |
112 </p> | |
113 | |
114 <pre> | |
115 <span class="tbd">[PENDING: someday this will be a real figure]</span> | |
116 toolstrip.html | |
117 styles.css | |
118 images: | |
119 myimage.png | |
120 ... | |
121 other: | |
122 more.html | |
123 morestyles.css | |
124 ... | |
125 </pre> | |
126 | |
127 <p> | |
128 Here's a table that shows the relative and absolute URLs of these files. | |
129 Keep in mind that the relative URL is the same no matter where you're using it &
mdash; | |
130 the relative URL of <code>myimage.png</code> is | |
131 <b>images/myimage.png</b>, | |
132 no matter whether it's being used by | |
133 <code>toolstrip.html</code> or <code>other/more.html</code>. | |
134 </p> | |
135 | |
136 <table> | |
137 <tr> | |
138 <th> File </th> | |
139 <th> Relative URL </th> | |
140 <th> Absolute URL </th> | |
141 </tr> | |
142 <tr> | |
143 <td> toolstrip.html </td> | |
144 <td> toolstrip.html </td> | |
145 <td> chrome-extension://aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/toolstrip.html </td> | |
146 </tr> | |
147 <tr> | |
148 <td> styles.css </td> | |
149 <td> styles.css </td> | |
150 <td> chrome-extension://aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/styles.css </td> | |
151 </tr> | |
152 <tr> | |
153 <td> myimage.png </td> | |
154 <td> images/myimage.png </td> | |
155 <td> chrome-extension://aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/images/myimage.png </
td> | |
156 </tr> | |
157 <tr> | |
158 <td> more.html </td> | |
159 <td> other/more.html </td> | |
160 <td> chrome-extension://aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/other/more.html </td> | |
161 </tr> | |
162 <tr> | |
163 <td> morestyles.css </td> | |
164 <td> other/morestyles.css </td> | |
165 <td> chrome-extension://aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/other/morestyles.css
</td> | |
166 </tr> | |
167 </table> | |
168 | |
169 <p class="tbd"> | |
170 [PENDING: Mention/reflect/link to | |
171 <a href="http://dev.chromium.org/developers/design-documents/extensions/i18n">in
ternationalization</a>] | |
172 </p> | 115 </p> |
173 | 116 |
174 | 117 |
175 <h3>The manifest file</h3> | 118 <h3>The manifest file</h3> |
176 | 119 |
177 <p> | 120 <p> |
178 The manifest file, called <code>manifest.json</code>, | 121 The manifest file, called <code>manifest.json</code>, |
179 gives information about the extension, | 122 gives information about the extension, |
180 such as the most important files | 123 such as the most important files |
181 and the capabilities that the extension might use. | 124 and the capabilities that the extension might use. |
(...skipping 12 matching lines...) Expand all Loading... |
194 }</pre> | 137 }</pre> |
195 | 138 |
196 <p> | 139 <p> |
197 For details, see | 140 For details, see |
198 <a href="manifest.html">Manifest Files</a>. | 141 <a href="manifest.html">Manifest Files</a>. |
199 </p> | 142 </p> |
200 | 143 |
201 <h2 id="arch">Architecture</h2> | 144 <h2 id="arch">Architecture</h2> |
202 | 145 |
203 <p> | 146 <p> |
204 The following pictures shows the web pages associated with a typical extension | 147 Remember that extensions are essentially web pages. |
205 (in this case, a simple toolstrip). | 148 The following figure shows what |
206 The first figure shows what an extension might look like | 149 a simple extension with a toolstrip |
| 150 might look like |
207 when there's a single browser window. | 151 when there's a single browser window. |
| 152 The toolstrip (T1) is at the bottom of the window. |
| 153 The HTML and JavaScript code for T1 |
| 154 is in a web page |
| 155 (<code>toolstrip.html</code>). |
208 </p> | 156 </p> |
209 | 157 |
210 <p class="tbd"> [PENDING: image goes here] </p> | 158 <img src="images/arch-1.gif"> |
211 | 159 |
212 <p> | 160 <p> |
213 The next figure shows that all the code in the extension's main HTML file | 161 Each time the user creates a new window, |
214 is duplicated each time you create a new window. | 162 the browser creates new toolstrips, page actions, and so on. |
215 In other words, each window has its own widgets for the extension, | 163 Each new bit of UI gets its own web page, |
216 which means each window adds another web page for that extension. | 164 which means that toolstrips (for example) |
| 165 that are in different windows can have different states. |
| 166 In the following figure, |
| 167 the two toolstrips (T1 and T2) |
| 168 are associated with two separate web pages, |
| 169 both of which contain code from <code>toolstrip.html</code>. |
| 170 This means that although both toolstrips use the same code, |
| 171 one can be blue while the other is green. |
217 </p> | 172 </p> |
218 | 173 |
219 <p class="tbd"> [PENDING: image goes here] </p> | 174 <img src="images/arch-2.gif"> |
220 | |
221 <p> | |
222 To more easily coordinate all these web pages and to avoid duplication, | |
223 you should design your extension | |
224 so that most of the code — | |
225 especially the state of the extension — | |
226 is in a <em>background page</em>, | |
227 as shown in the following figure. | |
228 </p> | |
229 | |
230 <p class="tbd"> [PENDING: image goes here] </p> | |
231 | 175 |
232 | 176 |
233 <h3 id="background">The background page</h3> | 177 <h3 id="background">The background page</h3> |
| 178 |
234 <p> | 179 <p> |
235 Any non-trivial extension can (and probably should) | 180 If your extension's web pages need to communicate |
236 have a background page. | 181 or otherwise share information — |
| 182 for example, if a browser event |
| 183 can affect more than just one window's toolstrip — |
| 184 the extension can use a background page. |
| 185 The following figure shows a background page |
| 186 (<code>background.html</code>) |
| 187 that controls |
| 188 the toolstrips in an extension. |
| 189 </p> |
| 190 |
| 191 <img src="images/arch-3.gif"> |
| 192 |
| 193 <p> |
237 The background page is an invisible page | 194 The background page is an invisible page |
238 where you put the main logic of the extension. | 195 where you can put the main logic of the extension. |
239 The extension's other pages should have | 196 The extension's other pages should have |
240 only the code that's necessary | 197 only the code that's necessary |
241 to show the state of the extension | 198 to show the state of the extension |
242 and to get input from the user.</p> | 199 and to get input from the user.</p> |
243 | 200 |
244 <p> | 201 <p> |
245 An extension's background page exists | |
246 before any of the extension's other pages exist. | |
247 It continues to exist as long as the browser is running | |
248 and the extension is installed, | |
249 even if other pages and windows go away. | |
250 </p> | |
251 | |
252 <p> | |
253 Your extension's UI — | |
254 its toolstrips, page actions, and so on — | |
255 should be dumb views. | |
256 When the view needs some state, | |
257 it should request it from the background page. | |
258 When the background page notices some state change, | |
259 it should update all the views. | |
260 </p> | |
261 | |
262 <p> | |
263 For more information, see | 202 For more information, see |
264 <a href="background_pages.html">Background Pages</a>. | 203 <a href="background_pages.html">Background Pages</a>. |
265 </p> | 204 </p> |
266 | 205 |
267 <h3 id="contentScripts">Content scripts</h3> | 206 <h3 id="contentScripts">Content scripts</h3> |
268 | 207 |
269 <p> | 208 <p> |
270 If you want your extension to interact with web pages, | 209 If your extension needs to interact with web pages, |
271 you need a content script. | 210 then it needs a <em>content script</em>. |
272 Content scripts are JavaScript files that run in the context of web pages. | 211 Content scripts are JavaScript files that run in the context of web pages. |
273 By using the standard Document Object Model (DOM), | 212 Using the standard |
274 they can read details of the web pages the browser visits, | 213 <a href="http://www.w3.org/TR/DOM-Level-2-HTML/">Document Object Model</a> |
| 214 (DOM), |
| 215 content scripts can read details of the web pages the browser visits, |
275 and they can make changes to the pages. | 216 and they can make changes to the pages. |
276 </p> | 217 </p> |
277 | 218 |
| 219 <p> |
| 220 In the following figure, |
| 221 the content script |
| 222 can read and modify |
| 223 the DOM for the web page displayed in Window 1. |
| 224 If the toolstrip's UI needs to change |
| 225 to reflect the web page's contents, |
| 226 the content script can request that change |
| 227 by sending a message to the parent extension. |
| 228 </p> |
| 229 |
| 230 <img src="images/arch-cs.gif"> |
| 231 |
278 <p class="comment"> | 232 <p class="comment"> |
279 [PENDING: add an architectural figure here, | 233 [PENDING: Add overview of message passing.] |
280 showing the extension's pages, a web page, and the content script | |
281 interacting with the web page. explain the figure.] | |
282 </p> | 234 </p> |
283 | 235 |
284 <p> | 236 <p> |
285 For more information, | 237 For more information, |
286 see <a href="content_scripts.html">Content Scripts</a>. | 238 see <a href="content_scripts.html">Content Scripts</a>. |
287 </p> | 239 </p> |
288 | 240 |
289 | 241 |
290 <h2>Communication </h2> | 242 <h2>Communication between pages </h2> |
291 | 243 |
292 <p> | 244 <p> |
293 Two kinds of communication happen within an extension: | 245 The HTML pages within an extension often need to communicate. |
| 246 An example is when the background page tells |
| 247 UI pages (such as toolstrips) |
| 248 to update their appearance. |
| 249 Because all of an extension's pages |
| 250 execute in same process on the same thread, |
| 251 the pages can make direct function calls to each other. |
294 </p> | 252 </p> |
295 | 253 |
296 <ul> | |
297 <li> | |
298 Communication <b>between pages</b> in the extension. | |
299 <br /> | |
300 For example, sometimes the background page | |
301 needs to update all the UI pages | |
302 to reflect a change in the extension's state. | |
303 </li> | |
304 <li> | |
305 Communication <b>between content scripts and the extension</b>. | |
306 <br /> | |
307 For example, | |
308 <span class="tbd">[PENDING: typical use case goes here]</span>. | |
309 See <a href="content_scripts.html">Content Scripts</a> | |
310 for information about this type of communication. | |
311 </li> | |
312 </ul> | |
313 | |
314 <p> | 254 <p> |
315 Here are some keys to communication between an extension's pages: | 255 To find pages in the extension, use |
| 256 <a href="extension.html"><code>chrome.extension</code></a> |
| 257 methods such as |
| 258 <code>getViews()</code>, |
| 259 <code>getBackgroundPage()</code>, |
| 260 and <code>getToolstrips()</code>. |
| 261 Once a page has a reference to other pages within the extension, |
| 262 the first page can invoke functions on the other pages. |
| 263 It can even manipulate their DOMs. |
316 </p> | 264 </p> |
317 | 265 |
318 <ul> | |
319 <li> | |
320 All the extension's pages execute in same process, | |
321 on the same thread. | |
322 </li> | |
323 <li> | |
324 Extension pages can use | |
325 <a href="extension.html"><code>chrome.extension</code></a> | |
326 methods such as | |
327 <code>getViews()</code>, | |
328 <code>getBackgroundPage()</code>, | |
329 and <code>getToolstrips()</code> | |
330 to get access to the extension's pages. | |
331 </li> | |
332 <li> | |
333 Once you have references to an extension's pages, | |
334 you can manipulate the DOM for each page. | |
335 </li> | |
336 <li> | |
337 You can also make direct function calls to the pages, | |
338 as shown in the following example. | |
339 </li> | |
340 </ul> | |
341 | |
342 <p> | 266 <p> |
343 Here's an example of | 267 Here's an example of |
344 communication between toolstrips and the background page. | 268 communication between toolstrips and the background page. |
345 </p> | 269 </p> |
346 | 270 |
347 <pre> | 271 <pre> |
348 //In background_page.html: | 272 //In background.html: |
349 function updateUI(checked) { | 273 function updateUI(checked) { |
350 var toolstrips = chrome.extension.getToolstrips(); | 274 var toolstrips = chrome.extension.getToolstrips(); |
351 for (var i in toolstrips) { | 275 for (var i in toolstrips) { |
352 if (toolstrips[i].enableCheckbox) | 276 if (toolstrips[i].enableCheckbox) |
353 toolstrips[i].enableCheckbox(checked); | 277 toolstrips[i].enableCheckbox(checked); |
354 } | 278 } |
355 } | 279 } |
356 | 280 |
357 //In toolstrip.html: | 281 //In toolstrip.html: |
358 function enableCheckbox(checked) { | 282 function enableCheckbox(checked) { |
(...skipping 11 matching lines...) Expand all Loading... |
370 <h2> Summary </h2> | 294 <h2> Summary </h2> |
371 | 295 |
372 <p class="tbd"> | 296 <p class="tbd"> |
373 [ | 297 [ |
374 PENDING: summarize, suggest where to go next. Probably: | 298 PENDING: summarize, suggest where to go next. Probably: |
375 <a href="getstarted.html">Getting Started</a>, | 299 <a href="getstarted.html">Getting Started</a>, |
376 <a href="tut_debugging.html">Debugging</a>, | 300 <a href="tut_debugging.html">Debugging</a>, |
377 <a href="devguide.html">Developer's Guide</a>. | 301 <a href="devguide.html">Developer's Guide</a>. |
378 ] | 302 ] |
379 </p> | 303 </p> |
OLD | NEW |