15

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 don't know how to do it. I can't even think of a 'nice hack'.

niton
  • 8,771
  • 21
  • 32
  • 52
Simon_Weaver
  • 140,023
  • 84
  • 646
  • 689

5 Answers5

21

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.onload = function(){
// The user can access youtube
};
image.onerror = function(){
// The user can't access youtube
};
image.src = "http://youtube.com/favicon.ico";
epascarello
  • 204,599
  • 20
  • 195
  • 236
tiangolo
  • 1,536
  • 1
  • 15
  • 29
  • 4
    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 Nov 26 '09 at 16:38
  • @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 Dec 16 '09 at 12:05
  • IE in windows phone 8.1 always calls the onerror function. It seems the browser doesn't consider .ico as a valid image format! – smohadjer Apr 21 '15 at 07:22
14

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
  • 5,470
  • 6
  • 36
  • 38
  • I like that better than my own solution – Tristan Havelick Nov 13 '08 at 02:15
  • isn't this goin to have a race condition? – Simon_Weaver Nov 13 '08 at 03:42
  • @simon how does this make for a race condition? – Tristan Havelick Nov 16 '08 at 17:55
  • 1
    @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 Nov 17 '08 at 02:34
  • 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 Nov 26 '09 at 16:36
  • 1
    The image.onload solution up above is probably a better solution - as evidenced by @Simon_Weaver above - height isn't consistently populated until it has loaded... and if it doesn't load, it might be undefined or zero. – Ben Jan 30 '12 at 18:59
8

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"></script>
    <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
  • 67,400
  • 20
  • 54
  • 64
1

I got stuck on this today and tried the favicon test but it wasnt working in IE. I was using the YouTube Player API Reference for iframe Embeds to embed youtube videos into my site so what I did is perform a check on the player var defined just before the onYouTubeIFrameReady with a delay on the javascript call.

<script> function YouTubeTester() {            
            if (player == undefined) {
                alert("youtube blocked");
            }
        }
</script>
<script>window.setTimeout("YouTubeTester()", 500);</script>

Seems to work for me. I needed the delay to get it to work in IE.

Sculper
  • 756
  • 2
  • 12
  • 24
mdogggg
  • 11
  • 1
0

This worked for me... Its also my first post, hope it helps some one too.

<?php

$v = file_get_contents("https://www.youtube.com/iframe_api");

//Tie counts to a variable
$test = substr_count($v, 'loading');

if ($test > 0)

{ ?>
    <iframe>YOUTUBE VIDEO GOES HERE</iframe>

    <?php
}

else

{

echo "<br/> no connection";

}

?>