Skip to content

Instantly share code, notes, and snippets.

@guillefd
Last active November 6, 2024 20:15
Show Gist options
  • Save guillefd/b35547eb6baef407bf092b6357197cef to your computer and use it in GitHub Desktop.
Save guillefd/b35547eb6baef407bf092b6357197cef to your computer and use it in GitHub Desktop.
Automatically redirect the last html page of the SCORM package to the moodle course main page
/**
* This script will automatically redirect the last html page of the SCORM package
* to the moodle course main page. The courseId and wwwroot is read from Moodle JS object "M".
*
* Important: this script will only work with a SCORM package running on a Moodle course.
* The script must be placed on the last HTML page the SCORM displays, for example
* in "Articulate Storyline" it should be the "/scormdriver/goodbye.html" file.
*/
(function() {
console.log('::autoredirect.js loaded');
console.log('::scorm-script-redirect > init');
// find M, get id and redirect
findM()
.then(() => {
// set window
var win = window.top;
// get M data
var courseId = win.M.cfg.courseId;
var wwwroot = win.M.cfg.wwwroot;
// check data
console.log('data', {wwwroot, courseId, url});
if(!courseId || !wwwroot) {
console.log('::scorm-script-redirect data is missing, cant build url');
return;
}
// set url
var url = wwwroot + '/course/view.php?id=' + courseId;
// redirect after 1 second
setTimeout(() => {
win.location.replace(url);
}, 1000);
})
.catch(e => {
console.log('err', e);
});
/**
* init M
* @returns Promise<void>
*/
function findM() {
return new Promise((resolve) => {
var dots = 'init M';
var win = window.top;
var interval = setInterval(function() {
console.log('::scorm-script-redirect '+dots);
if( win.M == "undefined" || win.M == "" || !win.M.hasOwnProperty('cfg') || win.M.cfg == "undefined"){
console.log('::scorm-script-redirect still waiting M'+dots);
dots+= '.';
}else{
console.log('::scorm-script-redirect M is loaded');
clearInterval(interval);
resolve();
}
}, 500);
});
}
})();
<html>
<head>
<meta charset="utf-8" lang="en">
</head>
<body>
<div class="goodbye-container">
<div class="bye-message">👋 Bye!</div>
<div class="exit-text">You may now leave this page.</div>
</div>
<!-- Add this line before the closing </body> tag -->
<!-- This script will automatically redirect the scorm goodbye.html to the course main page -->
<script src="autoredirect.js"></script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment