UtilInspectForBrowser

This is NodeJS's util.inspect ported to the browser for usability in other scripts.

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

  1. // Minified code is scary, so here is the full code for the `inspect` function.
  2. // Please note: Some function names etc. may seem odd, but the file is made realitivly small because of this. This module comes straight from NodeJS and was *compiled* for the browser.
  3. // This means I did not write this code myself (A compiler did!), it was simply compiled to work for the browser.
  4.  
  5. window.inspect = (() => {
  6. function e(e, o) {
  7. var c = {
  8. seen: [],
  9. stylize: t
  10. };
  11. return arguments.length >= 3 && (c.depth = arguments[2]), arguments.length >= 4 && (c.colors = arguments[3]), n(o) ? c.showHidden = o : o && b(c, o), r(c.showHidden) && (c.showHidden = !1), r(c.depth) && (c.depth = 2), r(c.colors) && (c.colors = !1), r(c.customInspect) && (c.customInspect = !0), c.colors && (c.stylize = i), d(c, e, c.depth)
  12. }
  13.  
  14. function t(e, t) {
  15. return e
  16. }
  17.  
  18. function n(e) {
  19. return "boolean" == typeof e
  20. }
  21.  
  22. function r(e) {
  23. return void 0 === e
  24. }
  25.  
  26. function i(t, n) {
  27. var r = e.styles[n];
  28. return r ? "[" + e.colors[r][0] + "m" + t + "[" + e.colors[r][1] + "m" : t
  29. }
  30.  
  31. function o(e) {
  32. return "function" == typeof e
  33. }
  34.  
  35. function c(e) {
  36. return "string" == typeof e
  37. }
  38.  
  39. function u(e) {
  40. return null === e
  41. }
  42.  
  43. function l(e, t) {
  44. return Object.prototype.hasOwnProperty.call(e, t)
  45. }
  46.  
  47. function s(e) {
  48. return a(e) && "[object RegExp]" === y(e)
  49. }
  50.  
  51. function a(e) {
  52. return "object" == typeof e && null !== e
  53. }
  54.  
  55. function p(e) {
  56. return a(e) && ("[object Error]" === y(e) || e instanceof Error)
  57. }
  58.  
  59. function f(e) {
  60. return a(e) && "[object Date]" === y(e)
  61. }
  62.  
  63. function y(e) {
  64. return Object.prototype.toString.call(e)
  65. }
  66.  
  67. function g(e) {
  68. return "[" + Error.prototype.toString.call(e) + "]"
  69. }
  70.  
  71. function d(t, i, a) {
  72. if (t.customInspect && i && o(i.inspect) && i.inspect !== e && (!i.constructor || i.constructor.prototype !== i)) {
  73. var y = i.inspect(a, t);
  74. return c(y) || (y = d(t, y, a)), y
  75. }
  76. var b = function(e, t) {
  77. if (r(t)) return e.stylize("undefined", "undefined");
  78. if (c(t)) {
  79. var i = "'" + JSON.stringify(t).replace(/^"|"$/g, "").replace(/'/g, "\\'").replace(/\\"/g, '"') + "'";
  80. return e.stylize(i, "string")
  81. }
  82. if (o = t, "number" == typeof o) return e.stylize("" + t, "number");
  83. var o;
  84. if (n(t)) return e.stylize("" + t, "boolean");
  85. if (u(t)) return e.stylize("null", "null")
  86. }(t, i);
  87. if (b) return b;
  88. var m = Object.keys(i),
  89. v = function(e) {
  90. var t = {};
  91. return e.forEach((function(e, n) {
  92. t[e] = !0
  93. })), t
  94. }(m);
  95. try {
  96. t.showHidden && Object.getOwnPropertyNames && (m = Object.getOwnPropertyNames(i))
  97. } catch (e) {}
  98. if (p(i) && (m.indexOf("message") >= 0 || m.indexOf("description") >= 0)) return g(i);
  99. if (0 === m.length) {
  100. if (o(i)) {
  101. var O = i.name ? ": " + i.name : "";
  102. return t.stylize("[Function" + O + "]", "special")
  103. }
  104. if (s(i)) return t.stylize(RegExp.prototype.toString.call(i), "regexp");
  105. if (f(i)) return t.stylize(Date.prototype.toString.call(i), "date");
  106. if (p(i)) return g(i)
  107. }
  108. var z, j = "",
  109. w = !1,
  110. S = ["{", "}"];
  111. (Array.isArray(i) && (w = !0, S = ["[", "]"]), o(i)) && (j = " [Function" + (i.name ? ": " + i.name : "") + "]");
  112. return s(i) && (j = " " + RegExp.prototype.toString.call(i)), f(i) && (j = " " + Date.prototype.toUTCString.call(i)), p(i) && (j = " " + g(i)), 0 !== m.length || w && 0 != i.length ? a < 0 ? s(i) ? t.stylize(RegExp.prototype.toString.call(i), "regexp") : t.stylize("[Object]", "special") : (t.seen.push(i), z = w ? function(e, t, n, r, i) {
  113. for (var o = [], c = 0, u = t.length; c < u; ++c) l(t, String(c)) ? o.push(h(e, t, n, r, String(c), !0)) : o.push("");
  114. return i.forEach((function(i) {
  115. i.match(/^\d+$/) || o.push(h(e, t, n, r, i, !0))
  116. })), o
  117. }(t, i, a, v, m) : m.map((function(e) {
  118. return h(t, i, a, v, e, w)
  119. })), t.seen.pop(), function(e, t, n) {
  120. if (e.reduce((function(e, t) {
  121. return t.indexOf("\n") >= 0 && 0, e + t.replace(/\u001b\[\d\d?m/g, "").length + 1
  122. }), 0) > 60) return n[0] + ("" === t ? "" : t + "\n ") + " " + e.join(",\n ") + " " + n[1];
  123. return n[0] + t + " " + e.join(", ") + " " + n[1]
  124. }(z, j, S)) : S[0] + j + S[1]
  125. }
  126.  
  127. function h(e, t, n, i, o, c) {
  128. var s, a, p;
  129. p = {
  130. value: void 0
  131. };
  132. try {
  133. p.value = t[o]
  134. } catch (e) {}
  135. try {
  136. Object.getOwnPropertyDescriptor && (p = Object.getOwnPropertyDescriptor(t, o) || p)
  137. } catch (e) {}
  138. if (p.get ? a = p.set ? e.stylize("[Getter/Setter]", "special") : e.stylize("[Getter]", "special") : p.set && (a = e.stylize("[Setter]", "special")), l(i, o) || (s = "[" + o + "]"), a || (e.seen.indexOf(p.value) < 0 ? (a = u(n) ? d(e, p.value, null) : d(e, p.value, n - 1)).indexOf("\n") > -1 && (a = c ? a.split("\n").map((function(e) {
  139. return " " + e
  140. })).join("\n").substr(2) : "\n" + a.split("\n").map((function(e) {
  141. return " " + e
  142. })).join("\n")) : a = e.stylize("[Circular]", "special")), r(s)) {
  143. if (c && o.match(/^\d+$/)) return a;
  144. (s = JSON.stringify("" + o)).match(/^"([a-zA-Z_][a-zA-Z_0-9]*)"$/) ? (s = s.substr(1, s.length - 2), s = e.stylize(s, "name")) : (s = s.replace(/'/g, "\\'").replace(/\\"/g, '"').replace(/(^"|"$)/g, "'"), s = e.stylize(s, "string"))
  145. }
  146. return s + ": " + a
  147. }
  148.  
  149. function b(e, t) {
  150. if (!t || !a(t)) return e;
  151. for (var n = Object.keys(t), r = n.length; r--;) e[n[r]] = t[n[r]];
  152. return e
  153. }
  154. return e.colors = {
  155. bold: [1, 22],
  156. italic: [3, 23],
  157. underline: [4, 24],
  158. inverse: [7, 27],
  159. white: [37, 39],
  160. grey: [90, 39],
  161. black: [30, 39],
  162. blue: [34, 39],
  163. cyan: [36, 39],
  164. green: [32, 39],
  165. magenta: [35, 39],
  166. red: [31, 39],
  167. yellow: [33, 39]
  168. }, e.styles = {
  169. special: "cyan",
  170. number: "yellow",
  171. boolean: "yellow",
  172. undefined: "grey",
  173. null: "bold",
  174. string: "green",
  175. date: "magenta",
  176. regexp: "red"
  177. }, e
  178. })();

QingJ © 2025

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