-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
71 lines (60 loc) · 2.6 KB
/
main.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
// Get HTML elements
const textArea = document.getElementById('searchInput');
const searchButton = document.getElementById('searchButton');
const createPlaylistButton = document.getElementById('createPlaylistButton');
const resultsSection = document.getElementById('results');
// Add event listener to search button
searchButton.addEventListener('click', async () => {
// Get text from textarea and split into rows
const rows = textArea.value.trim().split('\n');
// Clear previous results
resultsSection.innerHTML = '';
// Perform a search for each row and store promises in an array
const searchPromises = rows.map(row => searchYouTube(row));
// Wait for all search promises to resolve
const allResults = await Promise.all(searchPromises);
// Display each result
allResults.forEach(results => {
results.forEach(result => {
const resultElement = document.createElement('div');
resultElement.classList.add('result-row'); // Add class
resultElement.innerHTML = `
<a href="https://www.youtube.com/watch?v=${result.videoId}">
<img src="${result.thumbnail}" alt="${result.title}">
</a>
<a href="https://www.youtube.com/watch?v=${result.videoId}">
<span>${result.title}</span>
</a>
`;
resultsSection.appendChild(resultElement);
});
});
});
// Add event listener to create playlist button
createPlaylistButton.addEventListener('click', () => {
// Get all result elements
const resultElements = resultsSection.querySelectorAll('div');
// Extract video IDs from result elements
const videoIds = Array.from(resultElements).map(resultElement => {
const videoUrl = resultElement.querySelector('a').href;
const videoId = videoUrl.split('=')[1];
return videoId;
});
// Create YouTube playlist link
const playlistLink = `https://www.youtube.com/watch_videos?video_ids=${videoIds.join(',')}`;
// Open playlist link in new tab
window.open(playlistLink, '_blank');
});
async function searchYouTube(query) {
try {
const response = await fetch(`https://www.googleapis.com/youtube/v3/search?part=snippet&maxResults=1&type=video&q=${encodeURIComponent(query)}&key=${YOUTUBE_API_KEY}`);
const data = await response.json();
return data.items.map(item => ({
title: item.snippet.title,
videoId: item.id.videoId,
thumbnail: item.snippet.thumbnails.default.url
}));
} catch (error) {
console.error(error);
}
}