This commit is contained in:
张成
2026-03-18 15:46:57 +08:00
parent 37e39d35b8
commit 3d3b9b5dfa
12 changed files with 175 additions and 63 deletions

View File

@@ -1 +0,0 @@
// 已废弃:按需求改为写死定时任务(见 config/cron_tasks.js

View File

@@ -1,22 +1,18 @@
import dotenv from 'dotenv';
import fs from 'node:fs';
import path from 'node:path';
import puppeteer from 'puppeteer';
dotenv.config();
import { get_app_config } from '../config/app_config.js';
let browser_singleton = null;
function get_action_timeout_ms() {
return Number(process.env.ACTION_TIMEOUT_MS || 300000);
const cfg = get_app_config();
return cfg.crawler.action_timeout_ms;
}
function get_crx_src_path() {
const crx_src_path = process.env.CRX_SRC_PATH;
if (!crx_src_path) {
throw new Error('缺少环境变量 CRX_SRC_PATH');
}
return crx_src_path;
const cfg = get_app_config();
return cfg.crawler.crx_src_path;
}
function get_extension_id_from_targets(targets) {
@@ -52,15 +48,8 @@ async function wait_for_extension_id(browser, timeout_ms) {
}
function get_chrome_executable_path() {
// 优先环境变量,方便你后续切换版本
const from_env = process.env.CHROME_EXECUTABLE_PATH;
if (from_env) {
return path.resolve(from_env);
}
// 默认使用项目根目录的 chrome-win/chrome.exe
// 当前进程 cwd 通常是 server/,所以回到上一级
return path.resolve(process.cwd(), '../chrome-win/chrome.exe');
const cfg = get_app_config();
return path.resolve(cfg.crawler.chrome_executable_path);
}
export async function get_or_create_browser() {
@@ -79,8 +68,9 @@ export async function get_or_create_browser() {
throw new Error(`扩展 manifest.json 不存在: ${manifest_path}`);
}
const cfg = get_app_config();
const extension_path = raw_extension_path.replace(/\\/g, '/');
const headless = String(process.env.PUPPETEER_HEADLESS || 'false') === 'true';
const headless = cfg.crawler.puppeteer_headless;
const user_data_dir = path.resolve(process.cwd(), 'puppeteer_profile');
browser_singleton = await puppeteer.launch({