简体   繁体   English

在C#中运行javascript

[英]Run javascript in C#

I found a fairly complex function in a greasemonkey script that I would like to use in my C# app. 我在我想在我的C#应用​​程序中使用的oilmonkey脚本中找到了一个相当复杂的函数。 Basically I am parsing a page and I need to collect all or 4 members of var avar = {}; 基本上,我正在解析页面,并且需要收集var avar = {};全部或4个成员var avar = {}; (i haven't done this yet but they are all strings using var avar.name = "val" ) (我还没有这样做,但是它们都是使用var avar.name = "val"字符串)

Then I need to call the gm func which returns a string and takes in 3 strings. 然后,我需要调用gm func,它返回一个字符串并接受3个字符串。 How can I call the function in C#? 如何在C#中调用该函数? I am using .NET 3.5 我正在使用.NET 3.5

I'm assuming that you are after some code-reuse on the server-side or in some other freestanding app that processes HTML data. 我假设您正在服务器端或其他处理HTML数据的独立应用程序中重复使用一些代码。

You can compile (at least a subset of) JavaScript in .net using the Microsoft.JScript.JScriptCodeProvider class -- though note that the class warns 您可以使用Microsoft.JScript.JScriptCodeProvider类在.net中编译JavaScript(至少是其子集)-尽管请注意该类会警告

This API supports the .NET Framework infrastructure and is not intended to be used directly from your code. 此API支持.NET Framework基础结构,不能直接在您的代码中使用。

Once compiled the assembly generated (as specified by the CompilerParameters supplied to the provider) should be dynamically loadable. 编译后,生成的程序集(由提供程序提供的CompilerParameters指定)应该可以动态加载。 It would be advisable to examine the generated assembly with a tool like Reflector to see what it is you've actually generated, in terms of classes and namespaces. 建议使用Reflector之类的工具检查生成的程序集,以查看您实际上在类和名称空间方面生成的程序集。

Disclaimer -- I've only ever used this technique with the CSharpCodeProvider acting on C# source, but I would expect there to be a reasonable level of compatibility across .net languages for this sort of thing. 免责声明 -我只在CSharpCodeProvider作用于C#源代码时才使用了此技术,但是我希望这种事情在.net语言之间具有合理的兼容性。

EDIT -- For an example of compiling JavaScript from C# see this blog post on Verifying JavaScript syntax using C# . 编辑-有关从C#编译JavaScript的示例,请参阅此博客文章“使用C#验证JavaScript语法”

First, you probably want to consider exactly why you're trying to do this. 首先,您可能想确切地考虑为什么要这样做。 Is it that you want to use the algorithm from the JS in C#? 是否要在C#中使用JS中的算法? If so, go ahead. 如果是这样,请继续。 If you want to use C# in client-side code (ie the browser), go investigate Silverlight instead. 如果要在客户端代码(即浏览器)中使用C#,请改为研究Silverlight。

Second, I'm not sure that what you're trying to do is actually possible. 其次,我不确定您要执行的操作是否确实可行。 Depending on what youre trying to achieve, you may be better off translating the Javascript from the Greasemonkey app into C# 3.5 (assuming that the script's licensing conditions allow this), for use in your app. 根据您要实现的目标,最好将Greasemonkey应用程序中的Java脚本转换为C#3.5(假设脚本的许可条件允许这样做),以便在您的应用程序中使用。

The translation shouldn't be hugely difficult - C# has been getting more and more like JS in the last few versions. 翻译应该不会很困难-在最近的几个版本中,C#越来越像JS。 Just watch out for the "var" keyword; 只要注意“ var”关键字即可; it means something slightly different in C# to what it means in JS (contrast "type inference" in C# with "dynamic typing" in JS). 它在C#中的含义与JS中的含义略有不同(将C#中的“类型推断”与JS中的“动态类型”进行对比)。

Of course, maintaining both versions of the code after you've done this will be tricky and painful. 当然,在完成这两个版本的代码后,这将很棘手且痛苦。 I recommend keeping 1 authoritative version of the code if you can. 如果可以的话,我建议保留1个权威版本的代码。

Good luck! 祝好运!

Can you provide more information about your script and what you want to accomplish? 您能否提供有关脚本以及想要完成的工作的更多信息? Most Greasemonkey scripts interact with the DOM via the use of Javascript. 大多数Greasemonkey脚本通过使用 Javascript与DOM交互。 You can run Javascript in C# but the DOM will not be available to you. 您可以在C#中运行Javascript,但是DOM将不可用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM