Skip to content

Commit 53d349a

Browse files
committed
Inline a few JS functions
Helps sending less bytes to the client.
1 parent b344e1a commit 53d349a

File tree

1 file changed

+30
-97
lines changed

1 file changed

+30
-97
lines changed

assets/js/player.js

Lines changed: 30 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -60,29 +60,19 @@ videojs.Vhs.xhr.beforeRequest = function(options) {
6060
var player = videojs('player', options);
6161

6262
const storage = (() => {
63-
try {
64-
if (localStorage.length !== -1) {
65-
return localStorage;
66-
}
67-
} catch (e) {
68-
console.info('No storage available: ' + e);
69-
}
63+
try { if (localStorage.length !== -1) return localStorage; }
64+
catch (e) { console.info('No storage available: ' + e); }
65+
7066
return undefined;
7167
})();
7268

7369
if (location.pathname.startsWith('/embed/')) {
70+
var overlay_content = '<h1><a rel="noopener" target="_blank" href="' + location.origin + '/watch?v=' + video_data.id + '">' + player_data.title + '</a></h1>';
7471
player.overlay({
75-
overlays: [{
76-
start: 'loadstart',
77-
content: '<h1><a rel="noopener" target="_blank" href="' + location.origin + '/watch?v=' + video_data.id + '">' + player_data.title + '</a></h1>',
78-
end: 'playing',
79-
align: 'top'
80-
}, {
81-
start: 'pause',
82-
content: '<h1><a rel="noopener" target="_blank" href="' + location.origin + '/watch?v=' + video_data.id + '">' + player_data.title + '</a></h1>',
83-
end: 'playing',
84-
align: 'top'
85-
}]
72+
overlays: [
73+
{ start: 'loadstart', content: overlay_content, end: 'playing', align: 'top'},
74+
{ start: 'pause', content: overlay_content, end: 'playing', align: 'top'}
75+
]
8676
});
8777
}
8878

@@ -99,9 +89,7 @@ if (isMobile()) {
9989

10090
buttons = ["playToggle", "volumePanel", "captionsButton"];
10191

102-
if (video_data.params.quality !== 'dash') {
103-
buttons.push("qualitySelector")
104-
}
92+
if (video_data.params.quality !== 'dash') buttons.push("qualitySelector")
10593

10694
// Create new control bar object for operation buttons
10795
const ControlBar = videojs.getComponent("controlBar");
@@ -146,16 +134,12 @@ player.on('error', function (event) {
146134

147135
player.load();
148136

149-
if (currentTime > 0.5) {
150-
currentTime -= 0.5;
151-
}
137+
if (currentTime > 0.5) currentTime -= 0.5;
152138

153139
player.currentTime(currentTime);
154140
player.playbackRate(playbackRate);
155141

156-
if (!paused) {
157-
player.play();
158-
}
142+
if (!paused) player.play();
159143
}, 5000);
160144
}
161145
});
@@ -183,13 +167,8 @@ if (video_data.params.video_start > 0 || video_data.params.video_end > 0) {
183167

184168
player.markers({
185169
onMarkerReached: function (marker) {
186-
if (marker.text === 'End') {
187-
if (player.loop()) {
188-
player.markers.prev('Start');
189-
} else {
190-
player.pause();
191-
}
192-
}
170+
if (marker.text === 'End')
171+
player.loop() ? player.markers.prev('Start') : player.pause();
193172
},
194173
markers: markers
195174
});
@@ -217,9 +196,7 @@ if (video_data.params.save_player_pos) {
217196
const remeberedTime = get_video_time();
218197
let lastUpdated = 0;
219198

220-
if(!hasTimeParam) {
221-
set_seconds_after_start(remeberedTime);
222-
}
199+
if(!hasTimeParam) set_seconds_after_start(remeberedTime);
223200

224201
const updateTime = () => {
225202
const raw = player.currentTime();
@@ -233,9 +210,7 @@ if (video_data.params.save_player_pos) {
233210

234211
player.on("timeupdate", updateTime);
235212
}
236-
else {
237-
remove_all_video_times();
238-
}
213+
else remove_all_video_times();
239214

240215
if (video_data.params.autoplay) {
241216
var bpb = player.getChild('bigPlayButton');
@@ -433,26 +408,10 @@ function set_time_percent(percent) {
433408
player.currentTime(newTime);
434409
}
435410

436-
function play() {
437-
player.play();
438-
}
439-
440-
function pause() {
441-
player.pause();
442-
}
443-
444-
function stop() {
445-
player.pause();
446-
player.currentTime(0);
447-
}
448-
449-
function toggle_play() {
450-
if (player.paused()) {
451-
play();
452-
} else {
453-
pause();
454-
}
455-
}
411+
function play() { player.play(); }
412+
function pause() { player.pause(); }
413+
function stop() { player.pause(); player.currentTime(0); }
414+
function toggle_play() { player.paused() ? play() : pause(); }
456415

457416
const toggle_captions = (function () {
458417
let toggledTrack = null;
@@ -490,9 +449,7 @@ const toggle_captions = (function () {
490449
const tracks = player.textTracks();
491450
for (let i = 0; i < tracks.length; i++) {
492451
const track = tracks[i];
493-
if (track.kind !== 'captions') {
494-
continue;
495-
}
452+
if (track.kind !== 'captions') continue;
496453

497454
if (fallbackCaptionsTrack === null) {
498455
fallbackCaptionsTrack = track;
@@ -513,11 +470,7 @@ const toggle_captions = (function () {
513470
})();
514471

515472
function toggle_fullscreen() {
516-
if (player.isFullscreen()) {
517-
player.exitFullscreen();
518-
} else {
519-
player.requestFullscreen();
520-
}
473+
player.isFullscreen() ? player.exitFullscreen() : player.requestFullscreen();
521474
}
522475

523476
function increase_playback_rate(steps) {
@@ -560,27 +513,15 @@ window.addEventListener('keydown', e => {
560513
action = toggle_play;
561514
break;
562515

563-
case 'MediaPlay':
564-
action = play;
565-
break;
566-
567-
case 'MediaPause':
568-
action = pause;
569-
break;
570-
571-
case 'MediaStop':
572-
action = stop;
573-
break;
516+
case 'MediaPlay': action = play; break;
517+
case 'MediaPause': action = pause; break;
518+
case 'MediaStop': action = stop; break;
574519

575520
case 'ArrowUp':
576-
if (isPlayerFocused) {
577-
action = increase_volume.bind(this, 0.1);
578-
}
521+
if (isPlayerFocused) action = increase_volume.bind(this, 0.1);
579522
break;
580523
case 'ArrowDown':
581-
if (isPlayerFocused) {
582-
action = increase_volume.bind(this, -0.1);
583-
}
524+
if (isPlayerFocused) action = increase_volume.bind(this, -0.1);
584525
break;
585526

586527
case 'm':
@@ -619,12 +560,8 @@ window.addEventListener('keydown', e => {
619560
action = set_time_percent.bind(this, percent);
620561
break;
621562

622-
case 'c':
623-
action = toggle_captions;
624-
break;
625-
case 'f':
626-
action = toggle_fullscreen;
627-
break;
563+
case 'c': action = toggle_captions; break;
564+
case 'f': action = toggle_fullscreen; break;
628565

629566
case 'N':
630567
case 'MediaTrackNext':
@@ -642,12 +579,8 @@ window.addEventListener('keydown', e => {
642579
// TODO: Add support for previous-frame-stepping.
643580
break;
644581

645-
case '>':
646-
action = increase_playback_rate.bind(this, 1);
647-
break;
648-
case '<':
649-
action = increase_playback_rate.bind(this, -1);
650-
break;
582+
case '>': action = increase_playback_rate.bind(this, 1); break;
583+
case '<': action = increase_playback_rate.bind(this, -1); break;
651584

652585
default:
653586
console.info('Unhandled key down event: %s:', decoratedKey, e);

0 commit comments

Comments
 (0)