flash-swagger

swagger 快捷插件

当前为 2020-10-15 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name flash-swagger
  3. // @namespace https://www.flashexpress.com/
  4. // @version 0.2
  5. // @description swagger 快捷插件
  6. // @author 杨淼
  7. // @match http://*/swagger-ui.html*
  8. // @grant GM_addStyle
  9. // @run-at document-end
  10. // @require https://cdn.bootcdn.net/ajax/libs/jquery/3.4.1/jquery.min.js
  11. // @require https://cdn.bootcdn.net/ajax/libs/clipboard.js/2.0.6/clipboard.js
  12. // ==/UserScript==
  13.  
  14. (function() {
  15. 'use strict'
  16.  
  17. console.log('油猴启动')
  18. new ClipboardJS('.copy-btn')
  19.  
  20. function createATag(text, content) {
  21. const temp = $(`<button class="btn copy-btn" data-clipboard-text="${content}">${text}</button>`)
  22.  
  23. temp.on('click', function(event) {
  24. event.preventDefault()
  25. })
  26.  
  27. return temp
  28. }
  29.  
  30. function toHump(name) {
  31. return name.replace(/\-(\w)/g, function(all, letter){
  32. return letter.toUpperCase();
  33. });
  34. }
  35.  
  36. function createFunc(methoud, path, description) {
  37. const methoudName = toHump(path.match('[^/]+(?!.*/)')[0])
  38. switch (methoud) {
  39. case 'POST':
  40. return `// ${description}
  41. export const ${methoudName} = (data) => {
  42. return axiosRequest({
  43. url: '${path}',
  44. data
  45. })
  46. }`
  47.  
  48. case 'GET':
  49. return `// ${description}
  50. export const ${methoudName} = (params) => {
  51. return axiosRequest({
  52. url: '${path}',
  53. methods: 'get',
  54. params
  55. })
  56. }`
  57.  
  58. default:
  59. return '暂不支持当前请求格式'
  60. }
  61. }
  62.  
  63. $('body')
  64. .delegate('.opblock-tag', 'click', function(event) {
  65. console.log($(this).next())
  66. // DOM异步展示 用宏任务包裹
  67. setTimeout(() => {
  68. $(this).next().children('span').each((index, $el) => {
  69. console.log($el)
  70. $el = $($el)
  71.  
  72. const methoud = $($el).find('.opblock-summary-method').text()
  73. const path = $($el).find('.opblock-summary-path > a > span').text()
  74. const description = $($el).find('.opblock-summary-description').text()
  75.  
  76. $el.before($('<div></div>')
  77. .append(createATag('复制URL', path))
  78. .append(createATag('复制函数', createFunc(methoud, path, description)))
  79. )
  80. })
  81. })
  82. })
  83. })()