@@ -4,18 +4,21 @@ import (
44 "context"
55 "encoding/base64"
66 "fmt"
7- "github.com/chromedp/chromedp"
87 "log"
98 "os"
109 "path/filepath"
1110 "strings"
11+
12+ "github.com/chromedp/chromedp"
1213)
1314
14- const HTML = "html"
15- const FileProtocol = "file://"
16- const EchartsInstanceDom = "div[_echarts_instance_]"
17- const CanvasJs = "echarts.getInstanceByDom(document.querySelector('div[_echarts_instance_]'))" +
18- ".getDataURL({type: '%s', pixelRatio: %d, excludeComponents: ['toolbox']})"
15+ const (
16+ HTML = "html"
17+ FileProtocol = "file://"
18+ EchartsInstanceDom = "div[_echarts_instance_]"
19+ CanvasJs = "echarts.getInstanceByDom(document.querySelector('div[_echarts_instance_]'))" +
20+ ".getDataURL({type: '%s', pixelRatio: %d, excludeComponents: ['toolbox']})"
21+ )
1922
2023type SnapshotConfig struct {
2124 // Renderer canvas or svg, not used for now
@@ -73,9 +76,7 @@ func MakeSnapshot(config *SnapshotConfig) error {
7376 htmlPath , _ = filepath .Abs (htmlPath )
7477 }
7578
76- ctx , cancel := chromedp .NewContext (
77- context .Background (),
78- )
79+ ctx , cancel := chromedp .NewContext (context .Background ())
7980 defer cancel ()
8081
8182 htmlFullPath := filepath .Join (htmlPath , fileName + "." + HTML )
@@ -84,16 +85,12 @@ func MakeSnapshot(config *SnapshotConfig) error {
8485 defer func () {
8586 err := os .Remove (htmlFullPath )
8687 if err != nil {
87- log .Printf ("Failed to delete the file: %s\n " , err )
88+ log .Printf ("Failed to delete the file(%s), err : %s\n " , htmlFullPath , err )
8889 }
8990 }()
9091 }
9192
92- err := os .WriteFile (htmlFullPath , content , 0644 )
93- if err != nil {
94- return err
95- }
96-
93+ err := os .WriteFile (htmlFullPath , content , 0o644 )
9794 if err != nil {
9895 return err
9996 }
@@ -102,15 +99,13 @@ func MakeSnapshot(config *SnapshotConfig) error {
10299 quality = 1
103100 }
104101
105- executeJS := fmt .Sprintf (CanvasJs , suffix , quality )
106-
107102 var base64Data string
103+ executeJS := fmt .Sprintf (CanvasJs , suffix , quality )
108104 err = chromedp .Run (ctx ,
109105 chromedp .Navigate (fmt .Sprintf ("%s%s" , FileProtocol , htmlFullPath )),
110106 chromedp .WaitVisible (EchartsInstanceDom , chromedp .ByQuery ),
111107 chromedp .Evaluate (executeJS , & base64Data ),
112108 )
113-
114109 if err != nil {
115110 return err
116111 }
@@ -120,10 +115,9 @@ func MakeSnapshot(config *SnapshotConfig) error {
120115 return err
121116 }
122117
123- imageFullPath := fmt .Sprintf ("%s/%s.%s" , path , fileName , suffix )
124-
125- if err := os .WriteFile (imageFullPath , imgContent , 0644 ); err != nil {
126- log .Fatal (err )
118+ imageFullPath := filepath .Join (path , fmt .Sprintf ("%s.%s" , fileName , suffix ))
119+ if err := os .WriteFile (imageFullPath , imgContent , 0o644 ); err != nil {
120+ return err
127121 }
128122
129123 log .Printf ("Wrote %s.%s success" , fileName , suffix )
0 commit comments