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();
      }
    }
}
5
  • So you want to parse JSON content from a file? Commented Jul 27, 2014 at 6:12
  • possible duplicate of How to parse json in C#? Commented Jul 27, 2014 at 6: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? Commented Jul 27, 2014 at 6: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. Commented Jul 27, 2014 at 6:58
  • Probably too much work but there is a nodejs based parser UglifyJS github.com/mishoo/UglifyJS Commented Jul 29, 2014 at 1:59

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

2 Comments

Interesting...I'll keep it for future reference.
Either approach should allow you to reach the variables inside of a piece of js so in principle this should solve your question.
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");
                }
            });

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.