Guardium- 密码保护器 (第2天)

发布: (2025年12月20日 GMT+8 00:41)
10 min read
原文: Dev.to

Source: Dev.to

请提供您希望翻译的完整文本内容(除代码块和 URL 之外),我将按照要求将其翻译成简体中文并保持原有的 Markdown 格式。

概述

manifest.json 文件是 Chrome 扩展的核心,它是 Chrome 在加载或运行扩展之前读取的主要元数据和配置文件。它定义了扩展的 身份——包括名称、版本、描述、作者和图标——并明确指定 扩展在浏览器中的行为方式。通过 manifest,开发者声明 权限,这些权限控制扩展可以访问的浏览器 API 和网站,从而确保安全性和用户透明度。它还链接了扩展的所有主要组件,如 后台服务工作线程、内容脚本、弹出页面、选项页面以及可网页访问的资源,让 Chrome 知道何时何地执行每个部分。

Manifest Version 3 (MV3) 中,manifest.json 的作用更加关键,它通过 非持久化后台执行、更严格的权限处理和性能提升 等现代安全实践来强制执行安全规范,使其成为构建安全、高效、可维护的 Chrome 扩展的必备要素。

示例 manifest.json

{
  "manifest_version": 3,
  "name": "Guardium",
  "short_name": "Guardium",
  "description": "A secure browser-based password manager with autofill support",
  "version": "1.0.0",

  "action": {
    "default_title": "Open Guardium",
    "default_popup": "index.html"
  },

  "icons": {
    "16": "assets/icons/icon16.png",
    "32": "assets/icons/icon32.png",
    "48": "assets/icons/icon48.png",
    "128": "assets/icons/icon128.png"
  },

  "background": {
    "service_worker": "background.js",
    "type": "module"
  },

  "content_scripts": [
    {
      "matches": [""],
      "js": ["contentScript.js"],
      "run_at": "document_end",
      "all_frames": true
    }
  ],

  "permissions": [
    "activeTab",
    "tabs",
    "storage",
    "scripting",
    "alarms",
    "notifications"
  ],

  "host_permissions": [
    ""
  ],

  "web_accessible_resources": [
    {
      "resources": [
        "assets/icons/*",
        "injected/*.js"
      ],
      "matches": [""]
    }
  ],

  "options_page": "options.html",

  "commands": {
    "open-guardium": {
      "suggested_key": {
        "default": "Ctrl+Shift+G",
        "mac": "Command+Shift+G"
      },
      "description": "Open Guardium popup"
    }
  },

  "minimum_chrome_version": "114"
}

Explanation of Every Line

Line (excerpt)Explanation
"manifest_version": 3,Manifest Version – 指定 MV3,即最新的 Chrome 扩展标准。
优势:
• 更好的安全性
• 使用 Service Worker 代替后台页面
• 更严格的权限控制
"name": "Guardium",Extension Name – 在扩展页面、Chrome Web Store 和工具栏上显示的完整名称。
"short_name": "Guardium",Short Name – 在空间受限的地方使用(例如工具栏标签)。可选,但建议用于 UI 一致性。
"description": "A secure browser-based password manager with autofill support",Description – 在 Chrome Web Store 和扩展页面上显示的简要摘要。
"version": "1.0.0",Version Number – Chrome 用于管理更新的版本号。必须遵循语义化版本(major.minor.patch)。
"action": { … }Toolbar Action (Popup UI) – 定义扩展工具栏图标的行为。
"default_title": "Open Guardium",当用户将鼠标悬停在扩展图标上时显示的提示文字。
"default_popup": "index.html"点击图标时打开的 HTML 文件(即你的密码管理器 UI)。
"icons": { … }Icons – 声明各种尺寸的扩展图标。
"16": "assets/icons/icon16.png",用于工具栏。
"32": "assets/icons/icon32.png",用于高 DPI 显示和系统 UI。
"48": "assets/icons/icon48.png",用于 Chrome 扩展页面。
"128": "assets/icons/icon128.png"用于 Chrome Web Store 列表。
"background": { … }Background Service Worker – 定义扩展的后台逻辑。
"service_worker": "background.js",处理事件、监听消息、管理闹钟、认证、加密等的 JavaScript 文件。仅在需要时运行(非持久化)。
"type": "module"启用 ES 模块,允许使用 import/export,实现更清晰、更可扩展的架构。
"content_scripts": [ … ]Content Scripts – 声明注入到网页中的脚本。
"matches": [""],所有 网站上注入脚本(密码管理器需要检测登录表单)。
"js": ["contentScript.js"],在页面上下文中运行的脚本文件。
"run_at": "document_end",在 DOM 完全解析后执行。
"all_frames": true在每个框架中运行(包括 iframe)。
"permissions": [ … ]Permissions – 声明扩展可能使用的 Chrome API。
"host_permissions": [ "" ]内容脚本注入和任何网络请求所需的主机权限。
"web_accessible_resources": [ { … } ]可被网页访问的资源(例如图标、注入的脚本)。
"options_page": "options.html"当用户在扩展页面点击 “Details → Options” 时显示的页面。
"commands": { … }为扩展操作定义的键盘快捷键。
"minimum_chrome_version": "114"确保扩展仅在 Chrome 114 或更高版本上运行(某些 MV3 功能需要此版本)。

