How to block flash elements in the web browser control - c#

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.
e.g.
swfobject.embedSWF = function(){}

Related

Mimicking AJAX file uploads in webbrowser control is failing

I am having a problem but I am not sure where.
I have created a website which uses Angular, this might not be pertinent but then again it might be. Of the many features of this site one of them is the ability to upload a file and then get back a response. The response is a simple amount of JSON. The only browser I have to get this working with at this point in time in IE8. avoiding incoming thrown objects - I have tried everything to change this fact, but it is what it is.
I have gotten the site to mimic AJAX uploads by submitting a form and having the response redirected to an iframe. In fact I am using a module that does most of this for me.
So far all this works better than you would believe in IE8. I know right? Unbelievable!
But… there’s always a but…
I need to get this page working when running in a Webbrowser control in a winforms project. I get as far as uploading the file which happens successfully. My REST service gets it, saves it, and returns the correct response. When debugging I can even see that the response is available. But for some reason the load method for the iframe is never called. The iframe which I have made visible on the page is never populated with the JSON.
Again this works when running in IE but not when in the webbrowser control. I get no errors and have breakpoints and debugger statements everywhere. It’s like the response falls in a crack and is ignored.
Would anyone have any suggestions as to why this is happening? crossing fingers
Sorry for the delay I have been ..., well there is no adjective for how busy I have been.
The answer lied in the module that was chosen to help upload files. It worked for IE 8 straight up, but not when running in the web browser control. The module was dynamically binding an onload event to an iFrame which was also being dynamically create to handle the post back. Anyone that has worked with IE and needed to upload files might know of this method. This worked in IE8 but not in the web browser control. We modified it to add the onload event when the iFrame is created and that fixed the issue.

Waiting for page to be fully loaded using WatiN

I have two questions related to the same problem...
Q1) I am using WatiN(3.5) for automation of a website.
The situation is that I want to obtain a div tag when the result page is fully loaded but WatiN don't wait for that page to be campletely loaded and tries to obatin that div which results in getting div with null. This div is populated by AJAX. This is th code that I am using to avoid that error but it does not work.
while (resultDiv == null)
{
browser.Div("ui-tabs-1").WaitUntilExists();
resultDiv = browser.Div("ui-tabs-1");
}
So how I can wait for a page to be completely loaded by using WatiN?
Q2) I found a solution for above problem here but I stuck at a point as I could not find a reference of library for these interfaces i.e. IElement and IBrowser. These interfaces are bring used in the extension methods.
I have also asked the author of that article and waiting for his reply.
I am making this apllication by usng WatiN 2.5 and .Net framework 3.5 in VS 2010.
I have ran into similar problem with watin on a site using Ajax.
This is the workaround for this.
//After click on link/Tab/Button on which the result is loaded in non Ajax websites.
We have a function here, browser.WaitForComplete() but it works only when the page is in loading state. but in case of Ajax on a part of browser window gets updated. so no loading state for browser.
So one solution for this problem is
Use Thread.Sleep(10000); This time can vary upon the normal time the website takes to load the required div.
Thread.Sleep can be used but for anything other than a proof of concept that waiting for something to load is indeed the issue Thread.Sleep should be avoided. Sleeps add in unnecessary idle time if you sleep for the max time the action is going to take, and give false positive failures when waiting less time.
See Jeroen's link in his response here if you are testing an ASP.NET Ajax site: In WatiN how to wait until postback is complete - WaitForAsyncPostbackToComplete. I used this idea for some methods and properties to rid my code of a lot of long Sleep calls. Tests ran faster and results were much more reliable.
If the specific JS call won't work as you're using a different clientside framework, using the basic polling concept with shorter sleeps in a loop is going to do you better than long sleeps.

Handle infinite loop in third party javascript file (c# .net)

I need to load a third party script file(which I do not have any control), but I want to avoid hanging the page in case the script has an infinite loop in it.
I tried calling the javascript file and put it in an updatepanel only on a click of a button but the page still hangs. I even tried putting it in an iframe just to separate it from other controls and the same scenario happens.
Is there really NO way to stop an infinite loop once it has started executing?
You are trying to add handling to a bug that is already fixed, so all that is going to happen is your code is going to become more complex unnecessarily.
I would have faith in the fix that they have produced, and you could look into adding Unit Tests that will allow you to test the bug in a secure manner
Edit
If you think the bug still exists then you could create a small test example (i.e your unit test) to send to the third party in hope of a better fix
Use debugger; to debug the script file. Put debugger; symbol beginning of the script's method which will be executed and run in chrome (since it is easy to debug) . click F12 to switch on the debugger tool. The script execution will stop on the line you put the debugger; and debug it line by line.
If the script is executing an infinite loop the chrome tab will crash with a message "Aw, Snap!". You can refer the call stack to find the method causing problem.
One possible solution could be is using Web Worker and run your JS code inside it. After a while, if there is no any responce from it, call worker.terminate() to "kill" it.
Pay attention that web workers has strong limitations: for example you can not access UI element from within the code that runs in web worker scope, and it's not supported on old browsers.
I don't know if web worker is suitable in your case, but this one is option that may help.

Allowing a WPF browser application to navigate to a separate web page when finished?

I have a WPF browser application that collects user data and adds it to a database to tell them when their software is out of date.
All of that works fine, but the problem is when the application finishes its stuff, I want the web page itself to change (i.e., detect the web app has hit a 'finished' state, then autonagivate to a results page or something).
I can't think of a way to accomplish this, since the web app itself doesn't seem to be able to change the IFRAME it's contained in, much less the page outside of that, or signal to javascript or anything.
Any ideas?
I'd make an variable to keep progress/step of work. And a timer which would check if progress=="done" or sth.
Maybe this is not the best way of solving this but I don't know WPF much and that solution first came to mind

HtmlDocument.InvokeScript Issue in .NET WebBrowser Control

I'm trying to make a wrapper around last.fm and need to invoke some of the javascript functions in the page. Particularly the ones for Stop, Skip, Ban and Love.
InvokeScript seems to work fine with any script function that is within the page HTML itself, but not with scripts loaded from external script files.
For example, the actual call to the skip function is LFM.Flash.Player.skip(), so I tried the following:
_browser.Document.InvokeScript("LFM.Flash.Player.skip");
But all it does is return null and nothing happens on the page.
I've confirmed that that call at least works as typed using the Chrome inspector and the console. (Haven't figured out if there is a way to invoke arbitrary javascript in IE... any suggestions are appreciated)
Is there some special way that that function must be invoked?
Also, yes, I have tried it with and without the parentheses in the script call... still no luck.
Edit: To be clear, I am doing this in the WinForms Browser Control not in an ASP.NET page.
I had the same problem and that solved it for me:
Make sure there's no error in the console by manually loading the page in an external browser
IE Control seems to be caching the pages/scripts ==> I found that refreshing the page in the actual IE browser also refresh the page in .NET IE Control. You can also clear the cache with the debugging tools. I do this for everychange and it works ...
Hope this helps !
Aren't you missing parenthesis when calling skip? Otherwise this only "references" the function, it doesn't call it.

Categories

Resources