zoukankan      html  css  js  c++  java
  • 如何用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文件中实现。

  • 相关阅读:
    【Vue】 修饰符sync
    【VUE】vue路由跳转的方式
    【Element】elementui的Cascader 级联选择器,在懒加载的时候数据无法回显的解决方案
    【ES6】利用ES6 Set 将数组去重
    【.NETCORE】Refit 框架
    【.NETCORE】ASP.NET Core SignalR
    【Visual Studio Code】驼峰翻译助手
    VueX(Vue状态管理模式)
    hdmi 随笔
    ad 差分布线 等长布线
  • 原文地址:https://www.cnblogs.com/yczz/p/4161454.html
Copyright © 2011-2022 走看看