From 55e1df0789bcac53df49db3fe32673b077c43da9 Mon Sep 17 00:00:00 2001 From: wangchunxiang Date: Fri, 24 Oct 2025 16:42:46 +0800 Subject: [PATCH] =?UTF-8?q?chore(config):=20=E6=9B=B4=E6=96=B0Playwright?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E4=BB=A5=E6=94=AF=E6=8C=81=E8=87=AA=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=E6=B5=8F=E8=A7=88=E5=99=A8=E8=B7=AF=E5=BE=84=20-=20?= =?UTF-8?q?=E4=BF=AE=E6=94=B9PlaywrightManager=E4=BB=A5=E8=AF=BB=E5=8F=96P?= =?UTF-8?q?LAYWRIGHT=5FBROWSERS=5FPATH=E7=8E=AF=E5=A2=83=E5=8F=98=E9=87=8F?= =?UTF-8?q?=20-=20=E5=BD=93=E7=8E=AF=E5=A2=83=E5=8F=98=E9=87=8F=E6=9C=AA?= =?UTF-8?q?=E8=AE=BE=E7=BD=AE=E6=97=B6=E4=BD=BF=E7=94=A8=E9=BB=98=E8=AE=A4?= =?UTF-8?q?=E8=B7=AF=E5=BE=84=E5=B9=B6=E8=AE=B0=E5=BD=95=E6=97=A5=E5=BF=97?= =?UTF-8?q?=20-=20=E6=B7=BB=E5=8A=A0=E8=B7=B3=E8=BF=87=E6=B5=8F=E8=A7=88?= =?UTF-8?q?=E5=99=A8=E4=B8=8B=E8=BD=BD=E7=9A=84=E7=8E=AF=E5=A2=83=E5=8F=98?= =?UTF-8?q?=E9=87=8F=E9=85=8D=E7=BD=AE=20-=20=E4=BD=BF=E7=94=A8CreateOptio?= =?UTF-8?q?ns=E8=AE=BE=E7=BD=AE=E8=87=AA=E5=AE=9A=E4=B9=89=E7=8E=AF?= =?UTF-8?q?=E5=A2=83=E5=8F=98=E9=87=8F-=20=E5=BC=95=E5=85=A5hutool?= =?UTF-8?q?=E5=B7=A5=E5=85=B7=E7=B1=BB=E8=BF=9B=E8=A1=8C=E5=AD=97=E7=AC=A6?= =?UTF-8?q?=E4=B8=B2=E5=88=A4=E6=96=AD-=20=E6=B7=BB=E5=8A=A0=E5=BF=85?= =?UTF-8?q?=E8=A6=81=E7=9A=84=E5=AF=BC=E5=85=A5=E8=AF=AD=E5=8F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/compiler.xml | 2 +- .idea/git_toolbox_blame.xml | 6 +++++- .../com/fantaibao/config/PlaywrightManager.java | 17 ++++++++++++++++- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/.idea/compiler.xml b/.idea/compiler.xml index 2b262f4..8f158e3 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -13,7 +13,7 @@ - + diff --git a/.idea/git_toolbox_blame.xml b/.idea/git_toolbox_blame.xml index 7dc1249..f165acd 100644 --- a/.idea/git_toolbox_blame.xml +++ b/.idea/git_toolbox_blame.xml @@ -1,6 +1,10 @@ - \ No newline at end of file diff --git a/src/main/java/com/fantaibao/config/PlaywrightManager.java b/src/main/java/com/fantaibao/config/PlaywrightManager.java index bd62c69..4885fbf 100644 --- a/src/main/java/com/fantaibao/config/PlaywrightManager.java +++ b/src/main/java/com/fantaibao/config/PlaywrightManager.java @@ -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 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);