【问题标题】:How to block the images on web browser如何在网络浏览器上阻止图像
【发布时间】:2012-08-25 23:54:41
【问题描述】:

我正在使用WebBrowser 构建一个 C# 应用程序,并且我正在尝试找出一种阻止图像的方法,即让它们在网站加载时不显示(以便网站加载更容易)。

我尝试通过 webBrowser1.DocumentText 获取 webBrowser1.DocumentText 并使用 Regex.Replace 删除图像来删除 <img> 标签,但是当我使用代码时,它显示了一个带有 aaa 的空白页.有没有更好的方法来删除图像?非常感谢任何帮助。

代码:

var html_source = webBrowser1.DocumentText;
var newOne = Regex.Replace(html_source, "<img.*/>", "", RegexOptions.Multiline);
webBrowser1.DocumentText = newOne + "aaa";

更新:

我已经尝试过下面的代码(仅用于测试),但仍然只显示aaa

var html_source = webBrowser1.DocumentText;
webBrowser1.DocumentText = html_source + "aaa";

【问题讨论】:

标签: c# winforms webbrowser-control image


【解决方案1】:

编辑

在 SO 上找到 this question 和一个完整的项目,可以在 codeproject.com 上为您提供帮助。在此示例中,有一个 使用 webBrowser COM 组件的用户控件。正如我在原始答案中所写,我不认为可能会阻止 .net Framework WebBrowser 加载图像。在浏览器控件接收到纯 html 文本后,您需要访问以下级别以拦截加载图像

... 最晦涩和最重要的部分 控件是 IDispatch_Invoke_Handler()。 ... 如何实现 IDispatch::Invoke 以便 限制 IE 显示的内容(例如图像、ActiveX 控件、Java)。 我发现如果你添加一个 IDispatch_Invoke_Handler() 方法 COM 调度标识符为 -5512 的代码,这样就可以了 给你。一个非常晦涩的答案,但效果很好....

原创

你可以试试这个

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
     Debug.WriteLine("documentCompleted");
     HtmlDocument doc = webBrowser1.Document;
     foreach (HtmlElement  imgElemt in doc.Images)
     {
         imgElemt.SetAttribute("src", "");
     }
}

但正如 MSDN 所说

处理 DocumentCompleted 事件以接收通知,当 新文档完成加载。当 DocumentCompleted 事件发生时 发生时,新文档已完全加载,这意味着您可以访问 其内容通过 Document、DocumentText 或 DocumentStream 属性。

我认为您不能使用 .net 框架中的 webBrowser 控件来执行此操作。

