1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
|
---
title: Browser styles
slug: Mozilla/Add-ons/WebExtensions/user_interface/Browser_styles
tags:
- Browser style
- WebExtensions
- 扩展程序
- 指南
- 浏览器扩展程序
- 浏览器插件
translation_of: Mozilla/Add-ons/WebExtensions/user_interface/Browser_styles
---
<div>{{AddonSidebar}}</div>
<p>扩展程序包含的某些UI组件,例如: <a href="/en-US/docs/Mozilla/Add-ons/WebExtensions/user_interface/Popups">popups</a>, <a href="/en-US/docs/Mozilla/Add-ons/WebExtensions/user_interface/Sidebars">sidebars</a>, <a href="https://developer.mozilla.org/en-US/Add-ons/WebExtensions/user_interface/Options_pages">options pages</a> ,实际上可以用如下方式统一定义:</p>
<ol>
<li>创建一个HTML文件用于描述该UI组件的页面结构</li>
<li>在manifest.json中添加字段 (<code><a href="/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/browser_action">browser_action</a></code>, <code><a href="/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/page_action">page_action</a></code>, <code><a href="/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/sidebar_action">sidebar_action</a></code>, 或 <code><a href="/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/options_ui">options_ui</a></code>) 以指向其对应的页面</li>
</ol>
<p>这种方式面临的一个挑战是如何使用浏览器自带的样式,以使得组件的UI表现与浏览器的UI风格相符。要解决这个问题,可以在该字段的配置中增加一个可字段 <code>browser_sytle</code> ,如果设置了这个字段并且值为 <code>true</code> , 那么该UI组件的HTML将会被插入一个或多个样式表,样式表会使你的扩展程序的UI表现与浏览器的风格一致 (并且与其它同样应用了这个字段的扩展程序一致)。</p>
<p>若使用了 <code>browser_style: true</code> , 你需要在不同的浏览器主题中测试你的扩展程序,以确保其UI表现和期望的一致。 </p>
<div class="blockIndicator note">
<p><strong>谷歌浏览器Google Chrome</strong> 和 欧朋浏览器 <strong>Opera</strong> 使用字段名 <code>chrome_style</code> 而非<code>browser_style</code>, 因此如果要适配它们,你需要同时添加这两个字段。</p>
</div>
<p>在火狐浏览器中,这个样式文件可以在 <code>chrome://browser/content/extension.css</code>查看。为Mac OS X系统额外准备的样式文件也可以在 <code>chrome://browser/content/extension-mac.css</code> 查看。</p>
<p>大多数样式是自动应用的,但是某些元素需要添加非标准的类名 <code style="white-space: nowrap;">browser-style</code> 来获得其样式,如下表所示:</p>
<table class="fullwidth-table standard-table">
<thead>
<tr>
<th scope="col">Element</th>
<th scope="col">Example</th>
</tr>
</thead>
<tbody>
<tr>
<td><code><a href="/en-US/docs/Web/HTML/Element/button"><button></a></code></td>
<td>
<pre class="brush: html no-line-numbers">
<button class="browser-style">Click me</button></pre>
</td>
</tr>
<tr>
<td>
<p><code><a href="/en-US/docs/Web/HTML/Element/select"><select></a></code></p>
</td>
<td>
<pre class="brush: html no-line-numbers">
<select class="browser-style" name="select">
<option value="value1">Value 1</option>
<option value="value2" selected>Value 2</option>
<option value="value3">Value 3</option>
</select></pre>
</td>
</tr>
<tr>
<td><code><a href="/en-US/docs/Web/HTML/Element/textarea"><textarea></a></code></td>
<td>
<pre class="brush: html no-line-numbers">
<textarea class="browser-style">Write here</textarea></pre>
</td>
</tr>
<tr>
<td>Parent of an <code><a href="/en-US/docs/Web/HTML/Element/input"><input></a></code></td>
<td>
<pre class="brush: html no-line-numbers">
<div class="browser-style">
<input type="radio" id="op1" name="choices" value="op1"/>
<label for="op1">Option 1</label>
<input type="radio" id="op2" name="choices" value="op2"/>
<label for="op2">Option 2</label>
</div></pre>
</td>
</tr>
</tbody>
</table>
<div class="blockIndicator note">
<p>查看 {{bug(1465256)}} 以了解相关修订</p>
</div>
<h2 id="浏览器兼容性">浏览器兼容性</h2>
<p>{{Compat("webextensions.browser_style")}}</p>
<h2 id="火狐面板组件Firefox_Panel_Components">火狐面板组件Firefox Panel Components</h2>
<div class="blockIndicator nonStandard">
<p><strong>非标准</strong><br>
此功能不是通用标准,仅支持在firefox中使用</p>
</div>
<p><code>chrome://browser/content/extension.css</code> 样式文件中也包含了火狐面板组件的样式</p>
<p><a href="https://firefoxux.github.io/StyleGuide/#/navigation">legacy Firefox Style Guide</a> 使用示例</p>
<table class="fullwidth-table standard-table">
<thead>
<tr>
<th scope="col">Element</th>
<th scope="col">Example</th>
</tr>
</thead>
<tbody>
<tr>
<td>Header</td>
<td>
<pre class="brush: html">
<header class="panel-section panel-section-header">
<div class="icon-section-header"><img src="image.svg"/></div>
<div class="text-section-header">Header</div>
</header></pre>
</td>
</tr>
<tr>
<td>Footer</td>
<td>
<pre class="brush: html">
<footer class="panel-section panel-section-footer">
<button class="panel-section-footer-button">Cancel</button>
<div class="panel-section-footer-separator"></div>
<button class="panel-section-footer-button default">Confirm</button>
</footer></pre>
</td>
</tr>
<tr>
<td>Tabs</td>
<td>
<pre class="brush: html">
<div class="panel-section panel-section-tabs">
<button class="panel-section-tabs-button selected">Tab</button>
<div class="panel-section-tabs-separator"></div>
<button class="panel-section-tabs-button">Tab</button>
<div class="panel-section-tabs-separator"></div>
<button class="panel-section-tabs-button">Tab</button>
</div></pre>
</td>
</tr>
<tr>
<td>Form</td>
<td>
<pre class="brush: html">
<div class="panel-section panel-section-formElements">
<div class="panel-formElements-item">
<label for="name01">Label:</label>
<input type="text" value="Name" id="name01" />
</div>
<div class="panel-formElements-item">
<label for="picker01">Label:</label>
<select id="picker01">
<option value="value1" selected="true">Dropdown</option>
<option value="value2">List Item</option>
<option value="value3">List Item</option>
</select>
</div>
<div class="panel-formElements-item">
<label for="placeholder01">Label:</label>
<input type="text" placeholder="Placeholder" id="placeholder01" />
<button name="expander" class="expander"></button>
</div>
</div></pre>
</td>
</tr>
<tr>
<td>Menu</td>
<td>
<pre class="brush: html">
<div class="panel-section panel-section-list">
<div class="panel-list-item">
<div class="icon"></div>
<div class="text">List Item</div>
<div class="text-shortcut">Ctrl-L</div>
</div>
<div class="panel-list-item">
<div class="icon"></div>
<div class="text">List Item</div>
<div class="text-shortcut"></div>
</div>
<div class="panel-section-separator"></div>
<div class="panel-list-item disabled">
<div class="icon"></div>
<div class="text">Disabled List Item</div>
<div class="text-shortcut"></div>
</div>
<div class="panel-section-separator"></div>
<div class="panel-list-item">
<div class="icon"></div>
<div class="text">List Item</div>
<div class="text-shortcut"></div>
</div>
<div class="panel-list-item">
<div class="icon"></div>
<div class="text">List Item</div>
<div class="text-shortcut"></div>
</div>
</div></pre>
</td>
</tr>
</tbody>
</table>
<h3 id="Example">Example</h3>
<h4 id="HTML">HTML</h4>
<pre class="brush: html"><header class="panel-section panel-section-header">
<div class="icon-section-header"><!-- An image goes here. --></div>
<div class="text-section-header">Header</div>
</header>
<div class="panel-section panel-section-list">
<div class="panel-list-item">
<div class="icon"></div>
<div class="text">List Item</div>
<div class="text-shortcut">Ctrl-L</div>
</div>
<div class="panel-list-item">
<div class="icon"></div>
<div class="text">List Item</div>
<div class="text-shortcut"></div>
</div>
<div class="panel-section-separator"></div>
<div class="panel-list-item disabled">
<div class="icon"></div>
<div class="text">Disabled List Item</div>
<div class="text-shortcut"></div>
</div>
<div class="panel-section-separator"></div>
<div class="panel-list-item">
<div class="icon"></div>
<div class="text">List Item</div>
<div class="text-shortcut"></div>
</div>
<div class="panel-list-item">
<div class="icon"></div>
<div class="text">List Item</div>
<div class="text-shortcut"></div>
</div>
</div>
<footer class="panel-section panel-section-footer">
<button class="panel-section-footer-button">Cancel</button>
<div class="panel-section-footer-separator"></div>
<button class="panel-section-footer-button default">Confirm</button>
</footer></pre>
<div class="hidden">
<h4 id="CSS">CSS</h4>
<pre class="brush: css">/* Global */
html,
body {
background: white;
box-sizing: border-box;
color: #222426;
cursor: default;
display: flex;
flex-direction: column;
font: caption;
margin: 0;
padding: 0;
-moz-user-select: none;
}
body * {
box-sizing: border-box;
text-align: start;
}
button.panel-section-footer-button,
button.panel-section-tabs-button {
color: inherit;
background-color: unset;
font: inherit;
text-shadow: inherit;
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
border: none;
}
/* Panel Section */
.panel-section {
display: flex;
flex-direction: row;
}
.panel-section-separator {
background-color: rgba(0, 0, 0, 0.15);
min-height: 1px;
}
/* Panel Section - Header */
.panel-section-header {
border-bottom: 1px solid rgba(0, 0, 0, 0.15);
padding: 16px;
}
.panel-section-header > .icon-section-header {
background-position: center center;
background-repeat: no-repeat;
height: 32px;
margin-right: 16px;
position: relative;
width: 32px;
}
.panel-section-header > .text-section-header {
align-self: center;
font-size: 1.385em;
font-weight: lighter;
}
/* Panel Section - List */
.panel-section-list {
flex-direction: column;
padding: 4px 0;
}
.panel-list-item {
align-items: center;
display: flex;
flex-direction: row;
height: 24px;
padding: 0 16px;
}
.panel-list-item:not(.disabled):hover {
background-color: rgba(0, 0, 0, 0.06);
border-bottom: 1px solid rgba(0, 0, 0, 0.1);
border-top: 1px solid rgba(0, 0, 0, 0.1);
}
.panel-list-item:not(.disabled):hover:active {
background-color: rgba(0, 0, 0, 0.1);
}
.panel-list-item.disabled {
color: #999;
}
.panel-list-item > .icon {
flex-grow: 0;
flex-shrink: 0;
}
.panel-list-item > .text {
flex-grow: 10;
}
.panel-list-item > .text-shortcut {
color: #808080;
font-family: "Lucida Grande", caption;
font-size: .847em;
justify-content: flex-end;
}
.panel-section-list .panel-section-separator {
margin: 4px 0;
}
/* Panel Section - Footer */
.panel-section-footer {
background-color: rgba(0, 0, 0, 0.06);
border-top: 1px solid rgba(0, 0, 0, 0.15);
color: #1a1a1a;
display: flex;
flex-direction: row;
height: 41px;
margin-top: -1px;
padding: 0;
}
.panel-section-footer-button {
flex: 1 1 auto;
height: 100%;
margin: 0 -1px;
padding: 12px;
text-align: center;
}
.panel-section-footer-button > .text-shortcut {
color: #808080;
font-family: "Lucida Grande", caption;
font-size: .847em;
}
.panel-section-footer-button:hover {
background-color: rgba(0, 0, 0, 0.06);
}
.panel-section-footer-button:hover:active {
background-color: rgba(0, 0, 0, 0.1);
}
.panel-section-footer-button.default {
background-color: #0996f8;
box-shadow: 0 1px 0 #0670cc inset;
color: #fff;
}
.panel-section-footer-button.default:hover {
background-color: #0670cc;
box-shadow: 0 1px 0 #005bab inset;
}
.panel-section-footer-button.default:hover:active {
background-color: #005bab;
box-shadow: 0 1px 0 #004480 inset;
}
.panel-section-footer-separator {
background-color: rgba(0, 0, 0, 0.1);
width: 1px;
z-index: 99;
}</pre>
<hr>
<pre class="brush: css">/* Example specific – not part of chrome://browser/content/extension.css */
body {
background: #fcfcfc;
background-clip: padding-box;
border: 1px solid rgba(24,26,27,.2);
box-shadow: 0 3px 5px rgba(24,26,27,.1),0 0 7px rgba(24,26,27,.1);
box-sizing: content-box;
margin: 2em auto .5em;
width: 384px;
}
html {
min-height: 100vh;
}
html > body {
margin: auto;
}
.icon-section-header {
background-image: url("data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIzMiIgaGVpZ2h0PSIzMiIgdmlld0JveD0iMCAwIDMyIDMyIj48Y2lyY2xlIGZpbGw9IiMzNjM5NTkiIGN4PSIxNSIgY3k9IjE1IiByPSIxNSIvPjwvc3ZnPg==");
}</pre>
</div>
<h4 id="Result若查看失败,请切回英文版查看">Result(若查看失败,请切回英文版查看)</h4>
<p>{{EmbedLiveSample("Example","640","360")}}</p>
|