Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
11f2454fca | |||
cc7573428b |
85
chrome.go
85
chrome.go
@ -24,10 +24,10 @@ var chromes = map[string]*Chrome{}
|
|||||||
var chromesLock sync.Mutex
|
var chromesLock sync.Mutex
|
||||||
|
|
||||||
func (ch *Chrome) Close(vm *goja.Runtime) {
|
func (ch *Chrome) Close(vm *goja.Runtime) {
|
||||||
logger := gojs.GetLogger(vm)
|
// logger := gojs.GetLogger(vm)
|
||||||
if ch.browser != nil {
|
if ch.browser != nil {
|
||||||
ver, _ := ch.browser.Version()
|
// ver, _ := ch.browser.Version()
|
||||||
logger.Info("关闭Chrome浏览器", "id", ch.id, "browser", ver.Product, "userAgent", ver.UserAgent, "chromeURL", ch.chromeURL, "chromePath", ch.chromePath)
|
// logger.Info("关闭Chrome浏览器", "id", ch.id, "browser", ver.Product, "userAgent", ver.UserAgent, "chromeURL", ch.chromeURL, "chromePath", ch.chromePath)
|
||||||
ch.browser.Close()
|
ch.browser.Close()
|
||||||
ch.browser = nil
|
ch.browser = nil
|
||||||
}
|
}
|
||||||
@ -52,13 +52,61 @@ func CloseAllChrome(vm *goja.Runtime) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func StartChrome(showWindow *bool, vm *goja.Runtime) (*Chrome, error) {
|
type ChromeInfo struct {
|
||||||
logger := gojs.GetLogger(vm)
|
ProtocolVersion string
|
||||||
|
Product string
|
||||||
|
Revision string
|
||||||
|
UserAgent string
|
||||||
|
JsVersion string
|
||||||
|
ChromeURL 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) {
|
||||||
|
ver, err := ch.browser.Version()
|
||||||
|
if err != nil {
|
||||||
|
return ChromeInfo{
|
||||||
|
ChromeURL: ch.chromeURL,
|
||||||
|
ChromePath: ch.chromePath,
|
||||||
|
}, err
|
||||||
|
}
|
||||||
|
return ChromeInfo{
|
||||||
|
ProtocolVersion: ver.ProtocolVersion,
|
||||||
|
Product: ver.Product,
|
||||||
|
Revision: ver.Revision,
|
||||||
|
UserAgent: ver.UserAgent,
|
||||||
|
JsVersion: ver.JsVersion,
|
||||||
|
ChromeURL: ch.chromeURL,
|
||||||
|
ChromePath: ch.chromePath,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
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 {
|
||||||
@ -66,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 {
|
||||||
@ -97,8 +150,8 @@ func StartChrome(showWindow *bool, vm *goja.Runtime) (*Chrome, error) {
|
|||||||
if err := ch.browser.Connect(); err != nil {
|
if err := ch.browser.Connect(); err != nil {
|
||||||
return nil, gojs.Err(err)
|
return nil, gojs.Err(err)
|
||||||
}
|
}
|
||||||
ver, _ := ch.browser.Version()
|
// ver, _ := ch.browser.Version()
|
||||||
logger.Info("启动Chrome浏览器", "id", ch.id, "browser", ver.Product, "userAgent", ver.UserAgent, "chromeURL", ch.chromeURL, "chromePath", ch.chromePath)
|
// logger.Info("启动Chrome浏览器", "id", ch.id, "browser", ver.Product, "userAgent", ver.UserAgent, "chromeURL", ch.chromeURL, "chromePath", ch.chromePath)
|
||||||
|
|
||||||
chromesLock.Lock()
|
chromesLock.Lock()
|
||||||
chromes[ch.id] = ch
|
chromes[ch.id] = ch
|
||||||
|
4
go.mod
4
go.mod
@ -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
|
||||||
)
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user