This commit is contained in:
dengqichen 2025-11-19 15:53:06 +08:00
parent beca781934
commit 26988d3de8
4 changed files with 41 additions and 29 deletions

View File

@ -1,5 +1,9 @@
/** /**
* Logger - 统一日志工具 * Logger - 统一日志工具
*
* 修复 Windows 乱码问题
* - 统一使用 console.log (输出到 stdout)
* - 避免 console.error/warn (输出到 stderrWindows 下编码不同)
*/ */
const logger = { const logger = {
@ -19,16 +23,18 @@ const logger = {
/** /**
* 错误日志 * 错误日志
* 注意统一使用 console.log 而非 console.error避免 Windows stderr 编码问题
*/ */
error(tool, message) { error(tool, message) {
console.error(`[${tool}] ✗ ERROR: ${message}`); console.log(`[${tool}] ✗ ERROR: ${message}`);
}, },
/** /**
* 警告日志 * 警告日志
* 注意统一使用 console.log 而非 console.warn避免 Windows stderr 编码问题
*/ */
warn(tool, message) { warn(tool, message) {
console.warn(`[${tool}] ⚠ WARNING: ${message}`); console.log(`[${tool}] ⚠ WARNING: ${message}`);
}, },
/** /**

View File

@ -200,30 +200,30 @@ class ClickAction extends BaseAction {
const targetX = box.x + box.width / 2; const targetX = box.x + box.width / 2;
const targetY = box.y + box.height / 2; const targetY = box.y + box.height / 2;
// 第一段移动:先移动到附近(模拟人眼定位) // 第一段移动:先移动到附近(模拟人眼定位)- 更慢
const nearX = targetX + this.randomInt(-50, 50); const nearX = targetX + this.randomInt(-50, 50);
const nearY = targetY + this.randomInt(-50, 50); const nearY = targetY + this.randomInt(-50, 50);
const steps1 = this.randomInt(10, 20); const steps1 = this.randomInt(15, 30); // 增加步数,移动更慢
this.log('debug', `移动鼠标到附近: (${nearX.toFixed(0)}, ${nearY.toFixed(0)})`); this.log('debug', `移动鼠标到附近: (${nearX.toFixed(0)}, ${nearY.toFixed(0)})`);
await this.page.mouse.move(nearX, nearY, { steps: Math.floor(steps1 / 2) }); await this.page.mouse.move(nearX, nearY, { steps: steps1 });
await this.randomDelay(50, 150); await this.randomDelay(150, 400); // 增加延迟
// 第二段移动:移动到目标位置 // 第二段移动:移动到目标位置 - 更慢
this.log('debug', `移动鼠标到目标: (${targetX.toFixed(0)}, ${targetY.toFixed(0)})`); this.log('debug', `移动鼠标到目标: (${targetX.toFixed(0)}, ${targetY.toFixed(0)})`);
await this.page.mouse.move(targetX, targetY, { steps: Math.floor(steps1 / 2) }); await this.page.mouse.move(targetX, targetY, { steps: this.randomInt(10, 20) });
// 短暂停顿(模拟人类反应 // 短暂停顿(模拟人类反应和确认- 增加延迟
await this.randomDelay(50, 200); await this.randomDelay(200, 500);
// 点击(使用 down + up而不是 click // 点击(使用 down + up而不是 click
this.log('debug', '执行点击 (mouse down + up)...'); this.log('debug', '执行点击 (mouse down + up)...');
await this.page.mouse.down(); await this.page.mouse.down();
await this.randomDelay(50, 120); await this.randomDelay(80, 180); // 增加按压时间
await this.page.mouse.up(); await this.page.mouse.up();
// 点击后延迟(参考旧框架) // 点击后延迟(等待页面响应)- 增加延迟
await this.randomDelay(1000, 2000); await this.randomDelay(1200, 2500);
this.log('info', '✓ 人类行为点击完成'); this.log('info', '✓ 人类行为点击完成');

View File

@ -133,19 +133,25 @@ class FillFormAction extends BaseAction {
} }
/** /**
* 模拟人类输入 * 模拟人类输入更慢更真实
*/ */
async typeHumanLike(element, text) { async typeHumanLike(element, text) {
for (const char of text) { for (let i = 0; i < text.length; i++) {
const char = text[i];
// 每个字符延迟 100-250ms更慢
await element.type(char, { await element.type(char, {
delay: Math.random() * 100 + 50 // 50-150ms 随机延迟 delay: Math.random() * 150 + 100
}); });
// 每输入3-5个字符随机停顿一下模拟思考或调整手指
if (i > 0 && i % (Math.floor(Math.random() * 3) + 3) === 0) {
await new Promise(resolve => setTimeout(resolve, Math.random() * 800 + 300));
}
} }
// 随机暂停 // 输入完成后,短暂停顿(模拟检查输入)
if (Math.random() > 0.7) { await new Promise(resolve => setTimeout(resolve, Math.random() * 500 + 300));
await new Promise(resolve => setTimeout(resolve, Math.random() * 500 + 200));
}
} }
/** /**

View File

@ -119,7 +119,7 @@ class BaseAction {
} }
/** /**
* 人类行为延迟工具方法 * 人类行为延迟工具方法模拟真实用户操作节奏
*/ */
// 随机延迟 // 随机延迟
@ -128,24 +128,24 @@ class BaseAction {
await new Promise(resolve => setTimeout(resolve, delay)); await new Promise(resolve => setTimeout(resolve, delay));
} }
// 阅读页面延迟(1-3秒 // 阅读页面延迟(2-5秒- 模拟用户查看页面内容
async readPageDelay() { async readPageDelay() {
await this.randomDelay(1000, 3000); await this.randomDelay(2000, 5000);
} }
// 思考延迟(500-1500ms // 思考延迟(1-2.5秒)- 模拟填写表单后的思考
async thinkDelay() { async thinkDelay() {
await this.randomDelay(500, 1500); await this.randomDelay(1000, 2500);
} }
// 短暂停顿(200-500ms // 短暂停顿(300-800ms- 模拟操作间的自然停顿
async pauseDelay() { async pauseDelay() {
await this.randomDelay(200, 500); await this.randomDelay(300, 800);
} }
// 步骤间延迟1-2秒 // 步骤间延迟1.5-3秒- 模拟步骤之间的过渡
async stepDelay() { async stepDelay() {
await this.randomDelay(1000, 2000); await this.randomDelay(1500, 3000);
} }
} }