2

I have a JavaScript file with variables defined and values assigned to them. Is it possible to read the .js file with C# and easily retrieve the variables and values? I know what the variable names will be, but the values will be a json string. I'm hoping I don't have to resort to regular expressions.

EDIT: I do not have control over the contents of the JavaScript file. A third party drops the file to a location and I need to pick it up, read it, and parse it.

EDIT 2: Ok, forget I mentioned json. That actually has nothing to do with my question...which at this point doesn't seem to have a simple solution. So I went with reading the .js file line by line, searching for the variable name, and getting the value. Code below for reference.

using (StreamReader r = new StreamReader("myFile.js"))
{
  while(r.Peek() >= 0)
    {
      var line = r.ReadLine();
      if(line.IndexOf("myVariableName") > -1)
      {
         var arr = line.split("=");
         var variable = arr[0].Trim();
         var value = arr[1].Trim();
      }
    }
}
nthpixel
  • 3,041
  • 3
  • 30
  • 42
  • So you want to parse JSON content from a file? – Peter Gluck Jul 27 '14 at 06:12
  • possible duplicate of [How to parse json in C#?](http://stackoverflow.com/questions/6620165/how-to-parse-json-in-c) – Peter Gluck Jul 27 '14 at 06:13
  • Ultimately, I do have to parse json in C# but that's not the question I'm asking. That part I have no issues doing. My issue is, the JavaScript file has multiple variables defined, each with a json value assigned to it. When I read in the .js file with C#, is there a way to automatically convert all the variables/values in the js file to C# variables/values? Or is the only way to do it would be to use StreamReader's ReadLine() method? – nthpixel Jul 27 '14 at 06:25
  • I don't know a library that does that for you. Since this is quite a specific case I guess you will have to parse the .js file yourself. I hope it comes in an easy to read form. – Moti Azu Jul 27 '14 at 06:58
  • Probably too much work but there is a nodejs based parser UglifyJS https://github.com/mishoo/UglifyJS – Mike Cheel Jul 29 '14 at 01:59

2 Answers2

2

I had a similar issue in a Browser Helper Object I needed to to create to extract data from a web generated report and store it in a local file system.

I did following to access js values:

mshtml.IHTMLDocument document = (mshtml.IHTMLDocument)this.Explorer.IWebBrowser_Document;
object script = document.Script;

if (script != null)
{
    // Make the call:
    var jsvar = script.GetType().InvokeMember("eval", BindingFlags.InvokeMethod, null, script, new object[] { "HIS_VisitNumber" });

    // Cast...
    _strHISVisit = (string)jsvar;

    // Release the script object, and possibly the document object.
    Marshal.ReleaseComObject(script);
}

Here this.Explorer is the explorer object that is available through the Band Object (the BHO).

But of course, this approach is only possible if you would be able to leverage a browser (IE) which has all the facilities to load and execute js.

If you could not do such a thing, I guess what you would need is a full C# js parser or am I not understanding your question correctly?

In the latter case, you could use Javascript .NET

Kris
  • 2,100
  • 5
  • 31
  • 50
1

If you are a web developer and want to send data from a JavaScript file (client side) to a C# file (server side), you can use jQuery Ajax. You can pass variables to a function in a C# file. Remember that you must add the [WebMethod] attribute to your C# function, and the function must be static.

$.ajax({
      type: "post",
      url: "Default.aspx/YourMethodName",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                data: JSON.stringify({ varOne: one , varTwo:two }),
                success: function (v) {
                    alert("Success");
                },
                error: function (error) {
                    alert("Error");
                },
                complete: function () {
                    alert("Complete");
                }
            });
Jamal
  • 763
  • 7
  • 22
  • 32
HamidrezaTA
  • 29
  • 1
  • 5
  • We're getting the JavaScript file from a third party so all we can do is read it (in C#) and try to extract the data. I'll edit my question to be more clear. – nthpixel Jul 27 '14 at 06:07