谁能解释 chrome 扩展的清单文件中 "match_about_blank" 的用途是什么?

Can anyone explain that what is the use of "match_about_blank" in chrome extension's manifest file?

我是 chrome 扩展的新手,但不知道清单文件中 "match_about_blank" 属性 的使用。谁能用通俗易懂的话解释一下?

让我先引用 documentation of "match_about_blank":

Whether to insert the content script on about:blank and about:srcdoc. Content scripts will only be injected on pages when their inherit URL is matched by one of the declared patterns in the matches field. The inherit URL is the URL of the document that created the frame or window. Content scripts cannot be inserted in sandboxed frames.

要完全理解这一点,需要一些概念:

  • same-origin policy 是浏览器的一项基本安全功能,强制网页只能 运行 其他框架中的脚本或 windows 如果提供文档同源

  • 通常,页面的 "origin" 可以通过查看 URL 来确定。有一些例外情况是不可能的,包括 "about:blank"、"about:srcdoc" 和沙盒框架。

    • "about:blank"是空白页。当您创建一个空框架(例如<iframe>)或打开一个新的window(window.open())时,URL 就是"about:blank"。 为了允许打开者在此页面中使用 运行 脚本,空白页面与打开 window/frame 的 page/frame 具有相同的来源。

    • "about:srcdoc"是一个框架的URL,其内容是通过srcdoc attribute of an iframe设置的,继承父window的起源,类似于 "about:blank" 帧。

    • 有时一个框架的起源与任何其他起源不同,因此没有其他页面可以 运行 该框架中的脚本。 有两个达到此条件的方法:1) 当用户通过地址栏导航到 "about:blank" 时。 2) 当页面受到没有 "allow-same-origin" 指令的沙箱影响时(通过 Content-Security-Policy HTTP header or via the sandbox attribute of an iframe)。
  • Chrome 扩展不能 运行 其他网站的脚本,除非他们明确请求该网站的许可,或者通过 declaring host permissions in the "permissions" section of manifest.json, or by listing a site in the "matches" section of a content script.

当 URL 与 manifest.json 中的 "matches" 键匹配时,内容脚本在页面(和框架)中为 运行。 以前不能直接运行空白框里的脚本,因为"about:blank"不能用匹配模式匹配。 用户(包括广告拦截器的开发者)请求能够 运行 在空白帧中编写脚本(参见 issue 76429: Content scripts do not inject into frames with no src because their url is "about:blank")。

如果您对 "match_about_blank" 密钥开发背后的完整历史感兴趣,我建议您从 comment 35 开始阅读。 简而言之,"about:blank" 匹配模式本身是非常无用的,因为它会匹配来自所有来源的大量帧。因此,运行 脚本的能力是通过引入 match_about_blank 键(而不是支持 "about:blank" 匹配模式)实现的。

因此,如果您想 运行 空白帧中的脚本 ,您应该 仅使用 "match_about_blank":true。 大多数扩展也应该设置 "all_frames": true(否则脚本只会在空白的顶级框架中设置 运行s,而不是子框架)。因此,对于 example.com 中的 运行 脚本以及从中打开的所有空白 frames/windows,请在 manifest.json 中使用以下内容:

...
"content_scripts": [{
    "js": ["contentscript.js"],
    "matches": ["*://*.example.com/*"],
    "all_frames": true,
    "match_about_blank": true
}]
...

这里有一些例子来展示上述内容脚本声明的效果。

This page is at https://example.com/index.html
<!-- Content script will run here -->

<!-- This frame is about:blank, at the same origin as https://example.com -->
<iframe></iframe> <!-- Content script runs in the frame too -->

<!-- This frame is about:srcdoc, at the same origin as https://example.com -->
<iframe srcdoc="test"></iframe> <!-- Content script runs in the frame too -->

<!-- This frame is about:blank, but with a unique origin -->
<iframe sandbox=""></iframe> <!-- No content script -->

<!-- This frame is about:blank, at the same origin as https://example.com -->
<iframe sandbox="allow-same-origin"></iframe>
<!-- Content script runs in the above frame too -->

注意:如果开场符本身也是空白帧,则使用开场符的开场符,直到找到非空白开场符。如果没有这样的 opener,或者如果 opener 不匹配 "matches" 模式,那么内容脚本不会 运行.


同样,如果matchAboutBlanktrue和[=93,则使用chrome.tabs.executeScript动态插入的内容脚本只有运行在空白帧中=] 扩展程序有权在该框架或选项卡的(最近的非空白)打开器中 运行 脚本。