Skip to content

Commit 71f5077

Browse files
NormalPunchysmood
authored andcommitted
add EvalOnNewDocument; Enable page as default
1 parent 719a1b9 commit 71f5077

File tree

5 files changed

+108
-1
lines changed

5 files changed

+108
-1
lines changed

browser.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ func (b *Browser) IncognitoE() (*Browser, error) {
147147
}
148148

149149
// PageE doc is similar to the method Page
150+
// If url is empty, the default target will be "about:blank".
150151
func (b *Browser) PageE(url string) (*Page, error) {
151152
if url == "" {
152153
url = "about:blank"
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package main
2+
3+
import (
4+
"github.com/go-rod/rod"
5+
)
6+
7+
func main() {
8+
9+
browser := rod.New().Connect()
10+
11+
page := browser.Pages()[0]
12+
13+
page.SetUserAgent(nil)
14+
15+
page.EvalOnNewDocument(`
16+
17+
// https://github.com/chromedp/chromedp/issues/396#issuecomment-503351342
18+
19+
(function(w, n, wn) {
20+
// Pass the Webdriver Test.
21+
Object.defineProperty(n, 'webdriver', {
22+
get: () => false,
23+
});
24+
25+
// Pass the Plugins Length Test.
26+
// Overwrite the plugins property to use a custom getter.
27+
Object.defineProperty(n, 'plugins', {
28+
// This just needs to have length > 0 for the current test,
29+
// but we could mock the plugins too if necessary.
30+
get: () => [1, 2, 3, 4, 5],
31+
});
32+
33+
// Pass the Languages Test.
34+
// Overwrite the plugins property to use a custom getter.
35+
Object.defineProperty(n, 'languages', {
36+
get: () => ['en-US', 'en'],
37+
});
38+
39+
// Pass the Chrome Test.
40+
// We can mock this in as much depth as we need for the test.
41+
w.chrome = {
42+
runtime: {},
43+
};
44+
45+
// Pass the Permissions Test.
46+
const originalQuery = wn.permissions.query;
47+
return wn.permissions.query = (parameters) => (
48+
parameters.name === 'notifications' ?
49+
Promise.resolve({ state: Notification.permission }) :
50+
originalQuery(parameters)
51+
);
52+
53+
})(window, navigator, window.navigator);
54+
55+
`)
56+
57+
page.Navigate("https://intoli.com/blog/not-possible-to-block-chrome-headless/chrome-headless-test.html")
58+
59+
wait := page.WaitRequestIdle()
60+
wait()
61+
62+
page.Screenshot("chrome-headless-test.jpg")
63+
64+
}

page.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ func (p *Page) SetExtraHeadersE(dict []string) (func(), error) {
9898
}
9999

100100
// SetUserAgentE Allows overriding user agent with the given string.
101+
// If req is nil, the default user agent will be the same as a mac chrome.
101102
func (p *Page) SetUserAgentE(req *proto.NetworkSetUserAgentOverride) error {
102103
if req == nil {
103104
req = &proto.NetworkSetUserAgentOverride{
@@ -110,6 +111,7 @@ func (p *Page) SetUserAgentE(req *proto.NetworkSetUserAgentOverride) error {
110111
}
111112

112113
// NavigateE doc is similar to the method Navigate
114+
// If url is empty, it will navigate to "about:blank".
113115
func (p *Page) NavigateE(url string) error {
114116
if url == "" {
115117
url = "about:blank"
@@ -391,6 +393,16 @@ func (p *Page) AddStyleTagE(url, content string) error {
391393
return err
392394
}
393395

396+
// EvalOnNewDocumentE Evaluates given script in every frame upon creation (before loading frame's scripts).
397+
func (p *Page) EvalOnNewDocumentE(js string) (proto.PageScriptIdentifier, error) {
398+
res, err := proto.PageAddScriptToEvaluateOnNewDocument{Source: js}.Call(p)
399+
if err != nil {
400+
return "", err
401+
}
402+
403+
return res.Identifier, nil
404+
}
405+
394406
// EvalE thisID is the remote objectID that will be the this of the js function, if it's empty "window" will be used.
395407
// Set the byValue to true to reduce memory occupation.
396408
func (p *Page) EvalE(byValue bool, thisID proto.RuntimeRemoteObjectID, js string, jsArgs Array) (*proto.RuntimeRemoteObject, error) {
@@ -524,6 +536,11 @@ func (p *Page) initSession() error {
524536
}
525537
p.SessionID = obj.SessionID
526538

539+
err = proto.PageEnable{}.Call(p)
540+
if err != nil {
541+
return err
542+
}
543+
527544
res, err := proto.DOMGetDocument{}.Call(p)
528545
if err != nil {
529546
return err

page_test.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func (s *S) TestClosePage() {
104104
}
105105

106106
func (s *S) TestLoadState() {
107-
s.False(s.page.LoadState(&proto.PageEnable{}))
107+
s.True(s.page.LoadState(&proto.PageEnable{}))
108108
}
109109

110110
func (s *S) TestPageContext() {
@@ -197,6 +197,22 @@ func (s *S) TestPageAddStyleTag() {
197197
s.Equal("rgb(0, 128, 0)", res.String())
198198
}
199199

200+
func (s *S) TestPageEvalOnNewDocument() {
201+
p := s.browser.Page("")
202+
defer p.Close()
203+
204+
p.EvalOnNewDocument(`
205+
Object.defineProperty(navigator, 'rod', {
206+
get: () => "rod",
207+
});`)
208+
209+
// to activate the script
210+
p.Navigate("")
211+
212+
s.Equal("rod", p.Eval("() => navigator.rod").String())
213+
214+
}
215+
200216
func (s *S) TestUntilPage() {
201217
page := s.page.Timeout(3 * time.Second).Navigate(srcFile("fixtures/open-page.html"))
202218
defer page.CancelTimeout()

sugar.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ func (b *Browser) Incognito() *Browser {
3232
}
3333

3434
// Page creates a new tab
35+
// If url is empty, the default target will be "about:blank".
3536
func (b *Browser) Page(url string) *Page {
3637
p, err := b.PageE(url)
3738
kit.E(err)
@@ -98,12 +99,14 @@ func (p *Page) SetExtraHeaders(dict ...string) (cleanup func()) {
9899
}
99100

100101
// SetUserAgent Allows overriding user agent with the given string.
102+
// If req is nil, the default user agent will be the same as a mac chrome.
101103
func (p *Page) SetUserAgent(req *proto.NetworkSetUserAgentOverride) *Page {
102104
kit.E(p.SetUserAgentE(req))
103105
return p
104106
}
105107

106108
// Navigate to url
109+
// If url is empty, it will navigate to "about:blank".
107110
func (p *Page) Navigate(url string) *Page {
108111
kit.E(p.NavigateE(url))
109112
return p
@@ -265,6 +268,12 @@ func (p *Page) AddStyleTag(url string) *Page {
265268
return p
266269
}
267270

271+
// EvalOnNewDocument Evaluates given script in every frame upon creation (before loading frame's scripts).
272+
func (p *Page) EvalOnNewDocument(js string) {
273+
_, err := p.EvalOnNewDocumentE(js)
274+
kit.E(err)
275+
}
276+
268277
// Eval js on the page. The first param must be a js function definition.
269278
// For example page.Eval(`n => n + 1`, 1) will return 2
270279
func (p *Page) Eval(js string, params ...interface{}) proto.JSON {

0 commit comments

Comments
 (0)