Skip to content

Instantly share code, notes, and snippets.

@bkardell
Created August 4, 2012 00:41
Show Gist options
  • Save bkardell/3253097 to your computer and use it in GitHub Desktop.
Save bkardell/3253097 to your computer and use it in GitHub Desktop.
jorra.mediatime.1
(function(){
var granularity = 250,
matches = [],
farFuture = new Date().getTime() + (1000 * 60 * 60 * 24 * 2),
test;
test = function(el,o){
cTime = el.currentTime * 1000;
if(cTime >= o.start){
if(o.end === ':-media-end' && !el.ended){ return true; }
if(cTime <= o.end){ return true; }
}
return false;
};
setInterval(function(){
var match, i=0, n = 0, hasChanges, entry, rst;
for(;i<matches.length;i++){
match = matches[i];
hasChanges = false;
for(n in match['-media-time']){
entry = match['-media-time'][n];
rst = test(match,entry);
if(rst != entry.result){
entry.result = rst;
match['-media-time'][n] = entry;
hasChanges = true;
}
}
if(hasChanges){
match.setAttribute('data-media-timeon', new Date().getTime());
}
}
},granularity);
Hitch.add({
name: '-media-time',
base: 'audio,[data-media-timeon],video',
type: 'selector',
filter: function(m,args){
var times, start, end, simpleKey;
if(!args){ return; }
if(!m['-media-time']){ m['-media-time'] = {}; }
if(!m['-media-time'][args]){
try{
times = args.split(",");
start = parseInt(times[0],10);
end = farFuture;
if(times.length > 1){
if(times[1].replace(/\s/g, "") === ":-media-end"){
end = ":-media-end";
}else{
end = parseInt(times[1], 10);
}
}
m['-media-time'][args] = { start: start, end: end, result: false };
}catch(e){
return;
}
}
if(matches.indexOf(m) == -1){matches.push(m);}
//console.log("xx: " + JSON.parse(m.dataset[simpleKey]).result);
return (m['-media-time'] && m['-media-time'][args]) ? m['-media-time'][args].result : false;
}
});
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment