Skip to content

Instantly share code, notes, and snippets.

@teslamint
Last active August 29, 2015 14:12
Show Gist options
  • Select an option

  • Save teslamint/b207c2f4939c394ff61b to your computer and use it in GitHub Desktop.

Select an option

Save teslamint/b207c2f4939c394ff61b to your computer and use it in GitHub Desktop.
Loop video with specific range on Youtube.
/* License: Public Domain. */
(function(w,yt){
if (!w||!yt||typeof w._ytrepeat == "object") return;
function ytrepeat() {
this.startTime=0;
this.endTime=0;
this._timer=undefined;
}
ytrepeat.prototype.convertTime=function(str){
var array = str.split(':');
if (!array||array.count<2) return 0;
if (array.count==3)
return parseInt(array[0])*3600+parseInt(array[1])*60+parseInt(array[2]);
else
return parseInt(array[0])*60+parseInt(array[1]);
};
ytrepeat.prototype.init=function(){
this.startTime = this.convertTime(prompt('start time?(e.g. "0:12:34")'));
this.endTime = this.convertTime(prompt('end time?(e.g. "0:12:34")'));
if (this.startTime == this.endTime || this.endTime === 0) return;
if (this.startTime > this.endTime) {
var temp = this.startTime; this.startTime = this.endTime; this.endTime = temp;
}
this.run();
};
ytrepeat.prototype.run=function() {
if (this._timer != undefined) return;
this._timer = setInterval(this._loop, 1000);
yt.player.getPlayerByElement('player-api').addEventListener('onStateChange', function(e) {
if (e === -1) window._ytp.stop();
});
}
ytrepeat.prototype.stop=function() {
if (!this._timer) return;
clearInterval(this._timer);
}
ytrepeat.prototype._loop=function(){
var r=w._ytp;
var p=yt.player.getPlayerByElement('player-api');
if (!r||!p||!p.isReady()) return;
if(p.getPlayerState()===1){
var current_time = p.getCurrentTime();
if (current_time >= r.endTime) {
p.seekTo(r.startTime);
} else if (current_time < r.startTime) {
p.seekTo(r.startTime);
}
} else if (p.getPlayerState()===-1){
r.stop();
}
}
w._ytp = new ytrepeat;
w._ytp.init();
})(window,yt);
/* License: Public Domain. Add your bookmark with javascript: prefix. */
!function(t,e){function i(){this.startTime=0,this.endTime=0,this._timer=void 0}t&&e&&"object"!=typeof t._ytrepeat&&(i.prototype.convertTime=function(t){var e=t.split(":");return!e||e.count<2?0:3==e.count?3600*parseInt(e[0])+60*parseInt(e[1])+parseInt(e[2]):60*parseInt(e[0])+parseInt(e[1])},i.prototype.init=function(){if(this.startTime=this.convertTime(prompt('start time?(e.g. "0:12:34")')),this.endTime=this.convertTime(prompt('end time?(e.g. "0:12:34")')),this.startTime!=this.endTime&&0!==this.endTime){if(this.startTime>this.endTime){var t=this.startTime;this.startTime=this.endTime,this.endTime=t}this.run()}},i.prototype.run=function(){void 0==this._timer&&(this._timer=setInterval(this._loop,1e3),e.player.getPlayerByElement("player-api").addEventListener("onStateChange",function(t){-1===t&&window._ytp.stop()}))},i.prototype.stop=function(){this._timer&&clearInterval(this._timer)},i.prototype._loop=function(){var i=t._ytp,r=e.player.getPlayerByElement("player-api");if(i&&r&&r.isReady())if(1===r.getPlayerState()){var n=r.getCurrentTime();n>=i.endTime?r.seekTo(i.startTime):n<i.startTime&&r.seekTo(i.startTime)}else-1===r.getPlayerState()&&i.stop()},t._ytp=new i,t._ytp.init())}(window,yt);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment