* Custom print function for displaying output in the UI * @param {...*} args - Arguments to print to UI output
(...args)
| 346 | * @param {...*} args - Arguments to print to UI output |
| 347 | */ |
| 348 | function customPrintFunction(...args) { |
| 349 | if (containerId === undefined) { |
| 350 | return; // Not ready yet. |
| 351 | } |
| 352 | // take the args and stringify them, then add them to the output element |
| 353 | const cleanedArgs = args.map((arg) => { |
| 354 | if (typeof arg === 'object') { |
| 355 | try { |
| 356 | return JSON.stringify(arg).slice(0, 100); |
| 357 | } catch (e) { |
| 358 | return `${arg}`; |
| 359 | } |
| 360 | } |
| 361 | return arg; |
| 362 | }); |
| 363 | |
| 364 | const output = document.getElementById(outputId); |
| 365 | const allText = `${output.textContent + [...cleanedArgs].join(' ')}\n`; |
| 366 | // split into lines, and if there are more than 100 lines, remove one. |
| 367 | const lines = allText.split('\n'); |
| 368 | while (lines.length > MAX_STDOUT_LINES) { |
| 369 | lines.shift(); |
| 370 | } |
| 371 | output.textContent = lines.join('\n'); |
| 372 | } |
| 373 | |
| 374 | // DO NOT OVERRIDE ERROR! When something goes really wrong we want it |
| 375 | // to always go to the console. If we hijack it then startup errors become |