Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF WebBrowser HTMLDocument

Tags:

c#

wpf

I'm trying to inject some javascript code to prevent javascript error popup, but I cannot find HTMLDocument and IHTMLScriptElement in WPF:

var doc = browser.Document as HTMLDocument;

if (doc != null)
{
    //Create the sctipt element 
    var scriptErrorSuppressed = (IHTMLScriptElement)doc.createElement("SCRIPT");
    scriptErrorSuppressed.type = "text/javascript";
    scriptErrorSuppressed.text = m_disableScriptError;
    //Inject it to the head of the page 
    IHTMLElementCollection nodes = doc.getElementsByTagName("head");
    foreach (IHTMLElement elem in nodes)
    {
        var head = (HTMLHeadElement)elem;
        head.appendChild((IHTMLDOMNode)scriptErrorSuppressed);
    }
}
like image 783
Alvin Avatar asked Oct 11 '12 07:10

Alvin


2 Answers

To clarify, Microsoft.mshtml isn't the 'using', it's a reference.

Full solution:

  1. Add a project reference to Microsoft.mshtml

  2. Add using mshtml;

like image 139
HughHughTeotl Avatar answered Nov 06 '22 16:11

HughHughTeotl


I have solved the problem by using:

Microsoft.mshtml;
like image 28
Alvin Avatar answered Nov 06 '22 16:11

Alvin