要回答问题标题“如何在 C# 中解析和执行 JS”,这里是包装 Windows 脚本的代码段发动机。它支持 32 位和 64 位环境。
在您的具体情况下,这意味着根据 .JS 代码,您可能必须模拟/实现一些 HTML DOM 元素,例如“文档”、“窗口” ”等(使用“命名项目”功能和 MyItem 类。这正是 Internet Explorer 所做的)。
以下是一些示例你可以用它来做:
1)直接表达式求值:
Console.WriteLine(ScriptEngine.Eval("jscript", "1+2/3"));
将显示 1.66666666666667
2)带有可选参数的函数调用:
using (ScriptEngine engine = new ScriptEngine("jscript")) { ParsedScript parsed = engine.Parse("function MyFunc(x){return 1+2+x}"); Console.WriteLine(parsed.CallMethod("MyFunc", 3)); }
将显示6
3) 使用命名项和可选参数进行函数调用:
using (ScriptEngine engine = new ScriptEngine("jscript")) { ParsedScript parsed = engine.Parse("function MyFunc(x){return 1+2+x+My.Num}"); MyItem item = new MyItem(); item.Num = 4; engine.SetNamedItem("My", item); Console.WriteLine(parsed.CallMethod("MyFunc", 3)); } [ComVisible(true)] // Script engines are COM components. public class MyItem { public int Num { get; set; } }
将显示 10。
编辑:我已添加可以使用 CLSID 而不是脚本语言名称,因此我们可以重新使用新的快速 IE9“chakra”javascript 引擎,例如这个:
using (ScriptEngine engine = new ScriptEngine("{16d51579-a30b-4c8b-a276-0ff4dc41e755}")) { // continue with chakra now }
这是完整的来源:
/// <summary> /// Represents a Windows Script Engine such as JScript, VBScript, etc. /// </summary> public sealed class ScriptEngine : IDisposable { /// <summary> /// The name of the function used for simple evaluation. /// </summary> public const string MethodName = "EvalMethod"; /// <summary> /// The default scripting language name. /// </summary> public const string DefaultLanguage = JavaScriptLanguage; /// <summary> /// The JavaScript or jscript scripting language name. /// </summary> public const string JavaScriptLanguage = "javascript"; /// <summary> /// The javascript or jscript scripting language name. /// </summary> public const string VBScriptLanguage = "vbscript"; /// <summary> /// The chakra javascript engine CLSID. The value is {16d51579-a30b-4c8b-a276-0ff4dc41e755}. /// </summary> public const string ChakraClsid = "{16d51579-a30b-4c8b-a276-0ff4dc41e755}"; private IActiveScript _engine; private IActiveScriptParse32 _parse32; private IActiveScriptParse64 _parse64; internal ScriptSite Site; private Version _version; private string _name; [Guid("BB1A2AE1-A4F9-11cf-8F20-00805F2CD064"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] private interface IActiveScript { [PreserveSig] int SetScriptSite(IActiveScriptSite pass); [PreserveSig] int GetScriptSite(Guid riid, out IntPtr site); [PreserveSig] int SetScriptState(ScriptState state); [PreserveSig] int GetScriptState(out ScriptState scriptState); [PreserveSig] int Close(); [PreserveSig] int AddNamedItem(string name, ScriptItem flags); [PreserveSig] int AddTypeLib(Guid typeLib, uint major, uint minor, uint flags); [PreserveSig] int GetScriptDispatch(string itemName, out IntPtr dispatch); [PreserveSig] int GetCurrentScriptThreadID(out uint thread); [PreserveSig] int GetScriptThreadID(uint win32ThreadId, out uint thread); [PreserveSig] int GetScriptThreadState(uint thread, out ScriptThreadState state); [PreserveSig] int InterruptScriptThread(uint thread, out System.Runtime.InteropServices.ComTypes.EXCEPINFO exceptionInfo, uint flags); [PreserveSig] int Clone(out IActiveScript script); } [Guid("4954E0D0-FBC7-11D1-8410-006008C3FBFC"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] private interface IActiveScriptProperty { [PreserveSig] int GetProperty(int dwProperty, IntPtr pvarIndex, out object pvarValue); [PreserveSig] int SetProperty(int dwProperty, IntPtr pvarIndex, ref object pvarValue); } [Guid("DB01A1E3-A42B-11cf-8F20-00805F2CD064"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] private interface IActiveScriptSite { [PreserveSig] int GetLCID(out int lcid); [PreserveSig] int GetItemInfo(string name, ScriptInfo returnMask, out IntPtr item, IntPtr typeInfo); [PreserveSig] int GetDocVersionString(out string version); [PreserveSig] int OnScriptTerminate(object result, System.Runtime.InteropServices.ComTypes.EXCEPINFO exceptionInfo); [PreserveSig] int OnStateChange(ScriptState scriptState); [PreserveSig] int OnScriptError(IActiveScriptError scriptError); [PreserveSig] int OnEnterScript(); [PreserveSig] int OnLeaveScript(); } [Guid("EAE1BA61-A4ED-11cf-8F20-00805F2CD064"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] private interface IActiveScriptError { [PreserveSig] int GetExceptionInfo(out System.Runtime.InteropServices.ComTypes.EXCEPINFO exceptionInfo); [PreserveSig] int GetSourcePosition(out uint sourceContext, out int lineNumber, out int characterPosition); [PreserveSig] int GetSourceLineText(out string sourceLine); } [Guid("BB1A2AE2-A4F9-11cf-8F20-00805F2CD064"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] private interface IActiveScriptParse32 { [PreserveSig] int InitNew(); [PreserveSig] int AddScriptlet(string defaultName, string code, string itemName, string subItemName, string eventName, string delimiter, IntPtr sourceContextCookie, uint startingLineNumber, ScriptText flags, out string name, out System.Runtime.InteropServices.ComTypes.EXCEPINFO exceptionInfo); [PreserveSig] int ParseScriptText(string code, string itemName, IntPtr context, string delimiter, uint startingLineNumber, ScriptText flags, out object result, out System.Runtime.InteropServices.ComTypes.EXCEPINFO exceptionInfo); } [Guid("C7EF7658-E1EE-480E-97EA-D52CB4D76D17"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] private interface IActiveScriptParse64 { [PreserveSig] int InitNew(); [PreserveSig]
以上是如何在C#中解析并执行JavaScript代码?的详细内容。更多信息请关注PHP中文网其他相关文章!