-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd.go
102 lines (89 loc) · 2.23 KB
/
cmd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package embedhtml
import (
"context"
"errors"
"fmt"
"log"
"strings"
"time"
"github.com/PuerkitoBio/goquery"
"github.com/chromedp/cdproto/network"
"github.com/chromedp/chromedp"
)
const extension = ".embed.html"
type EmbedHTML struct {
Options
}
func (c *EmbedHTML) Run() (err error) {
if c.About {
fmt.Println("Visit https://github.com/gonejack/embede-html")
return
}
if len(c.HTML) == 0 {
return errors.New("no .html files given")
}
return c.run()
}
func (c *EmbedHTML) run() (err error) {
for _, html := range c.HTML {
err = c.process(html)
if err != nil {
return
}
}
return
}
func (c *EmbedHTML) process(html string) (err error) {
if strings.HasSuffix(html, extension) {
return
}
opt := chromedp.WithBrowserOption(
chromedp.WithDialTimeout(time.Minute),
)
ctx, cancel := chromedp.NewContext(context.TODO(), opt)
defer cancel()
content := ""
//output := strings.TrimSuffix(html, ".html") + extension
requests := make(map[network.RequestID]*network.Request)
response := make(map[network.RequestID]*network.Response)
respBody := make(map[network.RequestID][]byte)
chromedp.ListenTarget(ctx, func(v interface{}) {
switch ev := v.(type) {
case *network.EventRequestWillBeSent:
log.Printf("req: [reqId=%s, url=%s]", ev.RequestID, ev.Request.URL)
requests[ev.RequestID] = ev.Request
case *network.EventLoadingFinished:
log.Printf("rsp: [reqId=%s]", ev.RequestID)
case *network.EventResponseReceived:
response[ev.RequestID] = ev.Response
case *network.EventResponseReceivedExtraInfo:
case *network.EventDataReceived:
default:
}
})
err = chromedp.Run(ctx,
chromedp.Navigate(pathToURI(html)),
chromedp.ActionFunc(func(ctx context.Context) error {
for reqId := range requests {
body, err := network.GetResponseBody(reqId).Do(ctx)
if err == nil {
respBody[reqId] = body
}
}
return nil
}),
chromedp.ActionFunc(func(ctx context.Context) error {
return chromedp.OuterHTML("html", &content).Do(ctx)
}),
)
if err != nil {
return fmt.Errorf("cannot render %s: %s", html, err)
}
doc, err := goquery.NewDocumentFromReader(strings.NewReader(content))
if err != nil {
return
}
doc.Find("img,link,script").Each(func(i int, e *goquery.Selection) {
})
return
}