diff --git a/downloader/downloader.go b/downloader/downloader.go index 35bac6ed..9451273e 100644 --- a/downloader/downloader.go +++ b/downloader/downloader.go @@ -244,33 +244,33 @@ func writeFile(url string, file *os.File, headers map[string]string) (int, error } // PrintToPDF print to pdf -func PrintToPDF(v Datum, cookies map[string]string, path string) error { - - name := utils.FileName(v.Title, "pdf") - - filename := filepath.Join(path, name) - fmt.Printf("正在生成文件:【\033[37;1m%s\033[0m】 ", name) - - _, exist, err := utils.FileSize(filename) - - if err != nil { - fmt.Printf("\033[31;1m%s\033[0m\n", "失败"+err.Error()) - return err - } - - if exist { - fmt.Printf("\033[33;1m%s\033[0m\n", "已存在") - return nil - } - - err = utils.ColumnPrintToPDF(v.Enid, filename, cookies) - - if err != nil { - fmt.Printf("\033[31;1m%s\033[0m\n", "失败"+err.Error()) - return err - } - - fmt.Printf("\033[32;1m%s\033[0m\n", "完成") - - return nil -} +//func PrintToPDF(v Datum, cookies map[string]string, path string) error { +// +// name := utils.FileName(v.Title, "pdf") +// +// filename := filepath.Join(path, name) +// fmt.Printf("正在生成文件:【\033[37;1m%s\033[0m】 ", name) +// +// _, exist, err := utils.FileSize(filename) +// +// if err != nil { +// fmt.Printf("\033[31;1m%s\033[0m\n", "失败"+err.Error()) +// return err +// } +// +// if exist { +// fmt.Printf("\033[33;1m%s\033[0m\n", "已存在") +// return nil +// } +// +// err = utils.ColumnPrintToPDF(v.Enid, filename, cookies) +// +// if err != nil { +// fmt.Printf("\033[31;1m%s\033[0m\n", "失败"+err.Error()) +// return err +// } +// +// fmt.Printf("\033[32;1m%s\033[0m\n", "完成") +// +// return nil +//} diff --git a/go.mod b/go.mod index 1ee80592..adc09f41 100644 --- a/go.mod +++ b/go.mod @@ -7,11 +7,9 @@ require ( github.com/PuerkitoBio/goquery v1.9.2 github.com/SebastiaanKlippert/go-wkhtmltopdf v1.9.3 github.com/bmaupin/go-epub v1.1.0 - github.com/chromedp/cdproto v0.0.0-20241003230502-a4a8f7c660df - github.com/chromedp/chromedp v0.11.0 - github.com/gabriel-vasile/mimetype v1.4.5 + github.com/gabriel-vasile/mimetype v1.4.6 github.com/go-resty/resty/v2 v2.15.3 - github.com/gomarkdown/markdown v0.0.0-20240730141124-034f12af3bf6 + github.com/gomarkdown/markdown v0.0.0-20240930133441-72d49d9543d8 github.com/json-iterator/go v1.1.12 github.com/mattn/go-colorable v0.1.13 github.com/mitchellh/mapstructure v1.5.0 @@ -23,14 +21,8 @@ require ( require ( github.com/andybalholm/cascadia v1.3.2 // indirect - github.com/chromedp/sysutil v1.0.0 // indirect - github.com/gobwas/httphead v0.1.0 // indirect - github.com/gobwas/pool v0.2.1 // indirect - github.com/gobwas/ws v1.4.0 // indirect github.com/gofrs/uuid v4.4.0+incompatible // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect - github.com/josharian/intern v1.0.0 // indirect - github.com/mailru/easyjson v0.7.7 // indirect github.com/mattn/go-isatty v0.0.20 // indirect github.com/mattn/go-runewidth v0.0.16 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect @@ -39,7 +31,7 @@ require ( github.com/spf13/pflag v1.0.5 // indirect github.com/stretchr/testify v1.9.0 // indirect github.com/vincent-petithory/dataurl v1.0.0 // indirect - golang.org/x/net v0.28.0 // indirect + golang.org/x/net v0.30.0 // indirect golang.org/x/sys v0.26.0 // indirect - golang.org/x/text v0.17.0 // indirect + golang.org/x/text v0.19.0 // indirect ) diff --git a/go.sum b/go.sum index 6fec8b7f..14b50c49 100644 --- a/go.sum +++ b/go.sum @@ -8,43 +8,25 @@ github.com/andybalholm/cascadia v1.3.2 h1:3Xi6Dw5lHF15JtdcmAHD3i1+T8plmv7BQ/nsVi github.com/andybalholm/cascadia v1.3.2/go.mod h1:7gtRlve5FxPPgIgX36uWBX58OdBsSS6lUvCFb+h7KvU= github.com/bmaupin/go-epub v1.1.0 h1:XJyvvjchtUlbZ2P7eaEeB8EFw2NgVY5ycREFpmd6MKM= github.com/bmaupin/go-epub v1.1.0/go.mod h1:mBan+0WgVv5JbPNw1xfnfQoTRN9iPMKBshZwPOL0SY0= -github.com/chromedp/cdproto v0.0.0-20241003230502-a4a8f7c660df h1:cbtSn19AtqQha1cxmP2Qvgd3fFMz51AeAEKLJMyEUhc= -github.com/chromedp/cdproto v0.0.0-20241003230502-a4a8f7c660df/go.mod h1:GKljq0VrfU4D5yc+2qA6OVr8pmO/MBbPEWqWQ/oqGEs= -github.com/chromedp/chromedp v0.11.0 h1:1PT6O4g39sBAFjlljIHTpxmCSk8meeYL6+R+oXH4bWA= -github.com/chromedp/chromedp v0.11.0/go.mod h1:jsD7OHrX0Qmskqb5Y4fn4jHnqquqW22rkMFgKbECsqg= -github.com/chromedp/sysutil v1.0.0 h1:+ZxhTpfpZlmchB58ih/LBHX52ky7w2VhQVKQMucy3Ic= -github.com/chromedp/sysutil v1.0.0/go.mod h1:kgWmDdq8fTzXYcKIBqIYvRRTnYb9aNS9moAV0xufSww= github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/gabriel-vasile/mimetype v1.3.1/go.mod h1:fA8fi6KUiG7MgQQ+mEWotXoEOvmxRtOJlERCzSmRvr8= -github.com/gabriel-vasile/mimetype v1.4.5 h1:J7wGKdGu33ocBOhGy0z653k/lFKLFDPJMG8Gql0kxn4= -github.com/gabriel-vasile/mimetype v1.4.5/go.mod h1:ibHel+/kbxn9x2407k1izTA1S81ku1z/DlgOW2QE0M4= +github.com/gabriel-vasile/mimetype v1.4.6 h1:3+PzJTKLkvgjeTbts6msPJt4DixhT4YtFNf1gtGe3zc= +github.com/gabriel-vasile/mimetype v1.4.6/go.mod h1:JX1qVKqZd40hUPpAfiNTe0Sne7hdfKSbOqqmkq8GCXc= github.com/go-resty/resty/v2 v2.15.3 h1:bqff+hcqAflpiF591hhJzNdkRsFhlB96CYfBwSFvql8= github.com/go-resty/resty/v2 v2.15.3/go.mod h1:0fHAoK7JoBy/Ch36N8VFeMsK7xQOHhvWaC3iOktwmIU= -github.com/gobwas/httphead v0.1.0 h1:exrUm0f4YX0L7EBwZHuCF4GDp8aJfVeBrlLQrs6NqWU= -github.com/gobwas/httphead v0.1.0/go.mod h1:O/RXo79gxV8G+RqlR/otEwx4Q36zl9rqC5u12GKvMCM= -github.com/gobwas/pool v0.2.1 h1:xfeeEhW7pwmX8nuLVlqbzVc7udMDrwetjEv+TZIz1og= -github.com/gobwas/pool v0.2.1/go.mod h1:q8bcK0KcYlCgd9e7WYLm9LpyS+YeLd8JVDW6WezmKEw= -github.com/gobwas/ws v1.4.0 h1:CTaoG1tojrh4ucGPcoJFiAQUAsEWekEWvLy7GsVNqGs= -github.com/gobwas/ws v1.4.0/go.mod h1:G3gNqMNtPppf5XUz7O4shetPpcZ1VJ7zt18dlUeakrc= github.com/gofrs/uuid v3.1.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= github.com/gofrs/uuid v4.4.0+incompatible h1:3qXRTX8/NbyulANqlc0lchS1gqAVxRgsuW1YrTJupqA= github.com/gofrs/uuid v4.4.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= -github.com/gomarkdown/markdown v0.0.0-20240730141124-034f12af3bf6 h1:ZPy+2XJ8u0bB3sNFi+I72gMEMS7MTg7aZCCXPOjV8iw= -github.com/gomarkdown/markdown v0.0.0-20240730141124-034f12af3bf6/go.mod h1:JDGcbDT52eL4fju3sZ4TeHGsQwhG9nbDV21aMyhwPoA= +github.com/gomarkdown/markdown v0.0.0-20240930133441-72d49d9543d8 h1:4txT5G2kqVAKMjzidIabL/8KqjIK71yj30YOeuxLn10= +github.com/gomarkdown/markdown v0.0.0-20240930133441-72d49d9543d8/go.mod h1:JDGcbDT52eL4fju3sZ4TeHGsQwhG9nbDV21aMyhwPoA= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= -github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= -github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= -github.com/ledongthuc/pdf v0.0.0-20220302134840-0c2507a12d80 h1:6Yzfa6GP0rIo/kULo2bwGEkFvCePZ3qHDDTC3/J9Swo= -github.com/ledongthuc/pdf v0.0.0-20220302134840-0c2507a12d80/go.mod h1:imJHygn/1yfhB7XSJJKlFZKl/J+dCPAknuiaGOshXAs= -github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0= -github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= @@ -62,8 +44,6 @@ github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9G github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec= github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY= -github.com/orisano/pixelmatch v0.0.0-20220722002657-fb0b55479cde h1:x0TT0RDC7UhAVbbWWBzr41ElhJx5tXPWkIHA2HWPRuw= -github.com/orisano/pixelmatch v0.0.0-20220722002657-fb0b55479cde/go.mod h1:nZgzbfBr3hhjoZnS66nKrHmduYNpc34ny7RK4z5/HM0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= @@ -94,8 +74,8 @@ golang.org/x/net v0.0.0-20210505024714-0287a6fb4125/go.mod h1:9nx3DQGgdP8bBQD5qx golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns= -golang.org/x/net v0.28.0 h1:a9JDOJc5GMUJ0+UDqmLT86WiEy7iWyIhz8gz8E4e5hE= -golang.org/x/net v0.28.0/go.mod h1:yqtgsTWOOnlGLG9GFRrK3++bGOUEkNBoHZc8MEDWPNg= +golang.org/x/net v0.30.0 h1:AcW1SDZMkb8IpzCdQUaIq2sP4sZ4zw+55h6ynffypl4= +golang.org/x/net v0.30.0/go.mod h1:2wGyMJ5iFasEhkwi13ChkO/t1ECNC4X4eBKkVFyYFlU= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= @@ -123,8 +103,8 @@ golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= -golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc= -golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= +golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM= +golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= golang.org/x/time v0.6.0 h1:eTDhh4ZXt5Qf0augr54TN6suAUudPcawVZeIAPU7D4U= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= diff --git a/utils/chromedp.go b/utils/chromedp.go index 3ca4da8b..0eb18778 100644 --- a/utils/chromedp.go +++ b/utils/chromedp.go @@ -1,196 +1,196 @@ package utils -import ( - "context" - "log" - "math/rand" - "os" - "time" - - "github.com/chromedp/cdproto/cdp" - "github.com/chromedp/cdproto/network" - "github.com/chromedp/cdproto/page" - "github.com/chromedp/cdproto/runtime" - "github.com/chromedp/chromedp" -) - -// ColumnPrintToPDF print pdf -func ColumnPrintToPDF(aid string, filename string, cookies map[string]string) error { - var buf []byte - options := []chromedp.ExecAllocatorOption{ - chromedp.UserAgent(`Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36`), - } - options = append(chromedp.DefaultExecAllocatorOptions[:], options...) - c, _ := chromedp.NewExecAllocator(context.Background(), options...) - // create chrome instance - ctx, cancel := chromedp.NewContext( - c, - chromedp.WithLogf(log.Printf), - ) - defer cancel() - - // create a timeout - ctx, cancel = context.WithTimeout(ctx, 60*time.Second) - defer cancel() - - err := chromedp.Run(ctx, - chromedp.Tasks{ - // chromedp.Emulate(device.IPadProlandscape), - enableLifeCycleEvents(), - setCookies(cookies), - navigateAndWaitFor(`https://www.dedao.cn/courseArticle/`+aid, "networkIdle"), - chromedp.ActionFunc(func(ctx context.Context) error { - s := ` - var node = document.querySelector('.geetest_panel'); - if (node != null ) { - node.style.display='none'; - } - ` - _, exp, err := runtime.Evaluate(s).Do(ctx) - if err != nil { - return err - } - - if exp != nil { - return exp - } - - return nil - }), - chromedp.ActionFunc(func(ctx context.Context) error { - s := ` - var header = document.querySelector('.iget-header'); - if (header != null) { - header.style.display='none'; - } - var ctl = document.querySelector('.pageControl'); - if (ctl != null) { - ctl.style.display='none'; - } - ` - _, exp, err := runtime.Evaluate(s).Do(ctx) - if err != nil { - return err - } - - if exp != nil { - return exp - } - - return nil - }), - chromedp.ActionFunc(func(ctx context.Context) error { - s := ` - var buttons = document.getElementsByTagName('button'); - if (buttons != null) { - for (var i = 0; i < buttons.length; ++i){ - if(buttons[i].innerText === " 展开目录 " || buttons[i].innerText === " 设置文本 "){ - buttons[i].parentNode.style.display="none"; - break; - } - } - } - - var asides = document.getElementsByTagName('aside'); - if (asides != null ) { - for (var i = 0; i < asides.length; ++i){ - asides[i].style.display="none"; - } - } - ` - _, exp, err := runtime.Evaluate(s).Do(ctx) - if err != nil { - return err - } - - if exp != nil { - return exp - } - - return nil - }), - - chromedp.ActionFunc(func(ctx context.Context) error { - // sleep 防止 496 NoCertificate - time.Sleep(time.Second * time.Duration(rand.Int31n(5))) - var err error - buf, _, err = page.PrintToPDF().WithPrintBackground(true).Do(ctx) - return err - }), - }, - ) - - if err != nil { - return err - } - - return os.WriteFile(filename, buf, 0644) -} - -func setCookies(cookies map[string]string) chromedp.ActionFunc { - return func(ctx context.Context) error { - expr := cdp.TimeSinceEpoch(time.Now().Add(180 * 24 * time.Hour)) - - for key, value := range cookies { - err := network.SetCookie(key, value).WithExpires(&expr).WithDomain(".dedao.cn").WithHTTPOnly(true).Do(ctx) - if err != nil { - return err - } - } - return nil - } -} - -func enableLifeCycleEvents() chromedp.ActionFunc { - return func(ctx context.Context) error { - err := page.Enable().Do(ctx) - if err != nil { - return err - } - - return page.SetLifecycleEventsEnabled(true).Do(ctx) - } -} - -func navigateAndWaitFor(url string, eventName string) chromedp.ActionFunc { - return func(ctx context.Context) error { - _, _, _, err := page.Navigate(url).Do(ctx) - if err != nil { - return err - } - - return waitFor(ctx, eventName) - } -} - -// waitFor blocks until eventName is received. -// Examples of events you can wait for: -// -// init, DOMContentLoaded, firstPaint, -// firstContentfulPaint, firstImagePaint, -// firstMeaningfulPaintCandidate, -// load, networkAlmostIdle, firstMeaningfulPaint, networkIdle -// -// This is not super reliable, I've already found incidental cases where -// networkIdle was sent before load. It's probably smart to see how -// puppeteer implements this exactly. -func waitFor(ctx context.Context, eventName string) error { - ch := make(chan struct{}) - cctx, cancel := context.WithCancel(ctx) - chromedp.ListenTarget(cctx, func(ev interface{}) { - switch e := ev.(type) { - case *page.EventLifecycleEvent: - if e.Name == eventName { - cancel() - close(ch) - } - } - }) - - select { - case <-ch: - return nil - case <-ctx.Done(): - return ctx.Err() - } -} +//import ( +// "context" +// "log" +// "math/rand" +// "os" +// "time" +// +// "github.com/chromedp/cdproto/cdp" +// "github.com/chromedp/cdproto/network" +// "github.com/chromedp/cdproto/page" +// "github.com/chromedp/cdproto/runtime" +// "github.com/chromedp/chromedp" +//) +// +//// ColumnPrintToPDF print pdf +//func ColumnPrintToPDF(aid string, filename string, cookies map[string]string) error { +// var buf []byte +// options := []chromedp.ExecAllocatorOption{ +// chromedp.UserAgent(`Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36`), +// } +// options = append(chromedp.DefaultExecAllocatorOptions[:], options...) +// c, _ := chromedp.NewExecAllocator(context.Background(), options...) +// // create chrome instance +// ctx, cancel := chromedp.NewContext( +// c, +// chromedp.WithLogf(log.Printf), +// ) +// defer cancel() +// +// // create a timeout +// ctx, cancel = context.WithTimeout(ctx, 60*time.Second) +// defer cancel() +// +// err := chromedp.Run(ctx, +// chromedp.Tasks{ +// // chromedp.Emulate(device.IPadProlandscape), +// enableLifeCycleEvents(), +// setCookies(cookies), +// navigateAndWaitFor(`https://www.dedao.cn/courseArticle/`+aid, "networkIdle"), +// chromedp.ActionFunc(func(ctx context.Context) error { +// s := ` +// var node = document.querySelector('.geetest_panel'); +// if (node != null ) { +// node.style.display='none'; +// } +// ` +// _, exp, err := runtime.Evaluate(s).Do(ctx) +// if err != nil { +// return err +// } +// +// if exp != nil { +// return exp +// } +// +// return nil +// }), +// chromedp.ActionFunc(func(ctx context.Context) error { +// s := ` +// var header = document.querySelector('.iget-header'); +// if (header != null) { +// header.style.display='none'; +// } +// var ctl = document.querySelector('.pageControl'); +// if (ctl != null) { +// ctl.style.display='none'; +// } +// ` +// _, exp, err := runtime.Evaluate(s).Do(ctx) +// if err != nil { +// return err +// } +// +// if exp != nil { +// return exp +// } +// +// return nil +// }), +// chromedp.ActionFunc(func(ctx context.Context) error { +// s := ` +// var buttons = document.getElementsByTagName('button'); +// if (buttons != null) { +// for (var i = 0; i < buttons.length; ++i){ +// if(buttons[i].innerText === " 展开目录 " || buttons[i].innerText === " 设置文本 "){ +// buttons[i].parentNode.style.display="none"; +// break; +// } +// } +// } +// +// var asides = document.getElementsByTagName('aside'); +// if (asides != null ) { +// for (var i = 0; i < asides.length; ++i){ +// asides[i].style.display="none"; +// } +// } +// ` +// _, exp, err := runtime.Evaluate(s).Do(ctx) +// if err != nil { +// return err +// } +// +// if exp != nil { +// return exp +// } +// +// return nil +// }), +// +// chromedp.ActionFunc(func(ctx context.Context) error { +// // sleep 防止 496 NoCertificate +// time.Sleep(time.Second * time.Duration(rand.Int31n(5))) +// var err error +// buf, _, err = page.PrintToPDF().WithPrintBackground(true).Do(ctx) +// return err +// }), +// }, +// ) +// +// if err != nil { +// return err +// } +// +// return os.WriteFile(filename, buf, 0644) +//} +// +//func setCookies(cookies map[string]string) chromedp.ActionFunc { +// return func(ctx context.Context) error { +// expr := cdp.TimeSinceEpoch(time.Now().Add(180 * 24 * time.Hour)) +// +// for key, value := range cookies { +// err := network.SetCookie(key, value).WithExpires(&expr).WithDomain(".dedao.cn").WithHTTPOnly(true).Do(ctx) +// if err != nil { +// return err +// } +// } +// return nil +// } +//} +// +//func enableLifeCycleEvents() chromedp.ActionFunc { +// return func(ctx context.Context) error { +// err := page.Enable().Do(ctx) +// if err != nil { +// return err +// } +// +// return page.SetLifecycleEventsEnabled(true).Do(ctx) +// } +//} +// +//func navigateAndWaitFor(url string, eventName string) chromedp.ActionFunc { +// return func(ctx context.Context) error { +// _, _, _, err := page.Navigate(url).Do(ctx) +// if err != nil { +// return err +// } +// +// return waitFor(ctx, eventName) +// } +//} +// +//// waitFor blocks until eventName is received. +//// Examples of events you can wait for: +//// +//// init, DOMContentLoaded, firstPaint, +//// firstContentfulPaint, firstImagePaint, +//// firstMeaningfulPaintCandidate, +//// load, networkAlmostIdle, firstMeaningfulPaint, networkIdle +//// +//// This is not super reliable, I've already found incidental cases where +//// networkIdle was sent before load. It's probably smart to see how +//// puppeteer implements this exactly. +//func waitFor(ctx context.Context, eventName string) error { +// ch := make(chan struct{}) +// cctx, cancel := context.WithCancel(ctx) +// chromedp.ListenTarget(cctx, func(ev interface{}) { +// switch e := ev.(type) { +// case *page.EventLifecycleEvent: +// if e.Name == eventName { +// cancel() +// close(ch) +// } +// } +// }) +// +// select { +// case <-ch: +// return nil +// case <-ctx.Done(): +// return ctx.Err() +// } +//} diff --git a/utils/chromedp_test.go b/utils/chromedp_test.go index f106f351..e646389e 100644 --- a/utils/chromedp_test.go +++ b/utils/chromedp_test.go @@ -1,17 +1,18 @@ package utils -import ( - "os" - "testing" -) - -func TestPrintToPdf(t *testing.T) { - filename := "file.pdf" - err := ColumnPrintToPDF("Pvz6E94NYDg2JjQemzVL3rAkWQjnwp", filename, nil) - - if err != nil { - t.Fatal("PrintToPDF test is failure", err) - } else { - _ = os.Remove(filename) - } -} +// +//import ( +// "os" +// "testing" +//) +// +//func TestPrintToPdf(t *testing.T) { +// filename := "file.pdf" +// err := ColumnPrintToPDF("Pvz6E94NYDg2JjQemzVL3rAkWQjnwp", filename, nil) +// +// if err != nil { +// t.Fatal("PrintToPDF test is failure", err) +// } else { +// _ = os.Remove(filename) +// } +//}