Files

General Auto - 浏览器自动化控制扩展

简介

General Auto 是一个强大的 Chrome 浏览器自动化控制扩展,专为自动化测试和浏览器控制设计。

功能特性

核心功能

  • 完整的页面元素操作(点击、输入、滚动等)
  • Cookie 管理(获取、设置、清除)
  • 请求拦截和日志记录XHR、Fetch
  • 模拟人类行为(随机延迟、自然输入)
  • 元素等待和检测
  • 自定义脚本执行
  • 任务队列管理

API 功能

Content Script API (window.GeneralAuto)

// 执行任务
await GeneralAuto.executeTask({
  type: 'click',
  selector: 'button#submit',
  timeout: 5000
});

// 执行自定义脚本
await GeneralAuto.executeScript('document.title');

// Cookie 管理
const cookies = await GeneralAuto.getCookies();
await GeneralAuto.setCookies([...]);
await GeneralAuto.clearCookies();

// 导航
await GeneralAuto.navigateTo('https://example.com');

// 获取请求日志
const logs = await GeneralAuto.getRequestLogs();

Injected Script API (window.AutomationUtils)

// 模拟人类输入
await AutomationUtils.humanInput(element, 'Hello World');

// 模拟人类点击
await AutomationUtils.humanClick(element);

// 等待元素
const el = await AutomationUtils.waitForElement('.selector', 5000);

// 等待元素消失
await AutomationUtils.waitForElementRemoved('.loading');

// 随机延迟
await AutomationUtils.randomDelay(100, 500);

// 监听请求
AutomationUtils.onRequest((data) => {
  console.log('Request:', data);
});

// 监听响应
AutomationUtils.onResponse((data) => {
  console.log('Response:', data);
});

任务类型

支持的任务类型

类型 说明 参数
click 点击元素 selector
input 输入文本 selector, value
humanInput 模拟人类输入 selector, value
getText 获取文本 selector
getAttribute 获取属性 selector, value(属性名)
scroll 滚动页面 value: {x, y}
scrollToElement 滚动到元素 selector
wait 等待指定时间 value(毫秒)
waitForElement 等待元素出现 selector
evaluate 执行 JavaScript value(代码)

任务示例

// 点击按钮
await GeneralAuto.executeTask({
  type: 'click',
  selector: 'button[data-testid="submit"]',
  timeout: 5000,
  waitVisible: true
});

// 输入文本
await GeneralAuto.executeTask({
  type: 'input',
  selector: 'input[name="username"]',
  value: 'myusername',
  timeout: 5000
});

// 模拟人类输入
await GeneralAuto.executeTask({
  type: 'humanInput',
  selector: 'textarea',
  value: 'This is a human-like typing',
  timeout: 10000
});

// 获取文本
const result = await GeneralAuto.executeTask({
  type: 'getText',
  selector: '.title'
});
console.log(result.text);

// 滚动
await GeneralAuto.executeTask({
  type: 'scroll',
  value: { x: 0, y: 500 }
});

// 等待元素
await GeneralAuto.executeTask({
  type: 'waitForElement',
  selector: '.dynamic-content',
  timeout: 10000
});

安装说明

1. 开发者模式安装

  1. 打开 Chrome 浏览器
  2. 访问 chrome://extensions/
  3. 开启右上角的"开发者模式"
  4. 点击"加载已解压的扩展程序"
  5. 选择 expand/general-auto 文件夹

2. 在 Rod/Chromedp 中使用

// 使用 Rod
browser := rod.New().
    ControlURL(launcher.New().
        Headless(false).
        Set("load-extension", "d:\\Project\\go-account-register\\expand\\general-auto").
        MustLaunch()).
    MustConnect()

使用示例

Go 代码中使用

// 在浏览器启动时加载扩展
func OpenBrowserWithExtension() *rod.Browser {
    extensionPath := "d:\\Project\\go-account-register\\expand\\general-auto"
    
    browser := rod.New().
        ControlURL(launcher.New().
            Headless(false).
            Set("load-extension", extensionPath).
            MustLaunch()).
        MustConnect()
    
    return browser
}

// 在页面中执行自动化任务
func AutomateTwitterPost(page *rod.Page, content string) error {
    // 等待扩展加载
    time.Sleep(2 * time.Second)
    
    // 执行任务
    _, err := page.Eval(`
        (async () => {
            // 等待输入框
            await GeneralAuto.executeTask({
                type: 'waitForElement',
                selector: '[data-testid="tweetTextarea_0"]',
                timeout: 10000
            });
            
            // 输入内容
            await GeneralAuto.executeTask({
                type: 'humanInput',
                selector: '[data-testid="tweetTextarea_0"]',
                value: ` + "`" + content + "`" + `,
                timeout: 15000
            });
            
            // 点击发布按钮
            await GeneralAuto.executeTask({
                type: 'click',
                selector: '[data-testid="tweetButtonInline"]',
                timeout: 5000
            });
            
            return true;
        })()
    `)
    
    return err
}

页面控制台中使用

// 1. 点击登录按钮
await GeneralAuto.executeTask({
  type: 'click',
  selector: 'button.login-btn'
});

// 2. 等待登录框出现
await GeneralAuto.executeTask({
  type: 'waitForElement',
  selector: '#login-modal'
});

// 3. 输入用户名
await GeneralAuto.executeTask({
  type: 'humanInput',
  selector: 'input[name="username"]',
  value: 'myusername'
});

// 4. 输入密码
await GeneralAuto.executeTask({
  type: 'humanInput',
  selector: 'input[name="password"]',
  value: 'mypassword'
});

// 5. 点击提交
await GeneralAuto.executeTask({
  type: 'click',
  selector: 'button[type="submit"]'
});

配置选项

请求拦截配置

// 控制请求拦截
window.GeneralAutoConfig = {
  interceptXHR: true,      // 拦截 XHR 请求
  interceptFetch: true,    // 拦截 Fetch 请求
  logRequests: true,       // 记录请求日志
  requestCallbacks: [],    // 请求回调
  responseCallbacks: []    // 响应回调
};

注意事项

  1. 扩展需要在浏览器启动时加载
  2. 某些网站可能检测自动化行为,建议使用人类模拟功能
  3. 请求日志会占用内存,定期清理
  4. 开发模式下扩展可能需要手动刷新

更新日志

v1.0.0 (2025-01-21)

  • 初始版本发布
  • 实现基础自动化功能
  • 添加 Cookie 管理
  • 添加请求拦截
  • 添加人类行为模拟

许可证

MIT License

支持

如有问题,请联系开发团队。