惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Martin Fowler
Martin Fowler
V
Visual Studio Blog
有赞技术团队
有赞技术团队
T
Tailwind CSS Blog
B
Blog
I
InfoQ
博客园 - 三生石上(FineUI控件)
阮一峰的网络日志
阮一峰的网络日志
F
Fortinet All Blogs
H
Help Net Security
博客园 - Franky
宝玉的分享
宝玉的分享
博客园 - 司徒正美
C
Check Point Blog
G
Google Developers Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Jina AI
Jina AI
T
The Blog of Author Tim Ferriss
MongoDB | Blog
MongoDB | Blog
云风的 BLOG
云风的 BLOG
A
About on SuperTechFans
罗磊的独立博客
大猫的无限游戏
大猫的无限游戏
IT之家
IT之家

博客园 - 花生!~~

[技术分享] 谈谈网页数据采集中的“结构化”难题:从手动 XPath 到智能识别算法的演进 windows10 .NET http请求 tls1.3问题 微信历史版本下archive Cefsharp开发相关注意 .net PostgreSQL 相关 .net4.0 请求HTTPS出错:未能创建 SSL/TLS 安全通道 c#中的gcAllowVeryLargeObjects和OutOfMemoryException XML hexadecimal value 0x__, is an invalid character 创建窗口句柄时出错(error creating window handle) .net程序和管理员权限的一些事 - 花生!~~ - 博客园 .net下WinDbg使用说明 在.net中修改Webbrowser控件的IE版本 System.Data.SQLite安装的相关问题 TextBox Ctrl+A不能全选的问题 HttpWebRequest 跳转后(301,302)ResponseUri乱码问题 在sqlite中,如何删除字段? how to drop a column in sqlite 如何在centos下配置redis开机自启动 WinForm中的图表控件Chart XPath高级用法(冰山一角)
如何用c#本地代码实现与Webbrowser中的JavaScript交互
花生!~~ · 2014-12-13 · via 博客园 - 花生!~~

关键词:.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文件中实现。