refactor: 改进浏览器实例管理,按需创建浏览器实例 (#12)
- 移除全局浏览器单例,改为按需创建浏览器实例 - 添加 configs/browser.go 用于管理无头模式配置 - 更新 service.go 中所有方法,每次调用时创建新的浏览器实例 - 更新测试文件,使用新的浏览器管理方式 - 确保每个浏览器实例使用后正确关闭,避免资源泄漏 🤖 Generated with [Claude Code](https://claude.ai/code) Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -1,17 +1,12 @@
|
||||
package browser
|
||||
|
||||
import (
|
||||
"github.com/go-rod/rod"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/xpzouying/headless_browser"
|
||||
"github.com/xpzouying/xiaohongshu-mcp/cookies"
|
||||
)
|
||||
|
||||
var (
|
||||
browser *headless_browser.Browser
|
||||
)
|
||||
|
||||
func Init(headless bool) error {
|
||||
func NewBrowser(headless bool) *headless_browser.Browser {
|
||||
|
||||
opts := []headless_browser.Option{
|
||||
headless_browser.WithHeadless(headless),
|
||||
@@ -28,15 +23,5 @@ func Init(headless bool) error {
|
||||
logrus.Warnf("failed to load cookies: %v", err)
|
||||
}
|
||||
|
||||
browser = headless_browser.New(opts...)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewPage() *rod.Page {
|
||||
return browser.NewPage()
|
||||
}
|
||||
|
||||
func Close() {
|
||||
browser.Close()
|
||||
return headless_browser.New(opts...)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user