自定义console

 

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    * {
        padding: 0;
        margin: 0;
    }
</style>

<body>
    <img src="./Snipaste_2024-06-20_14-46-12.png" alt="">
    <script>
        (function name(window) {
            // ANSI 转义码定义颜色
            const COLORS = {
                reset: "\x1b[0m",
                bright: "\x1b[1m",
                fgRed: "\x1b[31m",
                fgGreen: "\x1b[32m",
                fgYellow: "\x1b[33m",
                fgBlue: "\x1b[34m",
                fgMagenta: "\x1b[35m",
                fgCyan: "\x1b[36m",
                fgWhite: "\x1b[37m",
                bgRed: "\x1b[41m",
                bgGreen: "\x1b[42m",
                bgYellow: "\x1b[43m",
                bgBlue: "\x1b[44m",
                bgMagenta: "\x1b[45m",
                bgCyan: "\x1b[46m",
                bgWhite: "\x1b[47m"
            }


            const _console = window.console

            // 封装公共函数
            const commFn = function (type, color) {
                const callback = console[type] || console['log']
                // const callback = console['log']
                return function (...arg) {
                    arg.unshift(type)
                    let content = arg.reduce((acc, val) => acc + `%c ${val} `, '') // 内容
                    let bg = arg.map((val, index) => {   // 背景
                        const _border = `padding: 2px; border:1px solid ${color}; border-left:none;`
                        const _color = `color: ${index == 0 ? '#fff' : color};`
                        const _background = `background: ${index == 0 ? color : 'transparent'};`
                        return _background + _border + _color
                    })
                    callback(content, ...bg)
                }
            }


            /****************************************************
                         log + error + sucess
            *****************************************************/
            const log_color = '#666'
            let log = commFn('log', log_color)

            const error_color = '#e95b5b'
            let error = commFn('error', error_color)

            const sucess_color = '#63973e'
            let sucess = commFn('sucess', sucess_color)


            /****************************************************
                         img
            *****************************************************/
            let img = (url, scale = 1) => {
                const img = new Image()
                img.onload = () => {
                    const c = document.createElement('canvas')
                    const ctx = c.getContext('2d')
                    if (!ctx) { return }
                    c.width = img.width
                    c.height = img.height
                    ctx.fillStyle = 'red'
                    ctx.fillRect(0, 0, c.width, c.height)
                    ctx.drawImage(img, 0, 0)
                    const dataUri = c.toDataURL('image/png')
                    _console.log(
                        `%c `,
                        `padding: ${Math.floor((img.height * scale) / 2)}px ${Math.floor((img.width * scale) / 2)}px;
                        background-image: url(${dataUri});
                        background-repeat: no-repeat;
                        background-size: ${img.width * scale}px ${img.height * scale}px;
                        color: transparent;
                        `
                    )

                }
                img.src = url
            }

            /****************************************************
                         table
            *****************************************************/
            const table = function (data) {

                if (!Array.isArray(data) || data.length === 0) {
                    return
                }

                // 获取表头
                const headers = Object.keys(data[0])

                // 计算每列的最大宽度
                const colWidths = headers.map(header => {
                    return Math.max(
                        header.length,
                        ...data.map(row => String(row[header] !== undefined ? row[header] : '').length)
                    )
                })

                // 创建分隔线
                const createSeparator = () => {
                    return COLORS.fgCyan + colWidths.map(width => '_'.repeat(width + 2)).join('')
                }

                // 打印行数据
                const printRow = (row) => {
                    return headers.map((header, i) => {
                        const cell = String(row[header] !== undefined ? row[header] : header)
                        return COLORS.fgCyan +  cell + ' '.repeat(colWidths[i] - cell.length)
                    }).join(' | ')
                }

                // 打印表格
                _console.log(createSeparator())
                _console.log(printRow(headers).toString())
                _console.log(createSeparator())
                data.forEach(row => {
                    _console.log(printRow(row))
                })
                _console.log(createSeparator())
            }


            /**************************************
                 off 关闭拦截,还原为系统自带
            **************************************/
            const off = () => {
                log = null
                error = null
                window.console = _console
            }

            window.console = {
                log,
                error,
                sucess,
                img,
                off,
                table
            }

        }(window))

        //  console.off()
        console.log('测试', '112')
        console.error('错误', '112')
        console.sucess('成功', '112')
        console.img('./01.png')

        // 示例表格数据
        const tableData = [
            { name: 'Alice', age: 25, city: 'New York' },
            { name: 'Bob', age: 30, city: 'San Francisco' },
            { name: 'Charlie', age: 22, city: 'Chicago' }
        ]

        console.table(tableData)

    </script>
</body>

</html>

借鉴:: https://juejin.cn/post/7371716384847364147

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值