Skip to content

Instantly share code, notes, and snippets.

@Scarygami
Created February 4, 2014 12:01
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save Scarygami/8802434 to your computer and use it in GitHub Desktop.
Save Scarygami/8802434 to your computer and use it in GitHub Desktop.
In an ideal world where everyone is running Chrome with the Chromecast extension installed things would be easy. But since this isn't the case here are some checks you might want to perform on your Chromecast-enabled website to make sure the behaviour isn't broken for other users.
<!DOCTYPE html>
<html>
<head>
<title>Chromecast init</title>
<script src="https://www.gstatic.com/cv/js/sender/v1/cast_sender.js"></script>
</head>
<body>
<div id="message"></div>
<script type="text/javascript">
(function (window) {
function showMessage(html) {
window.document.getElementById("message").innerHTML = html;
}
if (!window.chrome) {
// Not running in Chrome... inform users about it (or hide Chromecast functionality for non-Chrome users)
showMessage("Only works in <a href=\"https://www.google.com/chrome\">Chrome</a>.");
return;
}
if (!window.chrome.cast) {
// shouldn't happen since we load cast_sender.js synchronously
showMessage("Cast API not found, this shouldn't happen...");
return;
}
/*
* Check for Chromecast extension.
* Unfortunately no nice way to know whether the Extension is missing in the API so far
* Open feature request: https://code.google.com/p/google-cast-sdk/issues/detail?id=124
*/
if (window.chrome.cast.extensionId) {
waitForApi();
} else {
// Internal API call so might break, but best way for now without relying on a maximum timeout...
window.chrome.cast.ApiBootstrap_.findInstalledExtension_(function (extensionId) {
if (!extensionId) {
// Extension not installed, prompt user to install it
showMessage("Please install the <a href=\"https://chrome.google.com/webstore/detail/google-cast/boadgeojelhgndaghljhdicfkmllpafd\">Google Cast extension</a>.");
} else {
waitForApi();
}
});
}
function waitForApi() {
showMessage("Waiting for API to become ready...");
if (!window.chrome.cast.isAvailable) {
window.setTimeout(waitForApi, 100);
} else {
initializeCastApi();
}
}
function initializeCastApi() {
// we can start using the Cast API now
showMessage("Everything ready!");
}
}(this));
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment