Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome extension permission for "about:blank" page

My chrome extension is not using content_scripts because I do not want to inject the code into every page. Instead, when the user clicks on the button a popup window opens and injects code into the page.

So, in the manifest.json there is a permissions block:

"permissions": [
  "activeTab"
]

And in the popup.js there is this code:

chrome.windows.getCurrent( function(win) {
    chrome.tabs.query({
        'windowId': win.id,
        'active': true
    }, function(tabArray) {

        // inject css & js only on initial click
        chrome.tabs.executeScript( null, {
            code : 'document.querySelector( "body" ).classList.contains( "_myExtension_code_injected" )'
        }, function( result ) {
            if ( result && !result[0] ) {
                chrome.tabs.insertCSS( null, {
                    file: 'myExtension.css'
                });

                chrome.tabs.executeScript(null, {
                    file: 'myExtension.js'
                }, function(){
                    chrome.tabs.executeScript(null, {
                        code: 'myExtension.init();'
                    });
                });
            }
        });

    });
});

The problem is some web sites open a popup window with additional information. And the URL of that popup page is "about:blank". If I try to initialize the extension, I see this message in the console:

Unchecked runtime.lastError while running tabs.executeScript: Cannot access contents of url "about:blank". Extension manifest must request permission to access this host.

I do not see a way to add a "about:blank" page to the permissions. And I really don't want to start using content_scripts just so I can set the match_about_blank setting.

I've tried adding "about:blank" and "about:* to the permission and all I get is an installation error.

There were warnings when trying to install this extension: Permission 'about:*' is unknown or URL pattern is malformed.

Is there a solution?


Update: Here is a gist with everything you'll need. A link to a jsbin demo included, but the problem isn't with that site specifically. The extension was originally tested on a yahoo mail pop-out window which you get by choosing to print an email.

like image 337
Mottie Avatar asked Aug 29 '15 17:08

Mottie


People also ask

Can we use jquery in Chrome extension?

You can just put jquery. js into extension folder and include it in the manifest: { "name": "My extension", ... "content_scripts": [ { "matches": ["http://www.google.com/*"], "css": ["mystyles.


1 Answers

Try using chrome.tabs.executeScript({ file: "script.js", matchAboutBlank: true })

"If matchAboutBlank is true, then the code is also injected in about:blank and about:srcdoc frames if your extension has access to its parent document. Code cannot be inserted in top-level about:-frames. By default it is false."

https://developer.chrome.com/extensions/tabs#property-details-matchAboutBlank

like image 57
Daniel Herr Avatar answered Sep 17 '22 00:09

Daniel Herr