ocean-engine-doc-maker.user.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // ==UserScript==
  2. // @name OceanEngine Table Transformer
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.0
  5. // @description try to take over the world!
  6. // @author You
  7. // @match https://open.oceanengine.com/labels/7/docs/*
  8. // @grant GM_setClipboard
  9. // @run-at document-start
  10. // ==/UserScript==
  11. (function() {
  12. 'use strict';
  13. const convertType = (type) => {
  14. const isList = type.endsWith('[]')
  15. if (isList) type = type.substring(0, type.length - 2)
  16. switch (type.toLowerCase()) {
  17. case 'float': type = 'Float'; break
  18. case 'double': type = 'Double'; break
  19. case 'number':
  20. case 'int':
  21. case 'long':
  22. case 'integer': type = 'Long'; break
  23. case 'boolean': type = 'Boolean'; break
  24. case 'string': type = 'String'; break
  25. case 'json':
  26. case 'object': type = 'Object'; break
  27. }
  28. if (isList) type = `List<${type}>`
  29. return type
  30. }
  31. const parseName = (childNodes) => {
  32. if (!childNodes) return
  33. for (const node of childNodes) {
  34. if (node.nodeType === Node.TEXT_NODE) {
  35. const text = node.textContent.trim()
  36. if (text) return text
  37. }
  38. }
  39. }
  40. const parseRow = (tds) => {
  41. const name = parseName(tds[0].querySelector('div')?.childNodes)
  42. if (!name) return ''
  43. const optional = tds[0].querySelector('span.tag')?.innerText?.trim() !== '必填'
  44. const type = tds[1].innerText.trim()
  45. const comment = tds[2].innerText.trim()
  46. return ` val ${name.replace(/\n/g, '')}: ${convertType(type)}${optional ? '? = null' : ''}, // ${comment.replace(/\n/g, ' ')}\n`
  47. }
  48. const parseEnumRow = (tds) => {
  49. const name = parseName(tds[0].querySelector('div')?.childNodes)
  50. if (!name) return ''
  51. let safeName = name.toUpperCase().replace(/\n/g, '').replace(/[^A-Z0-9_]/g, '_')
  52. if (!/^[A-Z]/.test(safeName)) safeName = `_${safeName}`
  53. const comment = tds[1].innerText.trim()
  54. return `${name === safeName ? '' : ` @SerializedName("${name}")\n`} ${safeName}, // ${comment.replace(/\n/g, ' ')}\n`
  55. }
  56. const parseTable = (table, selector, result) => {
  57. let node = table.previousElementSibling
  58. let title
  59. while (node) {
  60. if (/^H\d$/.test(node.tagName)) {
  61. title = node.innerText.replace(/\s/g, '')
  62. break
  63. }
  64. node = node.previousElementSibling
  65. }
  66. const isEnum = table.querySelector('th')?.innerText?.trim() === '值'
  67. let content = isEnum ? `enum class ${title} {\n` : `data class ${title} (\n`
  68. table.querySelectorAll(selector).forEach(row => {
  69. content += (isEnum ? parseEnumRow : parseRow)(row.querySelectorAll('td'))
  70. })
  71. content += isEnum ? '}' : ')'
  72. result.innerText = content
  73. result.style.display = ''
  74. GM_setClipboard(result.innerText)
  75. }
  76. const ENUM_HINT = '允许值'
  77. const processEnumerationNode = (node) => {
  78. if (node.nodeType === Node.ELEMENT_NODE && node.tagName === 'CODE') {
  79. if (node.previousSibling?.tagName !== 'BR') node.parentNode.insertBefore(document.createElement('br'), node)
  80. node.innerText = node.innerText.replace(/"|'/g, '')
  81. } else if (node.nodeType === Node.TEXT_NODE && node.previousSibling?.tagName === 'CODE') {
  82. const comment = node.textContent.trim().replace(/、|,|,$/g, '')
  83. node.textContent = comment ? `,\u00A0//\u00A0${comment}` : ','
  84. }
  85. }
  86. const formatEnumerations = (table) => {
  87. table.querySelectorAll('tr > td:nth-child(3) > div').forEach(div => {
  88. if (div.innerText?.includes(ENUM_HINT)) {
  89. // 提示模式 允许值:blahblah
  90. let started = false
  91. let node = div.childNodes[0]
  92. while (node) {
  93. if (started) {
  94. processEnumerationNode(node)
  95. } else if (node.nodeType === Node.TEXT_NODE && node.textContent.includes(ENUM_HINT)) {
  96. started = true
  97. }
  98. node = node.nextSibling
  99. }
  100. } else {
  101. // 行首模式
  102. let node = div.querySelector('br + code')
  103. if (node?.previousSibling?.tagName !== 'BR') return
  104. while (node) {
  105. processEnumerationNode(node)
  106. node = node.nextSibling
  107. }
  108. }
  109. })
  110. }
  111. const createButton = (table) => {
  112. if (table.dataset.done) return
  113. table.dataset.done = true
  114. formatEnumerations(table)
  115. const levels = Array.from(table.querySelectorAll('tr')).reduce((s, row) => {
  116. if (row.className) s.add(row.className)
  117. return s
  118. }, new Set())
  119. const container = document.createElement('div')
  120. const result = document.createElement('pre')
  121. result.style.display = 'none'
  122. table.parentNode.insertBefore(container, table)
  123. table.parentNode.insertBefore(result, table)
  124. const addBtn = (level) => {
  125. const btn = document.createElement('button')
  126. btn.append(document.createTextNode(level))
  127. btn.addEventListener('click', () => parseTable(table, `tbody > tr.${level}`, result))
  128. container.append(btn)
  129. }
  130. for (const level of levels.values()) addBtn(level)
  131. }
  132. const ob = new MutationObserver(mutations => {
  133. mutations.forEach(({ target }) => {
  134. if (target?.tagName === 'DIV' && target?.classList?.contains('doc-content-body')) {
  135. target.querySelectorAll('table').forEach(createButton)
  136. }
  137. })
  138. })
  139. ob.observe(document.body, { subtree: true, childList: true })
  140. setTimeout(() => document.querySelectorAll('table').forEach(createButton), 3000)
  141. })();