C#调用JS的几种方法

作者:Lulus 时间:2022-09-29 23:24:18 

cmd调用phantomjs

官方资料:http://phantomjs.org/quick-start.html

手动执行

从官方下载phantomjs.exe,拷贝它与要执行的js同目录
打开cmd,输入命令行(参考官方资料的命令行)


phantomjs XX.js 参数1 参数2

获得结果

使用C#执行


//注意:保证phantomjs.exe和js在生成目录下存在
string url = "传参";
//这里调用cmd.exe
Process pProcess = new Process();
//调用phantomjs.exe
pProcess.StartInfo.FileName = $"phantomjs.exe所在路径(可以是相对路径)";
pProcess.StartInfo.RedirectStandardOutput = true;
pProcess.StartInfo.UseShellExecute = false;
pProcess.EnableRaisingEvents = false;
//在phantomjs.exe里面执行的命令
pProcess.StartInfo.Arguments = $"Test2.js所在路径(可以是相对路径) {url}";
pProcess.Start();

char[] spliter = { '\r' };
StreamReader sReader = pProcess.StandardOutput;
string[] output = sReader.ReadToEnd().Split(spliter);

foreach (string s in output)
 Console.WriteLine(s);

pProcess.WaitForExit();

//取出计算结果
Console.WriteLine(output[0]);
pProcess.Close();

JS如下:
function Test() {
 //创建phantomjs对象
 var system = require('system');
 //取出参数
 var data = system.args[1];
 console.log(Math.floor(data));
}

Test();
phantom.exit();

示例代码:https://github.com/zLulus/NotePractice/tree/dev3/Console/CodeLibrary/ExcuteJsByPhantomjs

C#调用JS库

1.jint:https://github.com/sebastienros/jint

可用,但是没有JS的环境
jQuery support:https://github.com/sebastienros/jint/issues/240


//引用:Jint
string filePath = $"{Environment.CurrentDirectory}//ExcuteJs//TestJs.js";
string data1 = "1";
string data2 = "2";
string jsCode = System.IO.File.ReadAllText(filePath);
var square = new Engine()
       .SetValue("data1", data1) // define a new variable
       .SetValue("data2", data2) // define a new variable
       .Execute(jsCode) // execute a statement
       .GetCompletionValue() // get the latest statement completion value
       .ToObject(); // converts the value to .NET

示例代码:https://github.com/zLulus/NotePractice/tree/dev3/Console/CodeLibrary/ExcuteJs

2.Microsoft.JScript

https://docs.microsoft.com/zh-cn/dotnet/api/microsoft.jscript?redirectedfrom=MSDN&view=netframework-4.8&WT.mc_id=DT-MVP-5003010

3.使用CefSharp创造浏览器环境

CefSharp参考我的资料:https://www.cnblogs.com/Lulus/p/7998297.html

(PS:还有几篇关于CefSharp的资料,在此不一一列出)

4.Microsoft.ClearScript(比较新,没有实验)    
https://github.com/Microsoft/ClearScript  

比较绕的一种方式

控制台http请求网页->网页调用js->得到结果js对象->结果返回给控制台(即时通讯:SignalR)

即时通讯参考我的资料:http://www.cnblogs.com/Lulus/p/8780595.html

比较麻烦的一种方式

JS翻译成C#……是的,翻译=.=

来源:https://www.cnblogs.com/Lulus/p/8780599.html

标签:c#,调用,js
0
投稿

猜你喜欢

  • Mybatis TypeHandler接口及继承关系示例解析

    2021-11-19 03:33:34
  • Spring bean配置单例或多例模式方式

    2023-01-18 04:03:57
  • Android绘制验证码的实例代码

    2023-10-30 13:57:15
  • C#中ListView用法实例

    2021-10-15 06:10:24
  • C#中事件的定义和使用

    2022-02-15 13:34:00
  • Android实现背景图滑动变大松开回弹效果

    2022-10-15 10:45:15
  • Android自定义控件简单实现侧滑菜单效果

    2023-10-30 16:56:57
  • 对Jpa中Entity关系映射中mappedBy的全面理解

    2023-07-25 03:48:39
  • 出现SLF4J: Failed to load class “org.slf4j.impl.StaticLoggerBinder“.的解决方法

    2021-09-01 12:02:09
  • Java反射机制详解

    2023-10-01 12:18:04
  • springboot读取application.yaml文件数据的方法

    2023-09-06 05:29:24
  • Android为Tiny4412设备驱动在proc目录下添加一个可读版本信息的文件

    2022-09-24 05:37:20
  • Android中Glide加载圆形图片和圆角图片实例代码

    2022-08-06 08:26:59
  • Java游戏俄罗斯方块的实现实例

    2022-11-26 12:35:49
  • C#中string用法实例详解

    2022-06-11 03:22:17
  • Android开发实现的ViewPager引导页功能(动态加载指示器)详解

    2021-10-16 17:40:35
  • SpringBoot集成Caffeine缓存的实现步骤

    2023-08-23 05:44:11
  • Android Studio实现弹窗设置

    2022-08-20 19:49:03
  • 解决fcitx输入法在IDEA中输入法候选框无法跟随光标移动的问题

    2021-09-21 14:48:25
  • C++深入探究引用的使用

    2023-04-28 18:02:03
  • asp之家 软件编程 m.aspxhome.com