构建选项页面
设计并实现持久化的选项页面,让用户能够配置扩展设置与偏好
构建选项页面 是 CoddyKit 上的免费 Browser Extensions Development (Chrome & Edge) 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Browser Extensions Development (Chrome & Edge) 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Browser Extensions Development (Chrome & Edge) 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Why Use an Options Page?
An options page is a dedicated UI for your extension where users can customize settings and preferences. Think of it as your extension's control panel!
- It provides a persistent place for users to configure how your extension behaves.
- Settings saved here stick around even if the browser closes.
- It's different from a popup, which is for quick, transient interactions.
Basic Options HTML
Every options page starts with an HTML file. This file will contain the structure of your settings interface.
Create a file named options.html in your extension's root directory. It's just like any other HTML document.
<!DOCTYPE html>
<html>
<head>
<title>My Extension Options</title>
<link rel="stylesheet" href="options.css">
</head>
<body>
<h1>Extension Settings</h1>
<p>Customize your extension here.</p>
<script src="options.js"></script>
</body>
</html>Linking Options in Manifest
To tell your browser extension where its options page is, you need to declare it in your manifest.json file.
Use the options_page key, pointing to your HTML file. This makes the page accessible via the browser's extension management UI.
{
"name": "My Options Extension",
"version": "1.0",
"manifest_version": 3,
"options_page": "options.html"
}Making Options Look Good
Just like any web page, you can style your options page using CSS to match your extension's branding or improve usability.
Create an options.css file and link it in your options.html. Here's a simple start:
body {
font-family: Arial, sans-serif;
padding: 20px;
min-width: 300px;
}
h1 {
color: #333;
}
button {
background-color: #007bff;
color: white;
border: none;
padding: 8px 15px;
border-radius: 4px;
cursor: pointer;
}Saving User Preferences
To make settings persistent, we use the chrome.storage API. It allows your extension to store data that persists across browser sessions.
chrome.storage.sync stores data synced across devices, while chrome.storage.local stores data only on the current device. Let's use sync for settings.
// options.js
document.getElementById('saveButton').addEventListener('click', () => {
const username = document.getElementById('usernameInput').value;
chrome.storage.sync.set({ 'username': username }, () => {
console.log('Username saved!');
alert('Settings saved!');
});
});Loading Saved Preferences
When the options page opens, you'll want to load any previously saved settings to display them to the user.
Use chrome.storage.sync.get() to retrieve data. This should typically happen when the DOM content is fully loaded.
// options.js
document.addEventListener('DOMContentLoaded', () => {
chrome.storage.sync.get(['username'], (result) => {
if (result.username) {
document.getElementById('usernameInput').value = result.username;
}
});
});Building a Dark Mode Toggle
Let's create a simple dark mode toggle. This example combines HTML, CSS, and JavaScript to save and load a user's preference for a dark theme.
We'll use a checkbox to control the theme and save its state using chrome.storage.sync.
<!DOCTYPE html>
<html>
<head>
<title>Theme Options</title>
<style>
body {
font-family: Arial, sans-serif;
padding: 20px;
min-width: 300px;
transition: background-color 0.3s, color 0.3s;
}
body.dark-mode {
background-color: #222;
color: #eee;
}
h1 {
color: #333;
}
body.dark-mode h1 {
color: #eee;
}
</style>
</head>
<body>
<h1>Theme Settings</h1>
<label>
<input type="checkbox" id="darkModeToggle">
Enable Dark Mode
</label>
<script>
const darkModeToggle = document.getElementById('darkModeToggle');
// Load saved preference
document.addEventListener('DOMContentLoaded', () => {
chrome.storage.sync.get(['darkMode'], (result) => {
darkModeToggle.checked = result.darkMode || false;
document.body.classList.toggle('dark-mode', darkModeToggle.checked);
});
});
// Save preference on change
darkModeToggle.addEventListener('change', () => {
const isDarkMode = darkModeToggle.checked;
chrome.storage.sync.set({ 'darkMode': isDarkMode }, () => {
document.body.classList.toggle('dark-mode', isDarkMode);
console.log('Dark mode preference saved:', isDarkMode);
});
});
</script>
</body>
</html>How Users Open Options
Users can access your extension's options page in a few ways:
- Right-click on your extension's icon in the toolbar and select 'Options'.
- Go to the browser's extension management page (e.g.,
chrome://extensions), find your extension, and click 'Details', then 'Extension options'.
There's no default button in the popup, you need to add one if you want to link from there (covered in a later lesson on UI communication).
Options Page UX Tips
Good options pages are intuitive and easy to use. Here are some tips:
- Provide Defaults: Always set sensible default values for your settings.
- Instant Feedback: Show a 'Saved!' message after settings are changed.
- Clear Labels: Use descriptive text for input fields and checkboxes.
- Organize: Group related settings logically, especially for complex extensions.
Options Page Check
Let's check your understanding of options pages and how they work.
Options Page Recap
Great job! You've learned how to build a persistent options page for your browser extension.
- An options page lets users customize settings.
- It's declared using
options_pageinmanifest.json. - You use
chrome.storage.syncorlocalto save and load preferences. - Good UX is key for a user-friendly options page.
Next, we'll explore how different parts of your extension can communicate with each other!
用 AI 导师学习 JavaScript — 免费
在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。
- 课程
- 12
- 课程
- 48
常见问题解答
「构建选项页面」课时是免费的吗?
是的 — 「构建选项页面」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Browser Extensions Development (Chrome & Edge) 课程的其余内容,请升级到 CoddyKit PRO。 Browser Extensions Development (Chrome & Edge) 课程共包含 4 节课。
「构建选项页面」这节课中我会学到什么?
设计并实现持久化的选项页面,让用户能够配置扩展设置与偏好 你通过在浏览器中直接运行的动手代码来练习 Browser Extensions Development (Chrome & Edge),全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Browser Extensions Development (Chrome & Edge) 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Browser Extensions Development (Chrome & Edge) 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。
「构建选项页面」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Browser Extensions Development (Chrome & Edge) 课中编写并运行代码吗?
能。每节 Browser Extensions Development (Chrome & Edge) 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。