Chrome内容脚本无法在about:空白页中加载

Les*_* Wu 4 javascript google-chrome-extension content-script

我正在开发一个Chrome扩展程序,它将根据以下清单加载内容脚本:

"content_scripts" : [
    {
      "matches" : [ "<all_urls>" ],
      "js" : [  "scripts/namespace/namespace.js",
                "scripts/log/Logger.js"]
       "run_at" : "document_start",
       "all_frames" : true
     }
]
Run Code Online (Sandbox Code Playgroud)

有一个样机站点,其HTML是:

<html>
<head>
<script language=javascript>
    var test;

    function makewindow() {
        test=window.open('mywin');
        test.window.document.write("<html><body><A onclick=window.close(); href= > click Me to Close</A>");
        test.window.document.write(" ---- </body></html>");
    }
</script>

</head>
<body>
<a href="" onclick="makewindow();return false;" >Click me to launch the external Window</a>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

如果我单击A链接,它将运行makewindow功能并弹出一个新窗口。

新窗口的URL为about:blank,并且内容脚本未加载到该新窗口中。

如何在新窗口中加载内容脚本?

ali*_*_15 6

在清单文件中,添加一行:

"content_scripts" : [
{
  "matches" : [ "<all_urls>" ],
  "match_about_blank" : true,
  "js" : [  "scripts/namespace/namespace.js",
            "scripts/log/Logger.js"]
   "run_at" : "document_start",
   "all_frames" : true
 }
]
Run Code Online (Sandbox Code Playgroud)


Bro*_*ams 5

更新:自Chrome 37(2014年8月26日),你可以设置match_about_blank标志文件,以true火为about:blank页面。

请参阅下面的alib_15的答案



对于版本37之前的Chrome:

您无法在about:blank页面上加载Chrome内容脚本(或用户脚本)。

这是因为about:它不是“许可方案”之一。从chrome扩展程序匹配模式

的匹配图案本质上是与开头的URL 允许方案httphttpsfileftp,或chrome-extension)...


在这种情况下,你可能会被关闭劫持更好makewindow()