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