如何用c#本地代码实现与Webbrowser中的JavaScript交互
关键词:.Net,Webbrowser,JavaScript,communication
参考:
链接:msdn实例-简单的相互调用
代码:
[PermissionSet(SecurityAction.Demand, Name="FullTrust")]
[System.Runtime.InteropServices.ComVisibleAttribute(true)]
public class Form1 : Form
{
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.AllowWebBrowserDrop = false;
webBrowser1.IsWebBrowserContextMenuEnabled = false;
webBrowser1.WebBrowserShortcutsEnabled = false;
webBrowser1.ObjectForScripting = this;
// Uncomment the following line when you are finished debugging.
//webBrowser1.ScriptErrorsSuppressed = true;
webBrowser1.DocumentText =
"<html><head><script>" +
"function test(message) { alert(message); }" +
"</script></head><body><button " +
"onclick=\"window.external.Test('called from script code')\">" +
"call client code from script code</button>" +
"</body></html>";
}
public void Test(String message)
{
MessageBox.Show(message, "client code");
}
private void button1_Click(object sender, EventArgs e)
{
webBrowser1.Document.InvokeScript("test",
new String[] { "called from client code" });
}
}
链接0:codeproject中VB和js的交互
链接1:自定义数据类型的参数传递
代码:
dynamic data = webBrowser1.Document.InvokeScript("eval", new[] {
"(function() { return { latitude: 1, longitude: 2 }; })()" });
MessageBox.Show("Data: " + data.latitude + ", " + data.longitude);
链接:添加js到已加载的网页
代码:
private void addScript(HtmlElement head, string scriptSource)
{
HtmlElement lhe_script = head.Document.CreateElement("script");
IHTMLScriptElement script = (IHTMLScriptElement)lhe_script.DomElement;
script.src = scriptSource;
head.AppendChild(lhe_script);
}
addScript(Webbrowser.Head, @"<Change File Path here>jquery.min.js");
addScript(WebBrowser.Head, @"InjectMonitor.js");
Selenium则是一个利用http协议,来实现js和其他语言之间的通信,他强大的地方是js部分。
ide/main/src/content/selenium-runner.js
// overide _executeCurrentCommand so we can collect stats of the commands executed
_executeCurrentCommand : function() {
/**
* Execute the current command.
*
* @return a function which will be used to determine when
* execution can continue, or null if we can continue immediately
*/
var command = this.currentCommand;
LOG.info("Executing: |" + command.command + " | " + command.target + " | " + command.value + " |");
var handler = this.commandFactory.getCommandHandler(command.command);
if (handler == null) {
throw new SeleniumError("Unknown command: '" + command.command + "'");
}
command.target = selenium.preprocessParameter(command.target);
command.value = selenium.preprocessParameter(command.value);
LOG.debug("Command found, going to execute " + command.command);
updateStats(command.command);
this.result = handler.execute(selenium, command);
this.waitForCondition = this.result.terminationCondition;
},
selenium-api
,CommandHandlerFactory
是Api核心,在selenium-api.js
,selenium-commandhandlers.js
文件中实现。
如何用c#本地代码实现与Webbrowser中的JavaScript交互的更多相关文章
- 在WebBrowser中执行javascript脚本的几种方法整理(execScript/InvokeScript/NavigateScript) 附完整源码
[实例简介] 涵盖了几种常用的 webBrowser执行javascript的方法,详见示例截图以及代码 [实例截图] [核心代码] execScript方式: 1 2 3 4 5 6 7 8 9 1 ...
- HTML中的javascript交互
在Android开发中,越来越多的商业项目使用了Android原生控件与WebView进行混合开发,当然不仅仅就是显示一个WebView那么简单,有时候还需要本地Java代码与HTML中的javasc ...
- 关于delphi点击webbrowser中任意一点的问题
关于delphi点击webbrowser中任意一点的问题 有时候我们需要delphi载入webbrowser1打开网页的时候 需要点击某一个点的位置 可能是坐标 可能是按钮 可能是其他的控件应该如何来 ...
- C#:WebBrowser中伪造referer,为何对流量统计器无效?
使用webbrowser伪造referer的方法:webBrowser1.Navigate(url, "_self", null, "Referer:http://www ...
- webBrowser中操作网页元素全攻略
原文 webBrowser中操作网页元素全攻略 1.获取非input控件的值: webBrowser1.Document.All["控件ID"].InnerText; 或webBr ...
- 从WebBrowser中取得Cookie 和 WebClient设置cookie!
原文:从WebBrowser中取得Cookie 和 WebClient设置cookie! 从WebBrowser中取得Cookie 的代码 CookieContainer myCookieContai ...
- 如何用PC标签在列表页中调出文章内容 phpcms
如何用PC标签在列表页中调出文章内容 phpcms v9 moreinfo=”"参数说明 {pc:content action="lists" catid="$ ...
- Android技巧分享——如何用电脑下载在Google play中应用的apk文件
[Android技巧分享系列] 1.Android技巧分享——让官方模拟器和genymotion虚拟机飞起来 2.Android技巧分享——如何用电脑下载在Google play中应用的apk文件 G ...
- 如何用Python统计《论语》中每个字的出现次数?10行代码搞定--用计算机学国学
编者按: 上学时听过山师王志民先生一场讲座,说每个人不论干什么,都应该学习国学(原谅我学了计算机专业)!王先生讲得很是吸引我这个工科男,可能比我的后来的那些同学听课还要认真些,当然一方面是兴趣.一方面 ...
随机推荐
- 215. Kth Largest Element in an Array(QuickSort)
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...
- win 下 nginx 的虚拟主机创建
1.在nginx安装目录下的conf下创建vhost目录,用于存放虚拟主机配置文件. 2.在nginx安装目录下的conf/nginx.conf的http{}中加入 include vhost/* ...
- datatables分页
一万条以下 var dataTables = $('#dataTables').DataTable(); 一万条以上 var dataTables = $('#dataTables').DataTab ...
- JMeter中用java修改文件名称
import java.io.File; String NewDataPath=bsh.args[0]; File SrcFile= new File(NewDataPath+"AutoTe ...
- 如何在c语言中源文件调用另一个源文件的函数
在源文件A1.c中调用A2.c 中的函数有两种方法: 1.在A2.c中有完整的函数定义,在A1.c中添加一下要用到的函数原型(声明)就可以了,例如:在A2.c中:有函数void A2(){...};在 ...
- dateframe_loc.iloc.ix
import pandas as pddf=pd.DataFrame({ "a":[1,2,3], "b":[4,5,6], "c":[7, ...
- 低配NOSQL
东西写的太简单了 都不好意思说是NOSQL 其实就是STL 的map容器记录了写入的信息 解析了下数据仅此. 分析的时候想了很多 比如学习redis的自写hash,动态调整hash表容量. 比如右值或 ...
- Android中webview跟JAVASCRIPT中的交互
在android的应用程序中,可以直接调用webview中的javascript代码,而webview中的javascript代码,也可以去调用ANDROID应用程序(也就是JAVA部分的代码).下面 ...
- 2018.07.22 洛谷P1967 货车运输(kruskal重构树)
传送门 这道题以前只会树剖和最小生成树+倍增. 而现在学习了一个叫做kruskal" role="presentation" style="position: ...
- oss上传文件夹-cloud2-泽优软件
泽优软件云存储上传控件(cloud2)支持上传整个文件夹,并在云空间中保留文件夹的层级结构,同时在数据库中也写入层级结构信息.文件与文件夹层级结构关系通过id,pid字段关联. 本地文件夹结构 文件 ...