-
Notifications
You must be signed in to change notification settings - Fork 3
/
goonscreen.js
141 lines (131 loc) · 4.45 KB
/
goonscreen.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
136
137
138
139
140
141
var config = {
content: [
{
type: "column",
isClosable: false,
content: [
{
type: "row",
isClosable: false,
content: [],
},
],
},
],
};
var myLayout = new window.GoldenLayout(config, $("#layoutContainer"));
myLayout.registerComponent("example", function (container, state) {
if (state.text.includes(".webm")) {
var videoContainer = $('<div class="video-container"></div>');
var videoElement = $(
'<video controls loop><source src="' +
state.text +
'" type="video/webm"></video>'
)[0];
videoContainer.append(videoElement);
container.getElement().append(videoContainer);
// Attempt to play with sound
var playPromise = videoElement.play();
if (playPromise !== undefined) {
playPromise
.then((_) => {
// Autoplay started
})
.catch((error) => {
// Autoplay was prevented, mute and try again
videoElement.muted = true;
videoElement.play();
});
}
} else {
container.getElement().html("<h2>" + state.text + "</h2>");
}
});
myLayout.init();
const MAX_VIDEOS_PER_ROW = 4;
const TOTAL_VIDEOS = 2320;
// Function to handle playing the video
function playVideo(videoNumber) {
console.log("Playing video number:", videoNumber); // Debugging log
// Ensure videoNumber is a string
videoNumber = videoNumber.toString().replace(/^0+/, '') || '0';
var videoPath = "videos/" + videoNumber + ".webm";
fetch(videoPath, { method: "HEAD" })
.then(function (response) {
if (response.ok) {
var column = myLayout.root.contentItems[0];
var currentRow = column.contentItems[column.contentItems.length - 1];
if (currentRow.contentItems.length < MAX_VIDEOS_PER_ROW) {
// Add to the current row if it's not full
var newItemConfig = {
title: "GOONSCREEN " + videoNumber,
type: "component",
componentName: "example",
componentState: { text: videoPath },
};
currentRow.addChild(newItemConfig);
} else {
// Create a new row if the current one is full
var newRowConfig = {
type: "row",
isClosable: false,
content: [
{
title: "GOONSCREEN " + videoNumber,
type: "component",
componentName: "example",
componentState: { text: videoPath },
},
],
};
column.addChild(newRowConfig);
}
} else {
alert("Video not found");
}
})
.catch(function () {
alert("Error checking video file");
});
}
// Event listener for the holy goon button :D
document.getElementById("confirmButton").addEventListener("click", function () {
var videoNumber = document.getElementById("videoInput").value;
playVideo(videoNumber);
document.getElementById("videoInput").value = ""; // Clear the search bar
});
// Event listener for pressing enter in search field
document.getElementById("videoInput").addEventListener("keypress", function (event) {
if (event.key === "Enter") {
var videoNumber = document.getElementById("videoInput").value;
playVideo(videoNumber);
document.getElementById("videoInput").value = ""; // Clear the search bar
}
});
// another event listener for the random search button
document.getElementById("randomButton").addEventListener("click", function () {
var randomVideoNumber = Math.floor(Math.random() * TOTAL_VIDEOS) + 1;
console.log("Random video number:", randomVideoNumber); // Debugging log
playVideo(randomVideoNumber.toString());
});
// Function to remove the most recently played video
function removeLastVideo() {
var column = myLayout.root.contentItems[0];
var currentRow = column.contentItems[column.contentItems.length - 1];
if (currentRow.contentItems.length > 0) {
currentRow.removeChild(currentRow.contentItems[currentRow.contentItems.length - 1]);
} else if (column.contentItems.length > 1) {
column.removeChild(currentRow);
}
}
// Event listener for hotkeys
document.addEventListener("keydown", function (event) {
if (event.key === "r") {
// Play a random video when 'r' is pressed
var randomVideoNumber = Math.floor(Math.random() * TOTAL_VIDEOS) + 1;
playVideo(randomVideoNumber.toString());
} else if (event.key === "d") {
// Remove the most recently played video when 'd' is pressed
removeLastVideo();
}
});