OLD | NEW |
1 <div id="pageData-name" class="pageData">Options</div> | 1 <div id="pageData-name" class="pageData">Options</div> |
2 <div id="pageData-showTOC" class="pageData">true</div> | 2 <div id="pageData-showTOC" class="pageData">true</div> |
3 <p>To allow users to customize the behavior of your extension, you may wish to p
rovide an options page. If you do, a link to it will be provided from the extens
ions management page at chrome://extensions. Clicking the Options link opens a n
ew tab pointing at your options page. | 3 <p>To allow users to customize the behavior of your extension, you may wish to p
rovide an options page. If you do, a link to it will be provided from the extens
ions management page at chrome://extensions. Clicking the Options link opens a n
ew tab pointing at your options page. |
4 | 4 |
5 <h2>Step 1: Declare your options page in the manifest</h2> | 5 <h2>Step 1: Declare your options page in the manifest</h2> |
6 | 6 |
7 <pre>{ | 7 <pre>{ |
8 "name": "My extension", | 8 "name": "My extension", |
9 ... | 9 ... |
10 <b>"options_page": "options.html"</b>, | 10 <b>"options_page": "options.html"</b>, |
11 ... | 11 ... |
12 }</pre> | 12 }</pre> |
13 | 13 |
14 | 14 |
15 <h2>Step 2: Write your options page</h2> | 15 <h2>Step 2: Write your options page</h2> |
16 | 16 |
17 Here is an example options page: | 17 Here is an example options page: |
18 | 18 |
19 <pre> | 19 <pre>// Save this script as `options.js` |
20 <html> | |
21 <head><title>My Test Extension Options</title></head> | |
22 <script type="text/javascript"> | |
23 | 20 |
24 // Saves options to localStorage. | 21 // Saves options to localStorage. |
25 function save_options() { | 22 function save_options() { |
26 var select = document.getElementById("color"); | 23 var select = document.getElementById("color"); |
27 var color = select.children[select.selectedIndex].value; | 24 var color = select.children[select.selectedIndex].value; |
28 localStorage["favorite_color"] = color; | 25 localStorage["favorite_color"] = color; |
29 | 26 |
30 // Update status to let user know options were saved. | 27 // Update status to let user know options were saved. |
31 var status = document.getElementById("status"); | 28 var status = document.getElementById("status"); |
32 status.innerHTML = "Options Saved."; | 29 status.innerHTML = "Options Saved."; |
(...skipping 10 matching lines...) Expand all Loading... |
43 } | 40 } |
44 var select = document.getElementById("color"); | 41 var select = document.getElementById("color"); |
45 for (var i = 0; i < select.children.length; i++) { | 42 for (var i = 0; i < select.children.length; i++) { |
46 var child = select.children[i]; | 43 var child = select.children[i]; |
47 if (child.value == favorite) { | 44 if (child.value == favorite) { |
48 child.selected = "true"; | 45 child.selected = "true"; |
49 break; | 46 break; |
50 } | 47 } |
51 } | 48 } |
52 } | 49 } |
| 50 document.addEventListener('DOMContentReady', restore_options); |
| 51 document.querySelector('#save').addEventListener('click', save_options); |
| 52 </pre> |
53 | 53 |
54 </script> | 54 <pre> |
| 55 <html> |
| 56 <head><title>My Test Extension Options</title></head> |
| 57 <script src="options.js"> |
55 | 58 |
56 <body onload="restore_options()"> | 59 <body> |
57 | 60 |
58 Favorite Color: | 61 Favorite Color: |
59 <select id="color"> | 62 <select id="color"> |
60 <option value="red">red</option> | 63 <option value="red">red</option> |
61 <option value="green">green</option> | 64 <option value="green">green</option> |
62 <option value="blue">blue</option> | 65 <option value="blue">blue</option> |
63 <option value="yellow">yellow</option> | 66 <option value="yellow">yellow</option> |
64 </select> | 67 </select> |
65 | 68 |
66 <br> | 69 <br> |
67 <button onclick="save_options()">Save</button> | 70 <div id="status"></div> |
| 71 <button id="save">Save</button> |
68 </body> | 72 </body> |
69 </html> | 73 </html> |
70 </pre> | 74 </pre> |
71 | 75 |
72 <h2>Important notes</h2> | 76 <h2>Important notes</h2> |
73 <ul> | 77 <ul> |
74 <li>We plan on providing some default css styles to encourage a consistent look
across different extensions' options pages. You can star <a href="http://crbug.c
om/25317">crbug.com/25317</a> to be notified of updates.</li> | 78 <li>We plan on providing some default css styles to encourage a consistent look
across different extensions' options pages. You can star <a href="http://crbug.c
om/25317">crbug.com/25317</a> to be notified of updates.</li> |
75 </ul> | 79 </ul> |
OLD | NEW |