-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
135 lines (129 loc) · 4.85 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Created by @oivas000
console.log("Created by @oivas000");
console.log("WARNING: Do not use it in a production deployment!");
console.log("WARNING: May cause heavy data usage.");
function listenSearch() {
const searchInput = document.querySelector('.search-input');
const searchButton = document.querySelector('.search-button');
searchButton.addEventListener('click', performSearch);
searchInput.addEventListener('keyup', event => {
if (event.key === 'Enter') {
performSearch();
}
});
}
function loadVideo() {
document.addEventListener('DOMContentLoaded', () => {
fetch('videos.list').then(response => response.text()).then(data => {
const container = document.getElementById('videoContainer');
const videoFiles = data.split('\n').filter(line => !line.startsWith('#') && !line.startsWith('*') && line.trim() !== '');
const limitedVideoFiles = videoFiles.slice(0, 10);
const randomLimitedVideoFiles = shuffleArray(limitedVideoFiles);
randomLimitedVideoFiles.forEach(file => {
const videoItem = document.createElement('div');
videoItem.classList.add('video-item');
const videoElement = document.createElement('video');
const sourceElement = document.createElement('source');
const downloadLink = document.createElement('a');
const videoTitle = document.createElement('div');
videoElement.controls = true;
videoElement.crossOrigin = 'anonymous';
videoElement.playsInline = true;
const xhr = new XMLHttpRequest();
xhr.open('GET', file, true);
xhr.responseType = 'blob';
xhr.onload = function () {
if (xhr.status === 200) {
var videoBlob = xhr.response;
var videoURL = window.URL.createObjectURL(videoBlob);
videoElement.srcObject = null;
videoElement.src = videoURL;
}
};
xhr.onerror = function () {
console.error('Failed to load the video file.');
};
xhr.send();
sourceElement.type = `video/${file.split('.').pop()}`;
downloadLink.href = file;
downloadLink.download = file;
downloadLink.textContent = 'Download';
const fileName = file.split('/').pop().split('.').shift();
videoTitle.classList.add('video-info');
videoTitle.textContent = fileName;
videoElement.appendChild(sourceElement);
videoElement.appendChild(downloadLink);
videoItem.appendChild(videoElement);
videoItem.appendChild(videoTitle);
container.appendChild(videoItem);
const player = new Plyr(videoElement, {
invertControls: true,
classNames: {
light: 'plyr--dark'
},
controls: ['play-large', 'play', 'progress', 'current-time', 'settings', 'fullscreen'],
settings: ['captions', 'quality', 'speed'],
volume: 1,
seekTime: 10
});
player.on('play', event => {
document.title = `Playing: ${fileName}`;
});
player.on('pause', () => {
document.title = 'LocalTube';
});
});
})
.catch(error => {
console.error('Failed to fetch video files:', error);
});
});
}
function performSearch() {
showLoader();
const searchInput = document.querySelector('.search-input');
const searchQuery = searchInput.value.toLowerCase();
const container = document.getElementById('videoContainer');
const videoItems = container.querySelectorAll('.video-item');
videoItems.forEach(videoItem => {
const video = videoItem.querySelector('video');
const fileName = video.querySelector('source').src.toLowerCase();
const videoTitle = videoItem.querySelector('.video-info').textContent.toLowerCase();
if (fileName.includes(searchQuery) || videoTitle.includes(searchQuery)) {
hideLoader();
videoItem.style.display = 'block';
} else {
setTimeout(() => {
hideLoader();
}, 1000);
videoItem.style.display = 'none';
const notFoundMessage = document.createElement('div');
notFoundMessage.textContent = 'Video not found';
notFoundMessage.classList.add('notFoundMessage');
container.appendChild(notFoundMessage);
}
});
}
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array;
}
function showLoader() {
const loader = document.querySelector('.loader');
const container = document.querySelector('.container');
loader.style.display = 'flex';
container.style.display = 'none';
}
function hideLoader() {
const loader = document.querySelector('.loader');
const container = document.querySelector('.container');
loader.style.display = 'none';
container.style.display = 'block';
}
showLoader();
listenSearch();
loadVideo();
hideLoader();