Greasy Fork 还支持 简体中文。

Add Raw link To GitHub Files

12/13/2023, 1:18:04 AM

  1. // ==UserScript==
  2. // @name Add Raw link To GitHub Files
  3. // @namespace Violentmonkey Scripts
  4. // @match https://github.com/*
  5. // @version 0.0.2
  6. // @description 12/13/2023, 1:18:04 AM
  7. // @grant GM_addStyle
  8. // @license MIT
  9. // ==/UserScript==
  10.  
  11. const cssText = `
  12.  
  13. @keyframes button384 {
  14. from{
  15. background-position-x: 1px;
  16. }
  17. to{
  18. background-position-x: 2px;
  19. }
  20. }
  21. [aria-label="Copy file name to clipboard"]:not([ls4yu]) {
  22. animation: button384 1ms linear 0s 1 normal forwards;
  23. }
  24.  
  25. .button385 {
  26. margin: 4px 2px;
  27. }
  28.  
  29. `;
  30. function f101(btn) {
  31. btn.setAttribute('ls4yu', '');
  32. let p = btn;
  33. let link;
  34. while ((p = p.parentNode) instanceof HTMLElement) {
  35. link = p.querySelector('a[href*="#diff-"]');
  36. if (link) {
  37. break;
  38. }
  39. }
  40. if (!link) return;
  41. p = link.parentElement;
  42. if (!p) return;
  43.  
  44.  
  45. if (p.querySelector('.button385')) return;
  46.  
  47. const code = link.querySelector('code');
  48. if (!code) return;
  49. const text = code.textContent.trim().replace(decodeURIComponent('%E2%80%8E'), '').trim();
  50.  
  51.  
  52. let commitId = '';
  53.  
  54. const m = /\/([\w\-]+)\/([\w\-]+)\/commit\/([0-9a-f]{40})(#|$)/.exec(location.pathname);
  55.  
  56. if (m && m[3]) {
  57. commitId = m[3];
  58. }
  59.  
  60. if (commitId) {
  61.  
  62. const a = document.createElement('a');
  63. a.classList.add('button385');
  64. p.insertBefore(a, link);
  65.  
  66. a.textContent = 'RAW';
  67. a.style.cursor = 'pointer';
  68.  
  69.  
  70.  
  71.  
  72. a.setAttribute('href', `https://github.com/${m[1]}/${m[2]}/raw/${commitId}/${text}`)
  73.  
  74. }
  75.  
  76. }
  77.  
  78.  
  79. document.addEventListener('animationstart', (evt) => {
  80. const animationName = evt.animationName;
  81. if (!animationName) return;
  82. if (animationName === 'button384') {
  83. f101(evt.target);
  84. }
  85. }, { passive: true, capture: true });
  86.  
  87. GM_addStyle(cssText);