File size: 3,633 Bytes
86b6928 e5bd85e bdbf92c 86b6928 e5bd85e 86b6928 b27d3ba e5bd85e b27d3ba e5bd85e b27d3ba e5bd85e b27d3ba 86b6928 e5bd85e 86b6928 b27d3ba 86b6928 b27d3ba 86b6928 b27d3ba 86b6928 b27d3ba e5bd85e b27d3ba e5bd85e b27d3ba 0ec2ae9 e5bd85e 0ec2ae9 e5bd85e 0ec2ae9 e5bd85e 86b6928 e5bd85e 86b6928 e5bd85e 86b6928 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
import fs from 'fs';
import path from 'path';
import { info, error } from './logger.js';
/**
* 创建人类可读的时间戳
* @returns {string} 格式化的时间戳 YYYY-MM-DD_HH-MM-SS
*/
export function getHumanReadableTimestamp() {
const now = new Date();
const year = now.getFullYear();
const month = String(now.getMonth() + 1).padStart(2, '0');
const day = String(now.getDate()).padStart(2, '0');
const hours = String(now.getHours()).padStart(2, '0');
const minutes = String(now.getMinutes()).padStart(2, '0');
const seconds = String(now.getSeconds()).padStart(2, '0');
return `${year}-${month}-${day}_${hours}-${minutes}-${seconds}`;
}
/**
* 确保截图目录存在
* @param {string} dir - 目录路径
*/
export function ensureScreenshotDirectory(dir) {
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
info(`创建截图目录: ${dir}`);
}
}
/**
* 检查 Cookie 是否可用(环境变量或文件)
* @param {string} cookieFile - Cookie 文件路径
* @param {string} cookiesFromEnv - 环境变量中的cookies
* @returns {boolean} Cookie是否可用
*/
export function checkCookieAvailability(cookieFile, cookiesFromEnv) {
// 优先检查环境变量
if (cookiesFromEnv) {
info('发现环境变量COOKIES,将使用环境变量中的cookies');
try {
JSON.parse(cookiesFromEnv);
return true;
} catch (err) {
error('环境变量COOKIES格式无效:', err.message);
info('将尝试使用cookie文件...');
}
}
// 检查cookie文件
if (!fs.existsSync(cookieFile)) {
error(`Cookie文件不存在: ${cookieFile}`);
info('请先运行 npm run login 进行登录,或设置环境变量COOKIES');
return false;
}
return true;
}
/**
* 检查 Cookie 文件是否存在(保持向后兼容)
* @param {string} cookieFile - Cookie 文件路径
* @returns {boolean} 文件是否存在
*/
export function checkCookieFile(cookieFile) {
if (!fs.existsSync(cookieFile)) {
error(`Cookie文件不存在: ${cookieFile}`);
info('请先运行 npm run login 进行登录');
return false;
}
return true;
}
/**
* 读取并解析 Cookie(优先使用环境变量)
* @param {string} cookieFile - Cookie 文件路径
* @param {string} cookiesFromEnv - 环境变量中的cookies
* @returns {Array} Cookie 数组
*/
export function loadCookies(cookieFile, cookiesFromEnv) {
try {
// 优先使用环境变量
if (cookiesFromEnv) {
info('从环境变量COOKIES加载cookies...');
const cookies = JSON.parse(cookiesFromEnv);
info(`已从环境变量加载 ${cookies.length} 个cookies`);
return cookies;
}
// 使用文件
if (fs.existsSync(cookieFile)) {
info(`从文件加载cookies: ${cookieFile}`);
const cookies = JSON.parse(fs.readFileSync(cookieFile, 'utf8'));
info(`已从文件加载 ${cookies.length} 个cookies`);
return cookies;
}
return [];
} catch (err) {
error('读取 Cookie 失败:', err);
throw err;
}
}
/**
* 保存截图
* @param {Object} page - Playwright 页面对象
* @param {string} screenshotDir - 截图目录
* @param {string} prefix - 文件名前缀
* @returns {string} 截图文件路径
*/
export async function saveScreenshot(page, screenshotDir, prefix = 'screenshot') {
ensureScreenshotDirectory(screenshotDir);
const timestamp = getHumanReadableTimestamp();
const screenshotPath = path.join(screenshotDir, `${prefix}.png`);
await page.screenshot({ path: screenshotPath });
info(`截图已保存: ${screenshotPath}`);
return screenshotPath;
}
|