🏠 返回首頁 

Greasy Fork is available in English.

GitHub Repo File Downloader

Allows you to download individual files directly from GitHub repository pages.


安装此脚本?
  1. // ==UserScript==
  2. // @name GitHub Repo File Downloader
  3. // @description Allows you to download individual files directly from GitHub repository pages.
  4. // @icon https://github.githubassets.com/favicons/favicon-dark.svg
  5. // @version 1.1
  6. // @author afkarxyz
  7. // @namespace https://github.com/afkarxyz/misc-scripts/
  8. // @supportURL https://github.com/afkarxyz/misc-scripts/issues
  9. // @license MIT
  10. // @match https://github.com/*
  11. // @grant none
  12. // ==/UserScript==
  13. (function() {
  14. 'use strict';
  15. const style = document.createElement('style');
  16. style.textContent = `
  17. .github-icon-replacer-hover {
  18. cursor: pointer;
  19. transition: transform 0.1s ease;
  20. }
  21. .github-icon-replacer-hover:hover {
  22. transform: scale(1.1);
  23. }
  24. `;
  25. document.head.appendChild(style);
  26. async function downloadFile(url, fileName) {
  27. try {
  28. const response = await fetch(url);
  29. const blob = await response.blob();
  30. const link = document.createElement('a');
  31. link.href = window.URL.createObjectURL(blob);
  32. link.download = fileName;
  33. link.style.display = 'none';
  34. document.body.appendChild(link);
  35. link.click();
  36. document.body.removeChild(link);
  37. } catch (error) {
  38. console.error('Download failed:', error);
  39. }
  40. }
  41. function replaceIcons() {
  42. const directoryRows = document.querySelectorAll('tr.react-directory-row');
  43. directoryRows.forEach(row => {
  44. const svgIcons = row.querySelectorAll('.react-directory-filename-column svg.color-fg-muted');
  45. svgIcons.forEach(svg => {
  46. if (!svg.dataset.replaced) {
  47. const wrapper = document.createElement('div');
  48. wrapper.style.display = 'inline-block';
  49. svg.innerHTML = `
  50. <g>
  51. <path d="M14.5,3.4l-2.9-2.9C11.2,0.2,10.8,0,10.3,0H3.8C2.8,0,2,0.8,2,1.8v12.5c0,1,0.8,1.8,1.8,1.8h9.5c1,0,1.8-0.8,1.8-1.8V4.7
  52. C15,4.2,14.8,3.8,14.5,3.4z M10.5,1.6L10.5,1.6l2.9,2.9l0,0h-2.7c-0.1,0-0.2-0.1-0.2-0.2V1.6z M13.5,14.2c0,0.1-0.1,0.2-0.2,0.2
  53. H3.8c-0.1,0-0.2-0.1-0.2-0.2V1.8c0-0.1,0.1-0.2,0.2-0.2H9v2.8C9,5.2,9.8,6,10.8,6h2.8V14.2z"/>
  54. <path d="M9.1,10.6V7.3c0-0.3-0.3-0.6-0.6-0.6S7.9,7,7.9,7.3v3.3L6.5,9.3C6.3,9,5.9,9,5.7,9.3c-0.2,0.2-0.2,0.6,0,0.8l2.4,2.4
  55. c0.2,0.2,0.6,0.2,0.8,0h0l2.4-2.4c0.2-0.2,0.2-0.6,0-0.8c-0.2-0.2-0.6-0.2-0.8,0L9.1,10.6z"/>
  56. </g>
  57. `;
  58. svg.setAttribute('viewBox', '0 0 16 16');
  59. svg.classList.add('github-icon-replacer-hover');
  60. svg.dataset.replaced = 'true';
  61. const fileLink = row.querySelector('a[href]');
  62. if (fileLink) {
  63. const downloadUrl = fileLink.href
  64. .replace('github.com', 'raw.githubusercontent.com')
  65. .replace('/blob/', '/');
  66. const fileName = fileLink.textContent.trim();
  67. svg.addEventListener('click', (e) => {
  68. e.preventDefault();
  69. e.stopPropagation();
  70. downloadFile(downloadUrl, fileName);
  71. });
  72. }
  73. }
  74. });
  75. });
  76. }
  77. const observer = new MutationObserver((mutations) => {
  78. for (let mutation of mutations) {
  79. if (mutation.type === 'childList') {
  80. replaceIcons();
  81. }
  82. }
  83. });
  84. observer.observe(document.body, { childList: true, subtree: true });
  85. replaceIcons();
  86. })();