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

EmulationSetEmulatedMedia is not working #641

Closed
denggj28 opened this issue Jul 2, 2022 · 13 comments
Closed

EmulationSetEmulatedMedia is not working #641

denggj28 opened this issue Jul 2, 2022 · 13 comments
Labels
question Questions related to rod upstream Upstream issue that can't solved by Rod

Comments

@denggj28
Copy link

denggj28 commented Jul 2, 2022

Rod Version: v0.107.3

The code to demonstrate your question

I am following document https://go-rod.github.io/i18n/zh-CN/#/emulation?id=%e9%85%8d%e8%89%b2%e6%96%b9%e6%a1%88%e5%92%8c%e5%aa%92%e4%bd%93

page := browser.MustPage()
_ = proto.EmulationSetEmulatedMedia{
    Media: "screen",
    Features: []*proto.EmulationMediaFeature{
        {Name: "prefers-color-scheme", Value: "dark"},
    },
}.Call(page)

What you got

But result of the following media query is false:

window.matchMedia('(prefers-color-scheme: dark)').matches

And I running my code with headless on Linux which have no any pointer devices, I try to emulate a device like this:

_ = proto.EmulationSetEmulatedMedia{
    Features: []*proto.EmulationMediaFeature{
        {Name: "any-pointer", Value: "fine"},
    },
}.Call(page)

But is not working too, media query is false:

window.matchMedia('(any-pointer: fine)').matches
@denggj28 denggj28 added the question Questions related to rod label Jul 2, 2022
@rod-robot
Copy link

Please fix the golang code in your markdown:

@@ golang markdown block 1 @@
1:1: expected 'package', found page
1:6: expected 'IDENT', found ':='
1:9: expected ';', found browser
@@ golang markdown block 2 @@
1:1: expected 'package', found _
1:3: expected 'IDENT', found '='
1:5: expected ';', found proto

generated by check-issue

@ysmood
Copy link
Member

ysmood commented Jul 2, 2022

Works fine for me:

func TestLab(t *testing.T) {
	g := setup(t)
	page := g.page

	out := page.MustEval(`() => window.matchMedia('(prefers-color-scheme: light)').matches`).Bool()

	g.True(out)

	err := proto.EmulationSetEmulatedMedia{
		Features: []*proto.EmulationMediaFeature{
			{Name: "prefers-color-scheme", Value: "dark"},
		},
	}.Call(page)
	g.E(err)

	out = page.MustEval(`() => window.matchMedia('(prefers-color-scheme: light)').matches`).Bool()

	g.False(out)
}

@ysmood ysmood added the needs info The description is not enough to tackle the problem label Jul 2, 2022
@denggj28
Copy link
Author

denggj28 commented Jul 2, 2022

thanks for your reply.
Your testing works, but I lanuch with headful mode, type this line in devtool console tab:

window.matchMedia('(prefers-color-scheme: dark)').matches

the result is false

and, any-pointer, any-hover, pointer, hover, whether Eval method nor devtool console tab, I can not see any effect.

@ysmood
Copy link
Member

ysmood commented Jul 2, 2022

Could you provide the full code that can reproduce the issue like what I did?

@denggj28
Copy link
Author

denggj28 commented Jul 2, 2022

there is my demo code

package main

import (
	"fmt"
	"github.com/go-rod/rod"
	"github.com/go-rod/rod/lib/launcher"
	"github.com/go-rod/rod/lib/proto"
	"time"
)

func main() {
	URL := launcher.New().
		Set("no-sandbox").
		Set("disable-gpu").
		Headless(true).
		MustLaunch()

	browser := rod.New().
		ControlURL(URL).
		MustConnect()

	page := browser.MustPage()

	m := proto.EmulationSetEmulatedMedia{
		Features: []*proto.EmulationMediaFeature{
			{Name: "any-pointer", Value: "fine"},
			{Name: "any-hover", Value: "hover"},
		},
	}
	err := m.Call(page)
	if err != nil {
		panic(err)
	}

	output := page.MustEval(`() => window.matchMedia('(any-pointer: fine)').matches`).Bool()
	// output is false
	fmt.Println(output)

	output = page.MustEval(`() => window.matchMedia('(any-hover: hover)').matches`).Bool()
	// output is false too
	fmt.Println(output)

	page.MustNavigate("https://arh.antoinevastel.com/bots/")

	time.Sleep(5 * time.Second)

	page.MustScreenshot("")
}

and the screenshot of fingerprint

screenshot

you known, my remote computer have no any pointer devices, I want to emulate a device via override media feature, but I failed.

@ysmood
Copy link
Member

ysmood commented Jul 2, 2022

Please read rule 4 in #322.

@denggj28
Copy link
Author

denggj28 commented Jul 2, 2022

@go-rod/everyone

I have simplified my code, and build a binary, running on remote, the same problem

@ysmood
Copy link
Member

ysmood commented Jul 3, 2022

Still, works fine for me, can you try the code below:

func TestLab(t *testing.T) {
	defaults.Show = true

	g := setup(t)
	page := g.page

	err := proto.EmulationSetEmulatedMedia{
		Features: []*proto.EmulationMediaFeature{
			{Name: "prefers-color-scheme", Value: "light"},
		},
	}.Call(page)
	g.E(err)

	out := page.MustEval(`() => window.matchMedia('(prefers-color-scheme: light)').matches`).Bool()

	g.True(out)

	err = proto.EmulationSetEmulatedMedia{
		Features: []*proto.EmulationMediaFeature{
			{Name: "prefers-color-scheme", Value: "dark"},
		},
	}.Call(page)
	g.E(err)

	out = page.MustEval(`() => window.matchMedia('(prefers-color-scheme: light)').matches`).Bool()

	g.False(out)
}

@denggj28
Copy link
Author

denggj28 commented Jul 3, 2022

I found your code works fine only use MustEval method, negative in the browser console, maybe browser changed the result.
And any-pointer, any-hover features are no effect, either MustEval method or browser console.

@ysmood
Copy link
Member

ysmood commented Jul 3, 2022

Better to always show you test code, or modify my code, maybe you have bugs in your test code. Read the step 3 in https://github.com/go-rod/rod/blob/master/.github/ISSUE_TEMPLATE/question.md

@denggj28
Copy link
Author

denggj28 commented Jul 3, 2022

I find the same issue on Puppeteer.
puppeteer/puppeteer#5096

@ysmood
Copy link
Member

ysmood commented Jul 4, 2022

Seems like an upstream bug.

@ysmood ysmood added upstream Upstream issue that can't solved by Rod and removed needs info The description is not enough to tackle the problem labels Jul 4, 2022
@denggj28
Copy link
Author

denggj28 commented Jul 5, 2022

that is a bad news for anti fingerprint scan 😢

@denggj28 denggj28 closed this as not planned Won't fix, can't repro, duplicate, stale Jul 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Questions related to rod upstream Upstream issue that can't solved by Rod
Projects
None yet
Development

No branches or pull requests

3 participants