Download Button for GreasyFork scripts

Adds a button to download the script

当前为 2024-01-29 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Download Button for GreasyFork scripts
  3. // @version 1.1
  4. // @author Rust1667
  5. // @description Adds a button to download the script
  6. // @match https://gf.qytechs.cn/*/scripts/*
  7. // @match https://sleazyfork.org/*/scripts/*
  8. // @grant none
  9. // @namespace https://gf.qytechs.cn/users/980489
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. // Find the install link element
  16. var installLink = document.querySelector('#install-area .install-link');
  17.  
  18. // Determine the file name
  19. const installURLlink = installLink.getAttribute('href');
  20.  
  21. function getFilenameFromUrl() {
  22. const url = installURLlink;
  23. var lastSlashIndex = url.lastIndexOf('/');
  24. var filenameWithExtension = url.substring(lastSlashIndex + 1);
  25. var decodedFilename = filenameWithExtension.replace(/%20/g, '_');
  26. return decodedFilename;
  27. }
  28.  
  29. // Create a download button
  30. var downloadButton = document.createElement('button');
  31. downloadButton.textContent = 'Download Script';
  32. downloadButton.style.marginLeft = '10px';
  33.  
  34. // Add click event listener to the download button
  35. downloadButton.addEventListener('click', function() {
  36. var scriptUrl = installLink.getAttribute('href');
  37. downloadFile(scriptUrl);
  38. });
  39.  
  40. // Insert the download button after the install link
  41. installLink.parentNode.insertBefore(downloadButton, installLink.nextSibling);
  42.  
  43. function downloadFile(url) {
  44. var xhr = new XMLHttpRequest();
  45. xhr.open('GET', url, true);
  46. xhr.responseType = 'blob';
  47. xhr.onload = function() {
  48. if (xhr.status === 200) {
  49. var blob = xhr.response;
  50. var url = window.URL.createObjectURL(blob);
  51. var a = document.createElement('a');
  52. a.href = url;
  53. a.download = getFilenameFromUrl();
  54. document.body.appendChild(a);
  55. a.click();
  56. window.URL.revokeObjectURL(url);
  57. }
  58. };
  59. xhr.send();
  60. }
  61. })();

QingJ © 2025

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