4

For a script I'm writing, I'd like to use the native window.open method. However, a script already loaded to which I don't have access, overwrites the global window.open method with a boolean (ouch).

I know how to restore the methods on the Document (via HTMLDocument.prototype), but I don't know how to restore them on the Window, as I can't seem to find the equivalent for that to Window. Window.prototype.open does not exist for example.

I have tried creating an iframe, and getting the open method from that contentWindow in the iframe, but the browser will block opening windows using open because it was probably created in another origin. Neither delete open; does work because open was defined using var in the globally loaded script.

So, how can I restore the open method, defined as 'native code' in Chrome?

I know there are similar questions around, but actually the main question is:

Is there a equivalent of HTMLDocument for the Window object?

Sander
  • 1,244
  • 1
  • 12
  • 24

1 Answers1

5

I've found this question and the accepted answer (using an iframe) could be used in your case.

The only issue is you can only use the retrieved version of window.open as long as the iframe is still in your document.

function customOpen() {
    // local variables definitions : 
    var url = "https://stackoverflow.com", iframe, _window;

    // creating an iframe and getting its version of window.open : 
    iframe = document.createElement("iframe");
    document.documentElement.appendChild(iframe);
    _window = iframe.contentWindow;

    // storing it in our window object
    window.nativeOpen = _window.open;   

    try {
        window.open(url);
    } catch (e) {
        console.warn(e); // checking that window.open is still broken 
    }
    window.nativeOpen(url);

    // deleting the iframe : 
    document.documentElement.removeChild(iframe);
}

document.getElementById("button").addEventListener("click", customOpen);

Another JSFiddle


Keeping the workaround answer in case someone needs it :

Can you execute a custom script prior to the execution of the script that redefines window.open? If so, you could create a copy of the window.open in another global variable.

It could look like this :

1. First : a backup script

window.nativeOpen = window.open;

2. Then, whatever the window.open overwriting script does :

window.open = false; // who does that, seriously?

3. Your window opening script, that'll use your window.open copy :

function customOpen() {
    var url = "https://stackoverflow.com";
    try {
        window.open(url);
    } catch (e) {
        console.warn(e);
    }
    window.nativeOpen(url);
}

JSFiddle example

Guillaume Georges
  • 3,878
  • 3
  • 14
  • 32
  • Thanks for your answer. That is my current workaround indeed :) But, I'm still looking for the original prototype. – Sander Dec 28 '17 at 12:09
  • Thanks! I've clarified my question a bit more, I think I'm actually looking for the equivalent of the HTMLDocument to document, but then the 'Window' version. I also don't have access to the script calling the open method and what it will call. – Sander Dec 28 '17 at 13:21