Greasy Fork is available in English.

Swagger复制地址

Swagger UI 3 复制 API path

  1. // ==UserScript==
  2. // @name Swagger复制地址
  3. // @name:en swagger_copy
  4. // @namespace https://github.com/kirileec/swagger_copy
  5. // @version 0.1.1
  6. // @description Swagger UI 3 复制 API path
  7. // @description:en Swagger UI 3 add copy button after operation (.opblock-summary) for copying API path
  8. // @license MIT
  9. // @author linx
  10. // @icon https://s3.bmp.ovh/imgs/2022/03/1e2657c3aa8567bd.png
  11. // @match *://*/swagger/index.html
  12. // @grant none
  13. // ==/UserScript==
  14.  
  15.  
  16. ; (() => {
  17.  
  18. function Toast(msg, duration) {
  19. duration = isNaN(duration) ? 3000 : duration;
  20. var m = document.createElement('div');
  21. m.innerHTML = msg;
  22. m.style.cssText = "font-family:siyuan;max-width:60%;min-width: 150px;padding:0 14px;height: 40px;color: rgb(255, 255, 255);line-height: 40px;text-align: center;border-radius: 4px;position: fixed;top: 50%;left: 50%;transform: translate(-50%, -50%);z-index: 999999;background: rgba(0, 0, 0,.7);font-size: 16px;";
  23. document.body.appendChild(m);
  24. setTimeout(function () {
  25. var d = 0.5;
  26. m.style.webkitTransition = '-webkit-transform ' + d + 's ease-in, opacity ' + d + 's ease-in';
  27. m.style.opacity = '0';
  28. setTimeout(function () {
  29. document.body.removeChild(m)
  30. }, d * 1000);
  31. }, duration);
  32. }
  33. function copyToClipBoard(content) {
  34. // navigator clipboard 需要https等安全上下文
  35. if (navigator.clipboard && window.isSecureContext) {
  36. // navigator clipboard 向剪贴板写文本
  37. Toast('The api path has been copied!', 2000);
  38. return navigator.clipboard.writeText(content);
  39. } else {
  40. // 创建text area
  41. let textArea = document.createElement("textarea");
  42. textArea.value = content;
  43. // 使text area不在viewport,同时设置不可见
  44. textArea.style.position = "absolute";
  45. textArea.style.opacity = 0;
  46. textArea.style.left = "-999999px";
  47. textArea.style.top = "-999999px";
  48. document.body.appendChild(textArea);
  49. //textArea.focus();
  50. textArea.select();
  51. return new Promise((res, rej) => {
  52. // 执行复制命令并移除文本框
  53. Toast('The api path has been copied!', 2000);
  54. document.execCommand('copy') ? res() : rej();
  55. textArea.remove();
  56. });
  57. }
  58. }
  59.  
  60. const sheets = `
  61. /* 复制按钮的样式 */
  62. .btn-copy {
  63. width: 16px;
  64. height: 16px;
  65. border: 0px;
  66. padding:0px;
  67. background-color: transparent;
  68. background-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAHJJREFUOE9jZKAQMOLQH8rAwJBFwOxpDAwMq3EZsJ9IhzkSMsARh0EwC3AaQMgBGAYQ7WeoyRgGEO1nQgYQ9POgMADZu/BYgAcKoWhjYGCAqUVJSKQYgGIHLCFRzQBCCQgjlmAuIDUhwS3ClRcIuQQuDwCk5B8RCCbZMAAAAABJRU5ErkJggg==");
  69. }
  70.  
  71. /* 按钮显隐样式 */
  72. #swagger-ui .opblock .toolkit-path-btn-group { margin-left: 10px;margin-top:5px; display: none; }
  73. #swagger-ui .opblock:hover .toolkit-path-btn-group { margin-left: 10px;margin-top:5px;display: block; }
  74. `
  75. // 插入样式
  76. const sheet = document.createTextNode(sheets)
  77. const el = document.createElement('style')
  78. el.id = 'swagger-toolkit-sheets'
  79. el.appendChild(sheet)
  80. document.getElementsByTagName('head')[0].appendChild(el)
  81. // 设置鼠标移动事件
  82. document.querySelector('#swagger-ui').addEventListener('mouseover', evt => {
  83. const opblock = evt.target.closest('.opblock')
  84. if (!opblock) return
  85. if (opblock.querySelector('.toolkit-path-btn-group')) return
  86. // 创建按钮
  87. const group = document.createElement('div')
  88. const copyBtn = document.createElement('button')
  89. group.classList.add('toolkit-path-btn-group')
  90. copyBtn.classList.add('btn-copy')
  91. group.appendChild(copyBtn)
  92. // 点击时执行复制
  93. copyBtn.addEventListener('click', evt => {
  94. console.log("click")
  95. evt.stopPropagation()
  96. const pathDOM = evt.target.closest('.opblock-summary-path')
  97. if (!pathDOM) return
  98. const pathLink = pathDOM.querySelector('a')
  99. if (!pathLink) return
  100. const path = pathLink.innerText
  101. copyToClipBoard(path);
  102.  
  103. })
  104.  
  105. const pathDOM = opblock.querySelector('.opblock-summary-path')
  106. if (pathDOM) pathDOM.appendChild(group)
  107. })
  108.  
  109.  
  110. })();