/*
* jQuery srt
*
* version 0.1 (November 28, 2008)
*
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*/
/*
usage:
html:
js:
$(document).ready(function() { $('.srt').srt(); });
in the above example, jquery.srt.js will try to load subtitles in all elements with 'srt' class.
'data-video' atribute is used to link to the related video,
if no data-srt is provided, the contents of the div is parsed as srt.
*/
(function($) {
function toSeconds(t) {
var s = 0.0
if(t) {
var p = t.split(':');
for(i=0;i=2) {
n = st[0];
i = strip(st[1].split(' --> ')[0]);
o = strip(st[1].split(' --> ')[1]);
t = st[2];
if(st.length > 2) {
for(j=3; j currentTime)
break
subtitle = s;
}
}
if(subtitle > 0) {
if(subtitle != currentSubtitle) {
opts.showSubtitle(subtitles[subtitle], subtitleElement);
// subtitleElement.html(subtitles[subtitle].t);
currentSubtitle=subtitle;
} else if(subtitles[subtitle].o < currentTime) {
opts.hideSubtitle(subtitleElement);
// subtitleElement.html('');
}
}
}, opts.interval);
}
function showSubtitle(sub, elem) {
elem.html(sub.t);
}
function hideSubtitle(elem) {
elem.html('');
}
jQuery.fn.srt = function(o) {
var that = this;
var opts = $.extend({
'interval': 100,
'id': that.attr('data-video'),
'url': that.attr('data-srt'),
'showSubtitle': showSubtitle,
'hideSubtitle': hideSubtitle,
'subtitlesLoaded': $.noop
}, o);
this.each(function() {
var subtitleElement = $(this);
var videoId = opts.id;
if(!videoId) return;
var srtUrl = opts.url;
if(srtUrl) {
$(this).load(srtUrl, function (responseText, textStatus, req) { playSubtitles(subtitleElement, opts)});
} else {
playSubtitles(subtitleElement, opts);
}
});
};
})(jQuery);