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

How to convert type *runtime.RemoteObject to string? #810

Closed
shelld3v opened this issue May 3, 2021 · 5 comments
Closed

How to convert type *runtime.RemoteObject to string? #810

shelld3v opened this issue May 3, 2021 · 5 comments

Comments

@shelld3v
Copy link
Contributor

shelld3v commented May 3, 2021

Currently, I want to get the raw response from chromedp.EvaluateAsDevTools. And from #526, I can see that res object must be in *runtime.RemoteObject type. But how I can convert it back to a string? To check for something like if res == "hello"?

invalid operation: res == "hello" (mismatched types *"github.com/chromedp/cdproto/runtime".RemoteObject and string)

Thanks a lot for taking time to read this!

@shelld3v
Copy link
Contributor Author

shelld3v commented May 3, 2021

Another question: I am doing something like this:

chromedp.EvaluateAsDevTools(`window.fool = function (txt){return txt}`, &res),
chromedp.Evaluate(`window.foo('a')`, &res),

Does the res after chromedp.EvaluateAsDevTools affects the result from chromedp.Evaluate?

@ZekeLu
Copy link
Member

ZekeLu commented May 4, 2021

for Q1:

if res.Type == "string" {
	val := string(res.Value)
	log.Println(val)
}

Please note that if the returned value is of type string (no undefined, you don't have to use runtime.RemoteObject. var res string should be more easy.


for Q2:

Try to combine the javascript code.

var res string

// ...
	chromedp.Evaluate("window.foo = function (txt){return txt};window.foo('a')", &res),
// ...

@shelld3v
Copy link
Contributor Author

shelld3v commented May 4, 2021

for Q2:

I have already thought about that, but what if the first evaluation returns an value, the second too, and I only want the second?

@ZekeLu
Copy link
Member

ZekeLu commented May 4, 2021

@shelld3v You can have a test yourself.

@shelld3v
Copy link
Contributor Author

shelld3v commented May 4, 2021

@shelld3v You can have a test yourself.

I am wondering if window.foo = function (txt){return txt} returns anything

@shelld3v shelld3v closed this as completed May 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants