5 Once you've finished this page
7 <a href=
"getstarted">Getting Started
</a> tutorial,
8 you'll be all set to start writing extensions.
11 <h2 id=
"what">The basics
</h2>
14 An extension is a zipped bundle of files
—HTML,
15 CSS, JavaScript, images, and anything else you need
—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.
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>.
33 <h3 id=
"extension-ui">Extension UIs
</h3>
36 Many extensions
—but not Chrome Apps
—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.
47 <table class=
"simple">
50 <img src=
"{{static}}/images/overview/browser-action.png"
51 width=
"147" height=
"100"
55 <img src=
"{{static}}/images/overview/page-action.png"
56 width=
"147" height=
"100"
60 <img src=
"{{static}}/images/overview/browser-action-with-popup.png"
61 width=
"147" height=
"100"
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).
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).
80 This
<a href=
"samples#news-reader">News Reader extension
</a>
81 features a browser action that,
83 shows a
<em>popup
</em>.
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
99 <h2 id=
"files">Files
</h2>
101 Each extension has the following files:
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
—for
110 example, image files
</li>
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>.
127 <h3 id=
"relative-urls">Referring to files
</h3>
130 You can put any file you like into an extension,
131 but how do you use it?
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>.
141 <img
<b>src=
"images/myimage.png"</b>>
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:
150 <b>chrome-extension://
</b><em><extensionID
></em><b>/
</b><em><pathToFile
></em>
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.
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.
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.
188 <h3 id=
"manifest">The manifest file
</h3>
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:
199 <pre data-filename=
"manifest.json">
201 "name":
"My Extension",
203 "description":
"Gets information from Google.",
204 "icons": {
"128":
"icon_128.png" },
209 "permissions": [
"http://*.google.com/",
"https://*.google.com/"],
212 "default_icon":
"icon_19.png",
213 "default_popup":
"popup.html"
219 <a href=
"manifest">Manifest Files
</a>.
222 <h2 id=
"arch">Architecture
</h2>
225 Many extensions have a
<em>background page
</em>,
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.
236 <h3 id=
"background_page">The background page
</h3>
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.
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." />
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
265 <!-- PENDING: Perhaps show a picture of many background page processes.
266 This could build on a figure that shows the process architecture. -->
269 See
<a href=
"event_pages">Event Pages
</a>
270 and
<a href=
"background_pages">Background Pages
</a>
274 <h3 id=
"pages">UI pages
</h3>
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.
284 use $(ref:tabs.create)
285 or
<code>window.open()
</code>
286 to display any other HTML files that are in the extension.
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.
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.) -->
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.
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)." />
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
325 <h3 id=
"contentScripts">Content scripts
</h3>
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>).
338 <!-- [PENDING: Consider explaining that the reason content scripts are separated from the extension is due to chrome's multiprocess design. Something like:
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.]
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,
353 the DOM for the displayed web page.
354 It cannot, however, modify the DOM of its parent extension's background page.
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)." />
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.
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." />
378 For more information,
379 see
<a href=
"content_scripts">Content Scripts
</a>.
383 <h2 id=
"apis"> Using the chrome.* APIs
</h2>
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
398 <h3 id=
"sync"> Asynchronous vs. synchronous methods
</h3>
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:
411 chrome.tabs.create(object
<em>createProperties
</em>, function
<em>callback
</em>)
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.
421 $(ref:runtime.getURL) method:
426 string chrome.runtime.getURL()
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.
437 <h3 id=
"sync-example"> Example: Using a callback
</h3>
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)).
449 If
<code>query()
</code> were synchronous,
450 you might write code like this:
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});
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:
471 chrome.tabs.query(object
<em>queryInfo
</em>, function
<em>callback
</em>)
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>.
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});
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.
505 <h3 id=
"chrome-more"> More details
</h3>
508 For more information, see the
509 <a href=
"api_index">chrome.* API docs
</a>
510 and watch this video:
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>
517 <h2 id=
"pageComm">Communication between pages
</h2>
520 The HTML pages within an extension often need to communicate.
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.
528 To find pages in the extension, use
529 <a href=
"extension"><code>chrome.extension
</code></a>
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.
538 <h2 id=
"incognito"> Saving data and incognito mode
</h2>
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
551 when the browser is incognito.
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,
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.
574 To detect whether a window is in incognito mode,
575 check the
<code>incognito
</code> property of the relevant
577 $(ref:windows.Window) object.
582 function saveTabData(tab, data) {
584 chrome.runtime.getBackgroundPage(function(bgPage) {
585 bgPage[tab.url] = data; // Persist data ONLY in memory
588 localStorage[tab.url] = data; // OK to store data
594 <h2 id=
"now-what"> Now what?
</h2>
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:
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>,
609 <a href=
"http://www.youtube.com/watch?v=B4M_a7xejYI&feature=PlayList&p=CA101D6A85FE9D4B&index=6">Extension Message Passing
</a>