doc.html-cp

doc.html复制小插件!

  1. // ==UserScript==
  2. // @name doc.html-cp
  3. // @namespace doc.html-cp-ll
  4. // @version 0.1
  5. // @description doc.html复制小插件!
  6. // @author ll
  7. // @require https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.8/clipboard.min.js
  8. // @match https://*/doc.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.children[0].children[1].children[1].innerHTML
  23. }
  24. /**
  25. * 得到接口url
  26. * @param {*} blockItem 元素
  27. * @returns path
  28. */
  29. function getRequestPath (blockItem) {
  30. if (!blockItem) return
  31. return blockItem.children[0].children[0].children[1].innerHTML
  32. }
  33. /**
  34. * 接口名字拼接
  35. * @param {*} path 接口路径
  36. * @returns api大驼峰名字
  37. */
  38. function getRequestName (path) {
  39. const lastPath = path.split('/')
  40. const nameList = lastPath[lastPath.length - 1].split('-')
  41. for(let i = 0; i < nameList.length; i++) {
  42. // 首字母大写
  43. nameList[i] = nameList[i].replace(nameList[i][0],nameList[i][0].toUpperCase())
  44. }
  45. return `api${nameList.join('')}`
  46. }
  47. /**
  48. * 得到接口注释
  49. * @param {*} blockItem 元素
  50. * @returns text
  51. */
  52. function getRequestNote (blockItem) {
  53. if (!blockItem) return
  54. return blockItem.children[0].children[4].children[1].innerHTML
  55. }
  56. // 复制
  57. function clipboardUrl (e) {
  58. e.preventDefault();
  59. var clipboard = new ClipboardJS('.copy_btn')
  60. clipboard.on('error', function(e) { window.alert('复制失败')})
  61. }
  62.  
  63. function handleCopyUrl (blockItem) {
  64. return getRequestPath(blockItem)
  65. }
  66.  
  67. function handleCopyFunc (blockItem, parentNote) {
  68. const method = getRequestMethod(blockItem)
  69. const path = getRequestPath(blockItem)
  70. const name = getRequestName(path)
  71. const note = getRequestNote(blockItem)
  72.  
  73. if (method === 'GET') {
  74. return `
  75. // ${parentNote}-${note}
  76. export const ${name} = (params) => {
  77. return axiosRequest({
  78. url: '${path}',
  79. method: 'get',
  80. params
  81. })
  82. }`
  83. } else {
  84. return `
  85. // ${parentNote}-${note}
  86. export const ${name} = (data) => {
  87. return axiosRequest({
  88. url: '${path}',
  89. data
  90. })
  91. }`
  92. }
  93. }
  94. // 生成btn
  95. function createBtnElement(text, clipboardText){
  96. const urlBtn = document.createElement('button')
  97. urlBtn.innerHTML = text
  98. urlBtn.classList.add('btn', 'btn-default', 'btn-info', 'copy_btn')
  99. urlBtn.dataset.clipboardText = clipboardText
  100. urlBtn.onclick = clipboardUrl
  101. return urlBtn
  102. }
  103. function setBtn (tagItem) {
  104. if (tagItem) {
  105. // hash:如果此元素下btn存在,就不再添加
  106. if (tagItem.getElementsByClassName('copy_btn').length) return
  107. setTimeout(function() {
  108. tagItem.children[0].children[0].append(createBtnElement('复制url', handleCopyUrl(tagItem)))
  109. const parentNote = document.querySelectorAll('.detailMenu.open')[0].title
  110. tagItem.children[0].children[1].append(createBtnElement('复制函数', handleCopyFunc(tagItem, parentNote)))
  111. }, 0)
  112. }
  113. }
  114. function hashChange(){
  115. // hash路由改变
  116. window.onhashchange = function() {
  117. const tagList = document.querySelectorAll('.tab-pane > .swbu-main')
  118. if (tagList.length) {
  119. // 直到dom加载完成
  120. for(let i = 0; i<tagList.length; i++) {
  121. setBtn(tagList[i])
  122. }
  123. }
  124. }
  125. }
  126. function init () {
  127. document.addEventListener('readystatechange', () => {
  128. let loadDom = false
  129. let timer = null
  130. if (!loadDom) {
  131. timer = setInterval(() => {
  132. const tagList = document.querySelectorAll('.tab-pane > .swbu-main')
  133. if (tagList.length) {
  134. // 直到dom加载完成
  135. clearInterval(timer)
  136. timer = null
  137. loadDom = true
  138. // 若第一页需要加复制按钮
  139. setBtn(tagList[0])
  140. }
  141. }, 1000)
  142. }
  143. })
  144.  
  145. hashChange()
  146. }
  147.  
  148. init()
  149. })();