Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Navigator.platform property reverts after navigation #908

Closed
6b70 opened this issue Jul 22, 2023 · 2 comments
Closed

Navigator.platform property reverts after navigation #908

6b70 opened this issue Jul 22, 2023 · 2 comments
Labels
question Questions related to rod

Comments

@6b70
Copy link

6b70 commented Jul 22, 2023

Rod Version: v0.113.4

The code to demonstrate your question

func navigatorTest() {
	chromePath, found := launcher.LookPath()
	if !found {
		panic("chrome not found")
	}

	u := launcher.New().
		Headless(false).
		Bin(chromePath).
		Leakless(true).
		MustLaunch()

	browser := rod.New().NoDefaultDevice().ControlURL(u).MustConnect()
	defer browser.MustClose()

	userAgentOptions := &proto.NetworkSetUserAgentOverride{
		UserAgent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36",
		Platform:  "MacIntel",
	}
	page := browser.MustPage("")
	err := page.SetUserAgent(userAgentOptions)
	if err != nil {
		panic(err)
	}

	page.MustNavigate("https://example.com/")
	utils.Sleep(20) //Check navigator.platform in console
	page.MustNavigate("https://google.com/")
	utils.Sleep(20) //Check navigator.platform in console
}

What you got

navigator.platform value in chrome dev console reverts from MacIntel to Win32 with the second page navigation.

What you expected to see

The value of all navigator properties to stay the same when navigating in the same page context.
The navigator.useragent property doesn't change after the navigation to the second site in this same code.

I am not sure if this is expected behavior when working with changing pages?

@6b70 6b70 added the question Questions related to rod label Jul 22, 2023
@ysmood
Copy link
Member

ysmood commented Jul 22, 2023

Works fine to me:

package main

import (
	"fmt"

	"github.com/go-rod/rod"
	"github.com/go-rod/rod/lib/proto"
)

func main() {
	page := rod.New().MustConnect().MustPage("")
	page.MustSetUserAgent(&proto.NetworkSetUserAgentOverride{
		UserAgent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36",
		Platform:  "XXXX",
	})

	page.MustNavigate("https://example.com/").MustWaitStable()
	fmt.Println(page.MustEval(`() => navigator.platform`).String())

	page.MustNavigate("https://google.com/").MustWaitStable()
	fmt.Println(page.MustEval(`() => navigator.platform`).String())
}

@6b70
Copy link
Author

6b70 commented Jul 22, 2023

I think my issue was due chrome devtools. The overridden navigator.platform property was preserved during navigation as long as DevTools isn't opened. When I open it (even if I dont run any commands), the navigator.platform reverts back when the page navigates to a new URL.
Maybe it resets the value when attaching the debugging session?
No issues when checking navigator with:

fmt.Println(page.MustEval(`() => navigator.platform`).String())

Thanks for the fast response

@6b70 6b70 closed this as completed Jul 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Questions related to rod
Projects
None yet
Development

No branches or pull requests

2 participants