How to Inject JavaScript in Webbrowser Control

How to inject Javascript in WebBrowser control?

For some reason Richard's solution didn't work on my end (insertAdjacentText failed with an exception). This however seems to work:

HtmlElement head = webBrowser1.Document.GetElementsByTagName("head")[0];
HtmlElement scriptEl = webBrowser1.Document.CreateElement("script");
IHTMLScriptElement element = (IHTMLScriptElement)scriptEl.DomElement;
element.text = "function sayHello() { alert('hello') }";
head.AppendChild(scriptEl);
webBrowser1.Document.InvokeScript("sayHello");

This answer explains how to get the IHTMLScriptElement interface into your project.

How to inject a Javascript script into webpage using WebBrowser control?

Add a reference to mshtml

Sample Image

In whatever event you want to inject the JavaScript:

var doc = (mshtml.HTMLDocument)webBrowser1.Document;
var head = doc.getElementsByTagName("head").Cast<mshtml.HTMLHeadElement>().First();
var script = (mshtml.IHTMLScriptElement)doc.createElement("script");
script.text = "function myFunction() { alert(\"Hello!\");}";
head.appendChild((mshtml.IHTMLDOMNode)script);

In whatever event you want to invoke the JavaScript from:

webBrowser1.InvokeScript("myFunction");

Result:

Sample Image

How to inject Javascript in WebBrowser control

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.

Also please see this code for WPF webbrowser control that uses ObjectForScripting property of WebBrowser which can help you in injecting javascript...

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.

How to inject Javascript in the WP7 WebBrowser control?

Unfortunately WebBrowser.Document is not available on WP7. But you can create and call a JavaScript function using InvokeScript. Have a look over here where I describe how.

In short: you don't use .Document and C# but create a piece of JavaScript instead. You then call eval with this script as parameter to invoke it. Like this:

webBrowser1.InvokeScript("eval", "  ...code goes here... ");

How to inject and execute a javascript function without modifiting document in webbrowser control?

Try this:

void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
var anyScripts = webBrowser1.Document.GetElementsByTagName("script");
if (anyScripts == null || anyScripts.Count == 0)
{
// at least one <script> element must be present for eval to work
var script = webBrowser1.Document.CreateElement("script");
webBrowser1.Document.Body.AppendChild(script);
}

// use anonymous functions

dynamic func = webBrowser1.Document.InvokeScript("eval", new[] {
"(function() { return function(elem, color) { elem.style.backgroundColor = color; } })()" });

var body = this.webBrowser1.Document.Body.DomElement;

func(body, "red");
}


Related Topics



Leave a reply



Submit