1

My script opens a new window and then writes its content. The destination window displays "Hello World" as expected, but the URL is the same as the window that the script ran in and I don't understand why.

Is there a way to build a new window without picking up the old URL?

function doTest() {
    var impl = document.implementation;
    var tempDoc = impl.createHTMLDocument("Hello");
    var tempBody = tempDoc.getElementsByTagName("body")[0];
    var tempDiv = tempDoc.createElement('div');
    var tempMsg = tempDoc.createTextNode('Hello World.');
    tempDiv.appendChild(tempMsg);
    tempBody.appendChild(tempDiv);

    var destWindow=window.open("about:blank", "title", null, false);
    var destDoc=destWindow.document;
    destDoc.open();
    destDoc.write("<html>"+tempDoc.documentElement.innerHTML+"</html>");    
    destDoc.close();
}


var btnTest = document.createElement( 'input' );
btnTest.setAttribute( 'value', 'Test' );
btnTest.setAttribute( 'type', 'button' );
btnTest.addEventListener('click',doTest,true);
document.body.appendChild( btnTest );


I'm using Firefox 20 and Greasemonkey 1.8.

adg
  • 552
  • 1
  • 6
  • 17
  • Clarification: if I don't open and close the document then the url contains about:blank as expected. But as soon as I open and close, even without doing the write, the url changes to the same url as the current page. – adg May 05 '13 at 03:11

1 Answers1

2

I don't think you can do this. This is probably a security feature; see Bug 337344, "Disable location bar hiding by default, to make chrome spoofing harder" and related bugs.

I think that as soon as parent-window javascript tries to alter the content, then the anti-spoofing protection forces the location to the opener's URL. This is true even if you open the window with a URL other than about:blank.

You can hide the location bar by change the setting dom.disable_window_open_feature.location to true in about:config and then hide the address bar by passing location=false in window.open.
But if you do, then the opener's URL will be prepended to the title bar.

You'll probably have to live with this and be grateful that phishing is that much harder. ;-) Or, if you can make a compelling case for why you should be able to alter or blank the location: (1) add it to your question and (2) open a bug or feature request with Mozilla.


Workaround:
You can set your script to also fire on about:blank, and then have GM make the page from the blank instance, not the opening instance. See this answer for an almost identical scenario/solution.

Be sure to open the window with an explicit about:blank, like so:

var destWindow = window.open("about:blank", "title", null);
Community
  • 1
  • 1
Brock Adams
  • 90,639
  • 22
  • 233
  • 295
  • thanks for the very clear info. I've been reading your numerous Greasemonkey answers for a few weeks now and the work you've done has been a huge help. – adg May 05 '13 at 04:10
  • 1
    Thanks. Glad to help. And, to quote my councilman: "Vote early and vote often." ;-) – Brock Adams May 05 '13 at 04:12