vincent-ng / 定位出行开放平台报文转表格

// ==UserScript==
// @name         定位出行开放平台报文转表格
// @namespace    http://tampermonkey.net/
// @license      MIT
// @version      0.3
// @description  try to take over the world!
// @author       You
// @match        https://pt-local-open.qijigps.com/application/message-analysis*
// @match        https://pt-open.leaf.linketech.cn/application/message-analysis*
// @match        https://pt-open-alpha.leaf.linketech.cn/application/message-analysis*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=qijigps.com
// @grant        none
// ==/UserScript==

function jsonToTable(json) {
	const columns = Array.from(
		json.reduce((acc, cur) => {
			Object.keys(cur).forEach((key) => acc.add(key))
			return acc
		}, new Set()),
	)
	const table = document.createElement('table')
	table.id = 'gen-info-table'
	table.style.tableLayout = 'auto'
	const thead = table.createTHead()
	thead.className = 'ant-table-thead'
	const header = thead.insertRow()
	columns.forEach((col) => {
		const th = document.createElement('th')
		th.textContent = col
		header.appendChild(th)
	})
	const tbody = table.createTBody()
	tbody.className = 'ant-table-tbody'
	json.forEach((row) => {
		const tr = tbody.insertRow()
		tr.className = 'ant-table-cell ant-table-cell-ellipsis'
		columns.forEach((col) => {
			const td = tr.insertCell()
			td.className = 'ant-table-cell ant-table-cell-ellipsis'
			td.textContent = row[col]
		})
	})
	return table
}

function addButton() {
	const button = document.createElement('button')
	button.className = 'ant-btn ant-btn-default'
	button.textContent = '报文转表格'
	button.onclick = () => {
		const json = [...document.querySelectorAll('.ant-table-content table:nth-last-child(1) tr td:nth-last-child(2)')]
			.map((e) => JSON.parse(e.innerText))
			.map((e) => ({
				...e,
				collect_time: e.collect_time ? new Date(e.collect_time * 1000).toLocaleString() : undefined,
			}))
		const table = jsonToTable(json)
        document.querySelectorAll('table#gen-info-table').forEach((e) => e.remove())
		document.querySelector('.ant-table-content').prepend(table)
	}
	document.querySelector('.ant-card-head-wrapper').appendChild(button)
}

(function () {
	window.onload = () => { setTimeout(addButton, 3000) }
}())