From 6afa2d4a7771a7488df7fa72dbeaec8c5ccd9008 Mon Sep 17 00:00:00 2001 From: johlanse Date: Thu, 24 Feb 2022 14:14:30 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E6=9B=B4=E6=94=B9=E6=B5=8F?= =?UTF-8?q?=E8=A7=88=E5=99=A8=E6=89=A7=E8=A1=8C=E6=96=87=E4=BB=B6=E4=BB=A5?= =?UTF-8?q?=E5=8F=8A=E4=B8=8B=E8=BD=BD=E8=B7=AF=E5=BE=84=EF=BC=8Cwindows?= =?UTF-8?q?=E7=8E=AF=E5=A2=83=E4=BD=BF=E7=94=A8=E7=B3=BB=E7=BB=9F=E8=87=AA?= =?UTF-8?q?=E5=B8=A6=E7=9A=84edge=E6=B5=8F=E8=A7=88=E5=99=A8=E8=BF=90?= =?UTF-8?q?=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/delete_old_file.md | 17 +++++++ lib/config.go | 3 +- lib/config_default.yml | 4 +- lib/core.go | 101 +++++++++++++++++++++++++++++++++++++++- 4 files changed, 121 insertions(+), 4 deletions(-) create mode 100644 docs/delete_old_file.md diff --git a/docs/delete_old_file.md b/docs/delete_old_file.md new file mode 100644 index 0000000..41a6998 --- /dev/null +++ b/docs/delete_old_file.md @@ -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/ +``` \ No newline at end of file diff --git a/lib/config.go b/lib/config.go index 8b9be55..cf59478 100644 --- a/lib/config.go +++ b/lib/config.go @@ -36,7 +36,8 @@ type Config struct { Host string `json:"host"` Port int `json:"port"` } `json:"web"` - Cron string `json:"cron"` + Cron string `json:"cron"` + EdgePath string `json:"edge_path"` } var ( diff --git a/lib/config_default.yml b/lib/config_default.yml index a85a9a9..7992574 100644 --- a/lib/config_default.yml +++ b/lib/config_default.yml @@ -42,4 +42,6 @@ tg: # 设置是否定时执行学习程序,格式为cron格式 # "9 19 * * *" 每天19点9分执行一次 # "* 10 * * *” 每天早上十点执行一次 -cron: "" \ No newline at end of file +cron: "" + +edge_path: "C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe" \ No newline at end of file diff --git a/lib/core.go b/lib/core.go index 5a03f9e..660fe57 100644 --- a/lib/core.go +++ b/lib/core.go @@ -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",