I have kinda simple problem. The task is to display just part of webpage loaded in WebBrowser
control in WPF application. I looked up the problem on stack, but none of the solution helped me nor solved the problem. So what I learned is, that the easiest way to solve this is to inject a javascript/jquery function into the head of loaded page and then call it using WebBrowser.InvokeScript
to do DOM manipulation, but whenever I call my function I get script error.
Here are functions that I tried, but none of them worked (I know nothing about jQuery or javascript so I don't really know how to approach the problem):
This one I found on stack (2nd answer here)
function _changeDivs() {
$('body >').hide();
$('#dex1').show().prependTo('body');
}
I read the jQuery documentation on prepend
and prependTo
and from what i learned I tried: (here I just tried to get the script to not throw errors)
function _changeDivs() {
$('body').prepend($('#dex1'));
}
Simple alert
works without problem. Here is code that I use to inject the script:
private void PageLoaded(object sender, NavigationEventArgs e)
{
var webBrowser = (WebBrowser) sender;
var doc = (mshtml.HTMLDocument)webBrowser.Document;
var head = doc.getElementsByTagName("head").Cast<mshtml.HTMLHeadElement>().First();
var script = (mshtml.IHTMLScriptElement)doc.createElement("script");
script.text = "function _changeDivs() { $('body >').hide(); $('#dex1').show().prependTo('body'); }";
head.appendChild((mshtml.IHTMLDOMNode)script);
webBrowser.InvokeScript("_changeDivs");
}
In case you'd want to know when PageLoaded is called:
webBrowser.LoadCompleted += PageLoaded;