Library: Color Logs

Log enhancer. Extends console.log, console.error, etc. Show console.debug logs by setting window.Debug = true.

目前为 2022-06-14 提交的版本,查看 最新版本

此脚本不应直接安装,它是供其他脚本使用的外部库。如果你需要使用该库,请在脚本元属性加入:// @require https://update.cn-greasyfork.org/scripts/38888/1060678/Library%3A%20Color%20Logs.js

  1. // ==UserScript==
  2. // @name Library: Color Logs
  3. // @namespace de.sidneys.userscripts
  4. // @homepage https://gist.githubusercontent.com/sidneys/5d44a978d18a1b91f554b2358406671d/raw/
  5. // @version 16.0.0
  6. // @description Log enhancer. Extends console.log, console.error, etc. Show console.debug logs by setting window.Debug = true.
  7. // @author sidneys
  8. // @icon https://www.greasespot.net/favicon.ico
  9. // @match http*://*/*
  10. // @grant unsafeWindow
  11. // ==/UserScript==
  12.  
  13.  
  14. /**
  15. * Get Debug State
  16. * @this window
  17. * @returns {Boolean} - Yes/No
  18. */
  19. let getIsDebug = () => !!unsafeWindow.Debug || !!unsafeWindow.DEBUG || !!this.Debug || !!this.DEBUG
  20. getIsDebug = getIsDebug.bind(this)
  21.  
  22. /**
  23. * Get Log Message Prefix
  24. * @returns {String} - Prefix
  25. */
  26. let getLogPrefix = () => GM.info.script.name
  27.  
  28.  
  29. /**
  30. * Original console
  31. * @type {Object}
  32. * @readonly
  33. */
  34. // const originalConsole = window.console
  35.  
  36. /**
  37. * Original log()
  38. * @type {function}
  39. * @readonly
  40. */
  41. const originalLog = unsafeWindow.console.log
  42.  
  43.  
  44. /**
  45. * Extended console logging methods
  46. * @type {Object}
  47. * @borrows window.console.debug as debug
  48. * @borrows window.console.error as error
  49. * @borrows window.console.info as info
  50. * @borrows window.console.log as log
  51. * @borrows window.console.warn as warn
  52. */
  53. const consoleMixin = {
  54. debug: function () {
  55. if (!getIsDebug()) { return }
  56.  
  57. const color = `rgb(255, 150, 70)`
  58.  
  59. originalLog.call(this, `🛠 %c[${getLogPrefix()}] %c${Array.from(arguments).join(' ')}`, `font-weight: 600; color: ${color};`, `font-weight: 400; color: ${color};`)
  60. },
  61. error: function () {
  62. const color = `rgb(220, 0, 30)`
  63.  
  64. originalLog.call(this, `🚨️ %c[${getLogPrefix()}] %c${Array.from(arguments).join(' ')}`, `font-weight: 600; color: ${color};`, `font-weight: 400; color: ${color};`)
  65. },
  66. info: function () {
  67. const color = `rgb(0, 200, 180)`
  68.  
  69. originalLog.call(this, `ℹ️ %c[${getLogPrefix()}] %c${Array.from(arguments).join(' ')}`, `font-weight: 600; color: ${color};`, `font-weight: 400; color: ${color};`)
  70. },
  71. log: function () {
  72. const color = `rgb(70, 70, 70)`
  73.  
  74. originalLog.call(this, `✳️ %c[${getLogPrefix()}] %c${Array.from(arguments).join(' ')}`, `font-weight: 600; color: ${color};`, `font-weight: 400; color: ${color};`)
  75. },
  76. warn: function () {
  77. const color = `rgb(255, 100, 0)`
  78.  
  79. originalLog.call(this, `⚠️ %c[${getLogPrefix()}] %c${Array.from(arguments).join(' ')}`, `font-weight: 600; color: ${color};`, `font-weight: 400; color: ${color};`)
  80. }
  81. }
  82.  
  83.  
  84. /**
  85. * Replace console logging methods
  86. * @mixes window.console
  87. */
  88. Object.assign(unsafeWindow.console, consoleMixin)