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

View File

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