Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restore native Window method

Tags:

javascript

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?

like image 888
Sander Avatar asked Oct 26 '25 07:10

Sander


1 Answers

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

like image 96
Guillaume Georges Avatar answered Oct 28 '25 21:10

Guillaume Georges



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!