overview.html revision 3dff810fe0cc4962a5fa554318e9bf8bc45f5274
1<div id="pageData-name" class="pageData">Overview</div> 2<div id="pageData-showTOC" class="pageData">true</div> 3 4<p> 5Once you've finished this page 6and the 7<a href="getstarted.html">Getting Started</a> tutorial, 8you'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, 14so unless otherwise stated, 15everything in this page applies to packaged apps. 16</p> 17 18<h2 id="what">The basics</h2> 19 20<p> 21An extension is a zipped bundle of files—HTML, 22CSS, JavaScript, images, and anything else you need—that 23adds functionality to the Google Chrome browser. 24Extensions are essentially web pages, 25and they can use all the 26<a href="api_other.html">APIs that the browser provides to web pages</a>, 27from XMLHttpRequest to JSON to HTML5. 28</p> 29 30<p> 31Extensions 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>. 34Extensions can also interact programmatically 35with browser features such as 36<a href="bookmarks.html">bookmarks</a> 37and <a href="tabs.html">tabs</a>. 38</p> 39 40<h3 id="extension-ui">Extension UIs</h3> 41 42<p> 43Many extensions—but not packaged apps—add 44UI to Google Chrome in the form of 45<a href="browserAction.html">browser actions</a> 46or <a href="pageAction.html">page actions</a>. 47Each extension can have at most one browser action or page action. 48Choose a <b>browser action</b> when the extension is relevant to most pages. 49Choose a <b>page action</b> when the extension's icon 50should appear or disappear, 51depending 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> 96Extensions (and packaged apps) can also present a UI in other ways, 97such as adding to the Chrome context menu, 98providing an options page, 99or using a content script that changes how pages look. 100See the <a href="devguide.html">Developer's Guide</a> 101for a complete list of extension features, 102with links to implementation details 103for each one. 104</p> 105 106 107<h3 id="packagedapp-ui">Packaged app UIs</h3> 108 109<p> 110A packaged app usually presents its main functionality using 111an HTML page that's bundled into the app. 112For example, the following packaged app 113displays 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> 121For more information, 122see <a href="apps.html">Packaged Apps</a>. 123</p> 124 125<h2 id="files">Files</h2> 126<p> 127Each 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—for 136 example, image files</li> 137</ul> 138 139<p> 140While you're working on your extension, 141you put all these files into a single folder. 142When you distribute your extension, 143the contents of the folder are packaged into a special ZIP file 144that has a <code>.crx</code> suffix. 145If you upload your extension using the 146<a href="https://chrome.google.com/webstore/developer/dashboard">Chrome Developer Dashboard</a>, 147the <code>.crx</code> file is created for you. 148For details on distributing extensions, 149see <a href="hosting.html">Hosting</a>. 150</p> 151 152 153<h3 id="relative-urls">Referring to files</h3> 154 155<p> 156You can put any file you like into an extension, 157but how do you use it? 158Usually, 159you can refer to the file using a relative URL, 160just as you would in an ordinary HTML page. 161Here's an example of referring to 162a file named <code>myimage.png</code> 163that's in a subfolder named <code>images</code>. 164</p> 165 166<pre> 167<img <b>src="images/myimage.png"</b>> 168</pre> 169 170<p> 171As you might notice while you use the Google Chrome debugger, 172every file in an extension is also accessible by an absolute URL like this: 173</p> 174 175<blockquote> 176<b>chrome-extension://</b><em><extensionID></em><b>/</b><em><pathToFile></em> 177</blockquote> 178 179<p> 180In that URL, the <em><extensionID></em> is a unique identifier 181that the extension system generates for each extension. 182You can see the IDs for all your loaded extensions 183by going to the URL <b>chrome://extensions</b>. 184The <em><pathToFile></em> is the location of the file 185under the extension's top folder; 186it'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> 195The manifest file, called <code>manifest.json</code>, 196gives information about the extension, 197such as the most important files 198and the capabilities that the extension might use. 199Here's a typical manifest file for a browser action 200that 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> 219For details, see 220<a href="manifest.html">Manifest Files</a>. 221</p> 222 223<h2 id="arch">Architecture</h2> 224 225<p> 226Many extensions have a <em>background page</em>, 227an invisible page 228that holds the main logic of the extension. 229An extension can also contain other pages 230that present the extension's UI. 231If an extension needs to interact with web pages that the user loads 232(as opposed to pages that are included in the extension), 233then the extension must use a content script. 234</p> 235 236 237<h3 id="background_page">The background page</h3> 238 239<p> 240The following figure shows a browser 241that has at least two extensions installed: 242a browser action (yellow icon) 243and a page action (blue icon). 244Both the browser action and the page action 245have background pages defined by HTML files. 246This figure shows the browser action's background page, 247which is defined by <code>background.html</code> 248and has JavaScript code that controls 249the 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> 257Although background pages can be useful, 258don't use one if you don't need it. 259Background pages are always open, 260so when a user installs many extensions that have background pages, 261Chrome'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> 269Here 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>—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> 294See <a href="background_pages.html">Background Pages</a> 295for more details. 296</p> 297 298<h3 id="pages">UI pages</h3> 299 300<p> 301Extensions can contain ordinary HTML pages that display the extension's UI. 302For example, a browser action can have a popup, 303which is implemented by an HTML file. 304Any extension can have an options page, 305which lets users customize how the extension works. 306Another type of special page is the override page. 307And finally, you can 308use <a href="tabs.html#method-create">chrome.tabs.create()</a> 309or <code>window.open()</code> 310to display any other HTML files that are in the extension. 311</p> 312 313<p> 314The HTML pages inside an extension 315have complete access to each other's DOMs, 316and they can invoke functions on each other. 317</p> 318 319<!-- PENDING: Change the following example and figure 320to use something that's not a popup? 321(It might lead people to think that popups need background pages.) --> 322 323<p> 324The following figure shows the architecture 325of a browser action's popup. 326The popup's contents are a web page 327defined by an HTML file 328(<code>popup.html</code>). 329This extension also happens to have a background page 330(<code>background.html</code>). 331The popup doesn't need to duplicate code 332that's in the background page 333because 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> 341See <a href="browserAction.html">Browser Actions</a>, 342<a href="options.html">Options</a>, 343<a href="override.html">Override Pages</a>, 344and the <a href="#pageComm">Communication between pages</a> section 345for more details. 346</p> 347 348 349<h3 id="contentScripts">Content scripts</h3> 350 351<p> 352If your extension needs to interact with web pages, 353then it needs a <em>content script</em>. 354A content script is some JavaScript 355that executes in the context of a page 356that's been loaded into the browser. 357Think of a content script as part of that loaded page, 358not 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 364Each extension runs in its own process. 365To have rich interaction with a web page, however, 366the extension must be able to 367run some code in the web page's process. 368Extensions accomplish this with content scripts.] 369--> 370 371<p> 372Content scripts can read details of the web pages the browser visits, 373and they can make changes to the pages. 374In the following figure, 375the content script 376can read and modify 377the DOM for the displayed web page. 378It 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> 386Content scripts aren't completely cut off from their parent extensions. 387A content script can exchange messages with its parent extension, 388as the arrows in the following figure show. 389For example, a content script might send a message 390whenever it finds an RSS feed in a browser page. 391Or a background page might send a message 392asking 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> 402For more information, 403see <a href="content_scripts.html">Content Scripts</a>. 404</p> 405 406 407<h2 id="apis"> Using the chrome.* APIs </h2> 408 409<p> 410In addition to having access to all the APIs that web pages and apps can use, 411extensions can also use Chrome-only APIs 412(often called <em>chrome.* APIs</em>) 413that allow tight integration with the browser. 414For example, any extension or web app can use the 415standard <code>window.open()</code> method to open a URL. 416But if you want to specify which window that URL should be displayed in, 417your extension can use the Chrome-only 418<a href="tabs.html#method-create">chrome.tabs.create()</a> 419method instead. 420</p> 421 422<h3 id="sync"> Asynchronous vs. synchronous methods </h3> 423<p> 424Most methods in the chrome.* APIs are <b>asynchronous</b>: 425they return immediately, without waiting for the operation to finish. 426If you need to know the outcome of that operation, 427then you pass a callback function into the method. 428That callback is executed later (potentially <em>much</em> later), 429sometime after the method returns. 430Here's an example of the signature for an asynchronous method: 431</p> 432 433<p> 434<code> 435chrome.tabs.create(object <em>createProperties</em>, function <em>callback</em>) 436</code> 437</p> 438 439<p> 440Other chrome.* methods are <b>synchronous</b>. 441Synchronous methods never have a callback 442because they don't return until they've completed all their work. 443Often, synchronous methods have a return type. 444Consider the 445<a href="extension.html#method-getBackgroundPage">chrome.extensions.getBackgroundPage()</a> method: 446</p> 447 448<p> 449<code> 450DOMWindow chrome.extension.getBackgroundPage() 451</code> 452</p> 453 454<p> 455This method has no callback and a return type of <code>DOMWindow</code> 456because it synchronously returns the background page 457and performs no other, asynchronous work. 458</p> 459 460 461<h3 id="sync-example"> Example: Using a callback </h3> 462 463<p> 464Say you want to navigate 465the user's currently selected tab to a new URL. 466To do this, you need to get the current tab's ID 467(using <a href="tabs.html#method-getSelected">chrome.tabs.getSelected()</a>) 468and 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> 473If <code>getSelected()</code> were synchronous, 474you 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> 485That approach fails 486because <code>getSelected()</code> is asynchronous. 487It returns without waiting for its work to complete, 488and it doesn't even return a value 489(although some asynchronous methods do). 490You can tell that <code>getSelected()</code> is asynchronous 491by the <em>callback</em> parameter in its signature: 492 493<p> 494<code> 495chrome.tabs.getSelected(integer <em>windowId</em>, function <em>callback</em>) 496</code> 497</p> 498 499<p> 500To fix the preceding code, 501you must use that callback parameter. 502The following code shows 503how to define a callback function 504that gets the results from <code>getSelected()</code> 505(as a parameter named <code>tab</code>) 506and 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> 518In this example, the lines are executed in the following order: 1, 4, 2. 519The callback function specified to <code>getSelected</code> is called 520(and line 2 executed) 521only after information about the currently selected tab is available, 522which is sometime after <code>getSelected()</code> returns. 523Although <code>update()</code> is asynchronous, 524this example doesn't use its callback parameter, 525since 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> 532For more information, see the 533<a href="api_index.html">chrome.* API docs</a> 534and 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> 544The HTML pages within an extension often need to communicate. 545<!-- [PENDING: For example, ...] --> 546Because all of an extension's pages 547execute in same process on the same thread, 548the pages can make direct function calls to each other. 549</p> 550 551<p> 552To find pages in the extension, use 553<a href="extension.html"><code>chrome.extension</code></a> 554methods such as 555<code>getViews()</code> and 556<code>getBackgroundPage()</code>. 557Once a page has a reference to other pages within the extension, 558the first page can invoke functions on the other pages, 559and 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> 568Extensions can save data using 569the HTML5 <a href="http://dev.w3.org/html5/webstorage/">web storage API</a> 570(such as <code>localStorage</code>) 571or by making server requests that result in saving data. 572Whenever you want to save something, 573first consider whether it's 574from a window that's in incognito mode. 575By default, extensions don't run in incognito windows, 576and packaged apps <em>do</em>. 577You need to consider what a user expects 578from your extension or packaged app 579when the browser is incognito. 580</p> 581 582<p> 583<em>Incognito mode</em> promises that the window will leave no tracks. 584When dealing with data from incognito windows, 585do your best to honor this promise. 586For example, if your extension normally 587saves browsing history to the cloud, 588don't save history from incognito windows. 589On the other hand, you can store 590your extension's settings from any window, 591incognito or not. 592</p> 593 594<p class="note"> 595<b>Rule of thumb:</b> 596If a piece of data might show where a user 597has been on the web or what the user has done, 598don't store it if it's from an incognito window. 599</p> 600 601<p> 602To detect whether a window is in incognito mode, 603check 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. 606For example: 607</p> 608 609<pre> 610var bgPage = chrome.extension.getBackgroundPage(); 611 612function 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> 624Now that you've been introduced to extensions, 625you should be ready to write your own. 626Here 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