-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathYouTube.html
176 lines (156 loc) · 6.38 KB
/
YouTube.html
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Mini YouTube</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; }
input, button { margin: 10px; padding: 10px; }
.video-card { border: 1px solid #ccc; padding: 10px; margin-bottom: 10px; }
</style>
</head>
<body>
<h1>Mini YouTube</h1>
<div id="sign-up">
<h2>Sign Up</h2>
<input type="text" id="username" placeholder="Enter Username">
<button onclick="signUp()">Sign Up</button>
</div>
<div id="upload-section" style="display:none;">
<h2>Upload Video Metadata</h2>
<input type="text" id="video-title" placeholder="Video Title">
<textarea id="video-description" placeholder="Video Description"></textarea><br>
<button onclick="uploadVideo()">Upload Video</button>
</div>
<div id="search-section">
<h2>Search Videos</h2>
<input type="text" id="search-input" placeholder="Search by title" oninput="searchVideos()">
<div id="search-results"></div>
</div>
<div id="video-list">
<h2>Uploaded Videos</h2>
<div id="videos"></div>
</div>
<script>
let db;
let currentUser = null;
// Initialize IndexedDB
function initDB() {
const request = indexedDB.open("miniYouTubeDB", 1);
request.onupgradeneeded = function(event) {
db = event.target.result;
const videoStore = db.createObjectStore("videos", { keyPath: "id", autoIncrement: true });
videoStore.createIndex("title", "title", { unique: false });
};
request.onsuccess = function(event) {
db = event.target.result;
loadVideos();
};
request.onerror = function(event) {
alert("Error opening database.");
};
}
// Sign up function
function signUp() {
const username = document.getElementById("username").value;
if (username) {
currentUser = username;
document.getElementById("sign-up").style.display = "none";
document.getElementById("upload-section").style.display = "block";
} else {
alert("Please enter a username.");
}
}
// Upload video metadata
function uploadVideo() {
const title = document.getElementById("video-title").value;
const description = document.getElementById("video-description").value;
if (title && description) {
const transaction = db.transaction(["videos"], "readwrite");
const videoStore = transaction.objectStore("videos");
videoStore.add({ title, description, uploader: currentUser });
transaction.oncomplete = function() {
alert("Video uploaded!");
loadVideos();
};
transaction.onerror = function() {
alert("Error uploading video.");
};
} else {
alert("Please fill in both title and description.");
}
}
// Load all videos
function loadVideos() {
const transaction = db.transaction(["videos"], "readonly");
const videoStore = transaction.objectStore("videos");
const request = videoStore.getAll();
request.onsuccess = function() {
const videos = request.result;
const videoList = document.getElementById("videos");
videoList.innerHTML = "";
videos.forEach(video => {
const videoCard = document.createElement("div");
videoCard.classList.add("video-card");
videoCard.innerHTML = `
<h3>${video.title}</h3>
<p>${video.description}</p>
<p><strong>Uploaded by:</strong> ${video.uploader}</p>
<button onclick="deleteVideo(${video.id})">Delete</button>
`;
videoList.appendChild(videoCard);
});
};
request.onerror = function() {
alert("Error loading videos.");
};
}
// Delete video
function deleteVideo(id) {
const transaction = db.transaction(["videos"], "readwrite");
const videoStore = transaction.objectStore("videos");
videoStore.delete(id);
transaction.oncomplete = function() {
alert("Video deleted!");
loadVideos();
};
transaction.onerror = function() {
alert("Error deleting video.");
};
}
// Search for videos by title
function searchVideos() {
const searchQuery = document.getElementById("search-input").value.toLowerCase();
const transaction = db.transaction(["videos"], "readonly");
const videoStore = transaction.objectStore("videos");
const index = videoStore.index("title");
const request = index.openCursor(IDBKeyRange.lowerBound(searchQuery));
request.onsuccess = function(event) {
const cursor = event.target.result;
const searchResults = document.getElementById("search-results");
searchResults.innerHTML = "";
if (cursor) {
const video = cursor.value;
const videoCard = document.createElement("div");
videoCard.classList.add("video-card");
videoCard.innerHTML = `
<h3>${video.title}</h3>
<p>${video.description}</p>
<p><strong>Uploaded by:</strong> ${video.uploader}</p>
`;
searchResults.appendChild(videoCard);
cursor.continue();
}
};
request.onerror = function() {
alert("Error searching videos.");
};
}
// Initialize the database when the page loads
window.onload = function() {
initDB();
};
</script>
</body>
</html>