reading the firebug console in javascript

后端 未结 5 582
春和景丽
春和景丽 2020-12-19 16:37

I\'m looking for a way to read the most recent command that was logged to the firebug console.

For example, I could have something that does

console.         


        
5条回答
  •  心在旅途
    2020-12-19 17:27

    Here's a more elaborate version I put together:

    /**
     * Console log with memory
     *
     * Example:
     *
     *     console.log(1);
     *     console.history[0]; // [1]
     *
     *     console.log(123, 456);
     *     console.history.slice(-1)[0]; // [123, 456]
     *
     *     console.log('third');
     *     // Setting the limit immediately trims the array,
     *     // just like .length (but removes from start instead of end).
     *     console.history.limit = 2;
     *     console.history[0]; // [123, 456], the [1] has been removed
     *
     * @author Timo Tijhof, 2012
     */
    console.log = (function () {
        var log  = console.log,
            limit = 10,
            history = [],
            slice = history.slice;
    
        function update() {
            if (history.length > limit) {
                // Trim the array leaving only the last N entries
                console.history.splice(0, console.history.length - limit);
            }
        }
    
        if (console.history !== undefined) {
            return log;
        }
    
        Object.defineProperty(history, 'limit', {
            get: function () { return limit; },
            set: function (val) {
                limit = val;
                update();
            }
        });
    
        console.history = history;
    
        return function () {
            history.push(slice.call(arguments));
            update();
            return log.apply(console, arguments);
        };
    
    }());
    

提交回复
热议问题