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

可选权限与运行时请求

按需请求权限,而不是在安装前一次性请求,从而建立信任、更快通过审核,并尽量缩小攻击面。

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

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

The Problem With Asking for Everything

Requesting many permissions at install time scares users and slows store review. The principle of least privilege says request only what you need, when you need it.

Optional permissions let you ask at runtime, in context.

Required vs Optional

The manifest separates permissions (granted at install) from optional_permissions (requested later). Move anything non-essential into optional.

{
  "permissions": ["storage"],
  "optional_permissions": ["bookmarks", "history"]
}

Optional Host Permissions

Site access can also be optional. Declare it under optional_host_permissions so users grant access to specific sites only when they choose.

{
  "optional_host_permissions": ["https://*/*"]
}

Requesting at Runtime

Call permissions.request in response to a user gesture, such as a button click. The browser shows a prompt.

const granted = await chrome.permissions.request({
  permissions: ['bookmarks']
})
console.log('Granted: ' + granted)

Must Come From a User Gesture

The request prompt only appears if triggered by a direct user action. Calling it on startup or from a timer will silently fail.

Wire it to a click handler.

button.addEventListener('click', async () => {
  await chrome.permissions.request({ permissions: ['history'] })
})

Checking Before Using

Before calling an API, check whether the permission is currently granted with permissions.contains.

const has = await chrome.permissions.contains({
  permissions: ['bookmarks']
})
if (has) loadBookmarks()

Removing Permissions

Let users revoke access they no longer want with permissions.remove. This shrinks your attack surface.

await chrome.permissions.remove({ permissions: ['history'] })

Reacting to Grants and Revokes

Listen for onAdded and onRemoved to enable or disable features as permissions change.

chrome.permissions.onRemoved.addListener((p) => {
  console.log('Removed: ' + p.permissions)
})

Designing the Request Flow

Explain why you need a permission before prompting. A short sentence and a clear button earns far more grants than a surprise system dialog.

Faster Store Review

Extensions with broad install-time permissions face deeper review. Moving sensitive scopes to optional often speeds approval and reduces warning banners.

Graceful Degradation

Always handle the case where a permission is denied. Disable or hide the feature rather than throwing errors, so the rest of the extension keeps working.

if (!granted) showFallbackMessage()

Quick Check

Test your optional permissions knowledge.

Recap

You learned runtime permission requests:

  • Split required and optional_permissions
  • Request from a user gesture with permissions.request
  • Check with contains, revoke with remove
  • React to onAdded and onRemoved
  • Degrade gracefully when denied

Least-privilege design builds trust and eases store review.

常见问题解答

「可选权限与运行时请求」课时是免费的吗?

是的 — 「可选权限与运行时请求」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 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) 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「可选权限与运行时请求」课时需要多长时间?

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

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

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

此课程中的所有课时

  1. 了解高级权限
  2. 安全编码实践
  3. 内容安全策略(CSP)
  4. 可选权限与运行时请求
← 返回 Browser Extensions Development (Chrome & Edge)