View difference between Paste ID: iz3C6RXt and ZGcpri47
SHOW: | | - or go back to the newest paste.
1
// Youtube for peoples with extremely slow connexion. Like me.
2
// All hacks like media.mediasource.enabled, media.cache_readhead_limit, etc. don't work ? 
3
// This is a manual JS way to force it.
4
5
/*
6
What it does :
7
It pauses your youtube video. When video stop buffering it manually move the seeker to force the download.
8
When the download is finished, it place the cursor back to it's original position. You are then ready to play a fully buffered video.
9
*/
10
11
/*
12
How to use :
13
Paste this script in the console of your browser (F12 > Console). Tested on Firefox Linux.
14
*/
15
16
// Note : Maximize your "browser.cache.disk.capacity" (type "about:config" in Firefox) if you are loading long videos
17
18
const success = [
19
 'background: rgba(0,204,0,0.2)',
20
 'color: yellow',
21
 'display: block',
22
 'text-align: left'
23
].join(';');
24
25
const failure = [
26
 'background: rgba(255,0,0,0.2)',
27
 'color: yellow',
28
 'display: block',
29
 'text-align: left'
30
].join(';');
31
32
const warning = [
33
 'background: rgba(55, 102, 0,0.2)',
34
 'color: orange',
35
 'display: block',
36
 'text-align: left'
37
].join(';');
38
39
console.info('%c Youtubeforce ftw!', success);
40
var current_download = 0;
41
var previous_download = 0;
42
var loop_since_download = 0;
43
var loop = 1;
44
var player = document.getElementById('movie_player'); 
45
var duration = player.getDuration();
46
var first_chunk_loaded =  Math.round((player.getVideoBytesLoaded() / player.getVideoBytesTotal())*100);
47
var last_chunk_loaded = 0;
48
var first_buffer_time = false;
49
var waiter = 0;
50
var total_restart_buffer = 0;
51
if(player){
52
	var inter = setInterval(function(){
53
		var d = new Date();
54
		current_download = player.getVideoBytesLoaded();
55
		if(waiter > 0) waiter--;
56
		if(waiter <= 0){
57
			if(player.getVideoBytesLoaded() != "NaN"){
58
				// Try to buffer if buffer is not at the end
59
				if(player.getVideoBytesLoaded() < (player.getVideoBytesTotal() - 0.09)){
60
					// Video is not loading anymore...
61
					if(current_download == previous_download){
62
						// Video buffer is stop. wait a bit.
63
						loop_since_download++;
64
						if(loop_since_download > 10){
65
							if(loop_since_download > 30){
66
								console.log("%c Slow connexion..., pause for 20 loops "+d, error);
67
								waiter = 20;
68
							}
69
							if(!first_buffer_time){
70
								first_buffer_time = player.getCurrentTime();
71
							}
72
							console.log("%c Restart buffer (10 loops since last download), and pause for 10 loops... (already restarted "+total_restart_buffer+" times "+d, warning);
73
							// sectotal * byteLoaded / byteTotal = sec loaded
74
							var current_time_loaded = player.getDuration() * (player.getVideoBytesLoaded() / player.getVideoBytesTotal());
75
							player.seekTo(current_time_loaded - 2); // - 2sec
76
							player.pauseVideo();
77
							waiter = 10;
78
							total_restart_buffer++;
79
						}
80
					// Video is still loading...
81
					}else{
82
						previous_download = player.getVideoBytesLoaded();
83
						loop_since_download = 0;
84
						var current_chunk_loaded =  Math.round((player.getVideoBytesLoaded() / player.getVideoBytesTotal()) * 100);
85
						console.info("%c Video is buffering properly ("+current_chunk_loaded+"%). "+loop+" loops, and "+loop_since_download+" iteration since last chunk, wait 10 loops "+d, success);
86
						waiter = 10;
87
					}
88
					loop++;
89
				}else{
90
					last_chunk_loaded =  Math.round((player.getVideoBytesLoaded() / player.getVideoBytesTotal())*100);
91
					console.log("%c Everything is loaded or the tab is inactive... Wait 50sec. Total number of buffer restart : "+total_restart_buffer+" and Total buffered : "+last_chunk_loaded+"%. Buffered on launch : "+first_chunk_loaded, success); 
92
					waiter = 50;
93
					// restarting to first buffer and play
94
					if(first_buffer_time){ 
95
						player.seekTo(first_buffer_time); 
96
						first_buffer_time = false; 
97
					}
98
				}
99
			}else{
100
				console.log("%c Error getting stream. Waiting..., wait 10 loops", error);
101
				waiter = 10;
102
			}
103
		}else{
104
				//console.log("We are waiting...");
105
			}
106
	},1000);
107
}