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