修改启动Chrome时的配置

This commit is contained in:
Star 2025-07-24 19:23:38 +08:00
parent cc7573428b
commit 11f2454fca
3 changed files with 40 additions and 15 deletions

View File

@ -62,6 +62,13 @@ type ChromeInfo struct {
ChromePath string ChromePath string
} }
type ChromeOption struct {
ChromeURL string
ChromeOption []string
ShowWindow bool
ChromeHtpProxy string
}
func (ch *Chrome) Info(showWindow *bool, vm *goja.Runtime) (ChromeInfo, error) { func (ch *Chrome) Info(showWindow *bool, vm *goja.Runtime) (ChromeInfo, error) {
ver, err := ch.browser.Version() ver, err := ch.browser.Version()
if err != nil { if err != nil {
@ -81,13 +88,25 @@ func (ch *Chrome) Info(showWindow *bool, vm *goja.Runtime) (ChromeInfo, error) {
}, nil }, nil
} }
func StartChrome(showWindow *bool, vm *goja.Runtime) (*Chrome, error) { func StartChrome(opt *ChromeOption, vm *goja.Runtime) (*Chrome, error) {
if opt == nil {
opt = &ChromeOption{}
}
if opt.ChromeURL == "" {
opt.ChromeURL = conf.ChromeURL
}
if opt.ChromeHtpProxy == "" {
opt.ChromeHtpProxy = conf.ChromeHtpProxy
}
if opt.ChromeOption == nil {
opt.ChromeOption = conf.ChromeOption
}
// logger := gojs.GetLogger(vm) // logger := gojs.GetLogger(vm)
ch := &Chrome{} ch := &Chrome{}
ch.id = u.UniqueId() ch.id = u.UniqueId()
ch.browser = rod.New() ch.browser = rod.New()
ch.chromeURL = conf.ChromeURL ch.chromeURL = opt.ChromeURL
if conf.ChromeURL == "" { if opt.ChromeURL == "" {
// 使用本地Chrome // 使用本地Chrome
ch.launcher = launcher.New() ch.launcher = launcher.New()
if localBrowserPath, hasLocalBrowser := launcher.LookPath(); hasLocalBrowser { if localBrowserPath, hasLocalBrowser := launcher.LookPath(); hasLocalBrowser {
@ -95,20 +114,25 @@ func StartChrome(showWindow *bool, vm *goja.Runtime) (*Chrome, error) {
ch.chromePath = localBrowserPath ch.chromePath = localBrowserPath
} }
// "--headless=new", "--no-sandbox", "--disable-dev-shm-usage", "--hide-scrollbars", "--font-render-hinting=none", "--disable-blink-features=AutomationControlled", "--disable-infobars", "--lang=zh-CN,zh", "--disable-extensions", "--disable-gpu", "--use-gl=swiftshader", "--ignore-gpu-blocklist", "--use-angle=swiftshader", "--disable-features=Translate" // "--headless=new", "--no-sandbox", "--disable-dev-shm-usage", "--hide-scrollbars", "--font-render-hinting=none", "--disable-blink-features=AutomationControlled", "--disable-infobars", "--lang=zh-CN,zh", "--disable-extensions", "--disable-gpu", "--use-gl=swiftshader", "--ignore-gpu-blocklist", "--use-angle=swiftshader", "--disable-features=Translate"
ch.launcher.Headless(!u.Bool(showWindow)).Set("disable-dev-shm-usage").Set("single-process").Set("disable-blink-features", "AutomationControlled") ch.launcher.Headless(!opt.ShowWindow).Set("disable-dev-shm-usage").Set("single-process").Set("disable-blink-features", "AutomationControlled")
ch.launcher.Set("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36 Edg/125.0.0.0") ch.launcher.Set("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36 Edg/125.0.0.0")
if opt.ChromeHtpProxy != "" {
ch.launcher.Proxy(opt.ChromeHtpProxy)
}
switch runtime.GOOS { switch runtime.GOOS {
case "linux": case "linux":
ch.launcher.Set("disable-setuid-sandbox") ch.launcher.Set("disable-setuid-sandbox")
case "windows": case "windows":
ch.launcher.Set("disable-features=RendererCodeIntegrity") ch.launcher.Set("disable-features=RendererCodeIntegrity")
} }
for _, opt := range conf.ChromeOption { if opt.ChromeOption != nil {
a := u.SplitTrimN(opt, "=", 2) for _, opt := range opt.ChromeOption {
if len(a) == 2 { a := u.SplitTrimN(opt, "=", 2)
ch.launcher.Set(flags.Flag(a[0]), a[1]) if len(a) == 2 {
} else { ch.launcher.Set(flags.Flag(a[0]), a[1])
ch.launcher.Set(flags.Flag(opt)) } else {
ch.launcher.Set(flags.Flag(opt))
}
} }
} }
if localChromeURL, err := ch.launcher.Launch(); err != nil { if localChromeURL, err := ch.launcher.Launch(); err != nil {

4
go.mod
View File

@ -3,7 +3,7 @@ module apigo.cc/gojs/http
go 1.23.0 go 1.23.0
require ( require (
apigo.cc/gojs v0.0.22 apigo.cc/gojs v0.0.23
apigo.cc/gojs/console v0.0.2 apigo.cc/gojs/console v0.0.2
apigo.cc/gojs/file v0.0.4 apigo.cc/gojs/file v0.0.4
apigo.cc/gojs/util v0.0.12 apigo.cc/gojs/util v0.0.12
@ -11,7 +11,7 @@ require (
github.com/gorilla/websocket v1.5.3 github.com/gorilla/websocket v1.5.3
github.com/ssgo/config v1.7.9 github.com/ssgo/config v1.7.9
github.com/ssgo/httpclient v1.7.8 github.com/ssgo/httpclient v1.7.8
github.com/ssgo/log v1.7.7 github.com/ssgo/log v1.7.9
github.com/ssgo/s v1.7.24 github.com/ssgo/s v1.7.24
github.com/ssgo/u v1.7.21 github.com/ssgo/u v1.7.21
) )

View File

@ -33,9 +33,10 @@ var defaultHttp = &Http{
} }
var conf = struct { var conf = struct {
Timeout int Timeout int
ChromeURL string ChromeURL string
ChromeOption []string ChromeOption []string
ChromeHtpProxy string
}{} }{}
func init() { func init() {