OLD | NEW |
(Empty) | |
| 1 <h1 class="page_title">Options</h1> |
| 2 <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 <h2>Step 1: Declare your options page in the manifest</h2> |
| 4 <pre>{ |
| 5 "name": "My extension", |
| 6 ... |
| 7 <b>"options_page": "options.html"</b>, |
| 8 ... |
| 9 }</pre> |
| 10 <h2>Step 2: Write your options page</h2> |
| 11 Here is an example options page: |
| 12 <pre>// Save this script as `options.js` |
| 13 // Saves options to localStorage. |
| 14 function save_options() { |
| 15 var select = document.getElementById("color"); |
| 16 var color = select.children[select.selectedIndex].value; |
| 17 localStorage["favorite_color"] = color; |
| 18 // Update status to let user know options were saved. |
| 19 var status = document.getElementById("status"); |
| 20 status.innerHTML = "Options Saved."; |
| 21 setTimeout(function() { |
| 22 status.innerHTML = ""; |
| 23 }, 750); |
| 24 } |
| 25 // Restores select box state to saved value from localStorage. |
| 26 function restore_options() { |
| 27 var favorite = localStorage["favorite_color"]; |
| 28 if (!favorite) { |
| 29 return; |
| 30 } |
| 31 var select = document.getElementById("color"); |
| 32 for (var i = 0; i < select.children.length; i++) { |
| 33 var child = select.children[i]; |
| 34 if (child.value == favorite) { |
| 35 child.selected = "true"; |
| 36 break; |
| 37 } |
| 38 } |
| 39 } |
| 40 document.addEventListener('DOMContentReady', restore_options); |
| 41 document.querySelector('#save').addEventListener('click', save_options); |
| 42 </pre> |
| 43 <pre> |
| 44 <html> |
| 45 <head><title>My Test Extension Options</title></head> |
| 46 <script src="options.js"> |
| 47 <body> |
| 48 Favorite Color: |
| 49 <select id="color"> |
| 50 <option value="red">red</option> |
| 51 <option value="green">green</option> |
| 52 <option value="blue">blue</option> |
| 53 <option value="yellow">yellow</option> |
| 54 </select> |
| 55 <br> |
| 56 <div id="status"></div> |
| 57 <button id="save">Save</button> |
| 58 </body> |
| 59 </html> |
| 60 </pre> |
| 61 <h2>Important notes</h2> |
| 62 <ul> |
| 63 <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> |
| 64 </ul> |
OLD | NEW |