Detecting if YouTube is blocked by company / ISP

2019-01-14 13:17发布

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'.

标签: flex youtube
5条回答
劫难
2楼-- · 2019-01-14 13:35

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.

查看更多
Animai°情兽
3楼-- · 2019-01-14 13:36

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";
查看更多
成全新的幸福
4楼-- · 2019-01-14 13:37

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>
查看更多
啃猪蹄的小仙女
5楼-- · 2019-01-14 13:57

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.

查看更多
做个烂人
6楼-- · 2019-01-14 13:59

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";

}

?>
查看更多
登录 后发表回答