Last active
August 29, 2015 13:56
-
-
Save Scarygami/8825093 to your computer and use it in GitHub Desktop.
Loads and prepares the Google Cast Chrome SDK. Just include the cast_loader.js then call cast_loader.onReady(function (result) { .... }), see included sample to see what the result codes mean.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (c) 2014 Gerwin Sturm, FoldedSoft e.U. / www.foldedsoft.at | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); you may | |
* not use this file except in compliance with the License. You may obtain | |
* a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | |
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | |
* License for the specific language governing permissions and limitations under | |
* the License. | |
*/ | |
(function (global) { | |
var doc = global.document, callbacks = [], loading = true, script; | |
global.cast_loader = { | |
READY: "ready", | |
WRONG_BROWSER: "wrong browser", | |
EXTENSION_MISSING: "extension missing", | |
SDK_NOT_FOUND: "sdk not found" | |
}; | |
function safe_callback(cb, result) { | |
if (!!cb && typeof cb === "function") { | |
global.setTimeout(function () { | |
try { cb(result); } catch (e) {} | |
}, 0); | |
} | |
} | |
function finish(state) { | |
var cb; | |
loading = false; | |
global.cast_loader.state = state; | |
while (callbacks.length > 0) { | |
cb = callbacks.shift(); | |
safe_callback(cb, state); | |
} | |
} | |
global.cast_loader.onReady = function (cb) { | |
if (loading) { | |
callbacks.push(cb); | |
} else { | |
safe_callback(cb, global.cast_loader.state); | |
} | |
}; | |
if (!global.chrome) { | |
finish(global.cast_loader.WRONG_BROWSER); | |
return; | |
} | |
script = doc.createElement("script"); | |
script.type = "text/javascript"; | |
script.onload = function () { | |
function waitForApi() { | |
if (!global.chrome.cast.isAvailable) { | |
global.setTimeout(waitForApi, 100); | |
} else { | |
finish(global.cast_loader.READY); | |
} | |
} | |
if (!global.chrome.cast) { | |
finish(global.cast_loader.SDK_NOT_FOUND); | |
return; | |
} | |
if (global.chrome.cast.extensionId) { | |
waitForApi(); | |
} else { | |
global.chrome.cast.ApiBootstrap_.findInstalledExtension_(function (extensionId) { | |
if (!extensionId) { | |
finish(global.cast_loader.EXTENSION_MISSING); | |
} else { | |
waitForApi(); | |
} | |
}); | |
} | |
}; | |
script.onerror = function (e) { | |
finish(global.cast_loader.SDK_NOT_FOUND); | |
}; | |
script.src = "https://www.gstatic.com/cv/js/sender/v1/cast_sender.js"; | |
doc.getElementsByTagName("head")[0].appendChild(script); | |
}(this)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Google Cast Loader Sample</title> | |
<script src="cast_loader.js"></script> | |
</head> | |
<body> | |
<div id="debug"></debug> | |
<script type="text/javascript"> | |
var debug = document.getElementById("debug"); | |
cast_loader.onReady(function (result) { | |
switch (result) { | |
case cast_loader.READY: | |
debug.innerHTML = "Ready to cast, chrome.cast methods/objects can be used"; | |
break; | |
case cast_loader.WRONG_BROWSER: | |
debug.innerHTML = "Browser not supported, use Chrome..."; | |
break; | |
case cast_loader.EXTENSION_MISSING: | |
debug.innerHTML = "Google Cast extension missing"; | |
break; | |
case cast_loader.SDK_NOT_FOUND: | |
debug.innerHTML = "Cast SDK not found, i.e. something went really wrong..."; | |
break; | |
} | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment