Github anchor enhance

Enhance all github link with badges

当前为 2019-09-25 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Github anchor enhance
  3. // @version 13
  4. // @grant GM.xmlHttpRequest
  5. // @run-at document-idle
  6. // @include *
  7. // @namespace https://gf.qytechs.cn/users/17676
  8. // @description Enhance all github link with badges
  9. // ==/UserScript==
  10. const reservedUsername = [
  11. "topics",
  12. "search",
  13. "ghost",
  14. "pulls",
  15. "issues",
  16. "marketplace",
  17. "explore",
  18. "discover",
  19. "notifications",
  20. "new",
  21. "organizations",
  22. "settings",
  23. "site",
  24. "about",
  25. "contact",
  26. "pricing",
  27. "apps",
  28. "features",
  29. "password_reset",
  30. ];
  31. const allBadgeClasses = [
  32. "added-stars-badge",
  33. "added-last-commit-badge",
  34. "added-followers-badge",
  35. ];
  36. async function appendBadge(el, className, url) {
  37. if (el.classList.contains(className)) {
  38. return;
  39. }
  40. return new Promise((resolve, reject) => {
  41. GM.xmlHttpRequest({
  42. method: "GET",
  43. url: url,
  44. onload: resp => {
  45. if (resp.status === 200) {
  46. if (!el.classList.contains(className)) {
  47. const img = document.createElement("img");
  48. img.src = `data:image/svg+xml;base64,${btoa(resp.response)}`;
  49. const containerClassNames = [
  50. "natescarlet-gmail-com",
  51. "badge-container",
  52. ];
  53. const selector = containerClassNames.map(i => "." + i).join("");
  54. /** @type {HTMLElement} */
  55. const container = el.querySelector(selector) || document.createElement("span");
  56. el.appendChild(container);
  57. container.classList.add(...containerClassNames);
  58. container.append(img);
  59. img.style.order = allBadgeClasses.indexOf(className).toString();
  60. container.style.display = "inline-flex";
  61. el.classList.add(className);
  62. }
  63. resolve();
  64. }
  65. reject(`${resp.status}: ${url}`);
  66. },
  67. onerror: reject,
  68. });
  69. });
  70. }
  71. async function appendStarsBadge(el) {
  72. const match = el.href && el.href.match(/https:\/\/github.com\/([^/]+)\/([^/?]+)$/);
  73. if (match) {
  74. const [, user, repository] = match;
  75. if (reservedUsername.includes(user)) {
  76. return;
  77. }
  78. await appendBadge(el, "added-stars-badge", `https://img.shields.io/github/stars/${user}/${repository}.svg?style=social`);
  79. }
  80. }
  81. async function appendLastCommitBadge(el) {
  82. const match = el.href && el.href.match(/https:\/\/github.com\/([^/]+)\/([^/?]+)$/);
  83. if (match) {
  84. const [, user, repository] = match;
  85. if (reservedUsername.includes(user)) {
  86. return;
  87. }
  88. await appendBadge(el, "added-last-commit-badge", `https://img.shields.io/github/last-commit/${user}/${repository}.svg`);
  89. }
  90. }
  91. async function appendFollowersBadge(el) {
  92. const match = el.href && el.href.match(/https:\/\/github.com\/([^/?]+)$/);
  93. if (match) {
  94. const [, user] = match;
  95. if (reservedUsername.includes(user)) {
  96. return;
  97. }
  98. await appendBadge(el, "added-followers-badge", `https://img.shields.io/github/followers/${user}.svg?style=social`);
  99. }
  100. }
  101. (async function () {
  102. document.addEventListener("mouseover", async (e) => {
  103. if (e.target instanceof HTMLAnchorElement) {
  104. const el = e.target;
  105. const u = new URL(el.href);
  106. if (location.hostname === u.hostname &&
  107. location.pathname === u.pathname) {
  108. // Skip self link
  109. return;
  110. }
  111. try {
  112. await Promise.all([
  113. appendStarsBadge(el),
  114. appendLastCommitBadge(el),
  115. appendFollowersBadge(el),
  116. ]);
  117. }
  118. catch (err) {
  119. console.error(err);
  120. }
  121. }
  122. }, {});
  123. })();

QingJ © 2025

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