0Pricing
Browser Extensions Development (Chrome & Edge) · 课时

创建交互式弹出界面

使用 HTML、CSS 和 JavaScript 开发动态且响应式的弹出界面,为用户提供快捷交互

创建交互式弹出界面 是 CoddyKit 上的免费 Browser Extensions Development (Chrome & Edge) 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Browser Extensions Development (Chrome & Edge) 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Browser Extensions Development (Chrome & Edge) 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

What are Extension Popups?

Browser extensions often need a small user interface to interact with. This is where popup pages come in!

  • A popup is a small window that appears when you click your extension's icon in the browser toolbar.
  • It's perfect for quick actions, displaying information, or simple settings.
  • Popups are temporary: they close automatically when you click outside them or switch tabs.

Declaring Your Popup UI

To tell your browser extension which HTML file to use as its popup, you specify it in your manifest.json file.

You use the "action" key, and then "default_popup" points to your HTML file.

{
  "manifest_version": 3,
  "name": "My Popup Extension",
  "version": "1.0",
  "action": {
    "default_popup": "popup.html",
    "default_icon": "icon.png"
  }
}

Popup's HTML Structure

Your popup is essentially a miniature web page. It uses standard HTML to define its structure and content.

Create a file named popup.html (or whatever you set in manifest.json) and add your HTML.

<!DOCTYPE html>
<html>
<head>
  <title>My Popup</title>
</head>
<body>
  <h1>Hello from Popup!</h1>
  <p>This is your extension's user interface.</p>
</body>
</html>

Styling Your Popup with CSS

Just like any web page, you can use CSS to style your popup. Link a CSS file in your popup.html's <head> section.

Keep popup styles concise for a small, focused UI.

/* popup.css */
body {
  font-family: sans-serif;
  width: 200px; /* Keep popups small */
  padding: 10px;
  background-color: #f0f0f0;
}
h1 {
  color: #333;
  font-size: 1.2em;
}

Adding Interactivity with JavaScript

To make your popup dynamic and responsive, you'll need JavaScript. Link your script file in your popup.html, usually before the closing </body> tag.

This script runs every time the popup is opened.

// popup.js
document.addEventListener('DOMContentLoaded', () => {
  console.log('Popup script loaded!');
  // Your interactive code goes here
});

Understanding Popup Script Execution

When a user clicks your extension icon, the browser loads and executes your popup.html and its linked JavaScript.

  • Each time the popup opens, the script runs from scratch.
  • It has access to standard web APIs (DOM, fetch) and specific browser extension APIs (chrome.*).
  • When the popup closes, its JavaScript context is destroyed.

Example: Simple Click Counter

Let's create a basic popup that counts how many times a button is clicked.

For this to work, your popup.html needs a <span id="count"> and a <button id="increment">.

// popup.js
let count = 0;
document.addEventListener('DOMContentLoaded', () => {
  const countDisplay = document.getElementById('count');
  const incrementButton = document.getElementById('increment');

  countDisplay.textContent = count;

  incrementButton.addEventListener('click', () => {
    count++;
    countDisplay.textContent = count;
  });
});

Accessing Browser Extension APIs

Popup scripts can directly use many chrome.* APIs to interact with the browser, tabs, and extension itself.

For example, you can get information about your extension using chrome.runtime.getManifest().

// popup.js
document.addEventListener('DOMContentLoaded', () => {
  const manifest = chrome.runtime.getManifest();
  const versionElement = document.getElementById('version');
  if (versionElement) {
    versionElement.textContent = `v${manifest.version}`;
  }
  console.log('Extension Name:', manifest.name);
});

Popup Best Practices

Since popups are temporary, it's crucial to keep them lightweight and performant.

  • Keep it small: A popup should have a clear, single purpose.
  • Fast loading: Avoid heavy images or complex computations.
  • Responsive design: Ensure it looks good on various screen sizes.
  • Minimize network requests: Fetch data efficiently or pre-fetch in the background script if possible.

Popup Knowledge Check

Let's test your understanding of extension popups.

Recap: Interactive Popups

Great job! You've learned the fundamentals of creating interactive popup UIs for your browser extensions.

  • Popups are small, temporary UIs linked in manifest.json via "action".
  • They use standard HTML, CSS, and JavaScript.
  • Popup scripts run when the popup opens and are destroyed when it closes.
  • They can access browser extension APIs for dynamic interactions.

Next, you'll explore how to build more persistent configuration UIs with Options Pages!

常见问题解答

「创建交互式弹出界面」课时是免费的吗?

是的 — 「创建交互式弹出界面」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Browser Extensions Development (Chrome & Edge) 课程的其余内容,请升级到 CoddyKit PRO。 Browser Extensions Development (Chrome & Edge) 课程共包含 4 节课。

「创建交互式弹出界面」这节课中我会学到什么?

使用 HTML、CSS 和 JavaScript 开发动态且响应式的弹出界面,为用户提供快捷交互 你通过在浏览器中直接运行的动手代码来练习 Browser Extensions Development (Chrome & Edge),全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Browser Extensions Development (Chrome & Edge) 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Browser Extensions Development (Chrome & Edge) 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。

「创建交互式弹出界面」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Browser Extensions Development (Chrome & Edge) 课中编写并运行代码吗?

能。每节 Browser Extensions Development (Chrome & Edge) 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 创建交互式弹出界面
  2. 构建选项页面
  3. 用户界面组件之间的通信
  4. 使用 CSS 设置主题与深色模式支持
← 返回 Browser Extensions Development (Chrome & Edge)