| 1 |
// Package main ... |
| 2 |
package main |
| 3 |
|
| 4 |
import ( |
| 5 |
"encoding/base64" |
| 6 |
"fmt" |
| 7 |
"io" |
| 8 |
"net/http" |
| 9 |
"os" |
| 10 |
|
| 11 |
"github.com/go-rod/rod" |
| 12 |
"github.com/go-rod/rod/lib/launcher" |
| 13 |
"github.com/go-rod/rod/lib/proto" |
| 14 |
"github.com/go-rod/rod/lib/utils" |
| 15 |
"github.com/ysmood/gson" |
| 16 |
) |
| 17 |
|
| 18 |
// For example, when you log into your github account, and you want to reuse the login session for automation task. |
| 19 |
// You can use this example to achieve such functionality. Rod will be just like your browser extension. |
| 20 |
func main() { |
| 21 |
// Make sure you have closed your browser completely, UserMode can't control a browser that is not launched by it. |
| 22 |
// Launches a new browser with the "new user mode" option, and returns the URL to control that browser. |
| 23 |
wsURL := launcher.NewUserMode().MustLaunch() |
| 24 |
|
| 25 |
browser := rod.New().ControlURL(wsURL).MustConnect().NoDefaultDevice() |
| 26 |
|
| 27 |
// Run a extension. Here we created a link previewer extension as an example. |
| 28 |
// With this extension, whenever you hover on a link a preview of the linked page will popup. |
| 29 |
linkPreviewer(browser) |
| 30 |
|
| 31 |
browser.MustPage() |
| 32 |
|
| 33 |
waitExit() |
| 34 |
} |
| 35 |
|
| 36 |
func linkPreviewer(browser *rod.Browser) { |
| 37 |
// Create a headless browser to generate preview of links on background. |
| 38 |
previewer := rod.New().MustConnect() |
| 39 |
previewer.MustSetCookies(browser.MustGetCookies()...) // share cookies |
| 40 |
pool := rod.NewPagePool(5) |
| 41 |
create := func() *rod.Page { return previewer.MustPage() } |
| 42 |
|
| 43 |
go browser.EachEvent(func(e *proto.TargetTargetCreated) { |
| 44 |
if e.TargetInfo.Type != proto.TargetTargetInfoTypePage { |
| 45 |
return |
| 46 |
} |
| 47 |
page := browser.MustPageFromTargetID(e.TargetInfo.TargetID) |
| 48 |
|
| 49 |
// Inject js to every new page |
| 50 |
page.MustEvalOnNewDocument(js) |
| 51 |
|
| 52 |
// Expose a function to the page to provide preview |
| 53 |
page.MustExpose("getPreview", func(url gson.JSON) (interface{}, error) { |
| 54 |
p := pool.MustGet(create) |
| 55 |
defer pool.Put(p) |
| 56 |
p.MustNavigate(url.Str()) |
| 57 |
return base64.StdEncoding.EncodeToString(p.MustScreenshot()), nil |
| 58 |
}) |
| 59 |
})() |
| 60 |
} |
| 61 |
|
| 62 |
var jsLib = get("https://unpkg.com/@popperjs/core@2") + get("https://unpkg.com/tippy.js@6") |
| 63 |
|
| 64 |
var js = fmt.Sprintf(`window.addEventListener('load', () => { |
| 65 |
%s |
| 66 |
|
| 67 |
function setup(el) { |
| 68 |
el.classList.add('x-set') |
| 69 |
tippy(el, {onShow: async (it) => { |
| 70 |
if (it.props.content.src) return |
| 71 |
let img = document.createElement('img') |
| 72 |
img.style.width = '400px' |
| 73 |
img.src = "data:image/png;base64," + await getPreview(el.href) |
| 74 |
it.setContent(img) |
| 75 |
}, content: 'loading...', maxWidth: 500}) |
| 76 |
} |
| 77 |
|
| 78 |
(function check() { |
| 79 |
Array.from(document.querySelectorAll('a:not(.x-set)')).forEach(setup) |
| 80 |
setTimeout(check, 1000) |
| 81 |
})() |
| 82 |
})`, jsLib) |
| 83 |
|
| 84 |
func get(u string) string { |
| 85 |
res, err := http.Get(u) |
| 86 |
utils.E(err) |
| 87 |
defer func() { _ = res.Body.Close() }() |
| 88 |
b, err := io.ReadAll(res.Body) |
| 89 |
utils.E(err) |
| 90 |
return string(b) |
| 91 |
} |
| 92 |
|
| 93 |
func waitExit() { |
| 94 |
fmt.Println("Press Enter to exit...") |
| 95 |
utils.E(fmt.Scanln()) |
| 96 |
os.Exit(0) |
| 97 |
} |