npmjs-explore

npmjs-explore/npmjs文件查看

  1. // ==UserScript==
  2. // @name npmjs-explore
  3. // @name:zh npmjs文件查看
  4. // @namespace https://github.com/lisonge
  5. // @version 1.0.3
  6. // @author lisonge
  7. // @description npmjs-explore/npmjs文件查看
  8. // @license ISC
  9. // @icon https://i.songe.li/favicon.svg
  10. // @homepage https://github.com/lisonge/npmjs-explore#readme
  11. // @homepageURL https://github.com/lisonge/npmjs-explore#readme
  12. // @source https://github.com/lisonge/npmjs-explore.git
  13. // @supportURL https://github.com/lisonge/npmjs-explore/issues
  14. // @include ^https:\/\/www.npmjs.com\/package\/.*
  15. // @match https://www.npmjs.com/package/*
  16. // @require https://cdn.jsdelivr.net/npm/preact@10.10.0
  17. // ==/UserScript==
  18.  
  19. // use vite-plugin-monkey@1.1.3 at 2022-07-22T07:45:07.318Z
  20.  
  21. (function(preact2) {
  22. "use strict";
  23. var t, u, r, o$1, i = 0, c = [], f = [], e$1 = preact2.options.__b, a = preact2.options.__r, v = preact2.options.diffed, l = preact2.options.__c, m = preact2.options.unmount;
  24. function p(t2, r2) {
  25. preact2.options.__h && preact2.options.__h(u, t2, i || r2), i = 0;
  26. var o2 = u.__H || (u.__H = { __: [], __h: [] });
  27. return t2 >= o2.__.length && o2.__.push({ __V: f }), o2.__[t2];
  28. }
  29. function y(n) {
  30. return i = 1, d(z, n);
  31. }
  32. function d(n, r2, o2) {
  33. var i2 = p(t++, 2);
  34. return i2.t = n, i2.__c || (i2.__ = [o2 ? o2(r2) : z(void 0, r2), function(n2) {
  35. var t2 = i2.t(i2.__[0], n2);
  36. i2.__[0] !== t2 && (i2.__ = [t2, i2.__[1]], i2.__c.setState({}));
  37. }], i2.__c = u), i2.__;
  38. }
  39. function _(r2, o2) {
  40. var i2 = p(t++, 3);
  41. !preact2.options.__s && w(i2.__H, o2) && (i2.__ = r2, i2.u = o2, u.__H.__h.push(i2));
  42. }
  43. function b() {
  44. for (var t2; t2 = c.shift(); )
  45. if (t2.__P)
  46. try {
  47. t2.__H.__h.forEach(j), t2.__H.__h.forEach(k), t2.__H.__h = [];
  48. } catch (u2) {
  49. t2.__H.__h = [], preact2.options.__e(u2, t2.__v);
  50. }
  51. }
  52. preact2.options.__b = function(n) {
  53. u = null, e$1 && e$1(n);
  54. }, preact2.options.__r = function(n) {
  55. a && a(n), t = 0;
  56. var o2 = (u = n.__c).__H;
  57. o2 && (r === u ? (o2.__h = [], u.__h = [], o2.__.forEach(function(n2) {
  58. n2.__V = f, n2.u = void 0;
  59. })) : (o2.__h.forEach(j), o2.__h.forEach(k), o2.__h = [])), r = u;
  60. }, preact2.options.diffed = function(t2) {
  61. v && v(t2);
  62. var i2 = t2.__c;
  63. i2 && i2.__H && (i2.__H.__h.length && (1 !== c.push(i2) && o$1 === preact2.options.requestAnimationFrame || ((o$1 = preact2.options.requestAnimationFrame) || function(n) {
  64. var t3, u2 = function() {
  65. clearTimeout(r2), g && cancelAnimationFrame(t3), setTimeout(n);
  66. }, r2 = setTimeout(u2, 100);
  67. g && (t3 = requestAnimationFrame(u2));
  68. })(b)), i2.__H.__.forEach(function(n) {
  69. n.u && (n.__H = n.u), n.__V !== f && (n.__ = n.__V), n.u = void 0, n.__V = f;
  70. })), r = u = null;
  71. }, preact2.options.__c = function(t2, u2) {
  72. u2.some(function(t3) {
  73. try {
  74. t3.__h.forEach(j), t3.__h = t3.__h.filter(function(n) {
  75. return !n.__ || k(n);
  76. });
  77. } catch (r2) {
  78. u2.some(function(n) {
  79. n.__h && (n.__h = []);
  80. }), u2 = [], preact2.options.__e(r2, t3.__v);
  81. }
  82. }), l && l(t2, u2);
  83. }, preact2.options.unmount = function(t2) {
  84. m && m(t2);
  85. var u2, r2 = t2.__c;
  86. r2 && r2.__H && (r2.__H.__.forEach(function(n) {
  87. try {
  88. j(n);
  89. } catch (n2) {
  90. u2 = n2;
  91. }
  92. }), u2 && preact2.options.__e(u2, r2.__v));
  93. };
  94. var g = "function" == typeof requestAnimationFrame;
  95. function j(n) {
  96. var t2 = u, r2 = n.__c;
  97. "function" == typeof r2 && (n.__c = void 0, r2()), u = t2;
  98. }
  99. function k(n) {
  100. var t2 = u;
  101. n.__c = n.__(), u = t2;
  102. }
  103. function w(n, t2) {
  104. return !n || n.length !== t2.length || t2.some(function(t3, u2) {
  105. return t3 !== n[u2];
  106. });
  107. }
  108. function z(n, t2) {
  109. return "function" == typeof t2 ? t2(n) : t2;
  110. }
  111. const getPackageMeta = () => {
  112. var _a, _b;
  113. const list = location.pathname.split("/");
  114. let name = list[2];
  115. let version = (_a = list[4]) != null ? _a : "latest";
  116. if (name.startsWith("@")) {
  117. name = list[2] + "/" + list[3];
  118. version = (_b = list[5]) != null ? _b : "latest";
  119. }
  120. return {
  121. name,
  122. version
  123. };
  124. };
  125. var o = 0;
  126. function e(_2, e2, n, t2, f2) {
  127. var l2, s, u2 = {};
  128. for (s in e2)
  129. "ref" == s ? l2 = e2[s] : u2[s] = e2[s];
  130. var a2 = { type: _2, props: u2, key: n, ref: l2, __k: null, __: null, __b: 0, __e: null, __d: void 0, __c: null, __h: null, constructor: void 0, __v: --o, __source: f2, __self: t2 };
  131. if ("function" == typeof _2 && (l2 = _2.defaultProps))
  132. for (s in l2)
  133. void 0 === u2[s] && (u2[s] = l2[s]);
  134. return preact2.options.vnode && preact2.options.vnode(a2), a2;
  135. }
  136. function App() {
  137. const [jsdelivrUrl, setJsdelivrUrl] = y("");
  138. const [unpkgUrl, setUnpkgUrl] = y("");
  139. _(() => {
  140. const invoke = () => {
  141. const {
  142. name,
  143. version
  144. } = getPackageMeta();
  145. setJsdelivrUrl(`https://cdn.jsdelivr.net/npm/${name}@${version}/`);
  146. setUnpkgUrl(`https://unpkg.com/browse/${name}@${version}/`);
  147. };
  148. invoke();
  149. const task = setInterval(invoke, 1e3);
  150. return () => {
  151. clearInterval(task);
  152. };
  153. }, []);
  154. return e(preact2.Fragment, {
  155. children: e("div", {
  156. style: {
  157. marginLeft: "20px"
  158. },
  159. children: [e("a", {
  160. href: unpkgUrl,
  161. title: unpkgUrl,
  162. target: "_blank",
  163. style: {
  164. color: "#bb2e3e",
  165. textDecoration: "none"
  166. },
  167. children: "unpkg"
  168. }), e("span", {
  169. style: {
  170. display: "inline-block",
  171. width: "10px"
  172. }
  173. }), e("a", {
  174. href: jsdelivrUrl,
  175. title: jsdelivrUrl,
  176. target: "_blank",
  177. style: {
  178. color: "#bb2e3e",
  179. textDecoration: "none"
  180. },
  181. children: "jsdelivr"
  182. })]
  183. })
  184. });
  185. }
  186. (() => {
  187. var _a;
  188. const {
  189. name
  190. } = getPackageMeta();
  191. const box = (_a = document.querySelector(`span[title="${name}"]`)) == null ? void 0 : _a.parentElement;
  192. if (!box) {
  193. console.warn("npmjs-explore inject failed");
  194. return;
  195. }
  196. const container = document.createElement("div");
  197. box.appendChild(container);
  198. preact2.render(e(App, {}), container);
  199. })();
  200. })(preact);