LateLog - Simple storage-based Log Library

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

当前为 2023-03-02 提交的版本,查看 最新版本

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

  1. // ==UserScript==
  2. // @name LateLog - Simple storage-based Log Library
  3. // @namespace satology.latelog
  4. // @version 0.1
  5. // @description Simple library to log to the storage, granting the possibility of viewing the log after the URL changes
  6. // @author satology
  7. // @grant GM_getValue
  8. // @grant GM_setValue
  9. // @grant GM_registerMenuCommand
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14. 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)}`;
  15.  
  16. class Storage {
  17. constructor(prefix = '', config = {}) {
  18. this.prefix = prefix;
  19. this.config = config;
  20. }
  21.  
  22. write(key, value, parseIt = false) {
  23. GM_setValue(this.prefix + key, parseIt ? JSON.stringify(value) : value);
  24. }
  25.  
  26. read(key, parseIt = false) {
  27. let value = GM_getValue(this.prefix + key);
  28. if(value && parseIt) {
  29. value = JSON.parse(value);
  30. }
  31. return value;
  32. }
  33. }
  34.  
  35. class LateLog {
  36. constructor(options = {}) {
  37. if(!this._isValidSetup(options)) return;
  38. this.storage = new Storage();
  39. this.options = {
  40. level: 5,
  41. maxLines: 200,
  42. addTimestamp: true,
  43. printWhileLogging: false,
  44. // showDisplayButton: true,
  45. ...options
  46. };
  47. this._init();
  48. }
  49.  
  50. _isValidSetup(options) {
  51. if(! (typeof GM_getValue === "function" &&
  52. typeof GM_setValue === "function" &&
  53. typeof GM_registerMenuCommand === "function") ) {
  54. console.warn(
  55. `LateLog cannot be initialized - At least one off the following @grant is missing in your script:
  56. @grant GM_getValue
  57. @grant GM_setValue
  58. @grant GM_registerMenuCommand`);
  59. return false;
  60. }
  61. // TODO: validate options
  62. return true;
  63. }
  64.  
  65. _init() {
  66. if (this.options.showDisplayButton) {
  67. let that = this;
  68. GM_registerMenuCommand('View log', function(evt) {
  69. that.display();
  70. });
  71. }
  72. return true;
  73. }
  74.  
  75. _write(msg, msgLevel = 1) {
  76. if (this.options.level < msgLevel) {
  77. return;
  78. }
  79.  
  80. let log = this.storage.read('log', true);
  81. log = log ? log : [];
  82.  
  83. if (msg === null) {
  84. msg = 'null';
  85. }
  86. if (msg === undefined) {
  87. msg = 'undefined';
  88. }
  89.  
  90. let savable = {
  91. lvl: msgLevel
  92. };
  93. if (this.options.addTimestamp) {
  94. savable.ts = `${latelog_getDateTimeString()}`;
  95. }
  96. if (msg instanceof Error) {
  97. savable.msg = msg.toString();
  98. } else if (typeof(msg) == 'object') {
  99. savable.msg = JSON.stringify(msg);
  100. savable.parse = true;
  101. } else {
  102. savable.msg = msg;
  103. }
  104. log.push(savable);
  105.  
  106. if(log.length > this.options.maxLines) {
  107. log.splice(0, log.length - this.options.maxLines);
  108. }
  109.  
  110. this.storage.write('log', log, true);
  111. this._formatPrint(savable);
  112. }
  113.  
  114. _getPrintFn(level) {
  115. switch(level) {
  116. case 1:
  117. return console.log;
  118. case 2:
  119. return console.info;
  120. case 3:
  121. return console.debug;
  122. case 4:
  123. return console.warn;
  124. case 5:
  125. return console.error;
  126. default:
  127. return console.log;
  128. }
  129. }
  130.  
  131. _formatPrint(x) {
  132. if (x.parse) {
  133. try {
  134. x.msg = JSON.parse(x.msg);
  135. } catch (err) {}
  136. if (this.options.addTimestamp) {
  137. this._getPrintFn(x.lvl)(`${x.ts}:`);
  138. }
  139. this._getPrintFn(x.lvl)(x.msg);
  140. } else {
  141. this._getPrintFn(x.lvl)(`${this.options.addTimestamp ? x.ts + ' ' : ''} ${x.msg}`);
  142. }
  143. }
  144.  
  145. clear() { this.storage.write('log', [], true); }
  146.  
  147. display() {
  148. let log = this.storage.read('log', true);
  149. if(!log) {
  150. console.log('LateLog - Nothing to show')
  151. return;
  152. }
  153.  
  154. log.forEach(x => {
  155. this._formatPrint(x);
  156. });
  157. }
  158.  
  159. log(msg) { this._write(msg, 1); }
  160.  
  161. info(msg) { this._write(msg, 2); }
  162.  
  163. debug(msg) { this._write(msg, 3); }
  164.  
  165. warn(msg) { this._write(msg, 4); }
  166.  
  167. error(msg) { this._write(msg, 5); }
  168. }
  169. })();