|
85 | 85 | if (action === 'eval') {
|
86 | 86 | try {
|
87 | 87 | const { script } = ev.data.args;
|
88 |
| - eval(script); |
| 88 | + (0, eval)(script); |
89 | 89 | send_ok();
|
90 | 90 | } catch (e) {
|
91 | 91 | send_error(e.message, e.stack);
|
|
140 | 140 | window.addEventListener('unhandledrejection', (event) => {
|
141 | 141 | parent.postMessage({ action: 'unhandledrejection', value: event.reason }, '*');
|
142 | 142 | });
|
143 |
| - }).call(this); |
144 | 143 |
|
145 |
| - let previous = { level: null, args: null }; |
| 144 | + let previous = { level: null, args: null }; |
| 145 | + |
| 146 | + ['clear', 'log', 'info', 'dir', 'warn', 'error', 'table'].forEach((level) => { |
| 147 | + const original = console[level]; |
| 148 | + console[level] = (...args) => { |
| 149 | + const stringifiedArgs = stringify(args); |
| 150 | + if (previous.level === level && previous.args && previous.args === stringifiedArgs) { |
| 151 | + parent.postMessage({ action: 'console', level, duplicate: true }, '*'); |
| 152 | + } else { |
| 153 | + previous = { level, args: stringifiedArgs }; |
| 154 | + |
| 155 | + try { |
| 156 | + parent.postMessage({ action: 'console', level, args }, '*'); |
| 157 | + } catch (err) { |
| 158 | + parent.postMessage({ action: 'console', level: 'unclonable' }, '*'); |
| 159 | + } |
| 160 | + } |
146 | 161 |
|
147 |
| - ['clear', 'log', 'info', 'dir', 'warn', 'error', 'table'].forEach((level) => { |
148 |
| - const original = console[level]; |
149 |
| - console[level] = (...args) => { |
150 |
| - const stringifiedArgs = stringify(args); |
151 |
| - if (previous.level === level && previous.args && previous.args === stringifiedArgs) { |
152 |
| - parent.postMessage({ action: 'console', level, duplicate: true }, '*'); |
153 |
| - } else { |
154 |
| - previous = { level, args: stringifiedArgs }; |
| 162 | + original(...args); |
| 163 | + }; |
| 164 | + }); |
155 | 165 |
|
156 |
| - try { |
157 |
| - parent.postMessage({ action: 'console', level, args }, '*'); |
158 |
| - } catch (err) { |
159 |
| - parent.postMessage({ action: 'console', level: 'unclonable' }, '*'); |
160 |
| - } |
161 |
| - } |
| 166 | + [ |
| 167 | + { method: 'group', action: 'console_group' }, |
| 168 | + { method: 'groupEnd', action: 'console_group_end' }, |
| 169 | + { method: 'groupCollapsed', action: 'console_group_collapsed' } |
| 170 | + ].forEach((group_action) => { |
| 171 | + const original = console[group_action.method]; |
| 172 | + console[group_action.method] = (label) => { |
| 173 | + parent.postMessage({ action: group_action.action, label }, '*'); |
| 174 | + |
| 175 | + original(label); |
| 176 | + }; |
| 177 | + }); |
| 178 | + |
| 179 | + const timers = new Map(); |
| 180 | + const original_time = console.time; |
| 181 | + const original_timelog = console.timeLog; |
| 182 | + const original_timeend = console.timeEnd; |
162 | 183 |
|
163 |
| - original(...args); |
| 184 | + console.time = (label = 'default') => { |
| 185 | + original_time(label); |
| 186 | + timers.set(label, performance.now()); |
164 | 187 | };
|
165 |
| - }); |
166 |
| - |
167 |
| - [ |
168 |
| - { method: 'group', action: 'console_group' }, |
169 |
| - { method: 'groupEnd', action: 'console_group_end' }, |
170 |
| - { method: 'groupCollapsed', action: 'console_group_collapsed' } |
171 |
| - ].forEach((group_action) => { |
172 |
| - const original = console[group_action.method]; |
173 |
| - console[group_action.method] = (label) => { |
174 |
| - parent.postMessage({ action: group_action.action, label }, '*'); |
175 |
| - |
176 |
| - original(label); |
| 188 | + console.timeLog = (label = 'default') => { |
| 189 | + original_timelog(label); |
| 190 | + const now = performance.now(); |
| 191 | + if (timers.has(label)) { |
| 192 | + parent.postMessage( |
| 193 | + { |
| 194 | + action: 'console', |
| 195 | + level: 'system-log', |
| 196 | + args: [`${label}: ${now - timers.get(label)}ms`] |
| 197 | + }, |
| 198 | + '*' |
| 199 | + ); |
| 200 | + } else { |
| 201 | + parent.postMessage( |
| 202 | + { |
| 203 | + action: 'console', |
| 204 | + level: 'system-warn', |
| 205 | + args: [`Timer '${label}' does not exist`] |
| 206 | + }, |
| 207 | + '*' |
| 208 | + ); |
| 209 | + } |
177 | 210 | };
|
178 |
| - }); |
179 |
| - |
180 |
| - const timers = new Map(); |
181 |
| - const original_time = console.time; |
182 |
| - const original_timelog = console.timeLog; |
183 |
| - const original_timeend = console.timeEnd; |
184 |
| - |
185 |
| - console.time = (label = 'default') => { |
186 |
| - original_time(label); |
187 |
| - timers.set(label, performance.now()); |
188 |
| - }; |
189 |
| - console.timeLog = (label = 'default') => { |
190 |
| - original_timelog(label); |
191 |
| - const now = performance.now(); |
192 |
| - if (timers.has(label)) { |
193 |
| - parent.postMessage( |
194 |
| - { |
195 |
| - action: 'console', |
196 |
| - level: 'system-log', |
197 |
| - args: [`${label}: ${now - timers.get(label)}ms`] |
198 |
| - }, |
199 |
| - '*' |
200 |
| - ); |
201 |
| - } else { |
202 |
| - parent.postMessage( |
203 |
| - { action: 'console', level: 'system-warn', args: [`Timer '${label}' does not exist`] }, |
204 |
| - '*' |
205 |
| - ); |
206 |
| - } |
207 |
| - }; |
208 |
| - console.timeEnd = (label = 'default') => { |
209 |
| - original_timeend(label); |
210 |
| - const now = performance.now(); |
211 |
| - if (timers.has(label)) { |
212 |
| - parent.postMessage( |
213 |
| - { |
214 |
| - action: 'console', |
215 |
| - level: 'system-log', |
216 |
| - args: [`${label}: ${now - timers.get(label)}ms`] |
217 |
| - }, |
218 |
| - '*' |
219 |
| - ); |
220 |
| - } else { |
| 211 | + console.timeEnd = (label = 'default') => { |
| 212 | + original_timeend(label); |
| 213 | + const now = performance.now(); |
| 214 | + if (timers.has(label)) { |
| 215 | + parent.postMessage( |
| 216 | + { |
| 217 | + action: 'console', |
| 218 | + level: 'system-log', |
| 219 | + args: [`${label}: ${now - timers.get(label)}ms`] |
| 220 | + }, |
| 221 | + '*' |
| 222 | + ); |
| 223 | + } else { |
| 224 | + parent.postMessage( |
| 225 | + { |
| 226 | + action: 'console', |
| 227 | + level: 'system-warn', |
| 228 | + args: [`Timer '${label}' does not exist`] |
| 229 | + }, |
| 230 | + '*' |
| 231 | + ); |
| 232 | + } |
| 233 | + timers.delete(label); |
| 234 | + }; |
| 235 | + |
| 236 | + const original_assert = console.assert; |
| 237 | + console.assert = (condition, ...args) => { |
| 238 | + if (condition) { |
| 239 | + const stack = new Error().stack; |
| 240 | + parent.postMessage({ action: 'console', level: 'assert', args, stack }, '*'); |
| 241 | + } |
| 242 | + original_assert(condition, ...args); |
| 243 | + }; |
| 244 | + |
| 245 | + const counter = new Map(); |
| 246 | + const original_count = console.count; |
| 247 | + const original_countreset = console.countReset; |
| 248 | + |
| 249 | + console.count = (label = 'default') => { |
| 250 | + counter.set(label, (counter.get(label) || 0) + 1); |
221 | 251 | parent.postMessage(
|
222 |
| - { action: 'console', level: 'system-warn', args: [`Timer '${label}' does not exist`] }, |
| 252 | + { action: 'console', level: 'system-log', args: `${label}: ${counter.get(label)}` }, |
223 | 253 | '*'
|
224 | 254 | );
|
225 |
| - } |
226 |
| - timers.delete(label); |
227 |
| - }; |
| 255 | + original_count(label); |
| 256 | + }; |
| 257 | + |
| 258 | + console.countReset = (label = 'default') => { |
| 259 | + if (counter.has(label)) { |
| 260 | + counter.set(label, 0); |
| 261 | + } else { |
| 262 | + parent.postMessage( |
| 263 | + { |
| 264 | + action: 'console', |
| 265 | + level: 'system-warn', |
| 266 | + args: `Count for '${label}' does not exist` |
| 267 | + }, |
| 268 | + '*' |
| 269 | + ); |
| 270 | + } |
| 271 | + original_countreset(label); |
| 272 | + }; |
| 273 | + |
| 274 | + const original_trace = console.trace; |
228 | 275 |
|
229 |
| - const original_assert = console.assert; |
230 |
| - console.assert = (condition, ...args) => { |
231 |
| - if (condition) { |
| 276 | + console.trace = (...args) => { |
232 | 277 | const stack = new Error().stack;
|
233 |
| - parent.postMessage({ action: 'console', level: 'assert', args, stack }, '*'); |
234 |
| - } |
235 |
| - original_assert(condition, ...args); |
236 |
| - }; |
237 |
| - |
238 |
| - const counter = new Map(); |
239 |
| - const original_count = console.count; |
240 |
| - const original_countreset = console.countReset; |
241 |
| - |
242 |
| - console.count = (label = 'default') => { |
243 |
| - counter.set(label, (counter.get(label) || 0) + 1); |
244 |
| - parent.postMessage( |
245 |
| - { action: 'console', level: 'system-log', args: `${label}: ${counter.get(label)}` }, |
246 |
| - '*' |
247 |
| - ); |
248 |
| - original_count(label); |
249 |
| - }; |
250 |
| - |
251 |
| - console.countReset = (label = 'default') => { |
252 |
| - if (counter.has(label)) { |
253 |
| - counter.set(label, 0); |
254 |
| - } else { |
255 |
| - parent.postMessage( |
256 |
| - { |
257 |
| - action: 'console', |
258 |
| - level: 'system-warn', |
259 |
| - args: `Count for '${label}' does not exist` |
260 |
| - }, |
261 |
| - '*' |
262 |
| - ); |
263 |
| - } |
264 |
| - original_countreset(label); |
265 |
| - }; |
266 |
| - |
267 |
| - const original_trace = console.trace; |
268 |
| - |
269 |
| - console.trace = (...args) => { |
270 |
| - const stack = new Error().stack; |
271 |
| - parent.postMessage({ action: 'console', level: 'trace', args, stack }, '*'); |
272 |
| - original_trace(...args); |
273 |
| - }; |
274 |
| - |
275 |
| - function stringify(args) { |
276 |
| - try { |
277 |
| - return JSON.stringify(args, (key, value) => { |
278 |
| - // if we don't do this, our Set/Map from svelte/reactivity would show up wrong in the console |
279 |
| - if (value instanceof Map) { |
280 |
| - return { |
281 |
| - type: 'Map', |
282 |
| - value |
283 |
| - }; |
284 |
| - } |
285 |
| - if (value instanceof Set) { |
286 |
| - return { |
287 |
| - type: 'Set', |
288 |
| - value |
289 |
| - }; |
290 |
| - } |
291 |
| - return value; |
292 |
| - }); |
293 |
| - } catch (error) { |
294 |
| - return null; |
| 278 | + parent.postMessage({ action: 'console', level: 'trace', args, stack }, '*'); |
| 279 | + original_trace(...args); |
| 280 | + }; |
| 281 | + |
| 282 | + function stringify(args) { |
| 283 | + try { |
| 284 | + return JSON.stringify(args, (key, value) => { |
| 285 | + // if we don't do this, our Set/Map from svelte/reactivity would show up wrong in the console |
| 286 | + if (value instanceof Map) { |
| 287 | + return { |
| 288 | + type: 'Map', |
| 289 | + value |
| 290 | + }; |
| 291 | + } |
| 292 | + if (value instanceof Set) { |
| 293 | + return { |
| 294 | + type: 'Set', |
| 295 | + value |
| 296 | + }; |
| 297 | + } |
| 298 | + return value; |
| 299 | + }); |
| 300 | + } catch (error) { |
| 301 | + return null; |
| 302 | + } |
295 | 303 | }
|
296 |
| - } |
| 304 | + })(); |
297 | 305 | </script>
|
298 | 306 | </head>
|
299 | 307 | <body></body>
|
|
0 commit comments