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 Inject Javascript code into webpage and receive a return value #39

Open
AlexanderG84 opened this issue May 31, 2019 · 4 comments
Open

Comments

@AlexanderG84
Copy link

I can't understand how to inject a javascript code into the web page. Also how could I get a possible return from this script. I loaded the MasterDevs.ChromeDevTools Solution in C#.

Thanks

@AlexanderG84 AlexanderG84 changed the title Inject Javascript code into webpage and receive a return value How Inject Javascript code into webpage and receive a return value May 31, 2019
@ststeiger
Copy link

ststeiger commented Jul 8, 2019

string javaScriptToExecute = @"document.getElementById(""result"").innerHTML";


CommandResponse<EvaluateCommandResponse> evalResponse = await chromeSession.SendAsync(new EvaluateCommand
{
    Expression = javaScriptToExecute,
    // ContextId = 123
});

if (evalResponse.Result.ExceptionDetails != null)
    System.Console.WriteLine(evalResponse.Result.ExceptionDetails);
else
    System.Console.WriteLine(evalResponse.Result.Result.Value);

@AlexanderG84
Copy link
Author

Thank you very much Ststeiger! Works well! But If I wanted to see the web page on chrome and the impact of javascript on the page, is it possible? first i set an index in every tag( with a script), and after, i set border color only for some index( with inject a second script).

TY

@ststeiger
Copy link

ststeiger commented Jul 8, 2019

Sure, you can screenshot the page.

Full code in the sample program:
https://github.com/MasterDevs/ChromeDevTools/blob/master/source/Sample/Program.cs

If you need to wait a little after the functions executed:

// System.Threading.Thread.Sleep(waitInterval);
await System.Threading.Tasks.Task.Delay(waitInterval);

@AlexanderG84
Copy link
Author

thanks for the answer. I'm already using the example code. But my idea is the interact with the browser and see the page in "realtime". while browsing normally on chrome, I would like to be able to send scripts with external buttons from the browser. For example, while browsing the chrome tab, I wish I could press a key that colors all the "form" tags. it's possible? sorry if i wasn't clear

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