【讨论】:

    【解决方案2】:

    你可以试试这个:

    private void webBrowser1_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e)
    {
      if (webBrowser1.Document != null)
      {
         foreach (HtmlElement imgElemt in webBrowser1.Document.Images)
         {
            imgElemt.SetAttribute("src", "");
         }
       }
    }
    

    【讨论】:

      【解决方案3】:

      网络浏览器控件使用与 Internet Explorer 相同的设置。

      您可以轻松禁用图像,但请注意,它会影响 Internet Explorer,以及您的网络浏览器控件(以及其他使用 Internet Explorer 功能的程序)

      禁止加载图片:

      1.) 打开智能浏览器

      2.) 转到“工具”>“互联网选项”

      3.) 转到“高级”标签

      4.) 向下滚动直到找到“显示图片”复选框,然后取消选中它(它位于“多媒体”部分)

      此更改的效果存储在我相信的注册表中,因此您也应该能够以编程方式对其进行编辑。但是请记住,它不仅会影响您的应用程序。

      【讨论】:

        【解决方案4】:

        最近,我需要拦截和分析网络浏览器控件中的所有通信。我认为我使用的技术可以帮助您。

        你需要什么:

        • Awesomium.Net:基于 Chromium 引擎的 .net 控件
        • Fiddler Core :一个 http 内存代理,允许您监控 http 通信。
        • HtmlAgility pack :根据您选择的解决方案,HAP 可以帮助您以比正则表达式更可靠的方式动态更改 html 内容的 DOM。

        我选择使用 Awesomium 是因为它提供了比开箱即用的 Web 浏览器控件更多的功能。就我而言,它允许我定义要使用的代理,而不是系统范围的设置。

        Fiddler Core 用于拦截通信。它的 API 提供了在发出请求时拦截/篡改/...的方法。就我而言,我只是将响应正文转发到我的业务类,但在您的情况下,您应该能够过滤 mime-type 以更改 HTML DOM(使用 HtmlAgility 包!!!!!!) 或为图像返回非 200 http 状态。

        这是我使用的代码。我的应用是 WPF,但您可以轻松地将其调整为 winform:

        public partial class App : Application
        {
            static App()
            {
                // First, we set up the internal proxy
                SetupInternalProxy();
                // The we set up the awesomium engine
                SetupBrowser();
            }
            private static void SetupInternalProxy()
            {
                // My requirement is to get response content, so I use this event.
                // You may use other handlers if you have to tamper data.
                FiddlerApplication.AfterSessionComplete += FiddlerApplication_AfterSessionComplete;
                FiddlerApplication.Log.OnLogString += (o, s) => Debug.WriteLine(s);
        
                FiddlerCoreStartupFlags oFCSF = FiddlerCoreStartupFlags.Default;
        
                //this line is important as it will avoid changing the proxy for the whole system.
                oFCSF = (oFCSF & ~FiddlerCoreStartupFlags.RegisterAsSystemProxy);
        
                FiddlerApplication.Startup(0, oFCSF);
        
            }
            private static void SetupBrowser()
            {
                // We may be a new window in the same process.
                if (!WebCore.IsRunning)
                {
                    // Setup WebCore with plugins enabled.
                    WebCoreConfig config = new WebCoreConfig
                    {
                        // Here we plug the internal proxy to the awesomium engine
                        ProxyServer = "http://127.0.0.1:" + FiddlerApplication.oProxy.ListenPort.ToString(),
                        // Adapt others options related to your needs
                        EnablePlugins = true,
                        SaveCacheAndCookies = true,
                        UserDataPath = Environment.ExpandEnvironmentVariables(@"%APPDATA%\MyApp"),
                    };
                    WebCore.Initialize(config);
                }
                else
                {
                    throw new InvalidOperationException("WebCore should be already running");
                }
            }
            // Here is the handler where I intercept the response
            private static void FiddlerApplication_AfterSessionComplete(Session oSession)
            {
                // Send to business objects
                DoSomethingWith(
                    oSession.PathAndQuery,
                    oSession.ResponseBody,
                    oSession["Response", "Content-Type"]
                    );
        
            }
        }
        

        正如我在评论中所说,您可以使用 AfterSessionComplete 的另一个事件处理程序。这取决于您的要求(阅读提琴手核心 SDK 以获得帮助)。

        最后一句话:此代码从应用程序类(相当于 Winform 中的 Program 类)运行。您可能需要使用消息系统或发布全局事件(注意内存泄漏)才能在 windows 类中使用结果。您还必须知道 AfterSessionComplete 事件是从多个线程触发的,有时是同时触发的。您将使用某种 Invoking 在 UI 线程中工作。

        【讨论】:

        • 一个勇敢的尝试,但不是开箱即用的东西......事实上相当复杂,所以我投了反对票,对不起。
        • @Steve B ,我们正在使用 C# windows 窗体的 web 控件。但面临性能问题。想用chromeless抓取页面。它会优化性能吗?
        【解决方案5】:
        HtmlElementCollection elc = WebBrowser1.Document.GetElementsByTagName("img");
        foreach (HtmlElement el in elc)
        {
           if (el.GetAttribute("src") != null)
           {
               el.SetAttribute("src", "");
           }
        }
        

        如果有任何元素可能包含图像,那么它将位于img 标记中。

        【讨论】:

          【解决方案6】:

          您可以为此目的使用延迟加载加载技术。见http://engineering.slideshare.net/2011/03/faster-page-loads-with-image-lazy-loading/

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2015-04-13
            • 2013-10-23
            • 2013-06-28
            • 2016-04-26
            • 2011-02-09
            • 2012-12-23
            • 2010-11-20
            相关资源
            最近更新 更多