关键词:.Net,Webbrowser,JavaScript,communication

参考:

链接:msdn实例-简单的相互调用

代码:

  1. [PermissionSet(SecurityAction.Demand, Name="FullTrust")]
  2. [System.Runtime.InteropServices.ComVisibleAttribute(true)]
  3. public class Form1 : Form
  4. {
  5. private void Form1_Load(object sender, EventArgs e)
  6. {
  7. webBrowser1.AllowWebBrowserDrop = false;
  8. webBrowser1.IsWebBrowserContextMenuEnabled = false;
  9. webBrowser1.WebBrowserShortcutsEnabled = false;
  10. webBrowser1.ObjectForScripting = this;
  11. // Uncomment the following line when you are finished debugging.
  12. //webBrowser1.ScriptErrorsSuppressed = true;
  13. webBrowser1.DocumentText =
  14. "<html><head><script>" +
  15. "function test(message) { alert(message); }" +
  16. "</script></head><body><button " +
  17. "onclick=\"window.external.Test('called from script code')\">" +
  18. "call client code from script code</button>" +
  19. "</body></html>";
  20. }
  21. public void Test(String message)
  22. {
  23. MessageBox.Show(message, "client code");
  24. }
  25. private void button1_Click(object sender, EventArgs e)
  26. {
  27. webBrowser1.Document.InvokeScript("test",
  28. new String[] { "called from client code" });
  29. }
  30. }

链接0:codeproject中VB和js的交互

链接1:自定义数据类型的参数传递

代码:

  1. dynamic data = webBrowser1.Document.InvokeScript("eval", new[] {
  2. "(function() { return { latitude: 1, longitude: 2 }; })()" });
  3. MessageBox.Show("Data: " + data.latitude + ", " + data.longitude);

链接:添加js到已加载的网页

代码:

  1. private void addScript(HtmlElement head, string scriptSource)
  2. {
  3. HtmlElement lhe_script = head.Document.CreateElement("script");
  4. IHTMLScriptElement script = (IHTMLScriptElement)lhe_script.DomElement;
  5. script.src = scriptSource;
  6. head.AppendChild(lhe_script);
  7. }
  8. addScript(Webbrowser.Head, @"<Change File Path here>jquery.min.js");
  9. addScript(WebBrowser.Head, @"InjectMonitor.js");

Selenium则是一个利用http协议,来实现js和其他语言之间的通信,他强大的地方是js部分。

ide/main/src/content/selenium-runner.js

  1. // overide _executeCurrentCommand so we can collect stats of the commands executed
  2. _executeCurrentCommand : function() {
  3. /**
  4. * Execute the current command.
  5. *
  6. * @return a function which will be used to determine when
  7. * execution can continue, or null if we can continue immediately
  8. */
  9. var command = this.currentCommand;
  10. LOG.info("Executing: |" + command.command + " | " + command.target + " | " + command.value + " |");
  11. var handler = this.commandFactory.getCommandHandler(command.command);
  12. if (handler == null) {
  13. throw new SeleniumError("Unknown command: '" + command.command + "'");
  14. }
  15. command.target = selenium.preprocessParameter(command.target);
  16. command.value = selenium.preprocessParameter(command.value);
  17. LOG.debug("Command found, going to execute " + command.command);
  18. updateStats(command.command);
  19. this.result = handler.execute(selenium, command);
  20. this.waitForCondition = this.result.terminationCondition;
  21. },

selenium-api,CommandHandlerFactory是Api核心,在selenium-api.js,selenium-commandhandlers.js文件中实现。

