neddit

the frontend of the frontpage of the internet  https://neddit.ce9e.org
git clone https://git.ce9e.org/neddit.git

commit
f7ba85fb9d4bd2a586327a45cf7dba4a560cb181
parent
2659510c3c9768d2c99ff050a2318660ba4e97d7
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2023-10-03 05:37
convert video.js to module

Diffstat

M static/video.js 92 ++++++++++++++++++++++++++++++-------------------------------
M templates/base.html 2 +-

2 files changed, 46 insertions, 48 deletions


diff --git a/static/video.js b/static/video.js

@@ -1,55 +1,53 @@
    1    -1 (function() {
    2    -1 	var thottle = function(fn, timeout) {
    3    -1 		var blocked = false;
   -1     1 var thottle = function(fn, timeout) {
   -1     2 	var blocked = false;
    4     3 
    5    -1 		return function() {
    6    -1 			if (!blocked) {
    7    -1 				blocked = true;
    8    -1 				setTimeout(function() {
    9    -1 					blocked = false;
   10    -1 				}, timeout);
   11    -1 				fn();
   12    -1 			}
   13    -1 		};
   -1     4 	return function() {
   -1     5 		if (!blocked) {
   -1     6 			blocked = true;
   -1     7 			setTimeout(function() {
   -1     8 				blocked = false;
   -1     9 			}, timeout);
   -1    10 			fn();
   -1    11 		}
   14    12 	};
   -1    13 };
   15    14 
   16    -1 	window.addEventListener('scroll', thottle(function() {
   17    -1 		document.querySelectorAll('video').forEach(function(video) {
   18    -1 			if (video.paused) {
   19    -1 				return;
   20    -1 			}
   21    -1 			var rect = video.getBoundingClientRect();
   22    -1 			if (rect.bottom < 0 || window.innerHeight - rect.top < 0) {
   23    -1 				video.pause();
   24    -1 			}
   25    -1 		});
   26    -1 	}, 100), {passive: true});
   27    -1 
   -1    15 window.addEventListener('scroll', thottle(function() {
   28    16 	document.querySelectorAll('video').forEach(function(video) {
   29    -1 		if (video.canPlayType('audio/mpegurl') || video.canPlayType('application/dash+xml')) {
   -1    17 		if (video.paused) {
   30    18 			return;
   31    19 		}
   32    -1 
   33    -1 		var url = video.querySelectorAll('source')[4].src;
   34    -1 		var audio = document.createElement('audio');
   35    -1 		audio.src = url.replace('DASH_96', 'DASH_AUDIO_64');
   36    -1 		audio.preload = 'none';
   37    -1 		audio.loop = true;
   38    -1 		video.after(audio);
   39    -1 
   40    -1 		var sync = function() {
   41    -1 			audio.currentTime = video.currentTime;
   -1    20 		var rect = video.getBoundingClientRect();
   -1    21 		if (rect.bottom < 0 || window.innerHeight - rect.top < 0) {
   -1    22 			video.pause();
   42    23 		}
   43    -1 		video.onplay = function() {
   44    -1 			sync();
   45    -1 			audio.play();
   46    -1 		};
   47    -1 		video.onpause = function() {
   48    -1 			audio.pause();
   49    -1 		};
   50    -1 		video.onseeked = sync;
   51    -1 		video.onvolumechange = function() {
   52    -1 			audio.volume = video.volume;
   53    -1 		};
   54    24 	});
   55    -1 })();
   -1    25 }, 100), {passive: true});
   -1    26 
   -1    27 document.querySelectorAll('video').forEach(function(video) {
   -1    28 	if (video.canPlayType('audio/mpegurl') || video.canPlayType('application/dash+xml')) {
   -1    29 		return;
   -1    30 	}
   -1    31 
   -1    32 	var url = video.querySelectorAll('source')[4].src;
   -1    33 	var audio = document.createElement('audio');
   -1    34 	audio.src = url.replace('DASH_96', 'DASH_AUDIO_64');
   -1    35 	audio.preload = 'none';
   -1    36 	audio.loop = true;
   -1    37 	video.after(audio);
   -1    38 
   -1    39 	var sync = function() {
   -1    40 		audio.currentTime = video.currentTime;
   -1    41 	}
   -1    42 	video.onplay = function() {
   -1    43 		sync();
   -1    44 		audio.play();
   -1    45 	};
   -1    46 	video.onpause = function() {
   -1    47 		audio.pause();
   -1    48 	};
   -1    49 	video.onseeked = sync;
   -1    50 	video.onvolumechange = function() {
   -1    51 		audio.volume = video.volume;
   -1    52 	};
   -1    53 });

diff --git a/templates/base.html b/templates/base.html

@@ -31,6 +31,6 @@
   31    31 		</header>
   32    32 	{% endif %}
   33    33 	{% block content %}{% endblock %}
   34    -1 	<script src="/static/video.js"></script>
   -1    34 	<script src="/static/video.js" type="module"></script>
   35    35 </body>
   36    36 </html>