Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome content script does not load in about:blank page

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?

like image 685
Leslie Wu Avatar asked May 28 '13 06:05

Leslie Wu


2 Answers

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().

like image 190
Brock Adams Avatar answered Oct 17 '22 02:10

Brock Adams


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
 }
]
like image 32
alib_15 Avatar answered Oct 17 '22 02:10

alib_15