Download Button for GreasyFork scripts

Adds a button to download the script

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