5

I am developing a Chrome extension which will load content script according to the following manifest:

"content_scripts" : [
    {
      "matches" : [ "<all_urls>" ],
      "js" : [  "scripts/namespace/namespace.js",
                "scripts/log/Logger.js"]
       "run_at" : "document_start",
       "all_frames" : true
     }
]

There is a mockup site whose HTML is:

<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>

If I click the A link and it will run makewindow function and pops up a new window.

The URL of new window is about:blank, and content script is not loaded in this new window.

How to load content script in this new window?

Brock Adams
  • 90,639
  • 22
  • 233
  • 295
Leslie Wu
  • 760
  • 3
  • 13
  • 29

2 Answers2

7

Update: As of Chrome 37 (August 26, 2014), you can set the match_about_blank flagDoc to true to fire for about:blank pages.

See alib_15's answer below.



For Chrome prior to version 37:

You cannot load a Chrome content script (or userscript) on about:blank pages.

This is because about: is not one of the "permitted schemes". From chrome extensions Match Patterns:

A match pattern is essentially a URL that begins with a permitted scheme (http, https, file, ftp, or chrome-extension)...


In this case, you might be better off hijacking makewindow().

Community
  • 1
  • 1
Brock Adams
  • 90,639
  • 22
  • 233
  • 295
  • 1
    @alib_15, this was correct at the time of the question. Updated the answer, thanks. – Brock Adams Mar 31 '16 at 10:19
  • Thanks Brock. I'm writing my 1st extension and this caught me out too. I think it catches out a lot of people. I suspected your answer was correct at the time of writing but I felt a little mischievous. – alib_15 Apr 01 '16 at 18:17
6

In the manifest file, add a line:

"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
 }
]
alib_15
  • 286
  • 2
  • 11