feat(playwright): 添加Cookie拦截示例和登录示例功能新增PlaywrightService服务,提供Cookie拦截与手动登录示例。新增DesktopApplication桌面应用入口,集成JavaFX界面。添加相关依赖:Playwright、JavaFX、Spring Boot等。

配置Maven插件支持JavaFX应用打包。
完善README文档,说明项目结构与运行方式。
This commit is contained in:
wangchunxiang
2025-10-11 15:31:24 +08:00
parent 7e11a6fcf8
commit c1e6dc67b1
16 changed files with 662 additions and 1 deletions

View File

@@ -0,0 +1,95 @@
package com.fantaibao;
import com.fantaibao.service.PlaywrightService;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
//@SpringBootApplication
public class DesktopApplication extends Application {
private TextArea textArea;
private ConfigurableApplicationContext applicationContext;
private PlaywrightService playwrightService;
public static void main(String[] args) {
// 设置JavaFX系统属性
System.setProperty("javafx.application.platform", "egl");
SpringApplication.run(DesktopApplication.class, args);
launch(args);
}
@Override
public void init() throws Exception {
super.init();
applicationContext = (ConfigurableApplicationContext) SpringApplication.run(DesktopApplication.class);
//playwrightService = applicationContext.getBean(PlaywrightService.class);
playwrightService = new PlaywrightService();
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("饭太煲爬虫桌面程序");
textArea = new TextArea();
textArea.setPrefRowCount(20);
textArea.setPrefColumnCount(50);
textArea.setEditable(false);
Button playwrightButton = new Button("运行Cookie拦截示例");
playwrightButton.setOnAction(e -> runPlaywrightCookieExample());
Button loginButton = new Button("运行登录示例");
loginButton.setOnAction(e -> runLoginExample());
VBox vBox = new VBox(10, playwrightButton, loginButton, textArea);
vBox.setPadding(new Insets(10));
Scene scene = new Scene(vBox, 800, 600);
primaryStage.setScene(scene);
primaryStage.setOnCloseRequest(e -> {
if (applicationContext != null) {
applicationContext.close();
}
Platform.exit();
});
primaryStage.show();
}
private void runPlaywrightCookieExample() {
appendText("开始运行Playwright Cookie拦截示例...\n");
Thread thread = new Thread(() ->
playwrightService.interceptCookieExample(this::appendText)
);
thread.setDaemon(true);
thread.start();
}
private void runLoginExample() {
appendText("开始运行登录示例...\n");
Thread thread = new Thread(() ->
playwrightService.loginExample(this::appendText)
);
thread.setDaemon(true);
thread.start();
}
private void appendText(String text) {
Platform.runLater(() -> textArea.appendText(text + "\n"));
}
@Override
public void stop() {
if (applicationContext != null) {
applicationContext.close();
}
}
}