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