0Pricing
Browser Extensions Development (Chrome & Edge) · レッスン

オプションページの構築

ユーザーが拡張機能の設定や環境設定を行える、永続的なオプションページを設計・実装します。

「オプションページの構築」はCoddyKit上の無料Browser Extensions Development (Chrome & Edge)レッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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_page in manifest.json.
  • You use chrome.storage.sync or local to 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!

よくある質問

「オプションページの構築」レッスンは無料ですか?

はい。「オプションページの構築」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Browser Extensions Development (Chrome & Edge)コースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Browser Extensions Development (Chrome & Edge)コースには全4レッスンが含まれています。

「オプションページの構築」で何を学びますか?

ユーザーが拡張機能の設定や環境設定を行える、永続的なオプションページを設計・実装します。 ブラウザで直接実行するハンズオンコードでBrowser Extensions Development (Chrome & Edge)を演習し、24時間対応の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フィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. インタラクティブなポップアップUIの作成
  2. オプションページの構築
  3. UIコンポーネント間の通信
  4. CSSによるテーマ設定とダークモード対応
← Browser Extensions Development (Chrome & Edge)に戻る