Search code examples
javascriptgreasemonkey

Closing windows with Greasemonkey


I've been writing a Greasemonkey script for Chrome (drags and drops as an extension) for use with Facebook. I know that Greasemonkey is sandboxed and that, at least as far as Firefox goes, you cannot close windows with scripts unless you change a setting in the browser. However, I know that most browsers allow child windows to be closed by the parent window without needing the confirmation of the user. Anyways I'm trying to get my script to open a window, grab a text piece from an HTML object and close the window at the press of a button on the page.

My code for opening the window looks like this:

function birthday(linkAddress) {
var winNew=window.open(linkAddress, "_blank", "height=100", "width=100");
/*code to be run on page*/
    winNew.close()
}

The window opens fine but I get a javascript error in Chrome saying "cannot call method 'close' of undefined". I assume there is something wrong with my object related to Greasemonkey sandboxing, but I'm at a loss to determine what. Is it even possible to close a window using a Greasemonkey script in Chrome? Is there some setting I need to enable? Or is my code just wrong? Keep in mind that this code is running from a function in the main window page that I set equal to a function in my script using an onClick event of a button I wrote to the page. Also any tips for accessing DOM elements on the child page would be appreciated.

Thanks for any help!

(Sorry I'm quite new as well as out of practice with programming, and this is the first time I've written a Greasemonkey script)


Solution

  • The snippet of code posted is inadequate and does not match the stated operation of "close the window at the press of a button on the page".

    So the best guess is that either the close function is outside birthday() (where winNew is defined) or something in the unshown code is causing the problem.

    Another caveat is you need to be aware that the Greasemonkey script may also fire on the child window -- depending on if/what URL is specified when the child window is opened.

    Here is sample code that opens and closes a child window at the push of buttons:
    See it in action at jsfiddle.

    <html>
    <head>
    <script type="text/javascript">
        var chldWnd;
    
        function OpenChldWin ()
        {
            //chldWnd = window.open ("http://www.google.com");
            chldWnd = window.open ('','','height=200, width=200');
            chldWnd.document.write ('<p>My New Window</p>');    //-- Don't do if opening a URL in new window.
            chldWnd.focus();
        }
    
        function CloseChldWin ()
        {
            if (chldWnd.closed)
                alert ('Child window was already closed.');
            else
            {
                chldWnd.close ();
                alert ('Child window is NOW closed.');
            }
        }
    </script>
    </head>
    <body>
    <input type="button" value="Open Child Window"      onclick="OpenChldWin ()">
    <input type="button" value="Close the Child Window" onclick="CloseChldWin ()">
    </body>
    </html>