converted to jquery method, added .hasOwnProperty checks

This commit is contained in:
Sanjay B 2009-11-21 20:45:32 +05:30
parent abcdc08bd6
commit c5b3560fca
2 changed files with 39 additions and 19 deletions

View File

@ -7,6 +7,12 @@
cortado_location = "http://footage.stealthisfilm.com/static/cortado.video.jar";
</script>
<script type="text/javascript" src="jquery.srt.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('.srt').srt();
});
</script>
<style>
body {
color: #000;

View File

@ -9,15 +9,19 @@
*/
/*
usage:
html:
<video src="example.ogg" id="examplevideo" />
<div class="srt" data-video="examplevideo" data-srt="example.srt"></div>
jquery.srt.js will try to load subtitles in all elements with 'srt' class.
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.
*/
$(document).ready(function() {
(function() {
function toSeconds(t) {
var s = 0.0
if(t) {
@ -27,19 +31,22 @@ $(document).ready(function() {
}
return s;
}
function strip(s) {
return s.replace(/^\s+|\s+$/g,"");
}
function playSubtitles(subtitleElement) {
var videoId = subtitleElement.attr('data-video');
var srt = subtitleElement.text();
subtitleElement.text('');
srt = srt.replace(/\r\n|\r|\n/g, '\n')
subtitleElement.html('');
srt = srt.replace('\r\n|\r|\n', '\n')
var subtitles = {};
srt = strip(srt);
var srt_ = srt.split('\n\n');
for(s in srt_) {
if (srt_.hasOwnProperty(s)) {
st = srt_[s].split('\n');
if(st.length >=2) {
n = st[0];
@ -55,15 +62,20 @@ $(document).ready(function() {
subtitles[is] = {i:i, o: o, t: t};
}
}
}
var currentSubtitle = -1;
var ival = setInterval(function() {
var currentTime = document.getElementById(videoId).currentTime;
var subtitle = -1;
for(s in subtitles) {
if (subtitles.hasOwnProperty(s)) {
if(s > currentTime)
break
subtitle = s;
}
}
if(subtitle > 0) {
if(subtitle != currentSubtitle) {
subtitleElement.html(subtitles[subtitle].t);
@ -74,17 +86,19 @@ $(document).ready(function() {
}
}, 100);
}
$('.srt').each(function() {
jQuery.fn.srt = function() {
this.each(function() {
var subtitleElement = $(this);
var videoId = subtitleElement.attr('data-video');
if(!videoId) return;
var srtUrl = subtitleElement.attr('data-srt');
if(srtUrl) {
$(this).load(srtUrl, function (responseText, textStatus, req) { playSubtitles(subtitleElement)})
$(this).load(srtUrl, function (responseText, textStatus, req) { playSubtitles(subtitleElement)});
} else {
playSubtitles(subtitleElement);
}
});
});
};
})();