How to get javascript variable value from HTML using CsQuery

How to get javascript variable value from DOM using CsQuery?

<script type="text/javascript"> dealerdata = "HelloWorld" </script> 
+4
source share
1 answer

CsQuery only handles HTML - not javascript. Thus, you can easily get the contents of a script block as follows:

 CQ dom = @"<script type='text/javascript'> dealerdata = 'HelloWorld' </script>"; var script = dom["script"].Text(); // script == "dealerdata = 'HelloWorld' 

... but then you are on your own, this is JavaScript. In your example, this would be trivial:

 string[] parts = script.Split('='); string value = parts[1].Trim(); 

.. but this is only because you know exactly what the entrance looks like. For typical use cases, when you do not know in what context your purpose may be, this will not help you.

If you need to parse Javascript in .NET, I recommend the Jurassic project, a great JavaScript compiler. If speed is paramount, check out javascript.net . This wraps the Google V8 engine and will be much faster than the Jurassic, but will have .NET dependencies.

+2
source

Source: https://habr.com/ru/post/1484760/


All Articles