converted to jquery method, added .hasOwnProperty checks
This commit is contained in:
parent
abcdc08bd6
commit
c5b3560fca
|
@ -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;
|
||||||
|
|
|
@ -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];
|
||||||
|
@ -54,15 +61,20 @@ $(document).ready(function() {
|
||||||
os = toSeconds(o);
|
os = toSeconds(o);
|
||||||
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(s > currentTime)
|
if (subtitles.hasOwnProperty(s)) {
|
||||||
break
|
if(s > currentTime)
|
||||||
subtitle = s;
|
break
|
||||||
|
subtitle = s;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if(subtitle > 0) {
|
if(subtitle > 0) {
|
||||||
if(subtitle != currentSubtitle) {
|
if(subtitle != currentSubtitle) {
|
||||||
|
@ -74,17 +86,19 @@ $(document).ready(function() {
|
||||||
}
|
}
|
||||||
}, 100);
|
}, 100);
|
||||||
}
|
}
|
||||||
$('.srt').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)})
|
|
||||||
} else {
|
|
||||||
playSubtitles(subtitleElement);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
});
|
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)});
|
||||||
|
} else {
|
||||||
|
playSubtitles(subtitleElement);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
})();
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user