Jump to content

User:Mike Dillon/Scripts/timeOnload.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// Requires: [[User:Mike Dillon/Scripts/easydom.js]]
// Requires: [[User:Mike Dillon/Scripts/toolbox.js]]

/* <pre><nowiki> */

var onloadFunctTimes = {};
var oldOnloadFuncts;
var oldWindowFuncts;
var oldRunOnloadHook = window.runOnloadHook;
window.runOnloadHook = function () {
    var tf = function (func) {
        var start = new Date();
        func();
        var end = new Date();
        return {
            start: start,
            end:   end,
            diff:  end.getTime() - start.getTime()
        };
    };

    var wrapOnload = function (id, func) {
        return function () {
            onloadFunctTimes[id] = tf(func);
        };
    };

    oldWindowFuncts = {};
    var wrapWindowFunct = function (name, alias) {
        if (!alias) alias = name;
        oldWindowFuncts[alias] = window[name];
        window[name] = wrapOnload(alias, oldWindowFuncts[alias]);
    };

    /*
    wrapWindowFunct('histrowinit', 'hri');
    wrapWindowFunct('unhidetzbutton', 'uhtb');
    wrapWindowFunct('tabbedprefs', 'tp');
    wrapWindowFunct('updateTooltipAccessKeys', 'utak');
    wrapWindowFunct('akeytt');
    wrapWindowFunct('scrollEditBox', 'seb');
    wrapWindowFunct('setupCheckboxShiftClick', 'scsc');
    wrapWindowFunct('sortableTables', 'st');

    oldOnloadFuncts = window.onloadFuncts;
    window.onloadFuncts = [];
    for (var i in oldOnloadFuncts) {
        window.onloadFuncts[i] = wrapOnload(i, oldOnloadFuncts[i]);
    }
    */

    var loadTime = tf(oldRunOnloadHook);
    with (easydom) {
        var createTooltip = function (lt) {
            return "Start: " + lt.start.getTime() + ", End: " + lt.end.getTime();
        };

        var createOnloadFunctItem = function (id) {
            return li(
                { "title": createTooltip(onloadFunctTimes[id]) },
                a({ "href": "#",
                    "onclick": function () {
                        if (oldOnloadFuncts[id]) {
                            alert(oldOnloadFuncts[id]);
                        } else if (oldWindowFuncts[id]) {
                            alert(oldWindowFuncts[id]);
                        }
                        return false;
                    }
                }, id), ": ", onloadFunctTimes[id].diff, " ms");
        };

        var d = div({ title: createTooltip(loadTime) },
            strong('JS load time: '), loadTime.diff + ' ms');
        var list = ul();
        for (var id in onloadFunctTimes) {
            list.appendChild(createOnloadFunctItem(id));
        }
        if (list.childNodes.length) {
            d.appendChild(list);
        }

        addToolboxLink(null, d);
    }
};

/* </nowiki></pre> */