• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 <div id="pageData-name" class="pageData">Overview</div>
2 <div id="pageData-showTOC" class="pageData">true</div>
3 
4 <p>
5 Once you've finished this page
6 and the
7 <a href="getstarted.html">Getting Started</a> tutorial,
8 you'll be all set to start writing extensions and packaged apps.
9 </p>
10 
11 <p class="caution">
12 <strong>Note:</strong>
13 <em>Packaged apps</em> are implemented as extensions,
14 so unless otherwise stated,
15 everything in this page applies to packaged apps.
16 </p>
17 
18 <h2 id="what">The basics</h2>
19 
20 <p>
21 An extension is a zipped bundle of files&mdash;HTML,
22 CSS, JavaScript, images, and anything else you need&mdash;that
23 adds functionality to the Google Chrome browser.
24 Extensions are essentially web pages,
25 and they can use all the
26 <a href="api_other.html">APIs that the browser provides to web pages</a>,
27 from XMLHttpRequest to JSON to HTML5.
28 </p>
29 
30 <p>
31 Extensions can interact with web pages or servers using
32 <a href="content_scripts.html">content scripts</a> or
33 <a href="xhr.html">cross-origin XMLHttpRequests</a>.
34 Extensions can also interact programmatically
35 with browser features such as
36 <a href="bookmarks.html">bookmarks</a>
37 and <a href="tabs.html">tabs</a>.
38 </p>
39 
40 <h3 id="extension-ui">Extension UIs</h3>
41 
42 <p>
43 Many extensions&mdash;but not packaged apps&mdash;add
44 UI to Google Chrome in the form of
45 <a href="browserAction.html">browser actions</a>
46 or <a href="pageAction.html">page actions</a>.
47 Each extension can have at most one browser action or page action.
48 Choose a <b>browser action</b> when the extension is relevant to most pages.
49 Choose a <b>page action</b> when the extension's icon
50 should appear or disappear,
51 depending on the page.
52 </p>
53 
54 <table class="columns">
55 <tr>
56   <td width="33%">
57     <img src="images/overview/browser-action.png"
58       width="147" height="100"
59       alt="screenshot" />
60   </td>
61   <td width="33%">
62     <img src="images/overview/page-action.png"
63       width="147" height="100"
64       alt="screenshot" />
65   </td>
66   <td>
67     <img src="images/overview/browser-action-with-popup.png"
68       width="147" height="100"
69       alt="screenshot" />
70   </td>
71 </tr>
72 
73 <tr>
74   <td>
75     This <a href="samples.html#gmail">mail extension</a>
76     uses a <em>browser action</em>
77     (icon in the toolbar).
78   </td>
79   <td>
80     This <a href="samples.html#mappy">map extension</a>
81     uses a <em>page action</em>
82     (icon in the address bar)
83     and <em>content script</em>
84     (code injected into a web page).
85   </td>
86   <td>
87     This <a href="samples.html#news">news extension</a>
88     features a browser action that,
89     when clicked,
90     shows a <em>popup</em>.
91   </td>
92 </tr>
93 </table>
94 
95 <p>
96 Extensions (and packaged apps) can also present a UI in other ways,
97 such as adding to the Chrome context menu,
98 providing an options page,
99 or using a content script that changes how pages look.
100 See the <a href="devguide.html">Developer's Guide</a>
101 for a complete list of extension features,
102 with links to implementation details
103 for each one.
104 </p>
105 
106 
107 <h3 id="packagedapp-ui">Packaged app UIs</h3>
108 
109 <p>
110 A packaged app usually presents its main functionality using
111 an HTML page that's bundled into the app.
112 For example, the following packaged app
113 displays a Flash file within an HTML page.
114 </p>
115 
116 <img src="images/overview/flash-app.png"
117   width="372" height="300"
118   alt="screenshot" />
119 
120 <p>
121 For more information,
122 see <a href="apps.html">Packaged Apps</a>.
123 </p>
124 
125 <h2 id="files">Files</h2>
126 <p>
127 Each extension has the following files:
128 <!-- PENDING: This could use a picture -->
129 </p>
130 
131 <ul>
132   <li>A <b>manifest file</b></li>
133   <li>One or more <b>HTML files</b> (unless the extension is a theme)</li>
134   <li><em>Optional:</em> One or more <b>JavaScript files</b></li>
135   <li><em>Optional:</em> Any other files your extension needs&mdash;for
136   example, image files</li>
137 </ul>
138 
139 <p>
140 While you're working on your extension,
141 you put all these files into a single folder.
142 When you distribute your extension,
143 the contents of the folder are packaged into a special ZIP file
144 that has a <code>.crx</code> suffix.
145 If you upload your extension using the
146 <a href="https://chrome.google.com/webstore/developer/dashboard">Chrome Developer Dashboard</a>,
147 the <code>.crx</code> file is created for you.
148 For details on distributing extensions,
149 see <a href="hosting.html">Hosting</a>.
150 </p>
151 
152 
153 <h3 id="relative-urls">Referring to files</h3>
154 
155 <p>
156 You can put any file you like into an extension,
157 but how do you use it?
158 Usually,
159 you can refer to the file using a relative URL,
160 just as you would in an ordinary HTML page.
161 Here's an example of referring to
162 a file named <code>myimage.png</code>
163 that's in a subfolder named <code>images</code>.
164 </p>
165 
166 <pre>
167 &lt;img <b>src="images/myimage.png"</b>>
168 </pre>
169 
170 <p>
171 As you might notice while you use the Google Chrome debugger,
172 every file in an extension is also accessible by an absolute URL like this:
173 </p>
174 
175 <blockquote>
176 <b>chrome-extension://</b><em>&lt;extensionID></em><b>/</b><em>&lt;pathToFile></em>
177 </blockquote>
178 
179 <p>
180 In that URL, the <em>&lt;extensionID></em> is a unique identifier
181 that the extension system generates for each extension.
182 You can see the IDs for all your loaded extensions
183 by going to the URL <b>chrome://extensions</b>.
184 The <em>&lt;pathToFile></em> is the location of the file
185 under the extension's top folder;
186 it's the same as the relative URL.
187 </p>
188 
189 <!-- [PENDING: Should mention/reflect/link to <a href="http://dev.chromium.org/developers/design-documents/extensions/i18n">internationalization</a> when it's ready.] -->
190 
191 
192 <h3>The manifest file</h3>
193 
194 <p>
195 The manifest file, called <code>manifest.json</code>,
196 gives information about the extension,
197 such as the most important files
198 and the capabilities that the extension might use.
199 Here's a typical manifest file for a browser action
200 that uses information from google.com:
201 </p>
202 
203 <pre>
204 {
205   "name": "My Extension",
206   "version": "2.1",
207   "description": "Gets information from Google.",
208   "icons": { "128": "icon_128.png" },
209   "background_page": "bg.html",
210   "permissions": ["http://*.google.com/", "https://*.google.com/"],
211   "browser_action": {
212     "default_title": "",
213     "default_icon": "icon_19.png",
214     "default_popup": "popup.html"
215   }
216 }</pre>
217 
218 <p>
219 For details, see
220 <a href="manifest.html">Manifest Files</a>.
221 </p>
222 
223 <h2 id="arch">Architecture</h2>
224 
225 <p>
226 Many extensions have a <em>background page</em>,
227 an invisible page
228 that holds the main logic of the extension.
229 An extension can also contain other pages
230 that present the extension's UI.
231 If an extension needs to interact with web pages that the user loads
232 (as opposed to pages that are included in the extension),
233 then the extension must use a content script.
234 </p>
235 
236 
237 <h3 id="background_page">The background page</h3>
238 
239 <p>
240 The following figure shows a browser
241 that has at least two extensions installed:
242 a browser action (yellow icon)
243 and a page action (blue icon).
244 Both the browser action and the page action
245 have background pages defined by HTML files.
246 This figure shows the browser action's background page,
247 which is defined by <code>background.html</code>
248 and has JavaScript code that controls
249 the behavior of the browser action in both windows.
250 </p>
251 
252 <img src="images/overview/arch-1.gif"
253  width="232" height="168"
254  alt="Two windows and a box representing a background page (background.html). One window has a yellow icon; the other has both a yellow icon and a blue icon. The yellow icons are connected to the background page." />
255 
256 <p>
257 Although background pages can be useful,
258 don't use one if you don't need it.
259 Background pages are always open,
260 so when a user installs many extensions that have background pages,
261 Chrome's performance can suffer.
262 </p>
263 
264 <!-- PENDING: Perhaps show a picture of many background page processes.
265   This could build on a figure that shows the process architecture,
266   and perhaps the differences between packaged apps and extensions. -->
267 
268 <p>
269 Here are some examples of extensions that usually
270 <b>do not need</b> a background page:
271 </p>
272 
273 <ul>
274   <li> An extension with a browser action that
275     presents its UI solely through a popup
276     (and perhaps an options page).
277   </li>
278   <li>
279     An extension that provides an <em>override page</em>&mdash;a
280     page that replaces a standard Chrome page.
281   </li>
282   <li>
283     An extension with a content script
284     that doesn't use cross-origin XMLHttpRequests or localStorage,
285     and that doesn't need to use
286     <a href="#apis">extension APIs</a>.
287   </li>
288   <li>
289     An extension that has no UI except for an options page.
290   </li>
291 </ul>
292 
293 <p>
294 See <a href="background_pages.html">Background Pages</a>
295 for more details.
296 </p>
297 
298 <h3 id="pages">UI pages</h3>
299 
300 <p>
301 Extensions can contain ordinary HTML pages that display the extension's UI.
302 For example, a browser action can have a popup,
303 which is implemented by an HTML file.
304 Any extension can have an options page,
305 which lets users customize how the extension works.
306 Another type of special page is the override page.
307 And finally, you can
308 use <a href="tabs.html#method-create">chrome.tabs.create()</a>
309 or <code>window.open()</code>
310 to display any other HTML files that are in the extension.
311 </p>
312 
313 <p>
314 The HTML pages inside an extension
315 have complete access to each other's DOMs,
316 and they can invoke functions on each other.
317 </p>
318 
319 <!-- PENDING: Change the following example and figure
320 to use something that's not a popup?
321 (It might lead people to think that popups need background pages.) -->
322 
323 <p>
324 The following figure shows the architecture
325 of a browser action's popup.
326 The popup's contents are a web page
327 defined by an HTML file
328 (<code>popup.html</code>).
329 This extension also happens to have a background page
330 (<code>background.html</code>).
331 The popup doesn't need to duplicate code
332 that's in the background page
333 because the popup can invoke functions on the background page.
334 </p>
335 
336 <img src="images/overview/arch-2.gif"
337  width="256" height="168"
338  alt="A browser window containing a browser action that's displaying a popup. The popup's HTML file (popup.html) can communicate with the extension's background page (background.html)." />
339 
340 <p>
341 See <a href="browserAction.html">Browser Actions</a>,
342 <a href="options.html">Options</a>,
343 <a href="override.html">Override Pages</a>,
344 and the <a href="#pageComm">Communication between pages</a> section
345 for more details.
346 </p>
347 
348 
349 <h3 id="contentScripts">Content scripts</h3>
350 
351 <p>
352 If your extension needs to interact with web pages,
353 then it needs a <em>content script</em>.
354 A content script is some JavaScript
355 that executes in the context of a page
356 that's been loaded into the browser.
357 Think of a content script as part of that loaded page,
358 not as part of the extension it was packaged with
359 (its <em>parent extension</em>).
360 </p>
361 
362 <!-- [PENDING: Consider explaining that the reason content scripts are separated from the extension is due to chrome's multiprocess design.  Something like:
363 
364 Each extension runs in its own process.
365 To have rich interaction with a web page, however,
366 the extension must be able to
367 run some code in the web page's process.
368 Extensions accomplish this with content scripts.]
369 -->
370 
371 <p>
372 Content scripts can read details of the web pages the browser visits,
373 and they can make changes to the pages.
374 In the following figure,
375 the content script
376 can read and modify
377 the DOM for the displayed web page.
378 It cannot, however, modify the DOM of its parent extension's background page.
379 </p>
380 
381 <img src="images/overview/arch-3.gif"
382  width="238" height="169"
383  alt="A browser window with a browser action (controlled by background.html) and a content script (controlled by contentscript.js)." />
384 
385 <p>
386 Content scripts aren't completely cut off from their parent extensions.
387 A content script can exchange messages with its parent extension,
388 as the arrows in the following figure show.
389 For example, a content script might send a message
390 whenever it finds an RSS feed in a browser page.
391 Or a background page might send a message
392 asking a content script to change the appearance of its browser page.
393 </p>
394 
395 <img src="images/overview/arch-cs.gif"
396  width="238" height="194"
397  alt="Like the previous figure, but showing more of the parent extension's files, as well as a communication path between the content script and the parent extension." />
398 
399 <!-- [PENDING: Add overview of message passing.] -->
400 
401 <p>
402 For more information,
403 see <a href="content_scripts.html">Content Scripts</a>.
404 </p>
405 
406 
407 <h2 id="apis"> Using the chrome.* APIs </h2>
408 
409 <p>
410 In addition to having access to all the APIs that web pages and apps can use,
411 extensions can also use Chrome-only APIs
412 (often called <em>chrome.* APIs</em>)
413 that allow tight integration with the browser.
414 For example, any extension or web app can use the
415 standard <code>window.open()</code> method to open a URL.
416 But if you want to specify which window that URL should be displayed in,
417 your extension can use the Chrome-only
418 <a href="tabs.html#method-create">chrome.tabs.create()</a>
419 method instead.
420 </p>
421 
422 <h3 id="sync"> Asynchronous vs. synchronous methods </h3>
423 <p>
424 Most methods in the chrome.* APIs are <b>asynchronous</b>:
425 they return immediately, without waiting for the operation to finish.
426 If you need to know the outcome of that operation,
427 then you pass a callback function into the method.
428 That callback is executed later (potentially <em>much</em> later),
429 sometime after the method returns.
430 Here's an example of the signature for an asynchronous method:
431 </p>
432 
433 <p>
434 <code>
435 chrome.tabs.create(object <em>createProperties</em>, function <em>callback</em>)
436 </code>
437 </p>
438 
439 <p>
440 Other chrome.* methods are <b>synchronous</b>.
441 Synchronous methods never have a callback
442 because they don't return until they've completed all their work.
443 Often, synchronous methods have a return type.
444 Consider the
445 <a href="extension.html#method-getBackgroundPage">chrome.extensions.getBackgroundPage()</a> method:
446 </p>
447 
448 <p>
449 <code>
450 DOMWindow chrome.extension.getBackgroundPage()
451 </code>
452 </p>
453 
454 <p>
455 This method has no callback and a return type of <code>DOMWindow</code>
456 because it synchronously returns the background page
457 and performs no other, asynchronous work.
458 </p>
459 
460 
461 <h3 id="sync-example"> Example: Using a callback </h3>
462 
463 <p>
464 Say you want to navigate
465 the user's currently selected tab to a new URL.
466 To do this, you need to get the current tab's ID
467 (using <a href="tabs.html#method-getSelected">chrome.tabs.getSelected()</a>)
468 and then make that tab go to the new URL
469 (using <a href="tabs.html#method-update">chrome.tabs.update()</a>).
470 </p>
471 
472 <p>
473 If <code>getSelected()</code> were synchronous,
474 you might write code like this:
475 </p>
476 
477 <pre>
478    <b>//THIS CODE DOESN'T WORK</b>
479 <span class="linenumber">1: </span>var tab = chrome.tabs.getSelected(null); <b>//WRONG!!!</b>
480 <span class="linenumber">2: </span>chrome.tabs.update(tab.id, {url:newUrl});
481 <span class="linenumber">3: </span>someOtherFunction();
482 </pre>
483 
484 <p>
485 That approach fails
486 because <code>getSelected()</code> is asynchronous.
487 It returns without waiting for its work to complete,
488 and it doesn't even return a value
489 (although some asynchronous methods do).
490 You can tell that <code>getSelected()</code> is asynchronous
491 by the <em>callback</em> parameter in its signature:
492 
493 <p>
494 <code>
495 chrome.tabs.getSelected(integer <em>windowId</em>, function <em>callback</em>)
496 </code>
497 </p>
498 
499 <p>
500 To fix the preceding code,
501 you must use that callback parameter.
502 The following code shows
503 how to define a callback function
504 that gets the results from <code>getSelected()</code>
505 (as a parameter named <code>tab</code>)
506 and calls <code>update()</code>.
507 </p>
508 
509 <pre>
510    <b>//THIS CODE WORKS</b>
511 <span class="linenumber">1: </span>chrome.tabs.getSelected(null, <b>function(tab) {</b>
512 <span class="linenumber">2: </span>  chrome.tabs.update(tab.id, {url:newUrl});
513 <span class="linenumber">3: </span><b>}</b>);
514 <span class="linenumber">4: </span>someOtherFunction();
515 </pre>
516 
517 <p>
518 In this example, the lines are executed in the following order: 1, 4, 2.
519 The callback function specified to <code>getSelected</code> is called
520 (and line 2 executed)
521 only after information about the currently selected tab is available,
522 which is sometime after <code>getSelected()</code> returns.
523 Although <code>update()</code> is asynchronous,
524 this example doesn't use its callback parameter,
525 since we don't do anything about the results of the update.
526 </p>
527 
528 
529 <h3 id="chrome-more"> More details </h3>
530 
531 <p>
532 For more information, see the
533 <a href="api_index.html">chrome.* API docs</a>
534 and watch this video:
535 </p>
536 
537 <p>
538 <iframe title="YouTube video player" width="640" height="390" src="http://www.youtube.com/embed/bmxr75CV36A?rel=0" frameborder="0" allowfullscreen></iframe>
539 </p>
540 
541 <h2 id="pageComm">Communication between pages </h2>
542 
543 <p>
544 The HTML pages within an extension often need to communicate.
545 <!-- [PENDING: For example, ...] -->
546 Because all of an extension's pages
547 execute in same process on the same thread,
548 the pages can make direct function calls to each other.
549 </p>
550 
551 <p>
552 To find pages in the extension, use
553 <a href="extension.html"><code>chrome.extension</code></a>
554 methods such as
555 <code>getViews()</code> and
556 <code>getBackgroundPage()</code>.
557 Once a page has a reference to other pages within the extension,
558 the first page can invoke functions on the other pages,
559 and it can manipulate their DOMs.
560 </p>
561 
562 <!-- [PENDING: Here's an example of communication between xyz and the background page.  (code example goes here)] -->
563 
564 
565 <h2 id="incognito"> Saving data and incognito mode </h2>
566 
567 <p>
568 Extensions can save data using
569 the HTML5 <a href="http://dev.w3.org/html5/webstorage/">web storage API</a>
570 (such as <code>localStorage</code>)
571 or by making server requests that result in saving data.
572 Whenever you want to save something,
573 first consider whether it's
574 from a window that's in incognito mode.
575 By default, extensions don't run in incognito windows,
576 and packaged apps <em>do</em>.
577 You need to consider what a user expects
578 from your extension or packaged app
579 when the browser is incognito.
580 </p>
581 
582 <p>
583 <em>Incognito mode</em> promises that the window will leave no tracks.
584 When dealing with data from incognito windows,
585 do your best to honor this promise.
586 For example, if your extension normally
587 saves browsing history to the cloud,
588 don't save history from incognito windows.
589 On the other hand, you can store
590 your extension's settings from any window,
591 incognito or not.
592 </p>
593 
594 <p class="note">
595 <b>Rule of thumb:</b>
596 If a piece of data might show where a user
597 has been on the web or what the user has done,
598 don't store it if it's from an incognito window.
599 </p>
600 
601 <p>
602 To detect whether a window is in incognito mode,
603 check the <code>incognito</code> property of the relevant
604 <a href="tabs.html#type-Tab">Tab</a> or
605 <a href="windows.html#type-Window">Window</a> object.
606 For example:
607 </p>
608 
609 <pre>
610 var bgPage = chrome.extension.getBackgroundPage();
611 
612 function saveTabData(tab, data) {
613   if (tab.incognito) {
614     bgPage[tab.url] = data;       // Persist data ONLY in memory
615   } else {
616     localStorage[tab.url] = data; // OK to store data
617 }
618 </pre>
619 
620 
621 <h2 id="now-what"> Now what? </h2>
622 
623 <p>
624 Now that you've been introduced to extensions,
625 you should be ready to write your own.
626 Here are some ideas for where to go next:
627 </p>
628 
629 <ul>
630   <li> <a href="getstarted.html">Tutorial: Getting Started</a> </li>
631   <li> <a href="tut_debugging.html">Tutorial: Debugging</a> </li>
632   <li> <a href="devguide.html">Developer's Guide</a> </li>
633   <li> <a href="http://dev.chromium.org/developers/design-documents/extensions/samples">Samples</a> </li>
634   <li> <a href="http://www.youtube.com/view_play_list?p=CA101D6A85FE9D4B">Videos</a>,
635     such as
636     <a href="http://www.youtube.com/watch?v=B4M_a7xejYI&feature=PlayList&p=CA101D6A85FE9D4B&index=6">Extension Message Passing</a>
637     </li>
638 </ul>
639