From 39b2df36ee5ccea52558d0537bfee87058558e94 Mon Sep 17 00:00:00 2001 From: Agando Date: Wed, 10 Jun 2026 06:00:30 +0200 Subject: auto: 2026-06-10_06:00 changes --- modules/ext/console.js | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 modules/ext/console.js (limited to 'modules/ext') diff --git a/modules/ext/console.js b/modules/ext/console.js new file mode 100644 index 0000000..b0f6938 --- /dev/null +++ b/modules/ext/console.js @@ -0,0 +1,76 @@ +class JSConsoleModule { + constructor() { + this.commands = {}; + this.commandsMeta = { 'js': { argType: 'file' }, 'js-run': { argType: 'file' } }; + this.terminal = null; + } + + getHelpInfo() { + return { + 'js': 'js — Evaluate JavaScript in page context', + 'js-run': 'js-run — Load and execute a JS file from VFS' + }; + } + + registerCommands() { + this.commands['js'] = (args) => this.evalJS(args); + this.commands['js-run'] = (args) => this.evalJSFile(args); + } + + evalJS(args) { + if (!args[0]) return this.terminal.print('Usage: js ', 'warning'); + const expr = args.join(' '); + try { + const result = eval(expr); + const output = result === undefined ? 'undefined' : + typeof result === 'string' ? result : + result === null ? 'null' : + typeof result === 'object' ? JSON.stringify(result, null, 2) : + String(result); + this.terminal.print(output, 'success'); + } catch (e) { + this.terminal.print(`Error: ${e.message}`, 'error'); + } + } + + evalJSFile(args) { + if (!args[0]) return this.terminal.print('Usage: js-run ', 'warning'); + const path = args.join(' '); + try { + const content = this.terminal.fs.readFile(path); + if (content === null || content === undefined || content === false) { + return this.terminal.print(`js-run: ${path}: No such file`, 'error'); + } + // Wrap in an async IIFE so multi-line scripts with await work + const wrapped = `(async () => {\n${content}\n})()`; + const result = eval(wrapped); + // If it returns a promise, wait for it via .then + if (result && typeof result.then === 'function') { + result.then(val => { + const output = val === undefined ? '✓ Script executed' : + typeof val === 'string' ? val : + val === null ? 'null' : + typeof val === 'object' ? JSON.stringify(val, null, 2) : + String(val); + this.terminal.print(output, 'success'); + }).catch(e => { + this.terminal.print(`Error: ${e.message}`, 'error'); + }); + return; + } + const output = result === undefined ? '✓ Script executed' : + typeof result === 'string' ? result : + result === null ? 'null' : + typeof result === 'object' ? JSON.stringify(result, null, 2) : + String(result); + this.terminal.print(output, 'success'); + } catch (e) { + this.terminal.print(`Error: ${e.message}`, 'error'); + } + } + + seed() {} + init() {} +} + +if (typeof window !== 'undefined') window.JSConsoleModule = JSConsoleModule; -- cgit v1.2.3