2012-08-31 59 views
5

我建立一個C#應用程序與WebBrowser,我試圖找出一種方法來阻止圖像,也就是爲他們當網站被加載到不顯示在Web瀏覽器中的圖像(以便網站加載更容易)。如何阻止

我試圖通過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"; 
+3

你可能想要非貪婪的+?量詞:'「」' – AKX

+3

啊!用正則表達式解析HTML! http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – annonymously

回答

4

上SO編輯

發現this question和一個完整的項目,可以幫助你在codeproject.com。在此示例中,使用webBrowser COM組件有一個userControl。正如我在我原來的回答中寫道我不認爲它是可能會阻止.net框架WebBrowser加載圖像。您需要訪問級別以下攔截加載圖像瀏覽器控件後,已經收到了純HTML文本。

...的 控制最不起眼的重要組成部分是IDispatch_Invoke_Handler()。 ...如何實現IDispatch ::爲了調用限制哪些IE中顯示(如圖像,ActiveX控件,Java的)。 我發現,如果你在 代碼添加IDispatch_Invoke_Handler()方法的-5512的COM調度標識符,這樣做的工作 你。一個非常模糊的答案,但它運作良好....

ORIGINAL

你可以試試這個

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事件發生 ,新的文檔全部加載,這意味着你可以通過文檔,DocumentText,或DocumentStream 屬性訪問 其內容。

我不認爲你可以用.net框架的web瀏覽器控件來做到這一點。

1

最近,我有一個要求,攔截和分析瀏覽器控制中的所有通信。我認爲我使用的技術可以幫助你。

你需要什麼:

  • Awesomium.Net:對於.NET
  • Fiddler Core鉻引擎爲基礎的控制:一個http內存代理,它允許您監控HTTP通信。
  • HtmlAgility pack:根據您選擇的解決方案,HAP可以幫助您動態更改HTML內容的DOM,以比正則表達式更可靠的方式。

我選擇使用Awesomium,因爲它比開箱即用的Web瀏覽器控件提供了更多的功能。就我而言,它允許我定義要使用的代理而不是系統範圍的設置。

Fiddler Core用於攔截通信。其API提供攔截/篡改/ ...發出請求時的方法。就我而言,我只是將響應組件轉發給我的業務類,但就您的情況而言,您應該能夠對MIME類型進行過濾,以更改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事件是從多個線程觸發的,有時會同時觸發。您將使用某種調用來在UI線程中工作。

+0

一個勇敢的嘗試,但不是開箱即用......事實上,複雜的,所以我downvoted,對不起。 – tmighty

4

你可以試試這個:

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

WebBrowser控件使用Internet Explorer使用相同的設置。

,你可以輕鬆地禁用圖像,但請注意,它會影響IE瀏覽器,以及您的WebBrowser控件(和使用Internet Explorer功能的其他程序)

從裝載禁用圖片:

1 )。)開放integent探險

2.進入 '工具'> 'Internet選項'

3)進入 '高級' 選項卡

4。),直到你找到「顯示圖片」複選框向下滾動,並取消它(這是在「多媒體」部分)

這種變化的影響都存儲在註冊表中我beleive,所以你應該能夠編輯它在編程上也是如此。請記住,它會影響的不僅僅是您的應用程序,但是。

1
HtmlElementCollection elc = WebBrowser1.Document.GetElementsByTagName("img"); 
foreach (HtmlElement el in elc) 
{ 
    if (el.GetAttribute("src") != null) 
    { 
     el.SetAttribute("src", ""); 
    } 
} 

是否有可能包含的圖像那麼這將是在img標籤的任何元件。