清理后的 Manifest 概览

以下是您提供的 manifest.json 片段的整洁、可直接复制的版本。所有标题、代码块和说明性注释均保留,重复的占位符已被移除。

1️⃣ 内容脚本

{
  "js": ["contentScript.js"],
  "run_at": "document_end",
  "all_frames": true
}
  • js – 注入匹配页面的 JavaScript 文件。
  • run_at – 脚本在 DOM 加载完成后运行,确保表单和输入框已存在。
  • all_frames – 在主页面 以及 所有 iframe 中运行(对嵌入式登录表单尤为重要)。

2️⃣ 权限

[
  "activeTab",
  "tabs",
  "storage",
  "scripting",
  "alarms",
  "notifications"
]
权限功能说明
activeTab临时访问当前活动标签页。
tabs读取标签页元数据(URL、ID、标题)。
storage启用 chrome.storage 用于保存加密密码。
scripting允许动态脚本注入(MV3 所必需)。
alarms启用计划的后台任务。
notifications允许向用户发送通知(例如 “密码已保存”)。

3️⃣ 主机权限

[
  ""
]
  • 指定扩展可以访问的站点。
  • "" 表示扩展拥有与 任何 网站交互的权限——自动填充扩展通常需要此权限。

4️⃣ 可网页访问的资源

[
  {
    "resources": [
      "assets/icons/*",
      "injected/*.js"
    ],
    "matches": [""]
  }
]
  • 允许网页或内容脚本加载随扩展打包的文件。
  • resources – 可被外部访问的文件列表。
  • matches – 被允许请求这些资源的站点。

5️⃣ 选项页面

"options_page": "options.html"
  • 扩展的设置页面。
  • 用于配置偏好、安全设置以及同步选项。

6️⃣ 键盘快捷命令

{
  "open-guardium": {
    "suggested_key": {
      "default": "Ctrl+Shift+G",
      "mac": "Command+Shift+G"
    },
    "description": "Open Guardium popup"
  }
}
  • 定义打开 Guardium 弹出窗口的快捷键。
  • 平台特定键位(default 适用于 Windows/Linux,mac 适用于 macOS)。

7️⃣ Chrome 版本要求

"minimum_chrome_version": "114"
  • 确保扩展与所使用的 MV3 API 兼容。

✅ 最终总结

  • 完全符合 MV3 标准的 manifest.json
  • 支持 密码管理 + 自动填充
  • 已准备好提交至 Chrome Web Store
  • 覆盖 UI、background、content scripts、permissions 和 security。

📦 Chrome 扩展工作概览

扩展 UI 截图

Chrome 扩展被拆分为相互隔离的组件,通过 消息传递 进行通信:

组件角色
弹出 UI在用户点击扩展图标时运行;处理用户交互和显示。
后台服务工作线程“大脑”——响应事件,管理存储,执行安全逻辑;仅在需要时运行。
内容脚本注入网页;与页面的 DOM 交互(例如,检测表单、自动填充),同时保持隔离以确保安全。
选项页提供用于偏好设置、安全和同步的设置界面。
Web 可访问资源网页或内容脚本可以请求的文件(图标、注入脚本等)。

所有这些组件都在 manifest.json 中声明,Chrome 在启动时读取它,以了解 需要哪些权限、显示哪些 UI 元素以及注入哪些脚本

📊 架构图

Extension architecture diagram

该图示说明了 UI 组件内容脚本后台服务工作者 如何在 Chrome 内部(扩展沙箱)和 外部(网页)之间进行交互。

请随意将 JSON 代码片段直接复制到 manifest.json 文件中,根据项目结构调整路径或键,即可获得干净、可投入生产的 Guardium 密码管理器扩展的清单文件。

**Chrome Extension Architecture Overview**

The **background service worker** acts as the central controller: it handles logic, storage, alarms, notifications, and communication. All these parts communicate using **message passing (`runtime.message`)**.  

The background can also access **Chrome data and APIs** (storage, cookies, history, bookmarks) and can communicate with **external APIs** outside Chrome.  

Overall, the diagram highlights the **separation of concerns, security isolation, and message‑based communication** that make Chrome extensions safe and efficient.
Back to Blog

相关文章

阅读更多 »