wpf webbrowser注入javascript

来源:互联网 发布:python string 倒序 编辑:程序博客网 时间:2024/05/16 03:55
            HTMLDocument dom = (HTMLDocument)web.Document;

           // web.InvokeScript("window.onload=function(){alert('hello')}");

            dom.parentWindow.execScript("window.onload = function () { window.external.WebClick(this); }");//向页面注入javascript语句

其中window.external.WebClick(this)实现网页javascript调用后台代码

    [ComVisible(true)]
    public class HtmlBridge
    {
        
        public void WebClick(object source)
        {
            HTMLWindow2 htmlWindow = (HTMLWindow2)source;
            //MessageBox.Show("Received: " + source);
            foreach (IHTMLElement archor in htmlWindow.document.links)
            {
                archor.setAttribute("target", "_self");
            }

            //将所有的FORM的提交目标,指向本窗体
            foreach (IHTMLElement form in htmlWindow.document.forms)
            {
                form.setAttribute("target", "_self");
            }
        }
    }


关联过程

web.ObjectForScripting = new HtmlBridge();


其中web为webbrowser实例


1 0
原创粉丝点击