简体   繁体   中英

How to block flash elements in the web browser control

I building a C# application with a WebBrowser control in it and and I am trying to figure out a way to get the flash content within the web page to not display as it is sucking up a ton of my CPU and memory. I've been trying to remove the <object> tag within the page (there's only one) by getting it via browser.Document.GetElementsByTagName("object") and setting its outerHtml to an empty string. It returns one element (confirmed by the Count property) but accessing the first element ([0]) gives an index out of range error. I've tried doing it via a foreach loop but nothing happens at all and there are no errors. If I try to retrieve <div> elements instead of <object> elements, the foreach loop runs. Am I doing something wrong here, is there a bug, or is there a better way to remove the flash content? Any help greatly appreciated.

My code:

HtmlElementCollection flashElements = webBrowser.Document.GetElementsByTagName("object");
foreach (HtmlElement element in flashElements)
{
  element.OuterHtml = "";
  MessageBox.Show("Deleted");
}

By the way, this is being run after the DocumentCompleted event has been fired.

EDIT: I just figured out that while the DocumentCompleted event is being called, it is being called before JavaScript injects the flash content onto the page. I've tried a few "sleeping"-like methods but they all stop the web page from processing too. Any way to wait for more time (5 seconds or so) while the webpage continues to render?

Option 1: You could use a System.Threading.Timer to try and wait extra time for the dynamically injected Flash. That shouldn't hold up the WebBrowser from running while you wait.

Option 2: Try something like FiddlerCore (you would add this to your app), privoxy (this would run outside of your app on your local machine) or some other proxy to actually block the Flash (.swf) content from ever getting to your local machine. With FiddlerCore, you would monitor each web request/response and kill the request/response based on the HTTP content type, for instance. This option would incur the CPU/memory hit of proxying the network, but is still likely less than the Flash is using itself.

Why not use inject JavaScript on the page to disable writing the flash before it ever exists? I don't know what flash library the page is using, but it'd be fairly easy to rewrite the main method to do nothing.

eg

swfobject.embedSWF = function(){}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM