AngleSharp / AngleSharp.Js

:angel: Extends AngleSharp with a .NET-based JavaScript engine.
https://anglesharp.github.io
MIT License
103 stars 22 forks source link

How get executed javascript #67

Closed MHKarami97 closed 4 years ago

MHKarami97 commented 4 years ago

For example there is some code like this in some website:

<script> var test=12; var test1="num"; var result = test + "/" + test1; </script>

is there a way to get value of result after page loaded completely? in this code : "12/num"

FlorianRappl commented 4 years ago

If you have this script as-is on the page then, unfortunately, no.

It's like in a browser - you also cannot get the result of such an evaluation.

MHKarami97 commented 4 years ago

@FlorianRappl Thanks, yes it's on page, I get and save it as string, is there way to parse that string as javascript to excute value?

FlorianRappl commented 4 years ago

From the README:

var numEntries = document.ExecuteScript("document.querySelectorAll('div').length");

Does that help?

Note: The one above only works if you end with an expression, i.e., something that evaluates to a value. If you know already what variable is used, e.g., taking your example above, you could just append result.

Example:

// assumes there is only one script element on the apge
var scriptContent = document.QuerySelector("script").TextContent;
// assumes that the variable we are interested in is called "result"
var result = document.ExecuteScript($"{scriptContent}; result");