Parsing HTML to get script variable value

c# html-agility-pack javascript


I'm trying to find a method of accessing data between tags returned by a server I am making HTTP requests to. The document has multiple tags, but only one of the tags has JavaScript code between it, the rest are included from files. I want to accesses the code between the script tag.

An example of the code is:

    // Some HTML

        var spect = [['temper', 'init', []],
                    ['fw\/lib', 'init', [{staticRoot: '//'}]],


    // More HTML

I'm looking for an ideal way to grab the data between 'spect' and parse it. Sometimes there is a space between 'spect' and the '=' and sometimes there isn't. No idea why, but I have no control over the server.

I know this question may have been asked, but the responses suggest using something like HTMLAgilityPack, and I'd rather avoid using a library for this task as I only need to get the JavaScript from the DOM once.

8/10/2013 12:13:51 AM

Accepted Answer

Very simple example of how this could be easy using a HTMLAgilityPack and Jurassic library to evaluate the result:

var html = @"<html>
             // Some HTML
               var spect = [['temper', 'init', []],
               ['fw\/lib', 'init', [{staticRoot: '//'}]],
             // More HTML

// Grab the content of the first script element
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
var script = doc.DocumentNode.Descendants()
                             .Where(n => n.Name == "script")

// Return the data of spect and stringify it into a proper JSON object
var engine = new Jurassic.ScriptEngine();
var result = engine.Evaluate("(function() { " + script + " return spect; })()");
var json = JSONObject.Stringify(engine, result);




Note: I am not accounting for errors or anything else, this merely serves as an example of how to grab the script and evaluate for the value of spect.

There are a few other libraries for executing/evaluating JavaScript as well.

11/24/2017 6:59:32 AM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow