1

*Please note that this is not for a web based application, it's windows based.

I'm building an application where I will need the user to submit simple javascripts that will be run by the application.

The scripts will call functions that are part of the c# build.

An example:

C# code:

public void helloWorld()
{
    Debug.WriteLine("hello world");
}

Javascript submitted by user:

helloWorld();

The JavaScript would be parsed by the application at runtime and then call the required functions in my C# code.

Why?..

My app will be used by people with very little programming experience, they enter very simple JavaScripts and the app will attempt to automate a few tasks on the users computer. So my reason for using JavaScript is because it's simple and very easy to learn for someone with little experience.

Drahcir
  • 11,772
  • 24
  • 86
  • 128
  • 1
    Does it have to be JavaScript? This question contains a few useful hints: http://stackoverflow.com/questions/1394180/implementing-a-scripting-language-in-c-sharp – Jens Apr 16 '12 at 15:10

3 Answers3

3

It sounds like you want a JavaScript parser for your application. To be honest, I dont think what you're doing is possible, considering the context of the script and your code is different. However, this project seems to be doing something that may get you to the right place:

http://javascriptdotnet.codeplex.com/

Personally, I would think making some kind of XML format would be useful (like how UrlRewriter.net makes rewriting URLs easy):

 <xml>
 <commands>
     <!-- Expose a Set of Condition Objects to Select From -->
     <if condition="YourApplication.Conditions.RightClickOnDesktop">
         <print text="HelloWorld" />
     </if>
 </commands>
Tejs
  • 40,736
  • 10
  • 68
  • 86
  • Using XML is a good option that I hadn't considered, will explore further :) – Drahcir Apr 16 '12 at 15:08
  • Whether you write it in a Javascript-like syntax or some weird XML notation does not make any difference. The Javascript .NET looks promising though. – CodeCaster Apr 16 '12 at 15:29
3

Here is an example running a javascript code which, in turn, invokes a c# method

[System.Runtime.InteropServices.ComVisible(true)]
public class CSharpClass
{
    public void MsgBox(string s)
    {
        MessageBox.Show(s);
    }
}

-

Type scriptType = Type.GetTypeFromCLSID(Guid.Parse("0E59F1D5-1FBE-11D0-8FF2-00A0D10038BC"));

dynamic obj = Activator.CreateInstance(scriptType, false);
obj.Language = "Javascript";
obj.AddObject("mywindow", new CSharpClass(), true);
var result = obj.Eval(
    @"
        function test(){
            mywindow.MsgBox('hello'); 
        }
        test();
    "
    );
L.B
  • 114,136
  • 19
  • 178
  • 224
-1

Why do you "need the user to submit simple javascripts"? What is your application and what do users need it to do? Why have you decided a scripting language is the way to do this? I'm not saying that is the wrong answer, but that you have not justified this conclusion.

If your app will be used by "people with very little programming experience" I do not recommend implementing a scripting language. Basic concepts like source code and variables are very difficult for non-programmers to understand.

I suggest first investigating macro recording for user scripting. For .NET there is UI Automation and the White automation framework.

Dour High Arch
  • 21,513
  • 29
  • 75
  • 90