refactor: 更改浏览器执行文件以及下载路径,windows环境使用系统自带的edge浏览器运行

This commit is contained in:
johlanse 2022-02-24 14:14:30 +08:00
parent ebe945e176
commit 6afa2d4a77
4 changed files with 121 additions and 4 deletions

17
docs/delete_old_file.md Normal file
View File

@ -0,0 +1,17 @@
目前最新版本更新了浏览器的执行文件路径和playwright驱动文件的路径所以可以删除之前下载的文件。对应文件位置分别为
## windows
```text
C:\Users\用户\AppData\Local\ms-playwright\
C:\Users\用户\AppData\Local\ms-playwright-go\
```
## linux
```text
~/.cache/ms-playwright/
~/.cache/ms-playwright-go/
```
## mac
```text
/User/Library/Caches/ms-playwright/
/User/Library/Caches/ms-playwright-go/
```

View File

@ -37,6 +37,7 @@ type Config struct {
Port int `json:"port"`
} `json:"web"`
Cron string `json:"cron"`
EdgePath string `json:"edge_path"`
}
var (

View File

@ -43,3 +43,5 @@ tg:
# "9 19 * * *" 每天19点9分执行一次
# "* 10 * * *” 每天早上十点执行一次
cron: ""
edge_path: "C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"

View File

@ -11,6 +11,7 @@ import (
"image/png"
"io"
"net/url"
"os"
"runtime"
"time"
@ -23,6 +24,9 @@ import (
"golang.org/x/image/bmp"
)
// Core
// @Description:
//
type Core struct {
pw *playwright.Playwright
browser playwright.Browser
@ -31,6 +35,9 @@ type Core struct {
Push func(kind string, message string)
}
// Cookie
// @Description:
//
type Cookie struct {
Name string `json:"name" yaml:"name"`
Value string `json:"value" yaml:"value"`
@ -42,8 +49,98 @@ type Cookie struct {
SameSite string `json:"same_site" yaml:"same_site"`
}
// Init
/**
* @Description:
* @receiver c
*/
func (c *Core) Init() {
pwt, err := playwright.Run()
if runtime.GOOS == "windows" {
c.initWondows()
} else {
c.initNotWindows()
}
}
func (c *Core) initWondows() {
dir, err := os.Getwd()
if err != nil {
return
}
pwt, err := playwright.Run(&playwright.RunOptions{
DriverDirectory: dir + "/tools/driver/",
SkipInstallBrowsers: true,
Browsers: []string{"msedge"},
})
if err != nil {
log.Errorln("[core]", "初始化playwright失败")
log.Errorln("[core] ", err.Error())
return
}
c.pw = pwt
path := GetConfig().EdgePath
if path == "" {
path = "C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe"
}
browser, err := pwt.Chromium.Launch(playwright.BrowserTypeLaunchOptions{
Args: []string{
"--disable-extensions",
"--disable-gpu",
"--start-maximized",
"--no-sandbox",
"--window-size=500,450",
// "--mute-audio",
"--window-position=0,0",
"--ignore-certificate-errors",
// "--ignore-ssl-errors",
// "--disable-features=RendererCodeIntegrity",
// "--disable-blink-features",
// "--disable-blink-features=AutomationControlled",
},
Channel: nil,
ChromiumSandbox: nil,
Devtools: nil,
DownloadsPath: nil,
ExecutablePath: playwright.String(path),
HandleSIGHUP: nil,
HandleSIGINT: nil,
HandleSIGTERM: nil,
Headless: playwright.Bool(!c.ShowBrowser),
Proxy: nil,
SlowMo: nil,
Timeout: nil,
})
if err != nil {
log.Errorln("[core] ", "初始化chrome失败")
log.Errorln("[core] ", err.Error())
return
}
c.browser = browser
context, err := c.browser.NewContext()
c.browser.NewContext()
if err != nil {
return
}
c.context = &context
}
func (c *Core) initNotWindows() {
dir, err := os.Getwd()
if err != nil {
return
}
err = os.Setenv("PLAYWRIGHT_BROWSERS_PATH", dir+"/tools/browser/")
if err != nil {
log.Errorln("设置环境变量PLAYWRIGHT_BROWSERS_PATH失败" + err.Error())
err = nil
}
pwt, err := playwright.Run(&playwright.RunOptions{
DriverDirectory: dir + "/tools/driver/",
SkipInstallBrowsers: false,
Browsers: []string{"firefox"},
})
if err != nil {
log.Errorln("[core]", "初始化playwright失败")
log.Errorln("[core] ", err.Error())
@ -54,7 +151,7 @@ func (c *Core) Init() {
browser, err := pwt.Firefox.Launch(playwright.BrowserTypeLaunchOptions{
Args: []string{
"--disable-extensions",
// "--disable-gpu",
"--disable-gpu",
"--start-maximized",
"--no-sandbox",
"--window-size=500,450",