Greasyfork Code Copier

Adds a copy button for script code.

目前为 2025-04-07 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Greasyfork Code Copier
  3. // @description Adds a copy button for script code.
  4. // @icon https://greasyfork.org/vite/assets/blacklogo96-CxYTSM_T.png
  5. // @version 1.4
  6. // @author afkarxyz
  7. // @namespace https://github.com/afkarxyz/userscripts/
  8. // @supportURL https://github.com/afkarxyz/userscripts/issues
  9. // @license MIT
  10. // @match https://greasyfork.org/*/scripts/*
  11. // @match https://sleazyfork.org/*/scripts/*
  12. // @grant GM_xmlhttpRequest
  13. // @grant GM_setClipboard
  14. // @run-at document-end
  15. // ==/UserScript==
  16.  
  17. (function() {
  18. 'use strict';
  19. function createSVGIcon(path) {
  20. const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
  21. svg.setAttribute("viewBox", "0 0 384 512");
  22. svg.setAttribute("width", "16");
  23. svg.setAttribute("height", "16");
  24. const iconPath = document.createElementNS("http://www.w3.org/2000/svg", "path");
  25. iconPath.setAttribute("d", path);
  26. iconPath.setAttribute("fill", "currentColor");
  27. svg.appendChild(iconPath);
  28. return svg;
  29. }
  30.  
  31. function createCopyButton(scriptUrl) {
  32. const initialIconPath = 'M145.5 68c5.3-20.7 24.1-36 46.5-36s41.2 15.3 46.5 36c1.8 7.1 8.2 12 15.5 12l18 0c8.8 0 16 7.2 16 16l0 32-96 0-96 0 0-32c0-8.8 7.2-16 16-16l18 0c7.3 0 13.7-4.9 15.5-12zM192 0c-32.8 0-61 19.8-73.3 48L112 48C91.1 48 73.3 61.4 66.7 80L64 80C28.7 80 0 108.7 0 144L0 448c0 35.3 28.7 64 64 64l256 0c35.3 0 64-28.7 64-64l0-304c0-35.3-28.7-64-64-64l-2.7 0c-6.6-18.6-24.4-32-45.3-32l-6.7 0C253 19.8 224.8 0 192 0zM320 112c17.7 0 32 14.3 32 32l0 304c0 17.7-14.3 32-32 32L64 480c-17.7 0-32-14.3-32-32l0-304c0-17.7 14.3-32 32-32l0 16c0 17.7 14.3 32 32 32l96 0 96 0c17.7 0 32-14.3 32-32l0-16zM208 80a16 16 0 1 0 -32 0 16 16 0 1 0 32 0zM136 272a24 24 0 1 0 -48 0 24 24 0 1 0 48 0zm40-16c-8.8 0-16 7.2-16 16s7.2 16 16 16l96 0c8.8 0 16-7.2 16-16s-7.2-16-16-16l-96 0zm0 96c-8.8 0-16 7.2-16 16s7.2 16 16 16l96 0c8.8 0 16-7.2 16-16s-7.2-16-16-16l-96 0zm-64 40a24 24 0 1 0 0-48 24 24 0 1 0 0 48z';
  33. const alternateIconPath = 'M145.5 68c5.3-20.7 24.1-36 46.5-36s41.2 15.3 46.5 36c1.8 7.1 8.2 12 15.5 12l18 0c8.8 0 16 7.2 16 16l0 32-96 0-96 0 0-32c0-8.8 7.2-16 16-16l18 0c7.3 0 13.7-4.9 15.5-12zM192 0c-32.8 0-61 19.8-73.3 48L112 48C91.1 48 73.3 61.4 66.7 80L64 80C28.7 80 0 108.7 0 144L0 448c0 35.3 28.7 64 64 64l256 0c35.3 0 64-28.7 64-64l0-304c0-35.3-28.7-64-64-64l-2.7 0c-6.6-18.6-24.4-32-45.3-32l-6.7 0C253 19.8 224.8 0 192 0zM320 112c17.7 0 32 14.3 32 32l0 304c0 17.7-14.3 32-32 32L64 480c-17.7 0-32-14.3-32-32l0-304c0-17.7 14.3-32 32-32l0 16c0 17.7 14.3 32 32 32l96 0 96 0c17.7 0 32-14.3 32-32l0-16zM208 80a16 16 0 1 0 -32 0 16 16 0 1 0 32 0zm91.3 171.3c6.2-6.2 6.2-16.4 0-22.6s-16.4-6.2-22.6 0L160 345.4l-52.7-52.7c-6.2-6.2-16.4-6.2-22.6 0s-6.2 16.4 0 22.6l64 64c6.2 6.2 16.4 6.2 22.6 0l128-128z';
  34. const copyButton = document.createElement("a");
  35. copyButton.className = "install-help-link";
  36. copyButton.href = "javascript:void(0)";
  37. copyButton.style.marginLeft = "5px";
  38. copyButton.style.borderRadius = "10%";
  39. const icon = createSVGIcon(initialIconPath);
  40. copyButton.appendChild(icon);
  41. copyButton.addEventListener("click", function(event) {
  42. event.preventDefault();
  43. icon.firstChild.setAttribute("d", alternateIconPath);
  44. GM_xmlhttpRequest({
  45. method: "GET",
  46. url: scriptUrl,
  47. onload: function(response) {
  48. if (response.status === 200) {
  49. GM_setClipboard(response.responseText);
  50. setTimeout(() => {
  51. icon.firstChild.setAttribute("d", initialIconPath);
  52. }, 500);
  53. } else {
  54. setTimeout(() => {
  55. icon.firstChild.setAttribute("d", initialIconPath);
  56. }, 500);
  57. }
  58. },
  59. onerror: function() {
  60. setTimeout(() => {
  61. icon.firstChild.setAttribute("d", initialIconPath);
  62. }, 500);
  63. }
  64. });
  65. });
  66. return copyButton;
  67. }
  68.  
  69. function initialize() {
  70. const installButton = document.querySelector(".install-link");
  71. if (installButton) {
  72. const scriptUrl = installButton.href;
  73. const helpLink = document.querySelector(".install-help-link");
  74. if (helpLink) {
  75. const copyButton = createCopyButton(scriptUrl);
  76. helpLink.parentNode.insertBefore(copyButton, helpLink.nextSibling);
  77. }
  78. }
  79. }
  80.  
  81. if (document.readyState === "loading") {
  82. document.addEventListener("DOMContentLoaded", initialize);
  83. } else {
  84. initialize();
  85. }
  86. })();