如何用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文件中实现。

posted @ 2014-12-13 15:33  花生!~~  阅读(859)  评论(0编辑  收藏  举报