2020-03-15 14:46:08 -07:00
|
|
|
var player_data = JSON.parse(document.getElementById('player_data').innerHTML);
|
|
|
|
var video_data = JSON.parse(document.getElementById('video_data').innerHTML);
|
|
|
|
|
2019-05-06 09:23:14 -07:00
|
|
|
var options = {
|
2019-06-07 17:56:41 -07:00
|
|
|
preload: 'auto',
|
2019-05-20 10:15:48 -07:00
|
|
|
liveui: true,
|
2019-07-14 08:08:46 -07:00
|
|
|
playbackRates: [0.25, 0.5, 0.75, 1.0, 1.25, 1.5, 1.75, 2.0],
|
2019-05-06 09:23:14 -07:00
|
|
|
controlBar: {
|
|
|
|
children: [
|
2019-06-07 17:56:41 -07:00
|
|
|
'playToggle',
|
|
|
|
'volumePanel',
|
|
|
|
'currentTimeDisplay',
|
|
|
|
'timeDivider',
|
|
|
|
'durationDisplay',
|
|
|
|
'progressControl',
|
|
|
|
'remainingTimeDisplay',
|
2021-05-03 02:25:32 -07:00
|
|
|
'Spacer',
|
2019-06-07 17:56:41 -07:00
|
|
|
'captionsButton',
|
|
|
|
'qualitySelector',
|
|
|
|
'playbackRateMenuButton',
|
|
|
|
'fullscreenToggle'
|
2019-05-06 09:23:14 -07:00
|
|
|
]
|
2021-01-16 17:43:36 -08:00
|
|
|
},
|
|
|
|
html5: {
|
2021-02-23 22:02:55 -08:00
|
|
|
preloadTextTracks: false,
|
2022-01-30 07:22:31 -08:00
|
|
|
vhs: {
|
2021-01-16 17:43:36 -08:00
|
|
|
overrideNative: true
|
|
|
|
}
|
2019-05-06 09:23:14 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (player_data.aspect_ratio) {
|
|
|
|
options.aspectRatio = player_data.aspect_ratio;
|
|
|
|
}
|
|
|
|
|
|
|
|
var embed_url = new URL(location);
|
|
|
|
embed_url.searchParams.delete('v');
|
2022-02-19 08:03:22 -08:00
|
|
|
var short_url = location.origin + '/' + video_data.id + embed_url.search;
|
2019-05-06 09:23:14 -07:00
|
|
|
embed_url = location.origin + '/embed/' + video_data.id + embed_url.search;
|
|
|
|
|
2021-10-26 16:25:29 -07:00
|
|
|
var save_player_pos_key = "save_player_pos";
|
2021-10-25 17:59:36 -07:00
|
|
|
|
2022-01-30 07:22:31 -08:00
|
|
|
videojs.Vhs.xhr.beforeRequest = function(options) {
|
2020-12-29 14:58:24 -08:00
|
|
|
if (options.uri.indexOf('videoplayback') === -1 && options.uri.indexOf('local=true') === -1) {
|
2020-12-07 21:47:26 -08:00
|
|
|
options.uri = options.uri + '?local=true';
|
|
|
|
}
|
|
|
|
return options;
|
|
|
|
};
|
|
|
|
|
2019-08-16 14:01:14 -07:00
|
|
|
var player = videojs('player', options);
|
2019-05-06 09:23:14 -07:00
|
|
|
|
2022-02-19 08:03:22 -08:00
|
|
|
/**
|
|
|
|
* Function for add time argument to url
|
|
|
|
* @param {String} url
|
|
|
|
* @returns urlWithTimeArg
|
|
|
|
*/
|
|
|
|
function addCurrentTimeToURL(url) {
|
|
|
|
var urlUsed = new URL(url);
|
|
|
|
urlUsed.searchParams.delete('start');
|
|
|
|
var currentTime = Math.ceil(player.currentTime());
|
|
|
|
if (currentTime > 0)
|
|
|
|
urlUsed.searchParams.set('t', currentTime);
|
|
|
|
else
|
|
|
|
if (urlUsed.searchParams.has('t'))
|
|
|
|
urlUsed.searchParams.delete('t');
|
|
|
|
return urlUsed;
|
|
|
|
}
|
|
|
|
|
|
|
|
var shareOptions = {
|
|
|
|
socials: ['fbFeed', 'tw', 'reddit', 'email'],
|
|
|
|
|
|
|
|
get url() {
|
|
|
|
return addCurrentTimeToURL(short_url);
|
|
|
|
},
|
|
|
|
title: player_data.title,
|
|
|
|
description: player_data.description,
|
|
|
|
image: player_data.thumbnail,
|
|
|
|
get embedCode() {
|
|
|
|
return "<iframe id='ivplayer' width='640' height='360' src='" +
|
2022-02-21 15:21:47 -08:00
|
|
|
addCurrentTimeToURL(embed_url) + "' style='border:none;'></iframe>";
|
2022-02-19 08:03:22 -08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-12-26 04:45:27 -08:00
|
|
|
const storage = (() => {
|
2022-02-07 15:08:52 -08:00
|
|
|
try { if (localStorage.length !== -1) return localStorage; }
|
|
|
|
catch (e) { console.info('No storage available: ' + e); }
|
|
|
|
|
2021-12-26 04:45:27 -08:00
|
|
|
return undefined;
|
|
|
|
})();
|
2020-12-07 21:47:26 -08:00
|
|
|
|
2019-06-08 08:52:47 -07:00
|
|
|
if (location.pathname.startsWith('/embed/')) {
|
2022-02-07 15:08:52 -08:00
|
|
|
var overlay_content = '<h1><a rel="noopener" target="_blank" href="' + location.origin + '/watch?v=' + video_data.id + '">' + player_data.title + '</a></h1>';
|
2019-06-08 08:52:47 -07:00
|
|
|
player.overlay({
|
2022-02-07 15:08:52 -08:00
|
|
|
overlays: [
|
|
|
|
{ start: 'loadstart', content: overlay_content, end: 'playing', align: 'top'},
|
|
|
|
{ start: 'pause', content: overlay_content, end: 'playing', align: 'top'}
|
|
|
|
]
|
2019-06-08 08:52:47 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-02-07 04:57:14 -08:00
|
|
|
// Detect mobile users and initialize mobileUi for better UX
|
2021-04-10 23:12:35 -07:00
|
|
|
// Detection code taken from https://stackoverflow.com/a/20293441
|
|
|
|
|
|
|
|
function isMobile() {
|
|
|
|
try{ document.createEvent("TouchEvent"); return true; }
|
|
|
|
catch(e){ return false; }
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isMobile()) {
|
2021-04-10 20:22:30 -07:00
|
|
|
player.mobileUi();
|
2021-04-10 23:12:35 -07:00
|
|
|
|
|
|
|
buttons = ["playToggle", "volumePanel", "captionsButton"];
|
|
|
|
|
2022-02-07 15:08:52 -08:00
|
|
|
if (video_data.params.quality !== 'dash') buttons.push("qualitySelector")
|
2021-04-10 23:12:35 -07:00
|
|
|
|
|
|
|
// Create new control bar object for operation buttons
|
|
|
|
const ControlBar = videojs.getComponent("controlBar");
|
|
|
|
let operations_bar = new ControlBar(player, {
|
|
|
|
children: [],
|
|
|
|
playbackRates: [0.25, 0.5, 0.75, 1.0, 1.25, 1.5, 1.75, 2.0]
|
|
|
|
});
|
|
|
|
buttons.slice(1).forEach(child => operations_bar.addChild(child))
|
|
|
|
|
|
|
|
// Remove operation buttons from primary control bar
|
|
|
|
primary_control_bar = player.getChild("controlBar");
|
|
|
|
buttons.forEach(child => primary_control_bar.removeChild(child));
|
|
|
|
|
|
|
|
operations_bar_element = operations_bar.el();
|
|
|
|
operations_bar_element.className += " mobile-operations-bar"
|
|
|
|
player.addChild(operations_bar)
|
|
|
|
|
2022-02-07 04:57:14 -08:00
|
|
|
// Playback menu doesn't work when it's initialized outside of the primary control bar
|
2021-04-10 23:12:35 -07:00
|
|
|
playback_element = document.getElementsByClassName("vjs-playback-rate")[0]
|
|
|
|
operations_bar_element.append(playback_element)
|
|
|
|
|
|
|
|
// The share and http source selector element can't be fetched till the players ready.
|
|
|
|
player.one("playing", () => {
|
|
|
|
share_element = document.getElementsByClassName("vjs-share-control")[0]
|
|
|
|
operations_bar_element.append(share_element)
|
|
|
|
|
|
|
|
if (video_data.params.quality === 'dash') {
|
|
|
|
http_source_selector = document.getElementsByClassName("vjs-http-source-selector vjs-menu-button")[0]
|
|
|
|
operations_bar_element.append(http_source_selector)
|
|
|
|
}
|
|
|
|
})
|
2021-04-10 20:22:30 -07:00
|
|
|
}
|
|
|
|
|
2019-05-06 09:23:14 -07:00
|
|
|
player.on('error', function (event) {
|
|
|
|
if (player.error().code === 2 || player.error().code === 4) {
|
2021-01-10 14:05:08 -08:00
|
|
|
setTimeout(function (event) {
|
2022-02-07 04:57:14 -08:00
|
|
|
console.log('An error occurred in the player, reloading...');
|
2019-05-06 09:23:14 -07:00
|
|
|
|
|
|
|
var currentTime = player.currentTime();
|
|
|
|
var playbackRate = player.playbackRate();
|
|
|
|
var paused = player.paused();
|
|
|
|
|
|
|
|
player.load();
|
|
|
|
|
2022-02-07 15:08:52 -08:00
|
|
|
if (currentTime > 0.5) currentTime -= 0.5;
|
2019-05-06 09:23:14 -07:00
|
|
|
|
|
|
|
player.currentTime(currentTime);
|
|
|
|
player.playbackRate(playbackRate);
|
|
|
|
|
2022-02-07 15:08:52 -08:00
|
|
|
if (!paused) player.play();
|
2021-01-10 14:05:08 -08:00
|
|
|
}, 5000);
|
2019-05-06 09:23:14 -07:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-04-11 06:09:10 -07:00
|
|
|
// Enable VR video support
|
2021-09-14 16:37:23 -07:00
|
|
|
if (!video_data.params.listen && video_data.vr && video_data.params.vr_mode) {
|
2021-04-11 19:43:26 -07:00
|
|
|
player.crossOrigin("anonymous")
|
2021-09-10 00:42:39 -07:00
|
|
|
switch (video_data.projection_type) {
|
|
|
|
case "EQUIRECTANGULAR":
|
|
|
|
player.vr({projection: "equirectangular"});
|
|
|
|
default: // Should only be "MESH" but we'll use this as a fallback.
|
|
|
|
player.vr({projection: "EAC"});
|
|
|
|
}
|
2021-04-11 06:09:10 -07:00
|
|
|
}
|
|
|
|
|
2019-05-06 09:23:14 -07:00
|
|
|
// Add markers
|
|
|
|
if (video_data.params.video_start > 0 || video_data.params.video_end > 0) {
|
|
|
|
var markers = [{ time: video_data.params.video_start, text: 'Start' }];
|
|
|
|
|
|
|
|
if (video_data.params.video_end < 0) {
|
|
|
|
markers.push({ time: video_data.length_seconds - 0.5, text: 'End' });
|
|
|
|
} else {
|
|
|
|
markers.push({ time: video_data.params.video_end, text: 'End' });
|
|
|
|
}
|
|
|
|
|
|
|
|
player.markers({
|
|
|
|
onMarkerReached: function (marker) {
|
2022-02-07 15:08:52 -08:00
|
|
|
if (marker.text === 'End')
|
|
|
|
player.loop() ? player.markers.prev('Start') : player.pause();
|
2019-05-06 09:23:14 -07:00
|
|
|
},
|
|
|
|
markers: markers
|
|
|
|
});
|
|
|
|
|
|
|
|
player.currentTime(video_data.params.video_start);
|
|
|
|
}
|
|
|
|
|
|
|
|
player.volume(video_data.params.volume / 100);
|
|
|
|
player.playbackRate(video_data.params.speed);
|
|
|
|
|
2019-05-20 10:15:48 -07:00
|
|
|
player.on('waiting', function () {
|
|
|
|
if (player.playbackRate() > 1 && player.liveTracker.isLive() && player.liveTracker.atLiveEdge()) {
|
|
|
|
console.log('Player has caught up to source, resetting playbackRate.')
|
|
|
|
player.playbackRate(1);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-06-08 08:18:45 -07:00
|
|
|
if (video_data.premiere_timestamp && Math.round(new Date() / 1000) < video_data.premiere_timestamp) {
|
|
|
|
player.getChild('bigPlayButton').hide();
|
|
|
|
}
|
|
|
|
|
2021-10-26 16:25:29 -07:00
|
|
|
if (video_data.params.save_player_pos) {
|
2021-11-17 17:25:47 -08:00
|
|
|
const url = new URL(location);
|
|
|
|
const hasTimeParam = url.searchParams.has("t");
|
2021-10-25 17:59:36 -07:00
|
|
|
const remeberedTime = get_video_time();
|
|
|
|
let lastUpdated = 0;
|
|
|
|
|
2022-02-07 15:08:52 -08:00
|
|
|
if(!hasTimeParam) set_seconds_after_start(remeberedTime);
|
2021-10-25 17:59:36 -07:00
|
|
|
|
2021-10-26 15:43:28 -07:00
|
|
|
const updateTime = () => {
|
2021-10-25 17:59:36 -07:00
|
|
|
const raw = player.currentTime();
|
|
|
|
const time = Math.floor(raw);
|
|
|
|
|
2021-12-30 18:24:08 -08:00
|
|
|
if(lastUpdated !== time && raw <= video_data.length_seconds - 15) {
|
2021-10-25 17:59:36 -07:00
|
|
|
save_video_time(time);
|
|
|
|
lastUpdated = time;
|
|
|
|
}
|
2021-10-26 15:43:28 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
player.on("timeupdate", updateTime);
|
2021-10-25 17:59:36 -07:00
|
|
|
}
|
2022-02-07 15:08:52 -08:00
|
|
|
else remove_all_video_times();
|
2021-10-25 17:59:36 -07:00
|
|
|
|
2019-05-06 09:23:14 -07:00
|
|
|
if (video_data.params.autoplay) {
|
|
|
|
var bpb = player.getChild('bigPlayButton');
|
2019-06-08 08:18:45 -07:00
|
|
|
bpb.hide();
|
|
|
|
|
|
|
|
player.ready(function () {
|
|
|
|
new Promise(function (resolve, reject) {
|
|
|
|
setTimeout(() => resolve(1), 1);
|
|
|
|
}).then(function (result) {
|
|
|
|
var promise = player.play();
|
|
|
|
|
|
|
|
if (promise !== undefined) {
|
|
|
|
promise.then(_ => {
|
|
|
|
}).catch(error => {
|
|
|
|
bpb.show();
|
|
|
|
});
|
|
|
|
}
|
2019-05-06 09:23:14 -07:00
|
|
|
});
|
2019-06-08 08:18:45 -07:00
|
|
|
});
|
2019-05-06 09:23:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!video_data.params.listen && video_data.params.quality === 'dash') {
|
|
|
|
player.httpSourceSelector();
|
2020-12-13 01:16:26 -08:00
|
|
|
|
|
|
|
if (video_data.params.quality_dash != "auto") {
|
|
|
|
player.ready(() => {
|
|
|
|
player.on("loadedmetadata", () => {
|
|
|
|
const qualityLevels = Array.from(player.qualityLevels()).sort((a, b) => a.height - b.height);
|
2020-12-19 08:48:20 -08:00
|
|
|
let targetQualityLevel;
|
|
|
|
switch (video_data.params.quality_dash) {
|
|
|
|
case "best":
|
|
|
|
targetQualityLevel = qualityLevels.length - 1;
|
|
|
|
break;
|
|
|
|
case "worst":
|
|
|
|
targetQualityLevel = 0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
const targetHeight = Number.parseInt(video_data.params.quality_dash, 10);
|
|
|
|
for (let i = 0; i < qualityLevels.length; i++) {
|
|
|
|
if (qualityLevels[i].height <= targetHeight) {
|
|
|
|
targetQualityLevel = i;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-12-13 01:16:26 -08:00
|
|
|
for (let i = 0; i < qualityLevels.length; i++) {
|
2020-12-19 08:48:20 -08:00
|
|
|
qualityLevels[i].enabled = (i == targetQualityLevel);
|
2020-12-13 01:16:26 -08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2019-05-06 09:23:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
player.vttThumbnails({
|
2020-11-08 13:02:25 -08:00
|
|
|
src: location.origin + '/api/v1/storyboards/' + video_data.id + '?height=90',
|
|
|
|
showTimestamp: true
|
2019-05-06 09:23:14 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
// Enable annotations
|
2019-05-09 06:36:36 -07:00
|
|
|
if (!video_data.params.listen && video_data.params.annotations) {
|
2019-10-18 09:44:11 -07:00
|
|
|
window.addEventListener('load', function (e) {
|
|
|
|
var video_container = document.getElementById('player');
|
|
|
|
let xhr = new XMLHttpRequest();
|
|
|
|
xhr.responseType = 'text';
|
|
|
|
xhr.timeout = 60000;
|
|
|
|
xhr.open('GET', '/api/v1/annotations/' + video_data.id, true);
|
|
|
|
|
|
|
|
xhr.onreadystatechange = function () {
|
|
|
|
if (xhr.readyState === 4) {
|
|
|
|
if (xhr.status === 200) {
|
|
|
|
videojs.registerPlugin('youtubeAnnotationsPlugin', youtubeAnnotationsPlugin);
|
|
|
|
if (!player.paused()) {
|
2019-05-06 09:23:14 -07:00
|
|
|
player.youtubeAnnotationsPlugin({ annotationXml: xhr.response, videoContainer: video_container });
|
2019-10-18 09:44:11 -07:00
|
|
|
} else {
|
|
|
|
player.one('play', function (event) {
|
|
|
|
player.youtubeAnnotationsPlugin({ annotationXml: xhr.response, videoContainer: video_container });
|
|
|
|
});
|
|
|
|
}
|
2019-05-06 09:23:14 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-18 09:44:11 -07:00
|
|
|
window.addEventListener('__ar_annotation_click', e => {
|
|
|
|
const { url, target, seconds } = e.detail;
|
|
|
|
var path = new URL(url);
|
2019-05-06 09:23:14 -07:00
|
|
|
|
2019-10-18 09:44:11 -07:00
|
|
|
if (path.href.startsWith('https://www.youtube.com/watch?') && seconds) {
|
|
|
|
path.search += '&t=' + seconds;
|
|
|
|
}
|
2019-05-06 09:23:14 -07:00
|
|
|
|
2019-10-18 09:44:11 -07:00
|
|
|
path = path.pathname + path.search;
|
2019-05-06 09:23:14 -07:00
|
|
|
|
2019-10-18 09:44:11 -07:00
|
|
|
if (target === 'current') {
|
|
|
|
window.location.href = path;
|
|
|
|
} else if (target === 'new') {
|
|
|
|
window.open(path, '_blank');
|
|
|
|
}
|
|
|
|
});
|
2019-06-15 08:08:06 -07:00
|
|
|
|
2019-10-18 09:44:11 -07:00
|
|
|
xhr.send();
|
|
|
|
});
|
2019-05-06 09:23:14 -07:00
|
|
|
}
|
|
|
|
|
2019-08-16 14:01:14 -07:00
|
|
|
function increase_volume(delta) {
|
|
|
|
const curVolume = player.volume();
|
|
|
|
let newVolume = curVolume + delta;
|
|
|
|
if (newVolume > 1) {
|
|
|
|
newVolume = 1;
|
|
|
|
} else if (newVolume < 0) {
|
|
|
|
newVolume = 0;
|
|
|
|
}
|
|
|
|
player.volume(newVolume);
|
|
|
|
}
|
|
|
|
|
|
|
|
function toggle_muted() {
|
|
|
|
const isMuted = player.muted();
|
|
|
|
player.muted(!isMuted);
|
|
|
|
}
|
|
|
|
|
|
|
|
function skip_seconds(delta) {
|
|
|
|
const duration = player.duration();
|
|
|
|
const curTime = player.currentTime();
|
|
|
|
let newTime = curTime + delta;
|
|
|
|
if (newTime > duration) {
|
|
|
|
newTime = duration;
|
|
|
|
} else if (newTime < 0) {
|
|
|
|
newTime = 0;
|
|
|
|
}
|
|
|
|
player.currentTime(newTime);
|
|
|
|
}
|
|
|
|
|
2021-10-25 17:59:36 -07:00
|
|
|
function set_seconds_after_start(delta) {
|
|
|
|
const start = video_data.params.video_start;
|
|
|
|
player.currentTime(start + delta);
|
|
|
|
}
|
|
|
|
|
|
|
|
function save_video_time(seconds) {
|
|
|
|
const videoId = video_data.id;
|
|
|
|
const all_video_times = get_all_video_times();
|
|
|
|
|
|
|
|
all_video_times[videoId] = seconds;
|
|
|
|
|
|
|
|
set_all_video_times(all_video_times);
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_video_time() {
|
|
|
|
try {
|
|
|
|
const videoId = video_data.id;
|
|
|
|
const all_video_times = get_all_video_times();
|
|
|
|
const timestamp = all_video_times[videoId];
|
|
|
|
|
2021-10-26 15:43:28 -07:00
|
|
|
return timestamp || 0;
|
2021-10-25 17:59:36 -07:00
|
|
|
}
|
|
|
|
catch {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function set_all_video_times(times) {
|
2021-12-26 04:45:27 -08:00
|
|
|
if (storage) {
|
|
|
|
if (times) {
|
|
|
|
try {
|
|
|
|
storage.setItem(save_player_pos_key, JSON.stringify(times));
|
|
|
|
} catch (e) {
|
|
|
|
console.debug('set_all_video_times: ' + e);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
storage.removeItem(save_player_pos_key);
|
|
|
|
}
|
|
|
|
}
|
2021-10-25 17:59:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function get_all_video_times() {
|
2021-12-26 04:45:27 -08:00
|
|
|
if (storage) {
|
|
|
|
const raw = storage.getItem(save_player_pos_key);
|
|
|
|
if (raw !== null) {
|
|
|
|
try {
|
|
|
|
return JSON.parse(raw);
|
|
|
|
} catch (e) {
|
|
|
|
console.debug('get_all_video_times: ' + e);
|
|
|
|
}
|
|
|
|
}
|
2021-10-25 17:59:36 -07:00
|
|
|
}
|
2021-12-26 04:45:27 -08:00
|
|
|
return {};
|
2021-10-25 17:59:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function remove_all_video_times() {
|
2021-12-26 04:45:27 -08:00
|
|
|
set_all_video_times(null);
|
2021-10-25 17:59:36 -07:00
|
|
|
}
|
|
|
|
|
2019-08-16 14:01:14 -07:00
|
|
|
function set_time_percent(percent) {
|
|
|
|
const duration = player.duration();
|
|
|
|
const newTime = duration * (percent / 100);
|
|
|
|
player.currentTime(newTime);
|
|
|
|
}
|
|
|
|
|
2022-02-07 15:08:52 -08:00
|
|
|
function play() { player.play(); }
|
|
|
|
function pause() { player.pause(); }
|
|
|
|
function stop() { player.pause(); player.currentTime(0); }
|
|
|
|
function toggle_play() { player.paused() ? play() : pause(); }
|
2019-08-16 14:01:14 -07:00
|
|
|
|
2019-10-18 09:44:11 -07:00
|
|
|
const toggle_captions = (function () {
|
2019-08-16 14:01:14 -07:00
|
|
|
let toggledTrack = null;
|
2019-10-18 09:44:11 -07:00
|
|
|
const onChange = function (e) {
|
2019-08-16 14:01:14 -07:00
|
|
|
toggledTrack = null;
|
|
|
|
};
|
2019-10-18 09:44:11 -07:00
|
|
|
const bindChange = function (onOrOff) {
|
2019-08-16 14:01:14 -07:00
|
|
|
player.textTracks()[onOrOff]('change', onChange);
|
|
|
|
};
|
|
|
|
// Wrapper function to ignore our own emitted events and only listen
|
|
|
|
// to events emitted by Video.js on click on the captions menu items.
|
2019-10-18 09:44:11 -07:00
|
|
|
const setMode = function (track, mode) {
|
2019-08-16 14:01:14 -07:00
|
|
|
bindChange('off');
|
|
|
|
track.mode = mode;
|
2019-10-18 09:44:11 -07:00
|
|
|
window.setTimeout(function () {
|
2019-08-16 14:01:14 -07:00
|
|
|
bindChange('on');
|
|
|
|
}, 0);
|
|
|
|
};
|
|
|
|
bindChange('on');
|
2019-10-18 09:44:11 -07:00
|
|
|
return function () {
|
2019-08-16 14:01:14 -07:00
|
|
|
if (toggledTrack !== null) {
|
|
|
|
if (toggledTrack.mode !== 'showing') {
|
|
|
|
setMode(toggledTrack, 'showing');
|
|
|
|
} else {
|
|
|
|
setMode(toggledTrack, 'disabled');
|
|
|
|
}
|
|
|
|
toggledTrack = null;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Used as a fallback if no captions are currently active.
|
|
|
|
// TODO: Make this more intelligent by e.g. relying on browser language.
|
|
|
|
let fallbackCaptionsTrack = null;
|
|
|
|
|
|
|
|
const tracks = player.textTracks();
|
|
|
|
for (let i = 0; i < tracks.length; i++) {
|
|
|
|
const track = tracks[i];
|
2022-02-07 15:08:52 -08:00
|
|
|
if (track.kind !== 'captions') continue;
|
2019-08-16 14:01:14 -07:00
|
|
|
|
|
|
|
if (fallbackCaptionsTrack === null) {
|
|
|
|
fallbackCaptionsTrack = track;
|
|
|
|
}
|
|
|
|
if (track.mode === 'showing') {
|
|
|
|
setMode(track, 'disabled');
|
|
|
|
toggledTrack = track;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fallback if no captions are currently active.
|
|
|
|
if (fallbackCaptionsTrack !== null) {
|
|
|
|
setMode(fallbackCaptionsTrack, 'showing');
|
|
|
|
toggledTrack = fallbackCaptionsTrack;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
})();
|
|
|
|
|
|
|
|
function toggle_fullscreen() {
|
2022-02-07 15:08:52 -08:00
|
|
|
player.isFullscreen() ? player.exitFullscreen() : player.requestFullscreen();
|
2019-08-16 14:01:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function increase_playback_rate(steps) {
|
|
|
|
const maxIndex = options.playbackRates.length - 1;
|
|
|
|
const curIndex = options.playbackRates.indexOf(player.playbackRate());
|
|
|
|
let newIndex = curIndex + steps;
|
|
|
|
if (newIndex > maxIndex) {
|
|
|
|
newIndex = maxIndex;
|
|
|
|
} else if (newIndex < 0) {
|
|
|
|
newIndex = 0;
|
|
|
|
}
|
|
|
|
player.playbackRate(options.playbackRates[newIndex]);
|
|
|
|
}
|
|
|
|
|
|
|
|
window.addEventListener('keydown', e => {
|
|
|
|
if (e.target.tagName.toLowerCase() === 'input') {
|
|
|
|
// Ignore input when focus is on certain elements, e.g. form fields.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// See https://github.com/ctd1500/videojs-hotkeys/blob/bb4a158b2e214ccab87c2e7b95f42bc45c6bfd87/videojs.hotkeys.js#L310-L313
|
|
|
|
const isPlayerFocused = false
|
|
|
|
|| e.target === document.querySelector('.video-js')
|
|
|
|
|| e.target === document.querySelector('.vjs-tech')
|
|
|
|
|| e.target === document.querySelector('.iframeblocker')
|
|
|
|
|| e.target === document.querySelector('.vjs-control-bar')
|
2019-10-18 09:44:11 -07:00
|
|
|
;
|
2019-08-16 14:01:14 -07:00
|
|
|
let action = null;
|
|
|
|
|
|
|
|
const code = e.keyCode;
|
2019-08-18 21:22:39 -07:00
|
|
|
const decoratedKey =
|
|
|
|
e.key
|
2019-10-18 09:44:11 -07:00
|
|
|
+ (e.altKey ? '+alt' : '')
|
2019-08-18 21:22:39 -07:00
|
|
|
+ (e.ctrlKey ? '+ctrl' : '')
|
|
|
|
+ (e.metaKey ? '+meta' : '')
|
2019-10-18 09:44:11 -07:00
|
|
|
;
|
2019-08-18 21:22:39 -07:00
|
|
|
switch (decoratedKey) {
|
2019-10-18 09:44:11 -07:00
|
|
|
case ' ':
|
|
|
|
case 'k':
|
2019-08-19 03:10:25 -07:00
|
|
|
case 'MediaPlayPause':
|
2019-10-18 09:44:11 -07:00
|
|
|
action = toggle_play;
|
|
|
|
break;
|
|
|
|
|
2022-02-07 15:08:52 -08:00
|
|
|
case 'MediaPlay': action = play; break;
|
|
|
|
case 'MediaPause': action = pause; break;
|
|
|
|
case 'MediaStop': action = stop; break;
|
2019-08-19 03:10:25 -07:00
|
|
|
|
2019-10-18 09:44:11 -07:00
|
|
|
case 'ArrowUp':
|
2022-02-07 15:08:52 -08:00
|
|
|
if (isPlayerFocused) action = increase_volume.bind(this, 0.1);
|
2019-10-18 09:44:11 -07:00
|
|
|
break;
|
|
|
|
case 'ArrowDown':
|
2022-02-07 15:08:52 -08:00
|
|
|
if (isPlayerFocused) action = increase_volume.bind(this, -0.1);
|
2019-10-18 09:44:11 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'm':
|
|
|
|
action = toggle_muted;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'ArrowRight':
|
2019-08-19 03:10:25 -07:00
|
|
|
case 'MediaFastForward':
|
2021-02-17 05:33:26 -08:00
|
|
|
action = skip_seconds.bind(this, 5 * player.playbackRate());
|
2019-10-18 09:44:11 -07:00
|
|
|
break;
|
|
|
|
case 'ArrowLeft':
|
2019-08-19 03:10:25 -07:00
|
|
|
case 'MediaTrackPrevious':
|
2021-02-17 05:33:26 -08:00
|
|
|
action = skip_seconds.bind(this, -5 * player.playbackRate());
|
2019-10-18 09:44:11 -07:00
|
|
|
break;
|
|
|
|
case 'l':
|
2021-02-17 05:33:26 -08:00
|
|
|
action = skip_seconds.bind(this, 10 * player.playbackRate());
|
2019-10-18 09:44:11 -07:00
|
|
|
break;
|
|
|
|
case 'j':
|
2021-02-17 05:33:26 -08:00
|
|
|
action = skip_seconds.bind(this, -10 * player.playbackRate());
|
2019-10-18 09:44:11 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case '0':
|
|
|
|
case '1':
|
|
|
|
case '2':
|
|
|
|
case '3':
|
|
|
|
case '4':
|
|
|
|
case '5':
|
|
|
|
case '6':
|
|
|
|
case '7':
|
|
|
|
case '8':
|
|
|
|
case '9':
|
2022-02-07 15:01:53 -08:00
|
|
|
// Ignore numpad numbers
|
|
|
|
if (code > 57) break;
|
|
|
|
|
2019-10-18 09:44:11 -07:00
|
|
|
const percent = (code - 48) * 10;
|
|
|
|
action = set_time_percent.bind(this, percent);
|
|
|
|
break;
|
|
|
|
|
2022-02-07 15:08:52 -08:00
|
|
|
case 'c': action = toggle_captions; break;
|
|
|
|
case 'f': action = toggle_fullscreen; break;
|
2019-10-18 09:44:11 -07:00
|
|
|
|
|
|
|
case 'N':
|
2019-08-19 03:10:25 -07:00
|
|
|
case 'MediaTrackNext':
|
2019-10-18 09:44:11 -07:00
|
|
|
action = next_video;
|
|
|
|
break;
|
|
|
|
case 'P':
|
2019-08-19 03:10:25 -07:00
|
|
|
case 'MediaTrackPrevious':
|
2019-10-18 09:44:11 -07:00
|
|
|
// TODO: Add support to play back previous video.
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '.':
|
|
|
|
// TODO: Add support for next-frame-stepping.
|
|
|
|
break;
|
|
|
|
case ',':
|
|
|
|
// TODO: Add support for previous-frame-stepping.
|
|
|
|
break;
|
|
|
|
|
2022-02-07 15:08:52 -08:00
|
|
|
case '>': action = increase_playback_rate.bind(this, 1); break;
|
|
|
|
case '<': action = increase_playback_rate.bind(this, -1); break;
|
2019-10-18 09:44:11 -07:00
|
|
|
|
|
|
|
default:
|
|
|
|
console.info('Unhandled key down event: %s:', decoratedKey, e);
|
|
|
|
break;
|
2019-08-16 14:01:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (action) {
|
|
|
|
e.preventDefault();
|
|
|
|
action();
|
|
|
|
}
|
|
|
|
}, false);
|
|
|
|
|
|
|
|
// Add support for controlling the player volume by scrolling over it. Adapted from
|
|
|
|
// https://github.com/ctd1500/videojs-hotkeys/blob/bb4a158b2e214ccab87c2e7b95f42bc45c6bfd87/videojs.hotkeys.js#L292-L328
|
2019-10-18 09:44:11 -07:00
|
|
|
(function () {
|
2019-08-16 14:01:14 -07:00
|
|
|
const volumeStep = 0.05;
|
|
|
|
const enableVolumeScroll = true;
|
|
|
|
const enableHoverScroll = true;
|
|
|
|
const doc = document;
|
|
|
|
const pEl = document.getElementById('player');
|
|
|
|
|
|
|
|
var volumeHover = false;
|
|
|
|
var volumeSelector = pEl.querySelector('.vjs-volume-menu-button') || pEl.querySelector('.vjs-volume-panel');
|
|
|
|
if (volumeSelector != null) {
|
2019-10-18 09:44:11 -07:00
|
|
|
volumeSelector.onmouseover = function () { volumeHover = true; };
|
|
|
|
volumeSelector.onmouseout = function () { volumeHover = false; };
|
2019-08-16 14:01:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
var mouseScroll = function mouseScroll(event) {
|
2019-10-18 09:44:11 -07:00
|
|
|
var activeEl = doc.activeElement;
|
|
|
|
if (enableHoverScroll) {
|
|
|
|
// If we leave this undefined then it can match non-existent elements below
|
|
|
|
activeEl = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// When controls are disabled, hotkeys will be disabled as well
|
|
|
|
if (player.controls()) {
|
|
|
|
if (volumeHover) {
|
|
|
|
if (enableVolumeScroll) {
|
|
|
|
event = window.event || event;
|
|
|
|
var delta = Math.max(-1, Math.min(1, (event.wheelDelta || -event.detail)));
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
if (delta == 1) {
|
|
|
|
increase_volume(volumeStep);
|
|
|
|
} else if (delta == -1) {
|
|
|
|
increase_volume(-volumeStep);
|
|
|
|
}
|
|
|
|
}
|
2019-08-16 14:01:14 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
player.on('mousewheel', mouseScroll);
|
|
|
|
player.on("DOMMouseScroll", mouseScroll);
|
|
|
|
}());
|
|
|
|
|
2019-05-06 09:23:14 -07:00
|
|
|
// Since videojs-share can sometimes be blocked, we defer it until last
|
2020-12-13 01:16:26 -08:00
|
|
|
if (player.share) {
|
|
|
|
player.share(shareOptions);
|
|
|
|
}
|
2021-01-31 23:59:27 -08:00
|
|
|
|
2021-02-23 22:02:55 -08:00
|
|
|
// show the preferred caption by default
|
|
|
|
if (player_data.preferred_caption_found) {
|
|
|
|
player.ready(() => {
|
|
|
|
player.textTracks()[1].mode = 'showing';
|
|
|
|
});
|
2021-02-26 10:59:16 -08:00
|
|
|
}
|
|
|
|
|
2021-02-02 05:25:13 -08:00
|
|
|
// Safari audio double duration fix
|
2021-02-02 19:50:14 -08:00
|
|
|
if (navigator.vendor == "Apple Computer, Inc." && video_data.params.listen) {
|
|
|
|
player.on('loadedmetadata', function () {
|
2021-01-31 23:59:27 -08:00
|
|
|
player.on('timeupdate', function () {
|
|
|
|
if (player.remainingTime() < player.duration() / 2) {
|
|
|
|
player.currentTime(player.duration() + 1);
|
|
|
|
}
|
2021-02-09 09:05:29 -08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2021-04-08 04:15:15 -07:00
|
|
|
|
|
|
|
// Watch on Invidious link
|
|
|
|
if (window.location.pathname.startsWith("/embed/")) {
|
|
|
|
const Button = videojs.getComponent('Button');
|
|
|
|
let watch_on_invidious_button = new Button(player);
|
|
|
|
|
|
|
|
// Create hyperlink for current instance
|
|
|
|
redirect_element = document.createElement("a");
|
|
|
|
redirect_element.setAttribute("href", `http://${window.location.host}/watch?v=${window.location.pathname.replace("/embed/","")}`)
|
|
|
|
redirect_element.appendChild(document.createTextNode("Invidious"))
|
|
|
|
|
|
|
|
watch_on_invidious_button.el().appendChild(redirect_element)
|
|
|
|
watch_on_invidious_button.addClass("watch-on-invidious")
|
|
|
|
|
|
|
|
cb = player.getChild('ControlBar')
|
|
|
|
cb.addChild(watch_on_invidious_button)
|
|
|
|
};
|