views:

1242

answers:

4

We have Youtube videos on a site and want to detect if it is likely that they will not be able to view them due to (mostly likely) company policy or otherwise.

We have two sites:

1) Flex / Flash 2) HTML

I think with Flex I can attempt to download http://youtube.com/crossdomain.xml and if it is valid XML assume the site is available

But with HTML I dont know how to do it. I cant even think of a 'nice hack'

+5  A: 

This should work. Basically, it loads a youtube.com javascript file, then checks if a function in that file exists.

<html>

<head>
    <script src="http://www.youtube.com/js/account.js"&gt;&lt;/script&gt;
    <script>
     function has_you_tube()
     {
      if(typeof addVideosToQuicklist == 'function')
      {
       return true;
      }
      else
      {
       return false;
      }

     }
    </script>

</head>
<body>
    <script>alert( "has_youtube: " + has_you_tube() ); </script>
</body>


</html>
Tristan Havelick
Note, a nice way to test this is to add:127.0.0.1 www.youtube.comto your hosts file
Tristan Havelick
What? Why would 127.0.0.1/js/account.js exist and have that function in it?
Karl
@Karl: This is a way to test the case when youtube is *blocked*. Of course, the unblocked case would be tested by leaving this line out of hosts.
Tristan Havelick
+8  A: 

You can load an image from youtube using javascript and check its properties. The favicon is tiny and has a consistent url -

var image = new Image();
image.src = "http://youtube.com/favicon.ico";
if (image.height > 0) {
    // The user can access youtube
} else {
    // The user can't access youtube
}

I think this is slightly better than loading javascript because this won't try to run any code, and while youtube might rename their javascript files, or functions from those files, they are unlikely to ever rename their favicon.

lacker
I like that better than my own solution
Tristan Havelick
isn't this goin to have a race condition?
Simon_Weaver
neat idea, thanks for sharing.
mmattax
@simon how does this make for a race condition?
Tristan Havelick
@DrFredEdison ok i actually tried it this time. the first time i got 0 and the second time I got 16. so presumably it was getting it from the cache the second time - hence the 'race' condition. i'm not sure if Image has event handlers that will tell me when its complete or not but ill look into that
Simon_Weaver
I don't like this suggestion. If youtube is blocked, the blocking page may well have a favicon too, and this code will return a false positive
Colin Pickard
+3  A: 

I like lacker's solution, but yes, it creates a race condition. This will work and won't create a race contition:

var image = new Image();
image.src = "http://youtube.com/favicon.ico";
image.onload = function(){
// The user can access youtube
};
image.onerror = function(){
// The user can't access youtube
};
Tiangolo
as with lacker's solution, if the blocking page has a favicon this will cause a false positive. You should check for the presence of something distinct to youtube.
Colin Pickard
@Colin Pickard: Good point. Maybe you're right. But I guess that if there is a blocking page it would use a HTTP 404 (Not Found) or other Error Status Code, so I guess it will fail to retrieve the image.I guess the correct way would be to do a XMLHttpRequest and look at the response, but the favicon is easier to code and easier that, by example, a cellphone browser implements it
Tiangolo
A: 

This tutorial could help you access youtube even when it's blocked: http://www.tomatosoft.biz/blog/2010/04/11/download-youtube-while-its-blocked-by-isp/

jack