LateLog - Simple storage-based Log Library

Simple library to log to the storage, granting the possibility of viewing the log after the URL changes

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.greasyfork.org/scripts/461057/1156386/LateLog%20-%20Simple%20storage-based%20Log%20Library.js

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         LateLog - Simple storage-based Log Library
// @namespace    satology.latelog
// @version      0.2
// @description  Simple library to log to the storage, granting the possibility of viewing the log after the URL changes
// @author       satology
// @grant        GM_getValue
// @grant        GM_setValue
// @grant        GM_registerMenuCommand
// ==/UserScript==

var LateLog = (function() {
    'use strict';
    const latelog_getDateTimeString = (date = new Date()) => `${('' + date.getUTCFullYear()).slice(-2)}-${('0' + date.getUTCMonth()).slice(-2)}-${('0' + date.getUTCDate()).slice(-2)} ${('0' + date.getUTCHours()).slice(-2)}:${('0' + date.getUTCMinutes()).slice(-2)}:${('0' + date.getUTCSeconds()).slice(-2)}`;

    class Storage {
        constructor(prefix = '', config = {}) {
            this.prefix = prefix;
            this.config = config;
        }

        write(key, value, parseIt = false) {
            GM_setValue(this.prefix + key, parseIt ? JSON.stringify(value) : value);
        }

        read(key, parseIt = false) {
            let value = GM_getValue(this.prefix + key);
            if(value && parseIt) {
                value = JSON.parse(value);
            }
            return value;
        }
    }

    class LateLog {
        constructor(options = {}) {
            if(!this._isValidSetup(options)) return;
            this.storage = new Storage();
            this.options = {
                level: 5,
                maxLines: 200,
                addTimestamp: true,
                printWhileLogging: false,
                // showDisplayButton: true,
                ...options
            };
            this._init();
        }

        _isValidSetup(options) {
            if(! (typeof GM_getValue === "function" &&
                  typeof GM_setValue === "function" &&
                  typeof GM_registerMenuCommand === "function") ) {
                console.warn(
                    `LateLog cannot be initialized - At least one off the following @grant is missing in your script:
@grant        GM_getValue
@grant        GM_setValue
@grant        GM_registerMenuCommand`);
                return false;
            }
            // TODO: validate options
            return true;
        }

        _init() {
            let that = this;
            GM_registerMenuCommand('View log', function(evt) {
                console.log('Printing LateLog');
                that.display();
            });
            return true;
        }

        _write(msg, msgLevel = 1) {
            if (this.options.level < msgLevel) {
                return;
            }

            let log = this.storage.read('log', true);
            log = log ? log : [];

            if (msg === null) {
                msg = 'null';
            }
            if (msg === undefined) {
                msg = 'undefined';
            }

            let savable = {
                lvl: msgLevel
            };
            if (this.options.addTimestamp) {
                savable.ts = `${latelog_getDateTimeString()}`;
            }
            if (msg instanceof Error) {
                savable.msg = msg.toString();
            } else if (typeof(msg) == 'object') {
                savable.msg = JSON.stringify(msg);
                savable.parse = true;
            } else {
                savable.msg = msg;
            }
            log.push(savable);

            if(log.length > this.options.maxLines) {
                log.splice(0, log.length - this.options.maxLines);
            }

            this.storage.write('log', log, true);
            this._formatPrint(savable);
        }

        _getPrintFn(level) {
            switch(level) {
                case 1:
                    return console.log;
                case 2:
                    return console.info;
                case 3:
                    return console.debug;
                case 4:
                    return console.warn;
                case 5:
                    return console.error;
                default:
                    return console.log;
            }
        }

        _formatPrint(x) {
            if (x.parse) {
                try {
                    x.msg = JSON.parse(x.msg);
                } catch (err) {}
                if (this.options.addTimestamp) {
                    this._getPrintFn(x.lvl)(`${x.ts}:`);
                }
                this._getPrintFn(x.lvl)(x.msg);
            } else {
                this._getPrintFn(x.lvl)(`${this.options.addTimestamp ? x.ts + ' ' : ''} ${x.msg}`);
            }
        }

        clear() { this.storage.write('log', [], true); }

        display() {
            let log = this.storage.read('log', true);
            if(!log) {
                console.log('LateLog - Nothing to show')
                return;
            }

            log.forEach(x => {
                this._formatPrint(x);
            });
        }

        log(msg) { this._write(msg, 1); }

        info(msg) { this._write(msg, 2); }

        debug(msg) { this._write(msg, 3); }

        warn(msg) { this._write(msg, 4); }

        error(msg) { this._write(msg, 5); }
    }
    return LateLog;
})();