Last active
December 19, 2015 15:59
-
-
Save benjamine/5980855 to your computer and use it in GitHub Desktop.
Turn any text that looks like time to a time link alla youtube. clicking will change url hash, and (if found) a jwplayer to seek to that position
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
(function($){ | |
$.fn.timelinks = function(options){ | |
var options = options || {}; | |
this.each(function(){ | |
var textNodes = $(this).contents().filter(function() { | |
return this.nodeType === 3; //Node.TEXT_NODE | |
}).each(function(){ | |
var textNode = $(this); | |
var text = textNode.text(); | |
var timestampRegex = /(\d{0,2})\:(\d\d)(\:(\d\d))?(\s{0,2}[ap]\.?m)?/gim; | |
text = text.replace(timestampRegex, function(match, p1, p2, p3, p4, p5){ | |
if (p5) { | |
// it's just a day time, skip | |
return match; | |
} | |
var time = (typeof p3 == 'undefined' || p3 == null) ? | |
[0, parseInt(p1) || 0, parseInt(p2) || 0] : | |
[parseInt(p1) || 0, parseInt(p2) || 0, parseInt(p4) || 0]; | |
var href = '#t=' + (time[0] ? [time[0], 'h', time[1], 'm', time[2], 's'] : | |
time[1] ? [time[1], 'm', time[2], 's'] : | |
[time[2], 's']).join(''); | |
var seconds = (time[0] * 60 + time[1]) * 60 + time[2]; | |
return ['<a class="timelink" data-seconds="' + seconds + '" href="' + href + '">', match, '</a>' ].join(''); | |
}); | |
textNode.replaceWith($('<span>' + text + '</span>')); | |
}); | |
}); | |
}; | |
$(document).on('click', '.timelink', function(e){ | |
var seconds = parseInt($(e.target).data('seconds')); | |
if (window.jwplayer) { | |
jwplayer().seek(seconds); | |
} | |
}); | |
$(function(){ | |
$('p,span,div,strong,li,h1,h2,h3,h4,h5,caption').timelinks(); | |
}) | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment