Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(31)

Side by Side Diff: chrome/common/extensions/docs/server2/templates/articles/overview.html

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

Powered by Google App Engine
This is Rietveld 408576698