-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
97 lines (80 loc) · 2.79 KB
/
main.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
// Package main ...
package main
import (
"encoding/base64"
"fmt"
"io/ioutil"
"net/http"
"os"
"github.com/moredure/xrod"
"github.com/moredure/xrod/lib/launcher"
"github.com/moredure/xrod/lib/proto"
"github.com/moredure/xrod/lib/utils"
"github.com/ysmood/gson"
)
// For example, when you log into your github account, and you want to reuse the login session for automation task.
// You can use this example to achieve such functionality. Rod will be just like your browser extension.
func main() {
// Make sure you have closed your browser completely, UserMode can't control a browser that is not launched by it.
// Launches a new browser with the "new user mode" option, and returns the URL to control that browser.
wsURL := launcher.NewUserMode().MustLaunch()
browser := rod.New().ControlURL(wsURL).MustConnect().NoDefaultDevice()
// Run a extension. Here we created a link previewer extension as an example.
// With this extension, whenever you hover on a link a preview of the linked page will popup.
linkPreviewer(browser)
browser.MustPage()
waitExit()
}
func linkPreviewer(browser *rod.Browser) {
// Create a headless browser to generate preview of links on background.
previewer := rod.New().MustConnect()
previewer.MustSetCookies(browser.MustGetCookies()...) // share cookies
pool := rod.NewPagePool(5)
create := func() *rod.Page { return previewer.MustPage() }
go browser.EachEvent(func(e *proto.TargetTargetCreated) {
if e.TargetInfo.Type != proto.TargetTargetInfoTypePage {
return
}
page := browser.MustPageFromTargetID(e.TargetInfo.TargetID)
// Inject js to every new page
page.MustEvalOnNewDocument(js)
// Expose a function to the page to provide preview
page.MustExpose("getPreview", func(url gson.JSON) (interface{}, error) {
p := pool.Get(create)
defer pool.Put(p)
p.MustNavigate(url.Str())
return base64.StdEncoding.EncodeToString(p.MustScreenshot()), nil
})
})()
}
var jsLib = get("https://unpkg.com/@popperjs/core@2") + get("https://unpkg.com/tippy.js@6")
var js = fmt.Sprintf(`window.addEventListener('load', () => {
%s
function setup(el) {
el.classList.add('x-set')
tippy(el, {onShow: async (it) => {
if (it.props.content.src) return
let img = document.createElement('img')
img.style.width = '400px'
img.src = "data:image/png;base64," + await getPreview(el.href)
it.setContent(img)
}, content: 'loading...', maxWidth: 500})
}
(function check() {
Array.from(document.querySelectorAll('a:not(.x-set)')).forEach(setup)
setTimeout(check, 1000)
})()
})`, jsLib)
func get(u string) string {
res, err := http.Get(u)
utils.E(err)
defer func() { _ = res.Body.Close() }()
b, err := ioutil.ReadAll(res.Body)
utils.E(err)
return string(b)
}
func waitExit() {
fmt.Println("Press Enter to exit...")
utils.E(fmt.Scanln())
os.Exit(0)
}