ExplicitMessage_Inject

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

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

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

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

QingJ © 2025

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