WPF 网页浏览器 HTML 文档

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12834742/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 05:44:12  来源:igfitidea点击:

WPF WebBrowser HTMLDocument

c#wpf

提问by Alvin

I'm trying to inject some javascript code to prevent javascript error popup, but I cannot find HTMLDocumentand IHTMLScriptElementin WPF:

我正在尝试注入一些 javascript 代码来防止 javascript 错误弹出,但我在 WPF 中找不到HTMLDocumentIHTMLScriptElement

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);
    }
}

回答by HughHughTeotl

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

澄清Microsoft.mshtml一下,不是“使用”,而是参考。

Full solution:

完整解决方案:

  1. Add a project reference to Microsoft.mshtml

  2. Add using mshtml;

  1. 添加项目引用 Microsoft.mshtml

  2. 添加 using mshtml;

回答by Alvin

I have solved the problem by using:

我已经通过使用解决了这个问题:

Microsoft.mshtml;