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
猜你喜欢
- 今天查看登录日志,发现http_x_forwarded_for获取到的ip地址有些是内网ip地址,有些则是公网和内网ip地址一起获取到,用逗
- MybatisMyBatis ,是国内最火的持久层框架采用了ORM思想解决了实体类和数据库表映射的问题。对JDBC进行了封装,屏蔽了JDBC
- 这篇文章主要介绍了java通过Jsoup爬取网页过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的
- 前言现代化大型项目通常使用独立的数据库来存储数据,其中以采用关系型数据库居多。用于开发项目的高级语言(C#、Java等)是面向对象的,而关系
- 程序执行需要读取到安全敏感项必需在androidmanifest.xml中声明相关权限请求, 完整列表如下:1. android.permi
- Google的在Google I/O大会上推出了一款新的开发工具android studio。这是一款基于intellij IDE的开发工具
- 本文实例讲述了C#实现输入10个数存入到数组中并求max和min及平均数的方法。分享给大家供大家参考,具体如下:using System;u
- Java集合是java提供的工具包,包含了常用的数据结构:集合、链表、队列、栈、数组、映射等。Java集合工具包位置是java.util.*
- 本文实例讲述了C#检测是否有u盘插入的方法。分享给大家供大家参考。具体如下:该C#代码可监控是否有u盘插入,同时可以监控其它驱动器的变化us
- 单例模式是设计模式中最常见也最简单的一种设计模式,保证了在程序中只有一个实例存在并且能全局的访问到。比如在Android实际APP 开发中用
- 一、查询中排除标识字段1.1 测试查询@Testpublic void findAllTest() { List&
- 本文实例讲述了C#使用二分查找法判断指定字符的方法。分享给大家供大家参考,具体如下:private int sort_init(ref st
- 本文实例讲述了java对象转型的概念,分享给大家供大家参考。具体方法如下:对象转型(casting)注意事项如下:1、一个基类的引用类型变量
- public void refresh() throws BeansException, IllegalStateException { &
- @RequestParam设置默认可以传空值设置如下@RequestParam(value="CbqkJson[]",r
- SpringBoot JPA分页查询指定列并返回指定实体用习惯Mybatis,没用过jpa 真是各种踩坑了脑壳疼,一个分页弄老半天,原来就一
- 前言上一篇文章中我们通过自己开发了一个负载均衡组件,实现了随机算法的负载均衡功能,如果要实现其他算法,还需要修改代码增加相应的功能。这一篇文
- Jakarta commons fileupload组件可以处理HTTP请求及响应,很多时候被用来处理文件上传,但是近期发现,当我们自定义文
- 设计算法,计算两给定基因序列的相似程度。人类基因由4种核苷酸,分别用字母ACTG表示。要求编写一个程序,按以下规则比较两个基因序列并确定它们
- 一、前言随着互联网项目前后端分离方式的流行,前端与后端交给不同的人员开发,项目沟通成本也随之提高。主要表现在WebAPI接口的沟通,Swag