-
Notifications
You must be signed in to change notification settings - Fork 1
/
search.js
75 lines (65 loc) · 2.8 KB
/
search.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
const searchBtn = document.getElementById('search-btn');
const searchInput = document.getElementById('search');
// Replace 'YOUR_API_KEY' with your actual API key.
const API_KEY = 'AIzaSyBtRXOjmAeybXF6jImnkQ-R3NSFIosRGss';
const API_URL = 'https://www.googleapis.com/youtube/v3/search';
// Function to perform the YouTube API request and display results.
function searchYouTubeVideos(query) {
$.get(API_URL, {
part: 'snippet',
//part:'contentDetails',
q: query,
key: API_KEY
}, function (data) {
// Handle the API response (data.items contains the video results).
const videosContainer = $('#videos');
videosContainer.empty(); // Clear previous results
// Iterate through the video items and display them.
data.items.forEach(function (item, index) {
console.log(item);
const videoId = item.id.videoId;
const videoTitle = item.snippet.title;
const videoThumbnail = item.snippet.thumbnails.medium.url;
// Create a new video element and append it to the videos container.
const videoElement = `<div class="video-box">
<img src="${videoThumbnail}" alt="${videoTitle}" class="imgg">
<h3 style="
font-size: 12px;height: 4vh;
">${videoTitle}</h3>
<button class="play" id="add-playlist-${index}">Add to PlayList</button>
</div>`;
videosContainer.append(videoElement);
const addVideo = document.getElementById(`add-playlist-${index}`);
var vidIndex = 0;
addVideo.addEventListener('click', () => {
var video = {
vidIndex,
videoId,
videoThumbnail,
videoTitle
}
chrome.storage.local.get("playlist", (data) => {
if (typeof data.playlist === 'undefined') {
data.playlist = [video];
} else {
video.vidIndex = data.playlist.length;
data.playlist.push(video);
}
chrome.storage.local.set({ "playlist": data.playlist });
})
})
});
});
}
searchBtn.addEventListener('click', () => {
var searchTxt = searchInput.value;
console.log(searchTxt);
const alternate=document.getElementById('alternate');
alternate.classList.add('clear');
searchYouTubeVideos(`${searchTxt} videos`);
})
//back
const backBtn = document.getElementById('back');
backBtn.addEventListener('click', () => {
window.location.href = "popup.html";
})