权限与主机匹配
了解如何声明和请求必要权限,以及主机权限如何控制对网站的访问
权限与主机匹配 是 CoddyKit 上的免费 Browser Extensions Development (Chrome & Edge) 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Browser Extensions Development (Chrome & Edge) 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Browser Extensions Development (Chrome & Edge) 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Why Extensions Need Permissions
Just like apps on your phone, browser extensions often need special 'permissions' to do their job. These permissions define what your extension can and cannot do.
This system is crucial for user security and privacy. It prevents extensions from accessing information or performing actions they don't need, without your explicit consent.
Two Core Permission Types
In Manifest V3, permissions generally fall into two categories:
- API Permissions: Access to specific browser features (e.g., managing tabs, saving data).
- Host Permissions: Access to specific websites (e.g., reading content on example.com).
We'll look at how to declare both in your manifest.json file.
Declaring API Permissions
API permissions are listed under the "permissions" key in your manifest.json. Each string in the array corresponds to a specific browser API.
For example, "storage" lets your extension save user data, and "tabs" allows it to interact with browser tabs (like getting their URLs).
{
"manifest_version": 3,
"name": "My Extension",
"version": "1.0",
"permissions": [
"storage",
"tabs"
]
}The 'activeTab' Permission
The "activeTab" permission is unique. It's an API permission that grants your extension temporary host permissions to the currently active tab.
This access is granted only when the user *invokes* your extension (e.g., clicks its toolbar icon). It's a great way to reduce initial permission warnings, as access is only given when needed.
{
"manifest_version": 3,
"name": "ActiveTab Demo",
"version": "1.0",
"permissions": [
"activeTab"
]
}Understanding Host Permissions
Host permissions are crucial for defining which websites your extension can interact with. Without them, your extension cannot read or modify content on web pages.
This is a major security boundary. An extension with host permission for google.com cannot access facebook.com, protecting user data and ensuring privacy.
Specifying Host Permissions
Host permissions are declared in the "host_permissions" array in your manifest.json. You use URL match patterns to specify the domains.
A match pattern like "*://*.example.com/*" means access to all subdomains of example.com over HTTP or HTTPS.
{
"manifest_version": 3,
"name": "Host Access",
"version": "1.0",
"host_permissions": [
"*://developer.chrome.com/*",
"https://www.example.org/*"
]
}Match Pattern Wildcards
Match patterns use special characters:
*: Matches any string (except/in scheme/host, or#in path).<all_urls>: A special pattern that matches any URL (http,https,ftp,file). Use this with extreme caution as it grants wide access.
For example, "https://*.google.com/search?*" would match Google search results pages.
Combining API & Host Permissions
Often, your extension will need both API and host permissions. Here's an example for an extension that wants to execute a script on Wikipedia pages.
It needs the "scripting" API permission to run code, and host permission for Wikipedia to define where that code can run.
{
"manifest_version": 3,
"name": "Wiki Enhancer",
"version": "1.0",
"permissions": [
"scripting"
],
"host_permissions": [
"*://*.wikipedia.org/*"
]
}User Consent & Trust
When a user installs your extension, the browser displays a list of all requested permissions. This is their chance to understand what your extension can do.
Always request the minimum necessary permissions. Over-requesting can make users distrust your extension and choose not to install it. Be transparent!
Check Your Understanding
Which statements accurately describe host permissions in Manifest V3 extensions?
Permissions & Host Matching Recap
Great job! You've learned about the vital role of permissions in Manifest V3 extensions.
- API Permissions: Access browser features (e.g.,
"storage","tabs"). - Host Permissions: Control website access using URL match patterns (e.g.,
"*://*.google.com/*"). "activeTab"is a special API permission that grants temporary host access.- Always request the minimum necessary permissions to build user trust.
Understanding these concepts is key to building secure and functional extensions!
常见问题解答
「权限与主机匹配」课时是免费的吗?
是的 — 「权限与主机匹配」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 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) 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。
「权限与主机匹配」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Browser Extensions Development (Chrome & Edge) 课中编写并运行代码吗?
能。每节 Browser Extensions Development (Chrome & Edge) 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。