fix: 修复带换行符的提示词触发回车导致提前发送的问题 (closes #2)

This commit is contained in:
foxhui
2025-12-07 01:53:53 +08:00
Unverified
parent a25fbb3626
commit c5e9ff17da
+20
View File
@@ -169,6 +169,26 @@ export async function humanType(page, target, text) {
// 短文本:保持拟人化逐字输入
for (let i = 0; i < text.length; i++) {
const char = text[i];
const nextChar = text[i + 1];
// 处理换行符 (避免触发发送)
if (char === '\r' && nextChar === '\n') {
// Windows 换行符 (\r\n)
await page.keyboard.down('Shift');
await page.keyboard.press('Enter');
await page.keyboard.up('Shift');
i++; // 跳过 \n
await sleep(30, 100);
continue;
} else if (char === '\n' || char === '\r') {
// Unix/Mac 换行符 (\n 或 \r)
await page.keyboard.down('Shift');
await page.keyboard.press('Enter');
await page.keyboard.up('Shift');
await sleep(30, 100);
continue;
}
// 模拟错字 (5% 概率)
if (Math.random() < 0.05) {
await page.keyboard.type('x', { delay: random(50, 150) });