OLD | NEW |
(Empty) | |
| 1 <h1 class="page_title">Background Pages</h1> |
| 2 <p id="eventPageWarning" class="warning"> |
| 3 <em>Caution:</em> Consider using event pages instead. |
| 4 <a href="event_pages.html">Learn more</a>. |
| 5 </p> |
| 6 <p> |
| 7 A common need for extensions is to have |
| 8 a single long-running script to manage some task or state. |
| 9 Background pages to the rescue. |
| 10 </p> |
| 11 <p> |
| 12 As the <a href="overview.html#arch">architecture overview</a> explains, |
| 13 the background page is an HTML page that runs in the extension process. |
| 14 It exists for the lifetime of your extension, |
| 15 and only one instance of it at a time is active. |
| 16 </p> |
| 17 <p> |
| 18 In a typical extension with a background page, |
| 19 the UI — |
| 20 for example, the browser action or page action |
| 21 and any options page — |
| 22 is implemented by dumb views. |
| 23 When the view needs some state, |
| 24 it requests the state from the background page. |
| 25 When the background page notices a state change, |
| 26 the background page tells the views to update. |
| 27 </p> |
| 28 <h2 id="manifest">Manifest</h2> |
| 29 <p> |
| 30 Register your background page in the |
| 31 <a href="manifest.html">extension manifest</a>. |
| 32 In the common case, a background page |
| 33 does not require any HTML markup. |
| 34 These kind of background pages can be |
| 35 implemented using JavaScript files alone, |
| 36 like this: |
| 37 </p> |
| 38 <pre>{ |
| 39 "name": "My extension", |
| 40 ... |
| 41 <b>"background": { |
| 42 "scripts": ["background.js"] |
| 43 }</b>, |
| 44 ... |
| 45 }</pre> |
| 46 <p> |
| 47 A background page will be generated |
| 48 by the extension system |
| 49 that includes each of the files listed |
| 50 in the <code>scripts</code> property. |
| 51 </p> |
| 52 <p> |
| 53 If you need to specify HTML |
| 54 in your background page, you can |
| 55 do that using the <code>page</code> |
| 56 property instead: |
| 57 </p> |
| 58 <pre>{ |
| 59 "name": "My extension", |
| 60 ... |
| 61 <b>"background": { |
| 62 "page": "background.html" |
| 63 }</b>, |
| 64 ... |
| 65 }</pre> |
| 66 <p> |
| 67 If you need the browser to start up early—so |
| 68 you can display notifications, for example—then |
| 69 you might also want to specify the |
| 70 <a href="manifest.html#permissions">"background" permission</a>. |
| 71 </p> |
| 72 <h2>Details</h2> |
| 73 <p> |
| 74 You can communicate between your various pages using direct script calls, |
| 75 similar to how frames can communicate. |
| 76 The <a href="extension.html#method-getViews"><code>chrome.extension.getViews()</
code></a> method |
| 77 returns a list of window objects |
| 78 for every active page belonging to your extension, |
| 79 and the |
| 80 <a href="extension.html#method-getBackgroundPage"><code>chrome.extension.getBack
groundPage()</code></a> method |
| 81 returns the background page. |
| 82 </p> |
| 83 <h2 id="example">Example</h2> |
| 84 <p> |
| 85 The following code snippet demonstrates |
| 86 how the background page |
| 87 can interact with other pages in the extension. |
| 88 It also shows how you can use |
| 89 the background page to handle events |
| 90 such as user clicks. |
| 91 </p> |
| 92 <p> |
| 93 The extension in this example |
| 94 has a background page |
| 95 and multiple pages created |
| 96 (with |
| 97 <a href="tabs.html#method-create"><code>chrome.tabs.create()</code></a>) |
| 98 from a file named <code>image.html</code>. |
| 99 </p> |
| 100 <pre> |
| 101 <em>//In background.js:</em> |
| 102 // React when a browser action's icon is clicked. |
| 103 chrome.browserAction.onClicked.addListener(function(tab) { |
| 104 var viewTabUrl = chrome.extension.getURL('image.html'); |
| 105 var imageUrl = <em>/* an image's URL */</em>; |
| 106 // Look through all the pages in this extension to find one we can use. |
| 107 var views = chrome.extension.getViews(); |
| 108 for (var i = 0; i < views.length; i++) { |
| 109 var view = views[i]; |
| 110 // If this view has the right URL and hasn't been used yet... |
| 111 if (view.location.href == viewTabUrl && !view.imageAlreadySet) { |
| 112 // ...call one of its functions and set a property. |
| 113 view.setImageUrl(imageUrl); |
| 114 view.imageAlreadySet = true; |
| 115 break; // we're done |
| 116 } |
| 117 } |
| 118 }); |
| 119 <em>//In image.html:</em> |
| 120 <html> |
| 121 <script> |
| 122 function setImageUrl(url) { |
| 123 document.getElementById('target').src = url; |
| 124 } |
| 125 </script> |
| 126 <body> |
| 127 <p> |
| 128 Image here: |
| 129 </p> |
| 130 <img id="target" src="white.png" width="640" height="480"> |
| 131 </body> |
| 132 </html> |
| 133 </pre> |
OLD | NEW |