* feat: 添加 Chrome 浏览器支持和安装说明折叠 - 支持 Chrome 浏览器作为备选方案 - README 安装部分使用 details 标签折叠 - 优化浏览器配置和初始化逻辑 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> * docs: 更新 README.md 以包含 Windows 问题和登录部分说明 - 添加 Windows 问题的链接以供参考 - 更新 README 中的登录部分内容 --------- Co-authored-by: Claude <noreply@anthropic.com>
47 lines
1.0 KiB
Go
47 lines
1.0 KiB
Go
package browser
|
|
|
|
import (
|
|
"github.com/sirupsen/logrus"
|
|
"github.com/xpzouying/headless_browser"
|
|
"github.com/xpzouying/xiaohongshu-mcp/cookies"
|
|
)
|
|
|
|
type browserConfig struct {
|
|
binPath string
|
|
}
|
|
|
|
type Option func(*browserConfig)
|
|
|
|
func WithBinPath(binPath string) Option {
|
|
return func(c *browserConfig) {
|
|
c.binPath = binPath
|
|
}
|
|
}
|
|
|
|
func NewBrowser(headless bool, options ...Option) *headless_browser.Browser {
|
|
cfg := &browserConfig{}
|
|
for _, opt := range options {
|
|
opt(cfg)
|
|
}
|
|
|
|
opts := []headless_browser.Option{
|
|
headless_browser.WithHeadless(headless),
|
|
}
|
|
if cfg.binPath != "" {
|
|
opts = append(opts, headless_browser.WithChromeBinPath(cfg.binPath))
|
|
}
|
|
|
|
// 加载 cookies
|
|
cookiePath := cookies.GetCookiesFilePath()
|
|
cookieLoader := cookies.NewLoadCookie(cookiePath)
|
|
|
|
if data, err := cookieLoader.LoadCookies(); err == nil {
|
|
opts = append(opts, headless_browser.WithCookies(string(data)))
|
|
logrus.Debugf("loaded cookies from filesuccessfully")
|
|
} else {
|
|
logrus.Warnf("failed to load cookies: %v", err)
|
|
}
|
|
|
|
return headless_browser.New(opts...)
|
|
}
|