ExplicitMessage_Inject

[DEBUG] 信息显式化(注入版)

当前为 2021-07-18 提交的版本,查看 最新版本

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.gf.qytechs.cn/scripts/429525/951605/ExplicitMessage_Inject.js

  1. /**
  2. * ExplicitLog_Inject
  3. * @file [DEBUG] 显式日志(注入版)
  4. * @version 1.1.2.20210718
  5. * @author Laster2800
  6. */
  7.  
  8. (function() {
  9. (function() {
  10. try {
  11. const df = { include: '.*', exclude: '^LOG$' }
  12. const gmInclude = GM_getValue('ExplicitLog_Inject-include') ?? df.include
  13. const gmExclude = GM_getValue('ExplicitLog_Inject-exclude') ?? df.exclude
  14. let include = gmInclude ? new RegExp(gmInclude) : null
  15. let exclude = gmExclude ? new RegExp(gmExclude) : null
  16. // 日志
  17. const logs = ['log', 'warn', 'error']
  18. for (const log of logs) {
  19. const _ = console[log]
  20. console[log] = function() {
  21. const m = [arguments, log.toUpperCase()]
  22. if (match(m, include) && !match(m, exclude)) {
  23. explicit(arguments.length == 1 ? arguments[0] : JSON.stringify(arguments), log.toUpperCase())
  24. }
  25. return _.apply(console, arguments)
  26. }
  27. }
  28. // 菜单
  29. GM_registerMenuCommand('[DEBUG] 设置过滤器', () => {
  30. try {
  31. const sInclude = prompt(`【${GM_info.script.name}】\n\n设置匹配过滤器`, include?.source ?? df.include)
  32. if (typeof sInclude == 'string') {
  33. include = sInclude ? new RegExp(sInclude) : null
  34. GM_setValue('ExplicitLog_Inject-include', sInclude)
  35. }
  36. const sExclude = prompt(`【${GM_info.script.name}】\n\n设置排除过滤器`, exclude?.source ?? df.exclude)
  37. if (typeof sExclude == 'string') {
  38. exclude = sExclude ? new RegExp(sExclude) : null
  39. GM_setValue('ExplicitLog_Inject-exclude', sExclude)
  40. }
  41. } catch (e) {
  42. explicit(e)
  43. }
  44. })
  45. GM_registerMenuCommand('[DEBUG] 使用说明', () => window.open('https://gitee.com/liangjiancang/userscript/tree/master/script/ExplicitLog#使用说明'))
  46. } catch (e) {
  47. explicit(e)
  48. }
  49. })()
  50.  
  51. /**
  52. * 显式地显示信息
  53. * @param {*} msg 信息
  54. * @param {string} [label] 标记
  55. */
  56. function explicit(msg, label) {
  57. alert(`【${GM_info.script.name}】${label ? `【${label}】` : ''}\n\n${msg}`)
  58. }
  59.  
  60. /**
  61. * @param {*} obj 匹配对象
  62. * @param {RegExp} regex 匹配正则表达式
  63. * @param {number} [depth=5] 匹配查找深度
  64. * @returns {boolean} 是否匹配成功
  65. */
  66. function match(obj, regex, depth = 5) {
  67. if (obj && regex && depth > 0) {
  68. return core(obj, depth, new Set())
  69. } else {
  70. return false
  71. }
  72.  
  73. function core(obj, depth, objSet) {
  74. if (!obj) return false
  75. for (const key in obj) {
  76. if (regex.test(key)) {
  77. return true
  78. } else {
  79. try {
  80. const value = obj[key]
  81. if (value !== undefined && value !== null) {
  82. if (typeof value == 'object' || typeof value == 'function') {
  83. if (regex.test(value.toString())) {
  84. return true
  85. } else if (depth > 1) {
  86. if (!objSet.has(value)) {
  87. objSet.add(value)
  88. if (core(value, depth - 1)) {
  89. return true
  90. }
  91. }
  92. }
  93. } else if (regex.test(String(value))) {
  94. return true
  95. }
  96. }
  97. } catch (e) {
  98. // value that cannot be accessed
  99. }
  100. }
  101. }
  102. return false
  103. }
  104. }
  105. })()

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址