|
|
@@ -0,0 +1,83 @@
|
|
|
+// ==UserScript==
|
|
|
+// @name HuaWei Table Transformer
|
|
|
+// @namespace http://tampermonkey.net/
|
|
|
+// @version 1.0
|
|
|
+// @description try to take over the world!
|
|
|
+// @author You
|
|
|
+// @match https://developer.huawei.com/consumer/cn/doc/distribution/promotion/*
|
|
|
+// @grant none
|
|
|
+// ==/UserScript==
|
|
|
+
|
|
|
+(function() {
|
|
|
+ 'use strict';
|
|
|
+ const convertType = (type) => {
|
|
|
+ const isList = type.endsWith('[]')
|
|
|
+ if (isList) type = type.substring(0, type.length - 2)
|
|
|
+ switch (type.toLowerCase()) {
|
|
|
+ case 'long':
|
|
|
+ case 'float':
|
|
|
+ case 'double':
|
|
|
+ case 'int':
|
|
|
+ case 'integer': type = 'number'; break
|
|
|
+ case 'boolean': type = 'boolean'; break
|
|
|
+ case 'string': type = 'string'; break
|
|
|
+ }
|
|
|
+ if (isList) type += '[]'
|
|
|
+ return type
|
|
|
+ }
|
|
|
+ const parseRow = (row) => {
|
|
|
+ const tds = row.querySelectorAll('td')
|
|
|
+ if (tds.length < 3 || tds > 4) return ''
|
|
|
+ const withOptional = tds.length === 4
|
|
|
+ const name = tds[0].innerText.trim()
|
|
|
+ if (!name) return ''
|
|
|
+ const type = tds[1].innerText.trim()
|
|
|
+ const optional = withOptional ? tds[2].innerText === '是' : false
|
|
|
+ const comment = tds[withOptional ? 3 : 2].innerText.trim()
|
|
|
+ return ` ${name.replace(/\n/g, '')}${optional ? '?' : ''}: ${convertType(type)} // ${comment.replace(/\n/g, ' ')}\n`
|
|
|
+ }
|
|
|
+ const parseRows = (header, rows) => {
|
|
|
+ if (!rows.length) return
|
|
|
+ let content = `interface ${header} {\n`
|
|
|
+ rows.forEach(row => {
|
|
|
+ content += parseRow(row)
|
|
|
+ })
|
|
|
+ content += '}'
|
|
|
+ return content
|
|
|
+ }
|
|
|
+
|
|
|
+ const parseTable = (tableDiv) => {
|
|
|
+ // Has a header?
|
|
|
+ const header = tableDiv.previousElementSibling?.innerText?.trim()
|
|
|
+ if (header) {
|
|
|
+ const result = document.createElement('pre')
|
|
|
+ result.append(document.createTextNode(parseRows(header, tableDiv.querySelectorAll('tr:nth-child(n+2)'))))
|
|
|
+ tableDiv.prepend(result)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ const createButton = (container) => {
|
|
|
+ container.style.position = 'relative'
|
|
|
+ const btn = document.createElement('button')
|
|
|
+ btn.append(document.createTextNode('Dump'))
|
|
|
+ btn.addEventListener('click', () => {
|
|
|
+ parseTable(container)
|
|
|
+ btn.remove()
|
|
|
+ })
|
|
|
+ btn.style.position = 'absolute'
|
|
|
+ btn.style.top = '0'
|
|
|
+ btn.style.right = '-2em'
|
|
|
+ btn.style.zIndex = 10000
|
|
|
+ container.prepend(btn)
|
|
|
+ }
|
|
|
+
|
|
|
+ const ob = new MutationObserver(mutations => {
|
|
|
+ mutations.forEach(({ target }) => {
|
|
|
+ if (target?.tagName === 'DIV' && target?.classList?.contains('markdown-body')) {
|
|
|
+ target.querySelectorAll('div.tablenoborder').forEach(createButton)
|
|
|
+ }
|
|
|
+ })
|
|
|
+ })
|
|
|
+ ob.observe(document.body, { subtree: true, childList: true })
|
|
|
+
|
|
|
+})();
|