chore(config): 更新Playwright配置以支持自定义浏览器路径

- 修改PlaywrightManager以读取PLAYWRIGHT_BROWSERS_PATH环境变量
- 当环境变量未设置时使用默认路径并记录日志
- 添加跳过浏览器下载的环境变量配置
- 使用CreateOptions设置自定义环境变量- 引入hutool工具类进行字符串判断- 添加必要的导入语句
This commit is contained in:
2025-10-24 16:42:46 +08:00
parent 763444b9e8
commit 55e1df0789
3 changed files with 22 additions and 3 deletions

2
.idea/compiler.xml generated
View File

@@ -13,7 +13,7 @@
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<processorPath useClasspath="false">
<entry name="$PROJECT_DIR$/../../maven_repository/org/projectlombok/lombok/1.18.30/lombok-1.18.30.jar" />
<entry name="D:/maven_repository/org/projectlombok/lombok/1.18.30/lombok-1.18.30.jar" />
</processorPath>
<module name="fantaibao-crawler-desktop" />
</profile>

View File

@@ -1,6 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="GitToolBoxBlameSettings">
<option name="version" value="2" />
<option name="showEditorInlineBlameOverride">
<BoolValueOverride>
<option name="enabled" value="true" />
</BoolValueOverride>
</option>
</component>
</project>

View File

@@ -1,8 +1,12 @@
package com.fantaibao.config;
import cn.hutool.core.util.StrUtil;
import com.microsoft.playwright.Playwright;
import lombok.extern.slf4j.Slf4j;
import java.util.HashMap;
import java.util.Map;
/**
* Playwright单例管理类
* 用于全局管理和提供Playwright实例
@@ -18,7 +22,18 @@ public class PlaywrightManager {
throw new RuntimeException("请使用getInstance()方法获取实例");
}
try {
this.playwright = Playwright.create();
String playwrightBrowsersPath = System.getenv("PLAYWRIGHT_BROWSERS_PATH");
if (StrUtil.isBlank(playwrightBrowsersPath)) {
log.info("PLAYWRIGHT_BROWSERS_PATH环境变量未设置将使用默认路径");
playwright = Playwright.create();
return;
}
Map<String, String> env = new HashMap<>();
env.put("PLAYWRIGHT_BROWSERS_PATH",playwrightBrowsersPath);
env.put("PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD","1");
Playwright.CreateOptions options = new Playwright.CreateOptions();
options.setEnv(env);
this.playwright = Playwright.create(options);
} catch (Exception e) {
log.error("创建Playwright实例失败", e);
throw new RuntimeException("创建Playwright实例失败", e);