@@ -22,12 +22,15 @@ import (
2222
2323 "github.com/chromedp/cdproto/cdp"
2424 "github.com/chromedp/cdproto/dom"
25+ "github.com/chromedp/cdproto/runtime"
2526 "github.com/chromedp/chromedp"
2627 "github.com/kenshaw/rasterm"
2728 "golang.org/x/exp/maps"
2829)
2930
30- const dataSel = `div[data-ve-view]`
31+ const hdrSel = `#taw`
32+
33+ const dataSel = `#wob_wc`
3134
3235const svgSel = `#wob_d svg path`
3336
@@ -67,9 +70,9 @@ func run(ctx context.Context, verbose bool, timeout time.Duration, query, lang,
6770 return fmt .Errorf ("invalid unit %q" , unit )
6871 }
6972 switch typ = strings .ToLower (typ ); typ {
70- case "temp " :
71- typ = ""
72- case "" , "rain" , "wind" :
73+ case "" :
74+ typ = "temp "
75+ case "temp " , "rain" , "wind" :
7376 default :
7477 return fmt .Errorf ("invalid type %q" , typ )
7578 }
@@ -104,31 +107,44 @@ func run(ctx context.Context, verbose bool, timeout time.Duration, query, lang,
104107 ctx , cancel = context .WithTimeout (ctx , timeout )
105108 defer cancel ()
106109
107- // get nodes
108- var nodes []* cdp.Node
110+ // get
111+ var nodes , dataNodes []* cdp.Node
109112 if err := chromedp .Run (ctx ,
110113 chromedp .Navigate ("https://www.google.com/search?" + v .Encode ()),
111- chromedp .WaitVisible (dataSel , chromedp .ByQuery ),
112- chromedp .WaitVisible (svgSel , chromedp .ByQuery ),
113- chromedp .Nodes (dataSel , & nodes , chromedp .ByQuery , chromedp .NodeVisible ),
114+ chromedp .QueryAfter (hdrSel , func (ctx context.Context , id runtime.ExecutionContextID , n ... * cdp.Node ) error {
115+ nodes = append (nodes , n [0 ])
116+ return nil
117+ }, chromedp .ByQuery , chromedp .NodeVisible ),
118+ chromedp .QueryAfter (dataSel , func (ctx context.Context , id runtime.ExecutionContextID , n ... * cdp.Node ) error {
119+ nodes = append (nodes , n [0 ])
120+ return nil
121+ }, chromedp .ByQuery , chromedp .NodeVisible ),
122+ chromedp .Nodes (dataSel , & dataNodes , chromedp .ByQuery , chromedp .NodeVisible ),
114123 chromedp .ActionFunc (func (ctx context.Context ) error {
115- return dom .RequestChildNodes (nodes [0 ].NodeID ).WithDepth (- 1 ).Do (ctx )
124+ return dom .RequestChildNodes (dataNodes [0 ].NodeID ).WithDepth (- 1 ).Do (ctx )
116125 }),
117126 ); err != nil {
118127 return err
119128 }
120129
121130 // click on unit
122131 if unit != "" {
123- if node := findNode (`°` + unit , nodes ); node != nil {
132+ if node := findNode (`°` + unit , dataNodes ); node != nil {
124133 _ = chromedp .Run (ctx , chromedp .MouseClickNode (node ))
125134 }
126135 }
127136
128137 // click on type
129- if typ != "" {
138+ if typ != "temp " {
130139 _ = chromedp .Run (ctx , chromedp .Click ("wob_" + typ , chromedp .ByID ))
131140 }
141+ // hide other types
142+ _ = chromedp .Run (ctx , chromedp .QueryAfter (`#wob_d > div:first-child > *:not(#wob_` + typ + `)` , func (ctx context.Context , id runtime.ExecutionContextID , nodes ... * cdp.Node ) error {
143+ for _ , n := range nodes {
144+ _ = dom .SetAttributeValue (n .NodeID , "style" , "display:none;" ).Do (ctx )
145+ }
146+ return nil
147+ }))
132148
133149 // click on day
134150 if day != 0 {
@@ -137,7 +153,7 @@ func run(ctx context.Context, verbose bool, timeout time.Duration, query, lang,
137153
138154 // capture screenshot
139155 var buf []byte
140- if err := chromedp .Run (ctx , chromedp .ScreenshotScale ( dataSel , scale , & buf , chromedp . ByQuery )); err != nil {
156+ if err := chromedp .Run (ctx , chromedp .ScreenshotNodes ( nodes , scale , & buf )); err != nil {
141157 return err
142158 }
143159 // decode png
0 commit comments