Compare commits
No commits in common. "main" and "v0.0.6" have entirely different histories.
85
chrome.go
85
chrome.go
@ -24,10 +24,10 @@ var chromes = map[string]*Chrome{}
|
||||
var chromesLock sync.Mutex
|
||||
|
||||
func (ch *Chrome) Close(vm *goja.Runtime) {
|
||||
// logger := gojs.GetLogger(vm)
|
||||
logger := gojs.GetLogger(vm)
|
||||
if ch.browser != nil {
|
||||
// ver, _ := ch.browser.Version()
|
||||
// logger.Info("关闭Chrome浏览器", "id", ch.id, "browser", ver.Product, "userAgent", ver.UserAgent, "chromeURL", ch.chromeURL, "chromePath", ch.chromePath)
|
||||
ver, _ := ch.browser.Version()
|
||||
logger.Info("关闭Chrome浏览器", "id", ch.id, "browser", ver.Product, "userAgent", ver.UserAgent, "chromeURL", ch.chromeURL, "chromePath", ch.chromePath)
|
||||
ch.browser.Close()
|
||||
ch.browser = nil
|
||||
}
|
||||
@ -52,61 +52,13 @@ func CloseAllChrome(vm *goja.Runtime) {
|
||||
}
|
||||
}
|
||||
|
||||
type ChromeInfo struct {
|
||||
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)
|
||||
func StartChrome(showWindow *bool, vm *goja.Runtime) (*Chrome, error) {
|
||||
logger := gojs.GetLogger(vm)
|
||||
ch := &Chrome{}
|
||||
ch.id = u.UniqueId()
|
||||
ch.browser = rod.New()
|
||||
ch.chromeURL = opt.ChromeURL
|
||||
if opt.ChromeURL == "" {
|
||||
ch.chromeURL = conf.ChromeURL
|
||||
if conf.ChromeURL == "" {
|
||||
// 使用本地Chrome
|
||||
ch.launcher = launcher.New()
|
||||
if localBrowserPath, hasLocalBrowser := launcher.LookPath(); hasLocalBrowser {
|
||||
@ -114,25 +66,20 @@ func StartChrome(opt *ChromeOption, vm *goja.Runtime) (*Chrome, error) {
|
||||
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"
|
||||
ch.launcher.Headless(!opt.ShowWindow).Set("disable-dev-shm-usage").Set("single-process").Set("disable-blink-features", "AutomationControlled")
|
||||
ch.launcher.Headless(!u.Bool(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")
|
||||
if opt.ChromeHtpProxy != "" {
|
||||
ch.launcher.Proxy(opt.ChromeHtpProxy)
|
||||
}
|
||||
switch runtime.GOOS {
|
||||
case "linux":
|
||||
ch.launcher.Set("disable-setuid-sandbox")
|
||||
case "windows":
|
||||
ch.launcher.Set("disable-features=RendererCodeIntegrity")
|
||||
}
|
||||
if opt.ChromeOption != nil {
|
||||
for _, opt := range opt.ChromeOption {
|
||||
a := u.SplitTrimN(opt, "=", 2)
|
||||
if len(a) == 2 {
|
||||
ch.launcher.Set(flags.Flag(a[0]), a[1])
|
||||
} else {
|
||||
ch.launcher.Set(flags.Flag(opt))
|
||||
}
|
||||
for _, opt := range conf.ChromeOption {
|
||||
a := u.SplitTrimN(opt, "=", 2)
|
||||
if len(a) == 2 {
|
||||
ch.launcher.Set(flags.Flag(a[0]), a[1])
|
||||
} else {
|
||||
ch.launcher.Set(flags.Flag(opt))
|
||||
}
|
||||
}
|
||||
if localChromeURL, err := ch.launcher.Launch(); err != nil {
|
||||
@ -150,8 +97,8 @@ func StartChrome(opt *ChromeOption, vm *goja.Runtime) (*Chrome, error) {
|
||||
if err := ch.browser.Connect(); err != nil {
|
||||
return nil, gojs.Err(err)
|
||||
}
|
||||
// ver, _ := ch.browser.Version()
|
||||
// logger.Info("启动Chrome浏览器", "id", ch.id, "browser", ver.Product, "userAgent", ver.UserAgent, "chromeURL", ch.chromeURL, "chromePath", ch.chromePath)
|
||||
ver, _ := ch.browser.Version()
|
||||
logger.Info("启动Chrome浏览器", "id", ch.id, "browser", ver.Product, "userAgent", ver.UserAgent, "chromeURL", ch.chromeURL, "chromePath", ch.chromePath)
|
||||
|
||||
chromesLock.Lock()
|
||||
chromes[ch.id] = ch
|
||||
|
4
go.mod
4
go.mod
@ -3,7 +3,7 @@ module apigo.cc/gojs/http
|
||||
go 1.23.0
|
||||
|
||||
require (
|
||||
apigo.cc/gojs v0.0.23
|
||||
apigo.cc/gojs v0.0.22
|
||||
apigo.cc/gojs/console v0.0.2
|
||||
apigo.cc/gojs/file v0.0.4
|
||||
apigo.cc/gojs/util v0.0.12
|
||||
@ -11,7 +11,7 @@ require (
|
||||
github.com/gorilla/websocket v1.5.3
|
||||
github.com/ssgo/config v1.7.9
|
||||
github.com/ssgo/httpclient v1.7.8
|
||||
github.com/ssgo/log v1.7.9
|
||||
github.com/ssgo/log v1.7.7
|
||||
github.com/ssgo/s v1.7.24
|
||||
github.com/ssgo/u v1.7.21
|
||||
)
|
||||
|
Loading…
x
Reference in New Issue
Block a user