简体   繁体   English

如何在 WebBrowser 控件中注入 Javascript

[英]How to inject Javascript in WebBrowser control

There is a great tutorial here about windows forms这里有一个关于 Windows 窗体的很棒的教程

How to inject Javascript in WebBrowser control? 如何在 WebBrowser 控件中注入 Javascript?

I tried it and it works great我试过了,效果很好

But the problem is the objects used there is not recognized at wpf application.但问题是 wpf 应用程序无法识别那里使用的对象。 So what i am asking is what is the equivalent of the function below in wpf application.所以我要问的是 wpf 应用程序中下面的函数的等价物是什么。 Thank you.谢谢你。

HtmlElement head = webBrowser1.Document.GetElementsByTagName("head")[0];
HtmlElement scriptEl = webBrowser1.Document.CreateElement("script");
IHTMLScriptElement element = (IHTMLScriptElement)scriptEl.DomElement;
string srJquery = File.ReadAllText("jquery.txt");
element.text = srJquery;
head.AppendChild(scriptEl);  

the function above is working perfectly at windows form application c# 4.0 but the used objects such as HtmlElement is not recognized at WPF application.上面的函数在 windows 窗体应用程序 c# 4.0 上运行良好,但在 WPF 应用程序中无法识别使用的对象,例如 HtmlElement。

Does this work?这行得通吗?

    private void WebBrowser_LoadCompleted
       (object sender,
        System.Windows.Navigation.NavigationEventArgs e)
    {
        var webBrowser = sender as WebBrowser;

        var document
           = webBrowser.Document as mshtml.HTMLDocument;
        var ahref
           = document.getElementsByTagName("A").Cast<mshtml.IHTMLElement>().First();
        ahref.setAttribute(
           "onmouseenter",
           "javascript:alert('Hi');", 1);
    }

You need is Microsoft.mshtml (the .net API and not MS office one) reference.您需要的是Microsoft.mshtml (.net API 而不是 MS office one)参考。

Also please see this code for WPF webbrowser control that uses ObjectForScripting property of WebBrowser which can help you in injecting javascript...另请参阅使用WebBrowser ObjectForScripting属性的 WPF Webbrowser 控件的此代码,它可以帮助您注入 javascript...

http://blogs.msdn.com/b/wpf/archive/2011/05/27/how-does-wpf-webbrowser-control-handle-window-external-notify.aspx http://blogs.msdn.com/b/wpf/archive/2011/05/27/how-does-wpf-webbrowser-control-handle-window-external-notify.aspx

Let me know if this helps.如果这有帮助,请告诉我。

The best answer I found on the net was from http://clistax.com/question/qt/153748/st/4eb64b29180c4我在网上找到的最佳答案来自http://clistax.com/question/qt/153748/st/4eb64b29180c4

I believe the most simple method to inject Javascript in a WebBrowser Control HTML Document from c# is to invoke the "execStrip" method with the code to be injected as argument.我相信从 c# 在 WebBrowser Control HTML 文档中注入 Javascript 的最简单方法是调用“execStrip”方法,并将代码作为参数注入。

In this example the javascript code is injected and executed at global scope:在这个例子中,javascript 代码被注入并在全局范围内执行:

var jsCode="alert('hello world from injected code');";
WebBrowser.Document.InvokeScript("execScript", new Object[] { jsCode, "JavaScript" });

If you want to delay execution, inject functions and call them after:如果要延迟执行,请注入函数并在以下时间调用它们:

var jsCode="function greet(msg){alert(msg);};";
WebBrowser.Document.InvokeScript("execScript", new Object[] { jsCode, "JavaScript" });

WebBrowser.Document.InvokeScript("greet",new object[] {"hello world"});

This is valid for Windows Forms and WPF WebBrowser controls.这对 Windows 窗体和 WPF WebBrowser 控件有效。

This solution is not cross browser because "execScript" is defined only in IE and Chrome.此解决方案不是跨浏览器,因为“execScript”仅在 IE 和 Chrome 中定义。 But the question is about Microsoft WebBrowser controls and IE is the only one supported.但问题是关于 Microsoft WebBrowser 控件,而 IE 是唯一受支持的控件。

Dynamic makes this easy.动态使这变得容易。 You just have to remember we're working with a Microsoft.mshtml .您只需要记住我们正在使用Microsoft.mshtml

private void wb_Navigated(object sender, NavigationEventArgs e)
{

    if (wb != null && wb.Document != null)
    {
        dynamic document = wb.Document;
        dynamic script = document.createElement("script");
        script.type = @"text/javascript";
        script.text = @"alert('hello world');";
        try { document.head.appendChild(script); } catch (Exception ex) { } // Dynamic property head does not exist.
    }
}

Hint: Make sure your using Navigated and not Navigating to ensure the document is populated.提示:确保您使用Navigated而不是Navigating以确保填充文档。

I find myself setting script.text using System.IO.File.ReadAllText(pathToJs) to load local javascript files quite often.我发现自己script.text使用System.IO.File.ReadAllText(pathToJs)设置script.text来加载本地 javascript 文件。 Since the embedded we browser can often be outdated, it's become pretty standard for me to inject things like polyfill into my Windows Forms and WPF WebBrowser applications.由于嵌入式浏览器通常已经过时,因此将polyfill 之类的内容注入我的 Windows 窗体和 WPF WebBrowser应用程序已成为我的标准。

我发现这有助于讨论 wpf 中的 invokescript eqivilent 仅在 html 中执行脚本代码: http : //msdn.microsoft.com/en-us/library/cc452443.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM