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