ExplicitMessage_Inject

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

目前為 2021-07-18 提交的版本,檢視 最新版本

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.greasyfork.org/scripts/429525/951605/ExplicitMessage_Inject.js

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

/**
 * ExplicitLog_Inject
 * @file [DEBUG] 显式日志(注入版)
 * @version 1.1.2.20210718
 * @author Laster2800
 */

(function() {
  (function() {
    try {
      const df = { include: '.*', exclude: '^LOG$' }
      const gmInclude = GM_getValue('ExplicitLog_Inject-include') ?? df.include
      const gmExclude = GM_getValue('ExplicitLog_Inject-exclude') ?? df.exclude
      let include = gmInclude ? new RegExp(gmInclude) : null
      let exclude = gmExclude ? new RegExp(gmExclude) : null
      // 日志
      const logs = ['log', 'warn', 'error']
      for (const log of logs) {
        const _ = console[log]
        console[log] = function() {
          const m = [arguments, log.toUpperCase()]
          if (match(m, include) && !match(m, exclude)) {
            explicit(arguments.length == 1 ? arguments[0] : JSON.stringify(arguments), log.toUpperCase())
          }
          return _.apply(console, arguments)
        }
      }
      // 菜单
      GM_registerMenuCommand('[DEBUG] 设置过滤器', () => {
        try {
          const sInclude = prompt(`【${GM_info.script.name}】\n\n设置匹配过滤器`, include?.source ?? df.include)
          if (typeof sInclude == 'string') {
            include = sInclude ? new RegExp(sInclude) : null
            GM_setValue('ExplicitLog_Inject-include', sInclude)
          }
          const sExclude = prompt(`【${GM_info.script.name}】\n\n设置排除过滤器`, exclude?.source ?? df.exclude)
          if (typeof sExclude == 'string') {
            exclude = sExclude ? new RegExp(sExclude) : null
            GM_setValue('ExplicitLog_Inject-exclude', sExclude)
          }
        } catch (e) {
          explicit(e)
        }
      })
      GM_registerMenuCommand('[DEBUG] 使用说明', () => window.open('https://gitee.com/liangjiancang/userscript/tree/master/script/ExplicitLog#使用说明'))
    } catch (e) {
      explicit(e)
    }
  })()

  /**
   * 显式地显示信息
   * @param {*} msg 信息
   * @param {string} [label] 标记
   */
  function explicit(msg, label) {
    alert(`【${GM_info.script.name}】${label ? `【${label}】` : ''}\n\n${msg}`)
  }

  /**
   * @param {*} obj 匹配对象
   * @param {RegExp} regex 匹配正则表达式
   * @param {number} [depth=5] 匹配查找深度
   * @returns {boolean} 是否匹配成功
   */
  function match(obj, regex, depth = 5) {
    if (obj && regex && depth > 0) {
      return core(obj, depth, new Set())
    } else {
      return false
    }

    function core(obj, depth, objSet) {
      if (!obj) return false
      for (const key in obj) {
        if (regex.test(key)) {
          return true
        } else {
          try {
            const value = obj[key]
            if (value !== undefined && value !== null) {
              if (typeof value == 'object' || typeof value == 'function') {
                if (regex.test(value.toString())) {
                  return true
                } else if (depth > 1) {
                  if (!objSet.has(value)) {
                    objSet.add(value)
                    if (core(value, depth - 1)) {
                      return true
                    }
                  }
                }
              } else if (regex.test(String(value))) {
                return true
              }
            }
          } catch (e) {
            // value that cannot be accessed
          }
        }
      }
      return false
    }
  }
})()