修复撇号字符数组转义顺序导致字符串未闭合

This commit is contained in:
yu 2026-11-04 09:28:46 +08:00
parent bb03699cce
commit 143fcae34a
2 changed files with 5 additions and 4 deletions

View File

@ -159,9 +159,10 @@ const keyboardSend: CapabilityHandler = async (_ctx, node, args) => {
// 剪贴板粘贴:原文进剪贴板 + Ctrl+V,不做 SendKeys 转义
send = `Set-Clipboard -Value ${psString(keys)}; Start-Sleep -Milliseconds 120; [System.Windows.Forms.SendKeys]::SendWait('^v')`;
} else if (mode === "1" && isTextSend) {
// 人工模拟:逐字符;JS 侧把每个字符转义成 SendKeys 字面量(含单引号用 '' 转义),PS 侧只做数组循环
const escapedChars = Array.from(keys).map((ch) => sendKeysEscape(ch).replace(/'/g, "''"));
const arr = escapedChars.map((c) => `'${c}'`).join(",");
// 人工模拟:逐字符;JS 侧把每个字符转义成 SendKeys 字面量,PS 侧只做数组循环
// 数组元素必须用 psString 统一包裹转义(先包 '' 再转义内部引号,顺序反了会产生 ''' 未闭合字符串)
const escapedChars = Array.from(keys).map((ch) => sendKeysEscape(ch));
const arr = escapedChars.map((c) => psString(c)).join(",");
send = `$chars = @(${arr}); foreach ($c in $chars) { [System.Windows.Forms.SendKeys]::SendWait($c); Start-Sleep -Milliseconds ${intervalMs} }`;
} else {
// 后台接口 / 按键组合 / 人工模拟(按键组合组件):整段发送,文本特殊字符转义

View File

@ -60,7 +60,7 @@ describe("键盘输入组件真实冒烟", () => {
// Win11 记事本窗口标题是「无标题 - Notepad」; 沙盒内 Start-Process notepad 别名解析失败, 用完整路径
await ps("Start-Process $env:SystemRoot\\System32\\notepad.exe; $diag=''; $ok=$false; foreach($i in 1..24){ $procs=Get-Process Notepad -ErrorAction SilentlyContinue; if($procs){ $diag=\"procs:$($procs.Count) titles:$(($procs|ForEach-Object{$_.MainWindowTitle}) -join '|')\"; $p=Get-Process | Where-Object {$_.MainWindowTitle -like '*Notepad*'}; if($p){$ok=$true; break} } else { $diag='no-process' }; Start-Sleep -Milliseconds 500 }; if(-not $ok){ throw \"notepad not visible: $diag\" }");
try {
await invoke("Keyboard_Send_V1", { words: "目标窗口输入", interval: "5", fuctype: "0", window_title: "Notepad" });
await invoke("Keyboard_Send_V1", { words: "目标窗口输入'测试", interval: "5", fuctype: "1", window_title: "Notepad" });
await new Promise((r) => setTimeout(r, 300));
// activateWindowScript 内部已校验前台句柄=目标窗口, 这里再确认记事本进程窗口标题匹配
const fg = await ps("[Console]::OutputEncoding=[System.Text.UTF8Encoding]::new($false); (Get-Process Notepad | Where-Object { $_.MainWindowTitle } | Select-Object -First 1).MainWindowTitle");