Jump to gitsummarize from Github

Add a button to jump to gitsummarize from Github

  1. // ==UserScript==
  2. // @name Jump to gitsummarize from Github
  3. // @name:zh-CN Github 跳转至 gitsummarize
  4. // @namespace http://tampermonkey.net/
  5. // @version 0.1.1
  6. // @description Add a button to jump to gitsummarize from Github
  7. // @description:zh-CN 在 Github 页面添加一个按钮,跳转至 gitsummarize
  8. // @author shiquda
  9. // @namespace https://github.com/shiquda/shiquda_UserScript
  10. // @supportURL https://github.com/shiquda/shiquda_UserScript/issues
  11. // @match *://github.com/*
  12. // @include *://*github*/
  13. // @grant GM_addStyle
  14. // @grant GM_setValue
  15. // @grant GM_getValue
  16. // @license MIT
  17. // ==/UserScript==
  18.  
  19. // 判断当前path是否是一个 github repo,且位于项目的主页面
  20. function isGithubRepo(path) {
  21. return path.split('/').length === 3;
  22. }
  23.  
  24. function CreateUI() {
  25. const path = window.location.pathname;
  26. const gitsummarizeUrl = `https://gitsummarize.com${path}`;
  27.  
  28. const button = document.createElement('button');
  29. button.classList.add('Box-sc-g0xbh4-0', 'exSala', 'prc-Button-ButtonBase-c50BI');
  30. button.setAttribute('type', 'button');
  31. button.setAttribute('data-size', 'small');
  32. button.setAttribute('data-variant', 'default');
  33.  
  34. const buttonContent = document.createElement('span');
  35. buttonContent.classList.add('Box-sc-g0xbh4-0', 'gUkoLg', 'prc-Button-ButtonContent-HKbr-');
  36.  
  37. const leadingVisual = document.createElement('span');
  38. leadingVisual.classList.add('prc-Button-Visual-2epfX', 'prc-Button-VisualWrap-Db-eB');
  39.  
  40. const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
  41. svg.setAttribute('aria-hidden', 'true');
  42. svg.setAttribute('focusable', 'false');
  43. svg.setAttribute('class', 'octicon octicon-eye');
  44. svg.setAttribute('viewBox', '0 0 16 16');
  45. svg.setAttribute('width', '16');
  46. svg.setAttribute('height', '16');
  47. svg.setAttribute('fill', 'currentColor');
  48. svg.style.verticalAlign = 'text-bottom';
  49.  
  50. const svgPath = document.createElementNS('http://www.w3.org/2000/svg', 'path');
  51. svgPath.setAttribute('d', 'M8 2c1.981 0 3.671.992 4.933 2.078 1.27 1.091 2.187 2.345 2.637 3.023a1.62 1.62 0 0 1 0 1.798c-.45.678-1.367 1.932-2.637 3.023C11.67 13.008 9.981 14 8 14c-1.981 0-3.671-.992-4.933-2.078C1.797 10.83.88 9.576.43 8.898a1.62 1.62 0 0 1 0-1.798c.45-.677 1.367-1.931 2.637-3.022C4.33 2.992 6.019 2 8 2ZM1.679 7.932a.12.12 0 0 0 0 .136c.411.622 1.241 1.75 2.366 2.717C5.176 11.758 6.527 12.5 8 12.5c1.473 0 2.825-.742 3.955-1.715 1.124-.967 1.954-2.096 2.366-2.717a.12.12 0 0 0 0-.136c-.412-.621-1.242-1.75-2.366-2.717C10.824 4.242 9.473 3.5 8 3.5c-1.473 0-2.825.742-3.955 1.715-1.124.967-1.954 2.096-2.366 2.717ZM8 10a2 2 0 1 1-.001-3.999A2 2 0 0 1 8 10Z');
  52.  
  53. svg.appendChild(svgPath);
  54. leadingVisual.appendChild(svg);
  55.  
  56. const text = document.createElement('span');
  57. text.classList.add('prc-Button-Label-pTQ3x');
  58. text.textContent = 'GitSummarize';
  59.  
  60. buttonContent.appendChild(leadingVisual);
  61. buttonContent.appendChild(text);
  62. button.appendChild(buttonContent);
  63.  
  64. button.addEventListener('click', () => {
  65. window.open(gitsummarizeUrl, '_blank');
  66. });
  67.  
  68. const li = document.createElement('li');
  69. li.appendChild(button);
  70. li.addEventListener('click', () => {
  71. window.open(gitsummarizeUrl, '_blank');
  72. });
  73. document.querySelector('ul.pagehead-actions').insertBefore(li, document.querySelector('ul.pagehead-actions').firstChild);
  74.  
  75. }
  76.  
  77. (function () {
  78. "use strict";
  79.  
  80. // 获取当前路径
  81. const path = window.location.pathname;
  82.  
  83. // 判断当前path是否是一个 github repo,且位于项目的主页面
  84. if (isGithubRepo(path)) {
  85. CreateUI();
  86. }
  87. })();