Search code examples
javascriptmicrosoft-edgebookmarklet

why does window.open(''); usually work as expected; but sometimes fails?


My Test Case:

  • I browse to a webpage;
  • open DevTools console;
  • and execute windows.open('');

it usually works fine and I get a new blank window.

But if I browse to [NSFW] spankbang.com and do the same; it fails.

1-2yrs ago I wrote a bookmarklet for YouTube. It grabs <meta> tags and <script type="application/json"> blocks and dumps them to a blank page. It has worked great on YouTube and a ton of other sites. But, today I tried using it on [NSFW] spankbang.com and it fails!

This is on Windows 10 Pro using Edge.


Solution

  • because they delete the open fonction at this website

    // the open fonction are deleted
    delete window.open
    // throw error
    window.open('')
    

    and you can get it back like this

    console.log(typeof window.open); // "function"
    delete window.open;
    console.log(typeof window.open); // "undefined"
    
    //  "insert an iframe into DOM, to take window.open from its contentWindow"
    var iframe = document.createElement("iframe");
    document.body.appendChild(iframe);
    window.open = iframe.contentWindow.open;
    console.log(typeof window.open); // "function"
    // it work now!
    window.open('')
    

    and dont post adulte link here