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