我想用Puppeteer 进行一个游戏的自动签到,我要怎么做?
- User
我想用Puppeteer 进行一个游戏的自动签到,我要怎么做?
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
我想用Puppeteer 进行一个游戏的自动签到,我要怎么做?
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
使用Puppeteer进行游戏自动签到需要以下步骤:
npm install puppeteer
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch({
headless: false, // 调试时设为false,部署时可设为true
args: ['--no-sandbox', '--disable-setuid-sandbox']
});
try {
const page = await browser.newPage();
// 在这里添加你的自动化逻辑
} catch (error) {
console.error('发生错误:', error);
} finally {
await browser.close();
}
})();
// 导航到登录页面
await page.goto('https://game.example.com/login', { waitUntil: 'networkidle2' });
// 填写登录信息
await page.type('#username', '你的用户名');
await page.type('#password', '你的密码');
// 点击登录按钮
await page.click('#login-button');
// 等待登录完成
await page.waitForNavigation({ waitUntil: 'networkidle2' });
await page.goto('https://game.example.com/checkin', { waitUntil: 'networkidle2' });
// 等待签到按钮出现
await page.waitForSelector('#checkin-button', { visible: true });
// 点击签到按钮
await page.click('#checkin-button');
// 等待签到完成
await page.waitForSelector('.checkin-success', { visible: true });
如果游戏有验证码,你可以:
// 示例:等待用户手动输入验证码
console.log('请在浏览器中手动输入验证码...');
await page.waitForTimeout(30000); // 等待30秒
// 监听对话框事件
page.on('dialog', async dialog => {
console.log('对话框内容:', dialog.message());
await dialog.accept(); // 或dialog.dismiss()
});
// 保存cookie
const cookies = await page.cookies();
require('fs').writeFileSync('cookies.json', JSON.stringify(cookies));
// 加载cookie
const cookies = JSON.parse(require('fs').readFileSync('cookies.json'));
await page.setCookie(...cookies);
const puppeteer = require('puppeteer');
const fs = require('fs');
(async () => {
const browser = await puppeteer.launch({
headless: false,
args: ['--no-sandbox', '--disable-setuid-sandbox']
});
try {
const page = await browser.newPage();
// 设置视口大小
await page.setViewport({ width: 1280, height: 800 });
// 尝试加载cookie
if (fs.existsSync('cookies.json')) {
const cookies = JSON.parse(fs.readFileSync('cookies.json'));
await page.setCookie(...cookies);
}
// 导航到签到页面
await page.goto('https://game.example.com/checkin', {
waitUntil: 'networkidle2',
timeout: 30000
});
// 检查是否已登录
const isLoggedIn = await page.evaluate(() => {
return document.querySelector('.user-profile') !== null;
});
if (!isLoggedIn) {
// 如果未登录,执行登录流程
await page.goto('https://game.example.com/login');
await page.type('#username', 'your_username');
await page.type('#password', 'your_password');
await page.click('#login-button');
await page.waitForNavigation({ waitUntil: 'networkidle2' });
}
// 执行签到
await page.waitForSelector('#checkin-button', { visible: true });
await page.click('#checkin-button');
// 等待签到成功
await page.waitForSelector('.checkin-success', { visible: true });
// 保存cookie
const cookies = await page.cookies();
fs.writeFileSync('cookies.json', JSON.stringify(cookies));
console.log('签到成功!');
} catch (error) {
console.error('签到失败:', error);
} finally {
await browser.close();
}
})();
希望这个指南能帮助你实现游戏自动签到功能!