Browser styles
扩展程序包含的某些 UI 组件,例如: popups (en-US), sidebars, options pages ,实际上可以用如下方式统一定义:
- 创建一个 HTML 文件用于描述该 UI 组件的页面结构
- 在 manifest.json 中添加字段 (
browser_action,page_action(en-US),sidebar_action(en-US), 或options_ui(en-US)) 以指向其对应的页面
这种方式面临的一个挑战是如何使用浏览器自带的样式,以使得组件的 UI 表现与浏览器的 UI 风格相符。要解决这个问题,可以在该字段的配置中增加一个可字段 browser_sytle ,如果设置了这个字段并且值为 true , 那么该 UI 组件的 HTML 将会被插入一个或多个样式表,样式表会使你的扩展程序的 UI 表现与浏览器的风格一致 (并且与其他同样应用了这个字段的扩展程序一致)。
若使用了 browser_style: true , 你需要在不同的浏览器主题中测试你的扩展程序,以确保其 UI 表现和期望的一致。
备注: 谷歌浏览器 Google Chrome 和 欧朋浏览器 Opera 使用字段名 chrome_style 而非browser_style, 因此如果要适配它们,你需要同时添加这两个字段。
在火狐浏览器中,这个样式文件可以在 chrome://browser/content/extension.css查看。为 Mac OS X 系统额外准备的样式文件也可以在 chrome://browser/content/extension-mac.css 查看。
大多数样式是自动应用的,但是某些元素需要添加非标准的类名 browser-style 来获得其样式,如下表所示:
| Element | Example |
|---|---|
<button> |
html |
|
html |
|
<textarea> |
html |
Parent of an
<input>
|
html |
备注: 查看 Firefox bug 1465256 以了解相关修订
浏览器兼容性
webextensions.manifest.action
BCD tables only load in the browser
webextensions.manifest.browser_action
BCD tables only load in the browser
webextensions.manifest.page_action
BCD tables only load in the browser
webextensions.manifest.sidebar_action
BCD tables only load in the browser
webextensions.manifest.options_ui
BCD tables only load in the browser
火狐面板组件 Firefox Panel Components
备注: 此功能不是通用标准,仅支持在 firefox 中使用
chrome://browser/content/extension.css 样式文件中也包含了火狐面板组件的样式
legacy Firefox Style Guide 使用示例
| Element | Example |
|---|---|
| Header |
html |
| Footer |
html |
| Tabs |
html |
| Form |
html |
| Menu |
html |
示例
HTML
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>