|
|
@@ -1,8 +1,8 @@
|
|
|
// ==UserScript==
|
|
|
-// @name OceanEngine Document Maker
|
|
|
+// @name OceanEngine Table Transformer
|
|
|
// @namespace http://tampermonkey.net/
|
|
|
// @version 1.0
|
|
|
-// @description Translate document to enumeration and type defintion
|
|
|
+// @description try to take over the world!
|
|
|
// @author You
|
|
|
// @match https://open.oceanengine.com/labels/7/docs/*
|
|
|
// @grant GM_setClipboard
|
|
|
@@ -38,8 +38,7 @@
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- const parseRow = (row) => {
|
|
|
- const tds = row.querySelectorAll('td')
|
|
|
+ const parseRow = (tds) => {
|
|
|
const name = parseName(tds[0].querySelector('div')?.childNodes)
|
|
|
if (!name) return ''
|
|
|
const optional = tds[0].querySelector('span.tag')?.innerText?.trim() !== '必填'
|
|
|
@@ -47,18 +46,31 @@
|
|
|
const comment = tds[2].innerText.trim()
|
|
|
return ` val ${name.replace(/\n/g, '')}: ${convertType(type)}${optional ? '? = null' : ''}, // ${comment.replace(/\n/g, ' ')}\n`
|
|
|
}
|
|
|
- const parseRows = (rows) => {
|
|
|
- if (!rows.length) return
|
|
|
- let content = `data class Foo (\n`
|
|
|
- rows.forEach(row => {
|
|
|
- content += parseRow(row)
|
|
|
- })
|
|
|
- content += ')'
|
|
|
- return content
|
|
|
+ const parseEnumRow = (tds) => {
|
|
|
+ const name = parseName(tds[0].querySelector('div')?.childNodes)
|
|
|
+ if (!name) return ''
|
|
|
+ let safeName = name.toUpperCase().replace(/\n/g, '').replace(/[^A-Z0-9_]/g, '_')
|
|
|
+ if (!/^[A-Z]/.test(safeName)) safeName = `_${safeName}`
|
|
|
+ const comment = tds[1].innerText.trim()
|
|
|
+ return `${name === safeName ? '' : ` @SerializedName("${name}")\n`} ${safeName}, // ${comment.replace(/\n/g, ' ')}\n`
|
|
|
}
|
|
|
-
|
|
|
const parseTable = (table, selector, result) => {
|
|
|
- result.innerText = parseRows(table.querySelectorAll(selector))
|
|
|
+ let node = table.previousElementSibling
|
|
|
+ let title
|
|
|
+ while (node) {
|
|
|
+ if (/^H\d$/.test(node.tagName)) {
|
|
|
+ title = node.innerText.replace(/\s/g, '')
|
|
|
+ break
|
|
|
+ }
|
|
|
+ node = node.previousElementSibling
|
|
|
+ }
|
|
|
+ const isEnum = table.querySelector('th')?.innerText?.trim() === '值'
|
|
|
+ let content = isEnum ? `enum class ${title} {\n` : `data class ${title} (\n`
|
|
|
+ table.querySelectorAll(selector).forEach(row => {
|
|
|
+ content += (isEnum ? parseEnumRow : parseRow)(row.querySelectorAll('td'))
|
|
|
+ })
|
|
|
+ content += isEnum ? '}' : ')'
|
|
|
+ result.innerText = content
|
|
|
result.style.display = ''
|
|
|
GM_setClipboard(result.innerText)
|
|
|
}
|
|
|
@@ -131,5 +143,5 @@
|
|
|
})
|
|
|
})
|
|
|
ob.observe(document.body, { subtree: true, childList: true })
|
|
|
-
|
|
|
+ setTimeout(() => document.querySelectorAll('table').forEach(createButton), 3000)
|
|
|
})();
|