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

  • 相关阅读:
    移动端H5 QQ在线客服链接代码
    sql语句的优化技巧
    获取网页高度
    微信抽奖游戏
    H5中section和article标签之间的区别
    简易版九宫格相加数值相等
    两个单体内置对象_Global和Math
    特殊的引用类型
    引用类型-Array类型(二)~ 前端学习之路
    引用类型-Array类型~ 前端学习之路
  • 原文地址:https://www.cnblogs.com/yczz/p/4161454.html
Copyright © 2020-2023  润新知