如何用c#本地代码实现与Webbrowser中的JavaScript交互的更多相关文章

  1. 在WebBrowser中执行javascript脚本的几种方法整理(execScript/InvokeScript/NavigateScript) 附完整源码

    [实例简介] 涵盖了几种常用的 webBrowser执行javascript的方法,详见示例截图以及代码 [实例截图] [核心代码] execScript方式: 1 2 3 4 5 6 7 8 9 1 ...

  2. HTML中的javascript交互

    在Android开发中,越来越多的商业项目使用了Android原生控件与WebView进行混合开发,当然不仅仅就是显示一个WebView那么简单,有时候还需要本地Java代码与HTML中的javasc ...

  3. 关于delphi点击webbrowser中任意一点的问题

    关于delphi点击webbrowser中任意一点的问题 有时候我们需要delphi载入webbrowser1打开网页的时候 需要点击某一个点的位置 可能是坐标 可能是按钮 可能是其他的控件应该如何来 ...

  4. C#:WebBrowser中伪造referer,为何对流量统计器无效?

    使用webbrowser伪造referer的方法:webBrowser1.Navigate(url, "_self", null, "Referer:http://www ...

  5. webBrowser中操作网页元素全攻略

    原文 webBrowser中操作网页元素全攻略 1.获取非input控件的值: webBrowser1.Document.All["控件ID"].InnerText; 或webBr ...

  6. 从WebBrowser中取得Cookie 和 WebClient设置cookie!

    原文:从WebBrowser中取得Cookie 和 WebClient设置cookie! 从WebBrowser中取得Cookie 的代码 CookieContainer myCookieContai ...

  7. 如何用PC标签在列表页中调出文章内容 phpcms

    如何用PC标签在列表页中调出文章内容 phpcms v9 moreinfo=”"参数说明 {pc:content action="lists" catid="$ ...

  8. Android技巧分享——如何用电脑下载在Google play中应用的apk文件

    [Android技巧分享系列] 1.Android技巧分享——让官方模拟器和genymotion虚拟机飞起来 2.Android技巧分享——如何用电脑下载在Google play中应用的apk文件 G ...

  9. 如何用Python统计《论语》中每个字的出现次数?10行代码搞定--用计算机学国学

    编者按: 上学时听过山师王志民先生一场讲座,说每个人不论干什么,都应该学习国学(原谅我学了计算机专业)!王先生讲得很是吸引我这个工科男,可能比我的后来的那些同学听课还要认真些,当然一方面是兴趣.一方面 ...

随机推荐

  1. 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 ...

  2. win 下 nginx 的虚拟主机创建

    1.在nginx安装目录下的conf下创建vhost目录,用于存放虚拟主机配置文件.   2.在nginx安装目录下的conf/nginx.conf的http{}中加入 include vhost/* ...

  3. datatables分页

    一万条以下 var dataTables = $('#dataTables').DataTable(); 一万条以上 var dataTables = $('#dataTables').DataTab ...

  4. JMeter中用java修改文件名称

    import java.io.File; String NewDataPath=bsh.args[0]; File SrcFile= new File(NewDataPath+"AutoTe ...

  5. 如何在c语言中源文件调用另一个源文件的函数

    在源文件A1.c中调用A2.c 中的函数有两种方法: 1.在A2.c中有完整的函数定义,在A1.c中添加一下要用到的函数原型(声明)就可以了,例如:在A2.c中:有函数void A2(){...};在 ...

  6. dateframe_loc.iloc.ix

    import pandas as pddf=pd.DataFrame({ "a":[1,2,3], "b":[4,5,6], "c":[7, ...

  7. 低配NOSQL

    东西写的太简单了 都不好意思说是NOSQL 其实就是STL 的map容器记录了写入的信息 解析了下数据仅此. 分析的时候想了很多 比如学习redis的自写hash,动态调整hash表容量. 比如右值或 ...

  8. Android中webview跟JAVASCRIPT中的交互

    在android的应用程序中,可以直接调用webview中的javascript代码,而webview中的javascript代码,也可以去调用ANDROID应用程序(也就是JAVA部分的代码).下面 ...

  9. 2018.07.22 洛谷P1967 货车运输(kruskal重构树)

    传送门 这道题以前只会树剖和最小生成树+倍增. 而现在学习了一个叫做kruskal" role="presentation" style="position: ...

  10. oss上传文件夹-cloud2-泽优软件

    泽优软件云存储上传控件(cloud2)支持上传整个文件夹,并在云空间中保留文件夹的层级结构,同时在数据库中也写入层级结构信息.文件与文件夹层级结构关系通过id,pid字段关联. 本地文件夹结构 文件 ...