AngleSharp / AngleSharp.Js

:angel: Extends AngleSharp with a .NET-based JavaScript engine.

Home Page:https://anglesharp.github.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How get executed javascript

MHKarami97 opened this issue · comments

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"

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.

@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?

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");