0

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;
Community
  • 1
  • 1
vip1all
  • 122
  • 11
  • What error do you have... do you really have a `$ is undefined` or something like that?... What url are you loading into the web Browser control... do you own that site source? Try `alert($('body').prepend)` just to check if you get `function` or `[object]`... – David Espino Apr 14 '17 at 00:25
  • I do not own the site source as I am loading facebook login page. When I try `alert($('body').prepend)` I still get error: Error: Script error, Code: 0, URL: https://static.xx.fbcdn.net/rsrc.php/v3/y6/r/BO2vbV5PICF.js, then it shows me COMException, but couldn't really read something useful there. Code error also was 0 so I couldn't even google the error by its code :/ – vip1all Apr 14 '17 at 00:35

0 Answers0