Export profiling table data to JSON file with proper number formatting
Size
3.9 KB
Version
1.0.1
Created
Nov 27, 2025
Updated
3 months ago
1// ==UserScript==
2// @name SnakeViz Table to JSON Exporter
3// @description Export profiling table data to JSON file with proper number formatting
4// @version 1.0.1
5// @match http://127.0.0.1/*
6// @icon http://127.0.0.1:8080/favicon.ico
7// ==/UserScript==
8(function() {
9 'use strict';
10
11 function parseNumber(value) {
12 // Remove any whitespace
13 const cleaned = value.trim();
14
15 // If it's empty or not a number, return the original value
16 if (!cleaned || cleaned === '') return value;
17
18 // Try to parse as a number
19 const num = parseFloat(cleaned);
20
21 // If it's a valid number, return it as a number, otherwise return original
22 return !isNaN(num) ? num : value;
23 }
24
25 function exportTableToJSON() {
26 console.log('Starting table export...');
27
28 const table = document.querySelector('table');
29 if (!table) {
30 console.error('No table found on the page');
31 alert('No table found on the page');
32 return;
33 }
34
35 const rows = table.rows;
36 const data = [];
37
38 // Get headers from first row
39 const headers = [];
40 const headerRow = rows[0];
41 for (let i = 0; i < headerRow.cells.length; i++) {
42 headers.push(headerRow.cells[i].textContent.trim());
43 }
44
45 console.log('Headers:', headers);
46 console.log('Total rows:', rows.length);
47
48 // Process data rows (skip header row)
49 for (let i = 1; i < rows.length; i++) {
50 const row = rows[i];
51 const rowData = {};
52
53 for (let j = 0; j < row.cells.length; j++) {
54 const cellValue = row.cells[j].textContent.trim();
55 const header = headers[j];
56
57 // Parse numbers for numeric columns
58 rowData[header] = parseNumber(cellValue);
59 }
60
61 data.push(rowData);
62 }
63
64 console.log('Processed', data.length, 'rows');
65 console.log('Sample row:', data[0]);
66
67 // Convert to JSON
68 const jsonString = JSON.stringify(data, null, 2);
69
70 // Create blob and download
71 const blob = new Blob([jsonString], { type: 'application/json' });
72 const url = URL.createObjectURL(blob);
73 const a = document.createElement('a');
74 a.href = url;
75 a.download = 'table_export.json';
76 document.body.appendChild(a);
77 a.click();
78 document.body.removeChild(a);
79 URL.revokeObjectURL(url);
80
81 console.log('Export complete!');
82 alert('Table exported successfully! (' + data.length + ' rows)');
83 }
84
85 function addExportButton() {
86 // Create export button
87 const button = document.createElement('button');
88 button.textContent = 'Export Table to JSON';
89 button.style.cssText = `
90 position: fixed;
91 top: 10px;
92 right: 10px;
93 z-index: 10000;
94 padding: 10px 20px;
95 background-color: #4CAF50;
96 color: white;
97 border: none;
98 border-radius: 5px;
99 cursor: pointer;
100 font-size: 14px;
101 font-weight: bold;
102 box-shadow: 0 2px 5px rgba(0,0,0,0.2);
103 `;
104
105 button.addEventListener('mouseenter', function() {
106 this.style.backgroundColor = '#45a049';
107 });
108
109 button.addEventListener('mouseleave', function() {
110 this.style.backgroundColor = '#4CAF50';
111 });
112
113 button.addEventListener('click', exportTableToJSON);
114
115 document.body.appendChild(button);
116 console.log('Export button added');
117 }
118
119 // Wait for page to load
120 if (document.readyState === 'loading') {
121 document.addEventListener('DOMContentLoaded', addExportButton);
122 } else {
123 addExportButton();
124 }
125})();