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