-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug.js
More file actions
149 lines (135 loc) · 3.69 KB
/
Copy pathdebug.js
File metadata and controls
149 lines (135 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/* eslint-disable max-lines-per-function */
const createFloatingConsole = () => {
const commands = new Map()
// Attach console UI
const container = document.createElement('div')
container.style.position = 'fixed'
container.style.bottom = '10px'
container.style.right = '10px'
container.style.zIndex = '2147483647'
document.body.appendChild(container)
const shadow = container.attachShadow({ mode: 'open' })
shadow.innerHTML = `
<style>
#console-panel {
width: 320px;
max-height: 240px;
overflow-y: auto;
background: rgba(0, 0, 0, 0.85);
color: #0f0;
font-family: monospace;
font-size: 12px;
padding: 10px;
border-radius: 8px 8px 0 0;
box-shadow: 0 0 10px rgba(0,0,0,0.4);
white-space: pre-wrap;
display: flex;
flex-direction: column;
gap: 4px;
}
.log { color: #0f0; }
.warn { color: #ff5; }
.error { color: #f55; }
#command-input {
width: 100%;
border: none;
padding: 6px;
font-family: monospace;
font-size: 12px;
background: #111;
color: #0f0;
border-top: 1px solid #333;
border-radius: 0 0 8px 8px;
outline: none;
}
</style>
<div id="console-panel"></div>
<input id="command-input" placeholder="Enter command..." />
`
const panel = shadow.querySelector('#console-panel')
const input = shadow.querySelector('#command-input')
const appendEntry = (type, args) => {
const entry = document.createElement('div')
entry.className = type
entry.textContent = args.map(arg => {
try {
return typeof arg === 'object'
? JSON.stringify(arg, null, 2)
: String(arg)
} catch {
return '[unserializable]'
}
}).join(' ')
panel.appendChild(entry)
panel.scrollTop = panel.scrollHeight
}
// Patch console
const original = {
log: console.log,
warn: console.warn,
error: console.error
}
console.log = (...args) => {
original.log(...args)
appendEntry('log', args)
}
console.warn = (...args) => {
original.warn(...args)
appendEntry('warn', args)
}
console.error = (...args) => {
original.error(...args)
appendEntry('error', args)
}
// Safe eval-ish argument parser
const parseArgs = argsStr => {
const wrapped = `[${argsStr}]`
try {
return Function(`"use strict"; return ${wrapped}`)()
} catch {
console.error('Failed to parse arguments')
return []
}
}
// Command parser
const runCommand = input => {
const match = input.trim().match(/^(\w+)\s*\((.*)\)$/)
if (!match) {
console.error('Invalid command format. Use funcName(arg1, arg2, ...)')
return
}
// eslint-disable-next-line no-unused-vars
const [_, name, argsStr] = match
const fn = commands.get(name)
if (!fn) {
console.error(`Unknown command: ${name}`)
return
}
const args = parseArgs(argsStr)
try {
const result = fn(...args)
console.log(`> ${name} →`, result)
} catch (err) {
console.error(`Error in ${name}:`, err)
}
}
// Input handling
input.addEventListener('keydown', e => {
if (e.key === 'Enter') {
const val = input.value.trim().toLowerCase()
if (val) {
runCommand(val)
input.value = ''
}
}
})
// Expose command registration globally
window.registerCommand = (name, fn) => {
if (typeof name !== 'string' || typeof fn !== 'function') {
throw new Error('Usage: registerCommand("name", function)')
}
commands.set(name.toLowerCase(), fn)
console.log(`Registered command: ${name}`)
}
}
createFloatingConsole()