Getting all custom window children.

January 12th, 2017 | Permalink

On some websites their creators store some useful functions in window scope directly. For example, when site is all AJAX-driven et cetera, like vk.com, it may be useful from both solidity and performance perspectives, I believe. Of course, they say that “littering” global ojbect is bad, but it is bad only when you’re developing a library or do not exactly do what are you doing.

Now, as in previous post, by creating the IFrame and accessing its’ contentWindow we can get a “fresh” window object. It is useful when we want to restore some redefined native
code, but also we can also get all custom children of current window object. Getting all new variables in window, grouped by their type:

(function(ifr){
    document.body.appendChild(ifr);      
    var newKeys = {};

    Object.keys(window).forEach(function(key){
        if(typeof ifr.contentWindow[key] == 'undefined'){
            var type = typeof window[key];
            newKeys[type] = newKeys[type] || {};
            newKeys[type][key]=window[key];
        }
    });
    ifr.parentNode.removeChild(ifr);
    return newKeys;
})(document.createElement('iframe'));

This comes in handy sometimes. For example, when you’re writing a userscript for some website. Or just want to dig in someone else’s code which is interesting sometimes on complicated websites (as chrome developer console, for example, allows you to right-click the function and “Show function definition”, even allowing pretty-printing minified scripts)