Greasy Fork is available in English.

Copiable Operation ID For Swagger UI API Docs

Add operation ID, as well as an icon to copy the operation ID, to all API operations on any Swagger UI page

  1. // ==UserScript==
  2. // @name Copiable Operation ID For Swagger UI API Docs
  3. // @name:fr ID opération copiable pour docs API Swagger UI
  4. // @namespace tomchen.org
  5. // @include https://*
  6. // @include http://*
  7. // @grant GM_setClipboard
  8. // @version 1.1
  9. // @author Tom Chen (tomchen.org)
  10. // @license MIT
  11. // @description Add operation ID, as well as an icon to copy the operation ID, to all API operations on any Swagger UI page
  12. // @description:fr Ajoutez l'ID opération, ainsi qu'une icône pour copier l'ID opération, à toutes les opérations d'API sur une page de Swagger UI
  13. // ==/UserScript==
  14.  
  15. const swaggerUi = document.getElementById("swagger-ui");
  16. if (swaggerUi) {
  17. const opElId2opId = (opElId) => {
  18. const slices = opElId.split("-");
  19. return slices[slices.length - 1];
  20. };
  21.  
  22. const addCopiableOpIdToOpBlock = (opBlock) => {
  23. const summary = opBlock.querySelector("div.opblock-summary");
  24. const arrow = opBlock.querySelector("button.opblock-control-arrow");
  25. const opId = opElId2opId(opBlock.id);
  26. if (!summary.querySelector(".opblock-summary-operation-id")) {
  27. const span = document.createElement("span");
  28. span.innerHTML = opId;
  29. span.className = "opblock-summary-operation-id";
  30. summary.insertBefore(span, arrow);
  31. }
  32. const span = opBlock.querySelector("span.opblock-summary-operation-id");
  33. if (!span.querySelector("div.copy-to-clipboard")) {
  34. const div = document.createElement("div");
  35. div.className = "view-line-link copy-to-clipboard";
  36. div.innerHTML =
  37. '<svg width="15" height="16"><use href="#copy" xlink:href="#copy"></use></svg>';
  38. div.addEventListener("click", (e) => {
  39. e.stopPropagation();
  40. GM_setClipboard(opId);
  41. });
  42. span.appendChild(div, arrow);
  43. }
  44. };
  45.  
  46. const observeOptions = {
  47. subtree: true,
  48. childList: true,
  49. attributes: true,
  50. };
  51.  
  52. const observeCallback = () => {
  53. const opBlocks = document.querySelectorAll(".opblock");
  54. if (opBlocks.length === 0) {
  55. return;
  56. }
  57. [...opBlocks].forEach(addCopiableOpIdToOpBlock);
  58. };
  59. const observer = new MutationObserver(observeCallback);
  60. observer.observe(swaggerUi, observeOptions);
  61. }