@whitesev/utils 2.3.3 → 2.3.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs.js CHANGED
@@ -3116,6 +3116,9 @@ class Log {
3116
3116
  autoClearConsole: false,
3117
3117
  logMaxCount: 999,
3118
3118
  };
3119
+ /**
3120
+ * 颜色配置
3121
+ */
3119
3122
  #msgColorDetails = [
3120
3123
  "font-weight: bold; color: cornflowerblue",
3121
3124
  "font-weight: bold; color: cornflowerblue",
@@ -3123,16 +3126,16 @@ class Log {
3123
3126
  "font-weight: bold; color: cornflowerblue",
3124
3127
  ];
3125
3128
  /**
3126
- * @param _GM_info_ 油猴管理器的API GM_info,或者是一个对象,如{"script":{name:"Utils.Log"}},或者直接是一个字符串
3129
+ * @param __GM_info 油猴管理器的API GM_info,或者是一个对象,如{"script":{name:"Utils.Log"}},或者直接是一个字符串,用作tag名
3127
3130
  * @param console 可指定console对象为unsafeWindow下的console或者是油猴window下的console
3128
3131
  */
3129
- constructor(_GM_info_, console = window.console) {
3130
- if (typeof _GM_info_ === "string") {
3131
- this.tag = _GM_info_;
3132
+ constructor(__GM_info, console = window.console) {
3133
+ if (typeof __GM_info === "string") {
3134
+ this.tag = __GM_info;
3132
3135
  }
3133
- else if (typeof _GM_info_ === "object" &&
3134
- typeof _GM_info_?.script?.name === "string") {
3135
- this.tag = _GM_info_.script.name;
3136
+ else if (typeof __GM_info === "object" &&
3137
+ typeof __GM_info?.script?.name === "string") {
3138
+ this.tag = __GM_info.script.name;
3136
3139
  }
3137
3140
  this.#console = console;
3138
3141
  }
@@ -3207,24 +3210,33 @@ class Log {
3207
3210
  let { name: callerName, position: callerPosition } = this.parseErrorStack(stackSplit);
3208
3211
  let tagName = this.tag;
3209
3212
  let that = this;
3210
- function consoleMsg(_msg_) {
3211
- if (typeof _msg_ === "string") {
3212
- that.#console.log(`%c[${tagName}%c-%c${callerName}%c]%c %s`, ...that.#msgColorDetails, `color: ${color};${otherStyle}`, _msg_);
3213
+ /** tag的html输出格式 */
3214
+ let tagNameHTML = `%c[${tagName}%c`;
3215
+ /** 调用的函数名的html输出格式 */
3216
+ let callerNameHTML = `%c${callerName}%c]%c`;
3217
+ callerName.trim() !== "" && (callerNameHTML = "-" + callerNameHTML);
3218
+ /**
3219
+ * 输出消息到控制台
3220
+ * @param message
3221
+ */
3222
+ function consoleMsg(message) {
3223
+ if (typeof message === "string") {
3224
+ that.#console.log(`${tagNameHTML}${callerNameHTML} %s`, ...that.#msgColorDetails, `color: ${color};${otherStyle}`, message);
3213
3225
  }
3214
- else if (typeof _msg_ === "number") {
3215
- that.#console.log(`%c[${tagName}%c-%c${callerName}%c]%c %d`, ...that.#msgColorDetails, `color: ${color};${otherStyle}`, _msg_);
3226
+ else if (typeof message === "number") {
3227
+ that.#console.log(`${tagNameHTML}${callerNameHTML} %d`, ...that.#msgColorDetails, `color: ${color};${otherStyle}`, message);
3216
3228
  }
3217
- else if (typeof _msg_ === "object") {
3218
- that.#console.log(`%c[${tagName}%c-%c${callerName}%c]%c %o`, ...that.#msgColorDetails, `color: ${color};${otherStyle}`, _msg_);
3229
+ else if (typeof message === "object") {
3230
+ that.#console.log(`${tagNameHTML}${callerNameHTML} %o`, ...that.#msgColorDetails, `color: ${color};${otherStyle}`, message);
3219
3231
  }
3220
3232
  else {
3221
- that.#console.log(_msg_);
3233
+ that.#console.log(message);
3222
3234
  }
3223
3235
  }
3224
3236
  if (Array.isArray(msg)) {
3225
- msg.forEach((item) => {
3226
- consoleMsg(item);
3227
- });
3237
+ for (let index = 0; index < msg.length; index++) {
3238
+ consoleMsg(msg[index]);
3239
+ }
3228
3240
  }
3229
3241
  else {
3230
3242
  consoleMsg(msg);
@@ -3236,67 +3248,67 @@ class Log {
3236
3248
  }
3237
3249
  /**
3238
3250
  * 控制台-普通输出
3239
- * @param msg 需要输出的内容,如果想输出多个,修改成数组,且数组内的长度最大值为4个
3240
- * @param color 输出的颜色
3241
- * @param otherStyle 其它CSS
3251
+ * @param args 需要输出的内容
3252
+ * @example
3253
+ * log.info("输出信息","输出信息2","输出信息3","输出")
3242
3254
  */
3243
- info(msg, color = this.#details.infoColor, otherStyle) {
3255
+ info(...args) {
3244
3256
  if (this.#disable)
3245
3257
  return;
3246
- this.printContent.call(this, msg, color, otherStyle);
3258
+ this.printContent(args, this.#details.infoColor);
3247
3259
  }
3248
3260
  /**
3249
3261
  * 控制台-警告输出
3250
- * @param msg 需要输出的内容,如果想输出多个,修改成数组,且数组内的长度最大值为4个
3251
- * @param color 输出的颜色
3252
- * @param otherStyle 其它CSS
3262
+ * @param args 需要输出的内容
3263
+ * @example
3264
+ * log.warn("输出警告","输出警告2","输出警告3","输出警告4")
3253
3265
  */
3254
- warn(msg, color = this.#details.warnColor, otherStyle = "background: #FEF6D5;padding: 4px 6px 4px 0px;") {
3266
+ warn(...args) {
3255
3267
  if (this.#disable)
3256
3268
  return;
3257
- this.printContent.call(this, msg, color, otherStyle);
3269
+ this.printContent(args, this.#details.warnColor, "background: #FEF6D5;padding: 4px 6px 4px 0px;");
3258
3270
  }
3259
3271
  /**
3260
3272
  * 控制台-错误输出
3261
- * @param msg 需要输出的内容,如果想输出多个,修改成数组,且数组内的长度最大值为4个
3262
- * @param color 输出的颜色
3263
- * @param otherStyle 其它CSS
3273
+ * @param args 需要输出的内容
3274
+ * @example
3275
+ * log.error("输出错误","输出错误2","输出错误3","输出错误4")
3264
3276
  */
3265
- error(msg, color = this.#details.errorColor, otherStyle) {
3277
+ error(...args) {
3266
3278
  if (this.#disable)
3267
3279
  return;
3268
- this.printContent.call(this, msg, color, otherStyle);
3280
+ this.printContent(args, this.#details.errorColor);
3269
3281
  }
3270
3282
  /**
3271
3283
  * 控制台-成功输出
3272
- * @param msg 需要输出的内容,如果想输出多个,修改成数组,且数组内的长度最大值为4个
3273
- * @param color 输出的颜色
3274
- * @param otherStyle 其它CSS
3284
+ * @param args 需要输出的内容
3285
+ * @example
3286
+ * log.success("输出成功")
3275
3287
  */
3276
- success(msg, color = this.#details.successColor, otherStyle) {
3288
+ success(...args) {
3277
3289
  if (this.#disable)
3278
3290
  return;
3279
- this.printContent.call(this, msg, color, otherStyle);
3291
+ this.printContent(args, this.#details.successColor);
3280
3292
  }
3281
3293
  /**
3282
3294
  * 控制台-输出表格
3283
- * @param msg
3284
- * @param color 输出的颜色
3285
- * @param otherStyle 其它CSS
3295
+ * @param msg 需要输出的内容
3286
3296
  * @example
3287
3297
  * log.table([{"名字":"example","值":"123"},{"名字":"example2","值":"345"}])
3288
3298
  */
3289
- table(msg, color = this.#details.infoColor, otherStyle = "") {
3299
+ table(msg) {
3290
3300
  if (this.#disable)
3291
3301
  return;
3292
3302
  this.checkClearConsole();
3293
3303
  let stack = new Error().stack.split("\n");
3294
3304
  stack.splice(0, 1);
3295
3305
  let errorStackParse = this.parseErrorStack(stack);
3306
+ /** 堆栈函数名 */
3296
3307
  let stackFunctionName = errorStackParse.name;
3308
+ /** 堆栈位置 */
3297
3309
  let stackFunctionNamePosition = errorStackParse.position;
3298
3310
  let callerName = stackFunctionName;
3299
- this.#console.log(`%c[${this.tag}%c-%c${callerName}%c]%c`, ...this.#msgColorDetails, `color: ${color};${otherStyle}`);
3311
+ this.#console.log(`%c[${this.tag}%c-%c${callerName}%c]%c`, ...this.#msgColorDetails, `color: ${this.#details.infoColor};`);
3300
3312
  this.#console.table(msg);
3301
3313
  if (this.#details.debug) {
3302
3314
  this.#console.log(stackFunctionNamePosition);