debug

用于调试的脚本库

اعتبارا من 19-05-2018. شاهد أحدث إصدار.

لا ينبغي أن لا يتم تثبيت هذا السكريت مباشرة. هو مكتبة لسكبتات لتشمل مع التوجيه الفوقية // @require https://update.greasyfork.org/scripts/34143/598477/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.

ستحتاج إلى تثبيت إضافة مثل Stylus لتثبيت هذا النمط.

ستحتاج إلى تثبيت إضافة لإدارة أنماط المستخدم لتتمكن من تثبيت هذا النمط.

ستحتاج إلى تثبيت إضافة لإدارة أنماط المستخدم لتثبيت هذا النمط.

ستحتاج إلى تثبيت إضافة لإدارة أنماط المستخدم لتثبيت هذا النمط.

(لدي بالفعل مثبت أنماط للمستخدم، دعني أقم بتثبيته!)

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

(function(context) {
    var debugD = true; //debug默认设置
    //创建分组打印
    var consoleFactory = function(groupName, styleStr, type, debugMode) {
        debugMode = (debugMode === undefined) ? myDebugger.debugD : debugMode;
        type = type || 'log';
        /**
         * 创建的分组打印日志
         * @param  {bool} debugMode 是否启用日志
         */
        var log = function() {
            if (log.debugMode) {
                var argArr = Array.prototype.slice.apply(arguments);
                console[type].apply(null, argArr);
            }
        }
        log.debugMode = debugMode;
        /**
         * 打印对象
         * @param  {string} desc 对象描述
         * @param  {object} obj  对象数据
         */
        log.logObj = function(desc, obj) {
            if (this.debugMode) {
                var argArr = [].slice.call(arguments);
                var desc = argArr.shift();
                argArr.unshift('color:green');
                argArr.unshift(`%c[${desc}]`);

                console.log.apply(console,argArr);
            }
        }
        /**
         * 打印数组
         * @param  {string} desc 数组描述
         * @param  {array} arr  数组类型
         */
        log.logArr = function(desc, arr) {
            if (this.debugMode) {
                var argArr = [].slice.call(arguments);
                var desc = argArr.shift();
                console.group(`%c[${desc}]`, 'color:blue;font-size:13px');
                argArr.forEach(item=>{
                    console.table(item);
                });
                console.groupEnd();
            }
        }
        /**
         * 断言
         * @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);
            }
        }
        /**
         * 强调 用于突出显示的场合
         * @param  {string} desc    强调内容
         * @param  {string} bgColor 颜色
         */
        log.em = function(desc, bgColor) {
            bgColor = bgColor || 'green';
            if (debugMode) {
                console.log(`%c${desc}`, `font-size:18px;background-color:${bgColor};color:white;padding:4px`);
            }
        }
        return log;
    }

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

    var myDebugger = {
        consoleFactory: consoleFactory,
        debugTrue: debugTrue,
        debugD:debugD
    };

    context.myDebugger = myDebugger;

})(window);