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. For example, https://petstore.swagger.io/. For https://petstore.swagger.io/?displayOperationId=1, where the operation IDs are already added, the script will just add an icon to copy the operation ID.

Mint 2023.03.30.. Lásd a legutóbbi verzió

// ==UserScript==
// @name        Copiable Operation ID For Swagger UI API Docs
// @name:fr     ID opération copiable pour docs API Swagger UI
// @namespace   tomchen.org
// @include     https://*
// @include     http://*
// @grant       GM_setClipboard
// @version     1.0
// @author      Tom Chen (tomchen.org)
// @license     MIT
// @description Add operation ID, as well as an icon to copy the operation ID, to all API operations on any Swagger UI page. For example, https://petstore.swagger.io/. For https://petstore.swagger.io/?displayOperationId=1, where the operation IDs are already added, the script will just add an icon to copy the operation ID.
// @description:fr Ajoutez l'ID d'opération, ainsi qu'une icône pour copier l'ID d'opération, à toutes les opérations d'API sur n'importe quelle page de l'interface utilisateur Swagger. Par exemple, https://petstore.swagger.io/. Pour https://petstore.swagger.io/?displayOperationId=1, où les ID d'opération sont déjà ajoutés, le script ajoutera simplement une icône pour copier l'ID d'opération.
// ==/UserScript==

const swaggerUi = document.getElementById("swagger-ui");
if (swaggerUi) {
  const opElId2opId = (opElId) => {
    const slices = opElId.split("-");
    return slices[slices.length - 1];
  };

  const addCopiableOpIdToOpBlock = (opBlock) => {
    const button = opBlock.querySelector("button.opblock-summary-control");
    const arrow = opBlock.querySelector("svg.arrow");
    const opId = opElId2opId(opBlock.id);
    if (!button.querySelector(".opblock-summary-operation-id")) {
      const span = document.createElement("span");
      span.innerHTML = opId;
      span.className = "opblock-summary-operation-id";
      button.insertBefore(span, arrow);
    }
    const span = opBlock.querySelector("span.opblock-summary-operation-id");
    if (!span.querySelector("div.copy-to-clipboard")) {
      const div = document.createElement("div");
      div.className = "view-line-link copy-to-clipboard";
      div.innerHTML =
        '<svg width="15" height="16"><use href="#copy" xlink:href="#copy"></use></svg>';
      div.addEventListener("click", (e) => {
        e.stopPropagation();
        GM_setClipboard(opId);
      });
      span.appendChild(div, arrow);
    }
  };

  const observeOptions = {
    subtree: true,
    childList: true,
    attributes: true,
  };

  const observeCallback = () => {
    const opBlocks = document.querySelectorAll(".opblock");
    if (opBlocks.length === 0) {
      return;
    }
    [...opBlocks].forEach(addCopiableOpIdToOpBlock);
  };
  const observer = new MutationObserver(observeCallback);
  observer.observe(swaggerUi, observeOptions);
}
长期地址
遇到问题?请前往 GitHub 提 Issues,或加Q群1031348184

赞助商

Fishcpy

广告

Rainyun

一年攒够 12 元

云驰互联

云驰互联