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

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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);
}