Greasy Fork

Color Log Userscript Library

Colored logger for Greasemonkey user scripts. Drop-in decorator for 'window.console's debug(), info(), log(), warn(), error() methods. Control 'console.debug' log visibility via the 'window.DEBUG' global boolean.

目前为 2018-06-02 提交的版本。查看 最新版本

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.greasyfork.icu/scripts/38888/602069/Color%20Log%20Userscript%20Library.js

// ==UserScript==
// @name            Color Log Userscript Library
// @namespace       de.sidneys.userscripts
// @homepage        https://gist.githubusercontent.com/sidneys/5d44a978d18a1b91f554b2358406671d/raw/
// @version         12.0.0
// @description     Colored logger for Greasemonkey user scripts. Drop-in decorator for 'window.console's debug(), info(), log(), warn(), error() methods. Control 'console.debug' log visibility via the 'window.DEBUG' global boolean.
// @author          sidneys
// @icon            https://www.greasespot.net/favicon.ico
// @include         http*://*/*
// @grant           unsafeWindow
// ==/UserScript==

/**
 * Check DEBUG within window and Greasemonkey context
 * @returns {Boolean} - Yes/no
 */
let isDebug =  () => Boolean(unsafeWindow.DEBUG) || Boolean(this.DEBUG);
isDebug = isDebug.bind(this);

/**
 * Get log prefix
 * @returns {String} - Prefix
 */
let getPrefix =  () => GM_info.script.name;


/**
 * Original log method
 * @type {function}
 * @constant
 */
const originalLog = console.log;

/**
 * Color log
 * @type {Object}
 * @constant
 */
const colorLog = {
    debug() {
        if (!isDebug()) { return; }

        const color = `rgb(255, 150, 70)`;

        originalLog.call(this, `🛠 %c[${getPrefix()}] %c${Array.from(arguments).join(' ')}`, `font-weight: 600; color: ${color};`, `font-weight: 400; color: ${color};`);
    },
    error() {
        const color = `rgb(220, 0, 30)`;

        originalLog.call(this, `🚨️ %c[${getPrefix()}] %c${Array.from(arguments).join(' ')}`, `font-weight: 600; color: ${color};`, `font-weight: 400; color: ${color};`);
    },
    info() {
        const color = `rgb(0, 200, 180)`;

        originalLog.call(this, `ℹ️ %c[${getPrefix()}] %c${Array.from(arguments).join(' ')}`, `font-weight: 600; color: ${color};`, `font-weight: 400; color: ${color};`);
    },
    log() {
        const color = `rgb(70, 70, 70)`;

        originalLog.call(this, `✳️ %c[${getPrefix()}] %c${Array.from(arguments).join(' ')}`, `font-weight: 600; color: ${color};`, `font-weight: 400; color: ${color};`);
    },
    warn() {
        const color = `rgb(255, 100, 0)`;

        originalLog.call(this, `⚠️ %c[${getPrefix()}] %c${Array.from(arguments).join(' ')}`, `font-weight: 600; color: ${color};`, `font-weight: 400; color: ${color};`);
    }
};


/**
 * Main
 * Replace console methods (debug, info, log, warn, error)
 */
Object.assign(console, colorLog);