public
Created

Captivate SCORM 2004 Output, replaced findAPI and getAPI code

  • Download Gist
captivate.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
// set document.domain property here, if it works for your environment/SCORM implementation
// document.domain="";
var strURLFile = "sample.swf", // Name of the flash file
flashvars = {},
params = { bgcolor: "#f5f4f1", menu: "false" },
attributes = { id: "Captivate", name: "Captivate" },
CaptivateSWF; //Cache the reference to the SWF to avoid future lookups
 
 
function callbackFn(e){
//e.ref is the <object> aka SWF file. No need for getElementById
if(e.success && e.ref){
CaptivateSWF = e.ref;
CaptivateSWF.tabIndex = -1; //Set tabIndex to enable focus on non-form elements
CaptivateSWF.focus();
}
}
 
//Initialize SCORM API
SCORM_API = getAPI();
 
//Only embed SWF if SCORM API is found
if(SCORM_API){
 
//swfobject.embedSWF has a built-in domready call,
//so it doesn't need to be wrapped in a window.onload event.
swfobject.embedSWF(strURLFile + "?SCORM_API=1.0&SCORM_TYPE=0", "CaptivateContent", "641", "512", "10", false, flashvars, params, attributes, callbackFn);
 
} else {
 
//Provide a useful error message for the learner. Will only show up if SCORM API is not found!
swfobject.addDomLoadEvent(function(){
document.getElementById("CaptivateContent").innerHTML = "Sorry, but the course is not available at this time (SCORM API not found). Please try again. If you continue to encounter problems, please contact the course administrator.";
});
 
}
sample.htm
HTML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
<!-- Copyright [2008] Adobe Systems Incorporated. All rights reserved -->
<!-- saved from url=(0013)about:internet -->
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js"></script>
<script src="standard.js"></script>
<script src="SCORM_support/scorm_support.js"></script>
<script src="captivate.js"></script>
<style>
body { background: #f5f4f1; text-align: center; }
</style>
</head>
<body>
<div id="CaptivateContent"></div>
<noscript>
This course requires JavaScript to be enabled in your browser. Please enable JavaScript, then relaunch the course.
</noscript>
</body>
</html>
scorm_support.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
var SCORM_API = null,
foundAPI = false,
g_bFinishDone = false;
 
/* -------------------------------------------------------------------------
findAPI(window)
Adapted from pipwerks SCORM wrapper
https://github.com/pipwerks/scorm-api-wrapper
 
Looks for an object named API in parent and opener windows
 
Parameters: window (the browser window object).
Returns: Object if API is found, null if no API found
---------------------------------------------------------------------------- */
 
var findAPI = function(win){
 
var API,
findAttempts = 0,
findAttemptLimit = 500;
 
while (!win.API_1484_11 && win.parent && win.parent != win && findAttempts <= findAttemptLimit){
findAttempts++;
win = win.parent;
}
 
API = win.API_1484_11 || null;
 
/*
if(!API){
alert("Error finding API. \nFind attempts: " +findAttempts +". \nFind attempt limit: " +findAttemptLimit);
}
*/
 
return API;
 
};
 
 
/* -------------------------------------------------------------------------
getAPI()
Adapted from pipwerks SCORM wrapper
https://github.com/pipwerks/scorm-api-wrapper
 
Looks for an object named API_1484_11, first in the current window's frame
hierarchy and then, if necessary, in the current window's opener window
hierarchy (if there is an opener window).
 
Parameters: None.
Returns: Object if API found, null if no API found
---------------------------------------------------------------------------- */
 
var getAPI = function(){
 
var API = null,
win = window;
 
//Look in parent windows first
if(win.parent && win.parent != win){
API = findAPI(win.parent);
}
 
//Look in opener windows next
if(!API && win.top.opener){
API = findAPI(win.top.opener);
}
 
//Plateau LMS needs special hand-holding
if(!API && win.top.opener && win.top.opener.document) {
API = findAPI(win.top.opener.document);
}
 
//if(!API){ alert("getAPI failed: Can't find the API!"); }
 
return API;
 
};
 
 
//Quick way to check if SCORM API is available and communicative
function SCORM_API_Available() {
return (SCORM_API && SCORM_API.GetLastError && typeof SCORM_API.GetLastError() !== "undefined");
}
 
 
function Finish(){
 
if(SCORM_API_Available() && g_bFinishDone == false){
SCORM_API.Commit("");
g_bFinishDone = (SCORM_API.Terminate("") === "true");
}
return g_bFinishDone + ""; // Force type to string
 
}
 
 
function Captivate_DoExternalInterface(command, parameter, value, variable){
 
var strErr = "true";
 
//Ensure SCORM API is still available
if(!SCORM_API_Available()){ return; }
 
if(command === "Initialize"){
 
CaptivateSWF.SetScormVariable(variable, SCORM_API.Initialize(""));
 
if(SCORM_API.GetValue("cmi.completion_status") == "not attempted"){
SCORM_API.SetValue("cmi.completion_status", "incomplete");
}
 
} else if(command === "SetValue"){
 
strErr = SCORM_API.SetValue(parameter, value);
CaptivateSWF.SetScormVariable(variable, strErr);
 
} else if(command === "Terminate"){
 
g_bFinishDone = SCORM_API.Terminate("");
strErr = g_bFinishDone;
CaptivateSWF.SetScormVariable(variable, g_bFinishDone);
 
} else if(command === "Commit"){
 
strErr = SCORM_API.Commit("");
CaptivateSWF.SetScormVariable(variable, strErr);
 
} else if((value) && (value.length > 0)){
 
if(command === "GetLastError"){
 
strErr = SCORM_API.GetLastError();
CaptivateSWF.SetScormVariable(variable, strErr);
 
} else {
 
strErr = SCORM_API[command](parameter);
CaptivateSWF.SetScormVariable(variable, strErr);
 
}
 
}
 
return strErr;
 
}
 
window.onunload = Finish;

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.