跳到主要内容

manifest配置文件

manifest文件

manifest.json是Chrome插件必须的文件,且必须放在根目录上,用于配置插件的重要信息

官方文档

官方文档 - v3

配置解析

{
// 必填:插件使用的manifest版本号
"manifest_version": 3,

// 必填:插件名称
"name": "My Extension",

// 必填: 插件版本号
"version": "1.0.1",

// 选填: 插件描述
"description": "A plain text description",

// 选填: 插件图标,分3种图标:
// 1. 128x128: 安装期间和Chrome网上应用店使用
// 2. 48x48: 扩展管理页(`chrome://extensions`)中使用
// 3. 16x16: 工具栏上显示的图标
// 最好至少提供一个128x128的图标
"icons": {
"16": "icon16.png",
"48": "icon48.png",
"128": "icon128.png"
},

// ✨ 选填: 工具栏中的外观和行为
// https://developer.chrome.com/docs/extensions/reference/api/action?hl=zh-cn
"action": {
// 图标的宽度和高度必须为 16 个设备无关像素(dp),经验建议: 正方形色块颜色深的图标,更容易被用户识别
"default_icon": {
"16": "images/get_started16.png",
"32": "images/get_started32.png",
"48": "images/get_started48.png",
"128": "images/get_started128.png"
},
// 鼠标指针悬停在工具栏中的扩展程序图标上时,系统会显示提示(即标题)
"default_title": "Click here!"
// 鼠标点击工具栏中的扩展程序图标时,系统会显示的弹出窗口
"default_popup": "popup.html",
},

// ✨ 选填: 打开某些指定网页时要使用的 JavaScript 或 CSS 文件
// https://developer.chrome.com/docs/extensions/develop/concepts/content-scripts?hl=zh-cn
"content_scripts": [{
// 1. 匹配规则
"matches": [
// 匹配所有网址
"<all_urls>"
// regex
"http://*/*",
"https://*/*",
],
// 2. 排除匹配规则
"exclude_matches": [
"*://*/*business*"
],
// 3. 包含规则
"include_globs": [
"*nytimes.com/???s/*"
],
// 4. 排除规则
"exclude_globs": [
"*science*"
],
// 5. 本地css文件
"css": [
"assets/css/content.css"
],
// 6. 本地js文件
"js": [
"assets/js/content.js"
]
// 7. 代码注入时机, 默认document_idle
// document_start: DOM树构建完成,但是所有资源仍然未加载
// document_end: DOM树构建完成,所有资源加载完成
// document_idle: DOM树构建完成,所有资源加载完成,且页面空闲状态
"run_at": "document_idle",
}],

// ✨ 选填: 插件的后台脚本
// 特点:长期运行、独立于其他任何窗口或标签页的环境
// 应用场景:监听、响应、修改浏览器中发生的事件,或者执行一些异步任务,待需要时直接调用
// https://developer.chrome.com/docs/extensions/develop/migrate/to-service-workers?hl=zh-cn
"background": {
// 选填: 本地js文件
"service_worker": "pages/background/index.js",
// 选填: 当使用 ES 模块时,才需要 "type" 字段,只有一个值 "module"
"type": "module",
}

// ✨ 选填: 开发者工具
// https://developer.chrome.com/docs/extensions/how-to/devtools/extend-devtools?hl=zh-cn
"devtools_page": "devtools.html",

// ✨ 选填: 侧边栏
// https://developer.chrome.com/docs/extensions/reference/api/sidePanel?hl=zh-cn
// 开启侧边栏,必须设置permissions: ["sidePanel"]
"side_panel": {
"default_path": "src/pages/sidepanel/index.html",
},

// 选填: 替换浏览器的默认页面,分3种:
// 1. 新标签页
// 2. 书签管理器
// 3. 历史记录
"chrome_url_overrides": {
"newtab": "pages/newtab/index.html",
"bookmarks": "pages/bookmarks/index.html",
"history": "pages/history/index.html"
},

// 选填: 插件的选项页(设置页:右键扩展图标 -> 选项)
// https://developer.chrome.com/docs/extensions/develop/ui/options-page?hl=zh-cn
"options_page": "options.html",

// 选填: 插件权限
"permissions": [
// 常用权限: https://developer.chrome.com/docs/extensions/mv3/declare_permissions/
"tabs",
"activeTab",
"storage",
"https://*/"
],

// 选填: 声明了哪些扩展程序和网页可以通过 runtime.connect 和 runtime.sendMessage 连接到您的扩展程序。
// https://developer.chrome.com/docs/extensions/reference/manifest/externally-connectable?hl=zh-cn
"externally_connectable": {
// 允许连接的扩展程序的 ID, 当未设置externally_connectable时,默认ids为["*"],即允许所有扩展程序连接; 当设置externally_connectable时,若未指定 "ids": ["*"],其他扩展程序将无法连接到您的扩展程序
"ids": [
"*"
],
// 允许连接的网页的网址格式
// ✅ 有效网址 ❌ 网址无效
// *://example.com/ *://example.com/one/
// http://*.example.org/* <all_urls>
// https://example.com/* http://*/*
// 设置本地地址:"localhost:*/*" | "*://localhost/*",
"matches": [
"https://*.google.com/*",
"*://*.chromium.org/*",
],
// 允许扩展程序使用与其连接的网页的 TLS 通道 ID
"accepts_tls_channel_id": false
},

// 选填: 内容安全策略
// https://developer.chrome.com/docs/extensions/reference/manifest/content-security-policy?hl=zh-cn
"content_security_policy": {
// 扩展程序页面默认值
"extension_pages": "script-src 'self'; object-src 'self';",
// 沙盒化扩展程序页面默认值
"sandbox": "sandbox allow-scripts allow-forms allow-popups allow-modals; script-src 'self' 'unsafe-inline' 'unsafe-eval'; child-src 'self';"
},

// 定义扩展程序中可供网页或其他扩展程序访问的文件
// 可通过网址 chrome-extension://[PACKAGE ID]/[PATH] 获取网页中的资源,该网址可使用 runtime.getURL() 方法生成
"web_accessible_resources": [
{
// 根目录到给定资源的相对路径
"resources": [ "test1.png", "test2.png" ],
// 指定哪些网站可以访问这组资源
"matches": [
"https://web-accessible-resources-1.glitch.me/*",
"*://*/*"
]
},
{
"resources": [ "test3.png", "test4.png" ],
"matches": [ "https://web-accessible-resources-2.glitch.me/*" ],
"use_dynamic_url": true
}
]

// 作者
"author": "developer@example.com",
// 插件主页
"homepage_url": "https://path/to/homepage",

// 定义扩展程序中的键盘快捷键
// https://developer.chrome.com/docs/extensions/reference/api/commands?hl=zh-cn
"commands": {...},

// 在扩展程序之间共享的无权限资源集合
"export": {...},
"import": [{...}],

// 主机权限
"host_permissions": [...],
// 可选主机权限
"optional_host_permissions": ["..."],
// 可选权限
"optional_permissions": ["..."],

// 与 local 和 sync 存储区域不同,managed 存储区域需要将其结构声明为 JSON 架构
"storage": {
// 存储在 "storage" 清单密钥的 "managed_schema" 属性指示的文件中
"managed_schema": "schema.json"
},

"default_locale": "en",
"chrome_settings_overrides": {...},
"cross_origin_embedder_policy": {...},
"cross_origin_opener_policy": {...},
"declarative_net_request": {...},
"event_rules": [{...}],
"file_browser_handlers": [...],
"file_handlers": [...],
"file_system_provider_capabilities": {...},
"incognito": "spanning, split, or not_allowed",
"input_components": [{...}],
"key": "publicKey",
"minimum_chrome_version": "107",
"oauth2": {...},
"omnibox": {...},
"options_ui": {...},
"requirements": {...},
"sandbox": {...},
"short_name": "Short Name",
"tts_engine": {...},
"update_url": "https://path/to/updateInfo.xml",
"version_name": "1.0 beta",
}