📜 ⬆️ ⬇️

Running javascript in a C # program



The other day I looked through popular libraries on codeplex and saw a couple of libraries for working with javascript there. I immediately remembered the case when I had to write a small program for interacting with my account on one site. Therefore, without hesitation, I decided to try how they would have coped with that task.

Under the cut, I will tell you about the unrelated interaction with the site, how I solved the problem at that time and whether both libraries were able to cope with the same task. I’ll say right away that the following libraries will be discussed: jint and javascriptdotnet .

I will not give a link to the site, as well as examples of javascript functions that I used when testing, so that readers do not experiment with this resource, it will be discussed in general terms.
')
I will start with a small story of how development began, I went to the site, went to the office, then I loaded fiddler and started watching and recording which parameters are sent to where, by what methods and by what links. Pretty quickly, I sketched an algorithm for myself and started to implement it, but I ran into a problem, it turned out that one of the parameters that I took for a random number was not that, later it turned out that the call to the random () function in the most visible place served only to distract attention, what actually happens there helped me to install an add-on for Firefox called javascript deobfuscator , for those who are not familiar with this or similar add-ons, it looks like this when opening on google.com:



In my case it was somewhat easier, because no background scripts were executed, but it was clearly visible which scripts were executed with a particular action. It was then that I learned that to get my "random" number, more than 200 lines of code or a little more than 15 functions should be executed. The first thing I thought about was that I would quickly translate this code into C # and finish it, but the problem was that my javascript knowledge was superficial, and all the code was passed through the obfuscator and it was clear from the code that there were some cryptographic transformations occur, so I decided that it would be easier to execute this code in its original form.

In order to check whether my code is actually executed, I immediately chose the easiest way - using the webBrowser component, an example from Msdn:

webBrowser1.DocumentText =
"<html><head><script>" +
"function test(message) { alert(message); }" +
"</script></head><body><button " +
"onclick=\"window.external.Test('called from script code')\">" +
"call client code from script code</button>" +
"</body></html>" ;


* This source code was highlighted with Source Code Highlighter .

After that, just call the following code:
webBrowser1.Document.InvokeScript( "test" , new String [] { "called from client code" });

But this example is only suitable for the test, because I categorically did not want to use the webBrowser component in my program, so I began to look for other solutions and found the following example:

Steps for creating .dll:

1. Create a file in Notepad, only with this line:
class EvalClass { function Evaluate(expression: String) { return eval(expression); } }

2. Save the file as C: \ MyEval.js
3. Open VS Command Prompt (Start, Programs, VS, VS Tools)
4. Write Cd \ to be at the root of C: \
5. Write
jsc /t:library C:\MyEval.js

6. After that, a new file MyEval.dll will appear.
7. Copy MyEval.dll to the project and add a link to it (also need to refer to Microsoft.Jscript.dll).
8. Then you can access the library in the following way:
EvalClass jScriptEvaluator = new EvalClass();
object objResult = jScriptEvaluator.Evaluate(“1==1 && 2==2”);

objResult True.


In my case, I did everything absolutely as described above with the only difference that in the class I had much more functions, in fact at the time this code was enough for me.

Jint library


License: The MIT License (MIT)

First of all, I decided to try the examples, they all earned, but as I noticed that the codeplex also had problems with the parser, which “ate” brackets in some places, having a little corrected the examples, they all worked. Then I began to try his work on his set of functions, but unfortunately I could not get the library to return the desired value to me, googled, read the discussions. I tried to change something, but unfortunately I could not overcome String.fromCharCode ():

Jint.JintException: Method not defined: fromCharCode

The library connects as standard, add links and write:
using Jint;

After that, such a code is enough to call the function:
StreamReader sr = new StreamReader( @"C:\habr_javascript\MyEval.js" );
string script = sr.ReadToEnd();

JintEngine engine = new JintEngine();
engine.Run(script);

object num = engine.Run( "Evaluate(42210607);" );
MessageBox.Show(num.ToString());


* This source code was highlighted with Source Code Highlighter .


Javascriptdotnet library


License: New BSD License (BSD)

Having started one example, I immediately began to try my test example and bingo, it all worked the first time.

Specify the link to the library, add the using directive:
using Noesis.Javascript;

And we write code that is almost identical to the previous one:
StreamReader sr = new StreamReader( @"C:\habr_javascript\MyEval.js" );
string script = sr.ReadToEnd();

JavascriptContext context = new JavascriptContext();
context.Run(script);

object num = context.Run( "Evaluate(42210607);" );
MessageBox.Show(num.ToString());


* This source code was highlighted with Source Code Highlighter .


Conclusion


I would not like to say something bad about jint, because Most likely I just did not understand and it works no worse than javascriptdotnet. Although my program has already been working for more than 6 months, but I immediately wanted to simplify possible edits, since if the site developers change literally one character, then I have to rebuild the library, and in the case of jint and javascriptdotnet, I just need to edit the resource file. It’s also hard to imagine how you can add functions during program execution. The jint library is developing more actively and the latest release was most recently on February 14, 2011, when javascriptdotnet was updated on September 16, 2010. Libraries can be used not only for the method specified in the article, but also for testing your website. On the same stackoverflow, I saw that the developers used jint for testing server-side javascript, in fact, they also mention jint more. In general, I hope that this article was useful to you and, if necessary, it will be useful for you to interact with C # with javascript.

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


All Articles