Skip to content

Instantly share code, notes, and snippets.

@acspike
Created January 17, 2022 23:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acspike/2d44575b3c8afe3ffb98299252216259 to your computer and use it in GitHub Desktop.
Save acspike/2d44575b3c8afe3ffb98299252216259 to your computer and use it in GitHub Desktop.
Calculate total duration of a Kaltura playlist
// ==UserScript==
// @name Display total play time in playlist.
// @namespace gatech.edu
// @version 0.1.0
// @match *://canvasgatechtest.kaf.kaltura.com/*
// @match *://cdnapisec.kaltura.com/*
// @grant none
// @author Aaron Spike
// @description 1/25/2021
// ==/UserScript==
kWidget.addReadyCallback(function(playerId){
let totalDuration = '';
let description = '';
let kdp = document.getElementById(playerId);
kdp.kBind("playlistReady", function() {
const playlist = document.querySelector('.playlistInterface');
const desc = playlist.querySelector('.playlistDescription');
description = description || desc.innerText;
let total = 0;
playlist.querySelectorAll('.k-duration').forEach(function(elt){
let m, s;
[m,s] = elt.innerText.split(':');
total += m*60 + s*1;
});
totalDuration = ' ( <span id="current-duration-holder">0:00</span> / '+ Math.trunc(total/60) + ':' + (total%60) + ')';
desc.innerHTML = description + totalDuration;
});
kdp.kBind("playlistMiddleEntry", function(){
const playlist = document.querySelector('.playlistInterface');
const durElt = document.getElementById('current-duration-holder');
let total = 0;
Array.from(playlist.querySelectorAll('.k-duration')).some(function(elt){
console.log(elt);
let m, s;
[m,s] = elt.innerText.split(':');
total += m*60 + s*1;
return elt.offsetParent.classList.contains('active');
});
durElt.innerText = Math.trunc(total/60) + ':' + (total%60);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment