debug

用于调试的脚本库

Pada tanggal 24 November 2017. Lihat %(latest_version_link).

Skrip ini tidak untuk dipasang secara langsung. Ini adalah pustaka skrip lain untuk disertakan dengan direktif meta // @require https://update.greasyfork.org/scripts/34143/232856/debug.js

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         debug
// @namespace    https://github.com/yeomanye
// @version      0.6.1
// @include      *://*
// @description  用于调试的脚本库
// @author       Ming Ye
// ==/UserScript==

(function(context) {
    var debugD = true; //debug默认设置
    //创建分组打印
    var consoleFactory = function(groupName, styleStr, type, debugMode) {
        debugMode = (debugMode === undefined) || debugD;
        type = type || 'log';
        /**
         * 创建的分组打印日志
         * @param  {bool} debugMode 是否启用日志
         */
        var log = function(debugMode) {
            //初始化操作
            if (!log.nFirst) {
                log.nFirst = true;
                log.debugMode = debugMode;
                log.groupName = log.groupName || groupName;
                console.groupEnd();
                console.group('%c' + log.groupName, styleStr);
            }
            if (log.debugMode) {
                var argArr = Array.prototype.slice.apply(arguments);
                console[type].apply(null, argArr);
            }
        }
        /**
         * 打印对象
         * @param  {string} desc 对象描述
         * @param  {object} obj  对象数据
         */
        log.logObj = function(desc, obj, debugMode) {
            debugMode = (debugMode === undefined) || this.debugMode;
            if (debugMode) {
                console.group('%c' + desc, 'color:green;font-size:16px');
                console.log(obj);
                console.groupEnd();
            }
        }
        /**
         * 打印数组
         * @param  {string} desc 数组描述
         * @param  {array} arr  数组类型
         */
        log.logArr = function(desc, arr, debugMode) {
            debugMode = (debugMode === undefined) || this.debugMode;
            if (debugMode) {
                console.group('%c' + desc, 'color:blue;font-size:16px');
                console.table(arr);
                console.groupEnd();
            }
        }
        /**
         * 重置分组日志
         * @param  {string}  groupName 日志名
         * @param  {Boolean} debugMode 是否启用日志
         */
        log.reset = function(groupName, debugMode) {
            log.nFirst = false;
            log.debugMode = (debugMode === undefined) || true;
            log.groupName = groupName || this.groupName;
        }
        /**
         * 断言
         * @param  {bool} expr      表达式
         * @param  {string} msg       消息
         * @param  {bool} debugMode 是否启用
         */
        log.assert = function(expr,msg,debugMode){
            debugMode = (debugMode === undefined) || this.debugMode;
            if(debugMode){
                console.assert(expr,msg);
            }
        }
        return log;
    }

    // 当参数为true时开启调试
    var debugTrue = function(debugMode) {
        debugMode = (debugMode === undefined) || debugD;
        return function() {
            if (debugMode) debugger;
        }
    }

    context.myDebugger = {
        consoleFactory: consoleFactory,
        debugTrue: debugTrue
    };

})(window);