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

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==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.1
// @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
// @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
// ==/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 summary = opBlock.querySelector("div.opblock-summary");
    const arrow = opBlock.querySelector("button.opblock-control-arrow");
    const opId = opElId2opId(opBlock.id);
    if (!summary.querySelector(".opblock-summary-operation-id")) {
      const span = document.createElement("span");
      span.innerHTML = opId;
      span.className = "opblock-summary-operation-id";
      summary.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);
}