Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(125)

Side by Side Diff: chrome/common/extensions/docs/templates/articles/options2.html

Issue 691283003: Write docs for options_ui (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@inline-type
Patch Set: fixes Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 <h1>Options</h1>
2
3 <p class="warning">
4 This new way of writing options is only supported from Chrome 40 onwards.
5 <a href="options">See the old documentation</a>.
6 </p>
7
8 <p>
9 To allow users to customize the behavior of your extension, you may wish to
10 provide an options page.
11
12 <p>
13 If you do, an Options link will be shown on the extensions management page at
14 <em>chrome://extensions</em> which opens a dialogue containing your options
15 page:
16 </p>
17
18 <img src="{{static}}/images/options_ui.png" alt="Options UI screenshot">
mkearney1 2014/10/31 20:48:24 image name is options-ui.png, so image isn't showi
not at google - send to devlin 2014/10/31 21:51:39 Oops. That's so weird, works on my mac. Fixed.
19
20 <p>
21 Note: Always use the $(ref:storage.sync) API to persist your options. This will
mkearney1 2014/10/31 20:48:24 I'd be inclined to drop the "Note:". Just assert..
not at google - send to devlin 2014/10/31 21:51:39 Done.
22 make them accessible from script within your extension, on all of your user's
23 devices.
24 </p>
25
26 <h2 id="step-1">Step 1: Declare your options page in the manifest</h2>
27
28 <p>
29 Use the <code>options_ui</code> manifest field to declare the options page and
30 how it should be displayed:
31 </p>
32
33 <pre data-filename="manifest.json">
34 {
35 "name": "My extension",
36 ...
37 <b>"options_ui"</b>: {
38 // Required.
39 <b>"page": "options.html"</b>,
40 // Recommended.
41 <b>"chrome_style": true,</b>
42 // Not recommended; only provided for backwards compatibility,
43 // and will be unsupported in a future version of Chrome (TBD).
44 <b>//"open_in_tab": true</b>
45 },
46 ...
47 }
48 </pre>
49
50 {{+partials.manifest_type
51 type:apis.extensions.extensionsManifestTypes.byName.OptionsUI /}}
52
53 <h2 id="step-2">Step 2: Write your options page</h2>
54
55 <p>
56 Here is an example options page:
mkearney1 2014/10/31 20:48:24 Slight rewrite: Here's an example, including an
not at google - send to devlin 2014/10/31 21:51:40 Done. With or without the : at the end?
57 </p>
58
59 <pre data-filename="options.html">
60 &lt;!DOCTYPE html&gt;
61 &lt;html&gt;
62 &lt;head&gt;
63 &lt;title&gt;My Test Extension Options&lt;/title&gt;
64 &lt;style&gt;
65 body: { padding: 10px; }
66 &lt;/style&gt;
67 &lt;/head&gt;
68
69 &lt;body&gt;
70 Favorite color:
71 &lt;select id="color"&gt;
72 &lt;option value="red"&gt;red&lt;/option&gt;
73 &lt;option value="green"&gt;green&lt;/option&gt;
74 &lt;option value="blue"&gt;blue&lt;/option&gt;
75 &lt;option value="yellow"&gt;yellow&lt;/option&gt;
76 &lt;/select&gt;
77
78 &lt;label&gt;
79 &lt;input type="checkbox" id="like"&gt;
80 I like colors.
81 &lt;/label&gt;
82
83 &lt;div id="status"&gt;&lt;/div&gt;
84 &lt;button id="save"&gt;Save&lt;/button&gt;
85
86 &lt;script src="options.js"&gt;&lt;/script&gt;
87 &lt;/body&gt;
88 &lt;/html&gt;
89 </pre>
90
91 <pre data-filename="options.js">
92 // Saves options to chrome.storage.sync.
93 function save_options() {
94 var color = document.getElementById('color').value;
95 var likesColor = document.getElementById('like').checked;
96 chrome.storage.sync.set({
97 favoriteColor: color,
98 likesColor: likesColor
99 }, function() {
100 // Update status to let user know options were saved.
101 var status = document.getElementById('status');
102 status.textContent = 'Options saved.';
103 setTimeout(function() {
104 status.textContent = '';
105 }, 750);
106 });
107 }
108
109 // Restores select box and checkbox state using the preferences
110 // stored in chrome.storage.
111 function restore_options() {
112 // Use default value color = 'red' and likesColor = true.
113 chrome.storage.sync.get({
114 favoriteColor: 'red',
115 likesColor: true
116 }, function(items) {
117 document.getElementById('color').value = items.favoriteColor;
118 document.getElementById('like').checked = items.likesColor;
119 });
120 }
121 document.addEventListener('DOMContentLoaded', restore_options);
122 document.getElementById('save').addEventListener('click',
123 save_options);
124 </pre>
125
126 <h2 id="considerations">Considerations</h2>
127
128 <p>
129 Options pages embedded inside <em>chrome://extensions</em> have some subtle
130 behavior that should be considered while writing them, mostly caused by not
mkearney1 2014/10/31 20:48:24 Replace 'that should be considered' with 'you shou
not at google - send to devlin 2014/10/31 21:51:40 Done.
131 being hosted inside their own tabs.
132 </p>
133
134 <h3 id="tabs-api">Tabs API</h3>
135 <p>
136 Options page code cannot assume that they are hosted inside a tab, and this may
mkearney1 2014/10/31 20:48:24 'that they are' better as 'that it's'.
not at google - send to devlin 2014/10/31 21:51:40 Done.
137 affect how the $(ref:tabs Tabs API) can be used. For example,
138 <ul>
139 <li>$(ref:tabs.query) will never find a tab with your extension's options
140 page URL.</li>
141 <li>$(ref:tabs.onCreated) will not fire when your options page is
142 opened.</li>
143 <li>$(ref:tabs.onUpdated) will not fire when your options page load state
144 changes.</li>
145 <li>You cannot use $(ref:tabs.connect) or $(ref:tabs.sendMessage) to
146 communicate with your options page.</li>
147 </ul>
148 </p>
149 <p>
150 It is easy to work around these issues or use alternative APIs. Generally
151 speaking you shouldn't need to manipulate the tab containing your extensions
152 options page.
153 </p>
154 <p>
155 You can use $(ref:runtime.connect) and $(ref:runtime.sendMessage) because your
156 options page is an extension page.
157 </p>
158
159 <h3 id="messaging-api">Messaging APIs</h3>
160 <p>
161 If your options page sends a message using $(ref:runtime.connect) or
162 $(ref:runtime.sendMessage) then the $(ref:runtime.MessageSender.tab Tab's) URL
163 and $(ref:runtime.MessageSender.url MessageSender's URL) properties won't agree
164 - the tab URL will be of <em>chrome://settings</em> while the URL will be your
165 options page URL.
166 </p>
167
168 <h3 id="sizing">Sizing</h3>
169 <p>
170 The embedded dialogue should automatically determine its own size based on the
171 options page content. However, the dialogue may not find a good size for some
172 types of options pages. This problem is most common for options pages that
173 adjust their size based on window size.
174 </p>
175 <p>
176 If this is an issue, we recommend that you provide fixed minimum dimensions for
mkearney1 2014/10/31 20:48:23 Can you drop 'we recommend that you' and go with '
not at google - send to devlin 2014/10/31 21:51:40 Done.
177 the options page to ensure that the dialogue will find an appropriate size.
178 </p>
179
180 <h2 id="migration">Migrating from old options pages</h2>
181 <p class="warning">
182 At least until Chrome 40 is stable, you should specify <strong>both</strong>
183 the <code>options_ui</code> <strong>and</strong> the <code>options_page</code>
184 fields.
185 <br><br>
186 Older version of Chrome will only recognize <code>options_page</code>,
mkearney1 2014/10/31 20:48:24 Nit: 'Older version' should be 'Older versions'.
not at google - send to devlin 2014/10/31 21:51:40 Done.
187 and only open in tabs. Chrome 40 and onwards will prefer to use the
mkearney1 2014/10/31 20:48:24 'will prefer' better as 'prefers'.
not at google - send to devlin 2014/10/31 21:51:39 Done.
188 <code>options_ui</code> field if it's specified.
189 <br><br>
190 You can use this to provide separate options pages for compatibility.
mkearney1 2014/10/31 20:48:23 Do you need line 190? Seems redundant.
not at google - send to devlin 2014/10/31 21:51:40 I think it's redundant in a way, though pointing o
191 </p>
192
193 The <code>options_ui</code> manifest field and embedded extension options
194 were introduced in Chrome 40. Prior to these changes, options pages were always
195 displayed in new tabs and were declared using the <code>options_page</code>
196 field:
197 </p>
198
199 <pre data-filename="manifest.json">
200 {
201 "name": "My extension",
202 ...
203 "options_page": "options.html"
204 ...
205 }
206 </pre>
207 <p>
208 See <a href="options">this document</a> for full details.
209 </p>
210
211 <p>
212 Chrome will continue to support extensions that use the legacy
213 <code>options_page</code> manifest field, but new and existing extensions
214 should use the <code>options_ui</code> manifest field to ensure that their
215 options pages are displayed as intended.
216 </p>
217 <p>
218 If you specify both, Chrome 40 and onwards will ignore the value of
219 <code>options_page</code>.
220 </p>
221 <p>
222 In a future version of Chrome any extension which specifies
mkearney1 2014/10/31 20:48:24 Nit: comma after "In a future version of Chrome".
not at google - send to devlin 2014/10/31 21:51:39 Done.
223 <code>options_page</code> will change to match the <code>options_ui</code>
224 behavior - most importantly, they will always be embedded in
225 <em>chrome://extensions</em> - so migrate as soon as possible.
226 </p>
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698