Greasy Fork 支持简体中文。

swagger-cp

swagger复制小插件!

  1. // ==UserScript==
  2. // @name swagger-cp
  3. // @namespace swagger-cp-ll
  4. // @version 0.3
  5. // @description swagger复制小插件!
  6. // @author ll
  7. // @require https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.8/clipboard.min.js
  8. // @match https://*/swagger-ui.html
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. /**
  16. * 得到接口方法
  17. * @param {*} blockItem 元素
  18. * @returns GET/POST
  19. */
  20. function getRequestMethod (blockItem) {
  21. if (!blockItem) return
  22. return blockItem.getElementsByClassName('opblock-summary-method')[0].innerHTML
  23. }
  24. /**
  25. * 得到接口url
  26. * @param {*} blockItem 元素
  27. * @returns path
  28. */
  29. function getRequestPath (blockItem) {
  30. if (!blockItem) return
  31. const urlElement = blockItem.getElementsByClassName('nostyle')[0]
  32. return urlElement.children[0].innerHTML
  33. }
  34. /**
  35. * 接口名字拼接
  36. * @param {*} path 接口路径
  37. * @returns api大驼峰名字
  38. */
  39. function getRequestName (path) {
  40. const lastPath = path.split('/')
  41. const nameList = lastPath[lastPath.length - 1].split('-')
  42. for(let i = 0; i < nameList.length; i++) {
  43. // 首字母大写
  44. nameList[i] = nameList[i].replace(nameList[i][0],nameList[i][0].toUpperCase())
  45. }
  46. return `api${nameList.join('')}`
  47. }
  48. /**
  49. * 得到接口注释
  50. * @param {*} blockItem 元素
  51. * @returns text
  52. */
  53. function getRequestNote (blockItem) {
  54. if (!blockItem) return
  55. return blockItem.getElementsByClassName('opblock-summary-description')[0].innerHTML
  56. }
  57. // 复制
  58. function clipboardUrl (e) {
  59. e.preventDefault();
  60. var clipboard = new ClipboardJS('.copy_btn')
  61. clipboard.on('error', function(e) { window.alert('复制失败')})
  62. }
  63.  
  64. function handleCopyUrl (blockItem) {
  65. return getRequestPath(blockItem)
  66. }
  67.  
  68. function handleCopyFunc (blockItem, parentNote) {
  69. const method = getRequestMethod(blockItem)
  70. const path = getRequestPath(blockItem)
  71. const name = getRequestName(path)
  72. const note = getRequestNote(blockItem)
  73.  
  74. if (method === 'GET') {
  75. return `
  76. // ${parentNote}-${note}
  77. export const ${name} = (params) => {
  78. return axiosRequest({
  79. url: '${path}',
  80. method: 'get',
  81. params
  82. })
  83. }`
  84. } else {
  85. return `
  86. // ${parentNote}-${note}
  87. export const ${name} = (data) => {
  88. return axiosRequest({
  89. url: '${path}',
  90. data
  91. })
  92. }`
  93. }
  94. }
  95. // 生成btn
  96. function createBtnElement(text, clipboardText){
  97. const urlBtn = document.createElement('button')
  98. urlBtn.innerHTML = text
  99. urlBtn.style.width = '80px'
  100. urlBtn.style.height = '40px'
  101. urlBtn.style.margin = '0 5px 5px 0'
  102. urlBtn.classList.add('copy_btn')
  103. urlBtn.dataset.clipboardText = clipboardText
  104. urlBtn.onclick = clipboardUrl
  105. return urlBtn
  106. }
  107. function setBtn(blockList, parentNote){
  108. for(let i = 0; i< blockList.length; i++){
  109. blockList[i].parentNode.prepend(createBtnElement('复制函数', handleCopyFunc(blockList[i], parentNote)))
  110.  
  111. blockList[i].parentNode.prepend(createBtnElement('复制url', handleCopyUrl(blockList[i])))
  112. }
  113. }
  114. function initBtn (tagItem) {
  115. if (tagItem) {
  116. const parentNote = tagItem.getElementsByClassName('nostyle')[0].children[0].innerHTML
  117. const blockList = tagItem.parentNode.getElementsByClassName('opblock') || []
  118. if (blockList.length) {
  119. setBtn(blockList, parentNote)
  120. }
  121. tagItem.addEventListener('click', (e) => {
  122. setTimeout(() => {
  123. const blockList = tagItem.parentNode.getElementsByClassName('opblock') || []
  124. setBtn(blockList, parentNote)
  125. }, 0)
  126. }, false)
  127. }
  128. }
  129. function init () {
  130. document.addEventListener('readystatechange', () => {
  131. let loadDom = false
  132. let timer = null
  133. if (!loadDom) {
  134. timer = setInterval(() => {
  135. const tagList = document.querySelectorAll('.opblock-tag')
  136. if (tagList.length) {
  137. // 直到dom加载完成
  138. clearInterval(timer)
  139. timer = null
  140. loadDom = true
  141. for(let i = 0; i< tagList.length; i++){
  142. initBtn(tagList[i])
  143. }
  144. }
  145. }, 1000)
  146. }
  147. })
  148. }
  149.  
  150. init()
  151. })();