diff --git a/api.php b/api.php index a72342d..95c78e9 100644 --- a/api.php +++ b/api.php @@ -299,6 +299,14 @@ try { $result = adb_send_text($ip, $text); } elseif ($cmdType === 'launch_home') { $result = adb_send_key($ip, 'KEYCODE_HOME'); + } elseif ($cmdType === 'shell') { + $raw = (string) ($input['shell'] ?? $input['cmd'] ?? $input['text'] ?? ''); + $shellCmd = normalize_adb_shell_command($raw); + if ($shellCmd === '') { + json_out(['ok' => false, 'error' => 'Empty or comment-only shell command.'], 400); + } + $res = adb_run_shell_command($ip, $shellCmd, 30); + $result = ['ok' => $res['ok'], 'output' => $res['output'], 'command_line' => $shellCmd]; } else { json_out(['ok' => false, 'error' => 'Unsupported command type.'], 400); } diff --git a/assets/app.js b/assets/app.js index 707dd1a..685381c 100644 --- a/assets/app.js +++ b/assets/app.js @@ -478,6 +478,34 @@ } } + function parseBatchLine(line) { + let cmd = String(line || '').trim(); + if (!cmd || cmd.startsWith('#')) return null; + + // Strip inline comment + const hashIdx = cmd.indexOf(' #'); + if (hashIdx !== -1) cmd = cmd.slice(0, hashIdx).trim(); + if (!cmd || cmd.startsWith('#')) return null; + + // Strip adb prefixes + let shell = cmd; + const adbShellMatch = shell.match(/^adb\s+-s\s+\S+\s+shell\s+(.+)$/i) || shell.match(/^adb\s+shell\s+(.+)$/i); + if (adbShellMatch) { + shell = adbShellMatch[1].trim(); + } + + // Keycode if single token and looks like keycode + const isSingleToken = !/\s/.test(shell); + const keyCandidate = normalizeKeycode(shell); + if (isSingleToken && /^KEYCODE_[A-Z0-9_]+$/.test(keyCandidate)) { + // If original had adb shell prefix, treat as shell command unless clearly a keycode intent? No - "adb shell input keyevent KEYCODE_HOME" is shell. + // But our stripped shell would be "input keyevent KEYCODE_HOME" (has spaces) so won't hit here. Good. + return { type: 'key', value: keyCandidate, label: keyCandidate }; + } + + return { type: 'shell', value: shell, label: shell }; + } + async function runBatch() { const device = getSelectedDevice(); if (!device) { @@ -485,10 +513,7 @@ return; } const lines = String(els.batchInput.value || '').split(/\r?\n/); - const commands = lines - .map(l => l.trim()) - .filter(l => l && !l.startsWith('#')) - .map(normalizeKeycode); + const commands = lines.map(parseBatchLine).filter(Boolean); if (!commands.length) { setAlert(els.commandAlert, 'Enter at least one command (one per line).', 'warning'); @@ -501,23 +526,25 @@ let failCount = 0; for (let i = 0; i < commands.length; i++) { - const key = commands[i]; - els.scanMeta.textContent = `Batch: ${i + 1}/${commands.length} ${key}`; + const cmd = commands[i]; + els.scanMeta.textContent = `Batch: ${i + 1}/${commands.length} ${cmd.label}`; try { - const data = await api('command', { ip: device.ip, command: 'key', keycode: key }, 'POST'); - if (data.ok) { - okCount++; - log(`BATCH ${i + 1}/${commands.length} ${key} -> ${device.ip}: ok`); + let data; + if (cmd.type === 'key') { + data = await api('command', { ip: device.ip, command: 'key', keycode: cmd.value }, 'POST'); } else { - failCount++; - log(`BATCH ${i + 1}/${commands.length} ${key} -> ${device.ip}: ${data.error || data.output || 'failed'}`); + data = await api('command', { ip: device.ip, command: 'shell', shell: cmd.value }, 'POST'); } + const ok = !!data.ok; + if (ok) okCount++; else failCount++; + const detail = data.output ? ` | ${String(data.output).slice(0, 200)}` : ''; + log(`BATCH ${i + 1}/${commands.length} [${cmd.type}] ${cmd.label} -> ${device.ip}: ${ok ? 'ok' : (data.error || 'failed')}${detail}`); } catch (e) { failCount++; - log(`BATCH ${i + 1}/${commands.length} ${key} -> ${device.ip}: ${e.message}`); + log(`BATCH ${i + 1}/${commands.length} [${cmd.type}] ${cmd.label} -> ${device.ip}: ${e.message}`); } if (i < commands.length - 1) { - await new Promise(r => setTimeout(r, 250)); + await new Promise(r => setTimeout(r, 300)); } } diff --git a/index.php b/index.php index c353fe0..59626e5 100644 --- a/index.php +++ b/index.php @@ -136,8 +136,8 @@ declare(strict_types=1);
Batch Commands
Runs keycodes in order, one per line. Lines starting with # and empty lines are ignored. Only whitelisted keycodes are accepted.
Runs commands in order. Supports keycodes (KEYCODE_HOME), shell commands (pm ...), and full adb shell ... lines. Lines starting with # and inline # comments are ignored.