-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathytsearch.js
164 lines (133 loc) · 6.29 KB
/
ytsearch.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
$(document).ready(function(){
var API_KEY = "AIzaSyAYUJirQE2io-hjqAce9IJCQw9D-F6aFNk";
// var API_KEY = "AIzaSyASw0iFX4bRcwz10Lmm12CdTgxF-NLzve4";
var selectedCategories = new Set();
// Determine whether to use the stored query or default query
var defaultQuery = "Software Quality Assurance";
// Fetch and display search results with the determined query
videoSearch(API_KEY, defaultQuery, 12, "short");
// Function to handle category button click
function handleCategoryClick() {
var categoryKeyword = $(this).data("category");
if (selectedCategories.has(categoryKeyword)) {
selectedCategories.delete(categoryKeyword);
} else {
selectedCategories.add(categoryKeyword);
}
$(this).toggleClass("selected");
updateSearchInput();
}
// Load predefined categories
$(".category-button").on("click", handleCategoryClick);
// Load custom keywords from local storage
var customKeywords = localStorage.getItem("customKeywords");
if (customKeywords) {
customKeywords = JSON.parse(customKeywords);
customKeywords.forEach(function(keyword) {
addCustomCategoryButton(keyword);
if (selectedCategories.has(keyword)) {
$(".category-button[data-category='" + keyword + "']").addClass("selected");
}
});
}
// Function to add custom category button
function addCustomCategoryButton(keyword) {
var newCategoryButton = $("<button class='category-button' data-category='" + keyword + "'>" + keyword + "</button>");
newCategoryButton.on("click", handleCategoryClick);
$(".category-buttons").append(newCategoryButton);
}
// Handle search button
$("#search-button").click(function () {
sessionStorage.setItem("search", $("#search").val());
sessionStorage.setItem("videoDuration" , "short");
sessionStorage.setItem("searchExecuted", "true");
window.location.href = 'index.html';
});
// + button function to add keyword
$("#addKeywordButton").click(function (){
console.log("Add Custom Keyword clicked");
$("#keywordPopup").css("display", "block");
});
// cancel button to close add keyword textbox
$("#cancelKeyword").click(function (){
$("#keywordPopup").css("display", "none");
});
// confirm button to add a keyword button
$("#submitKeyword").click(function () {
var customKeyword = $("#customKeyword").val();
if (customKeyword.trim() !== "") {
var customKeywords = localStorage.getItem("customKeywords");
if (customKeywords) {
customKeywords = JSON.parse(customKeywords);
customKeywords.push(customKeyword);
} else {
customKeywords = [customKeyword];
}
localStorage.setItem("customKeywords", JSON.stringify(customKeywords));
addCustomCategoryButton(customKeyword);
updateSearchInput();
// Clear the input and close the popup
$("#customKeyword").val("");
$("#keywordPopup").css("display", "none");
}
});
// Function to update the search input
function updateSearchInput() {
var combinedCategories = Array.from(selectedCategories).join(" ");
var $searchInput = $("#search");
$searchInput.val(combinedCategories);
}
// Function to clear all self-added keywords
$("#clearAllKeywords").click(function () {
localStorage.removeItem("customKeywords");
$(".custom-category-button").remove(); // Remove custom buttons from the DOM
selectedCategories.clear(); // Clear the selectedCategories set
updateSearchInput();
location.reload();
});
// search video according to input
function videoSearch(key, search, maxResults, videoDuration) {
console.log("1");
$("#videos").empty();
$.get("https://www.googleapis.com/youtube/v3/search?key=" + key
+ "&type=video&part=snippet&maxResults=" + maxResults + "&q=" + search + "&videoDuration=" + videoDuration,
function(data){
var videosContainer = $("<div class='row'></div>");
data.items.forEach(function (item, index) {
var videoColumn = $("<div class='col-md-4'></div>");
var videoContainer = $("<div class='video-container'></div>");
var thumbnail = $("<img src='" + item.snippet.thumbnails.medium.url + "' alt='Video Thumbnail'>");
var videoTitle = $("<p class='video-title'></p>");
videoTitle.text(item.snippet.title);
thumbnail.on("click", function () {
var videoId = item.id.videoId
localStorage.setItem('selectedVideoId', videoId);
sessionStorage.setItem("selectedVideoTitle", item.snippet.title);
window.location.href = 'videoPlayer.html';
$("#search").val("");
});
console.log("2");
videoContainer.append(thumbnail);
videoContainer.append(videoTitle);
videoColumn.append(videoContainer);
videosContainer.append(videoColumn);
if ((index + 1) % 3 === 0) {
$("#videos").append(videosContainer);
videosContainer = $("<div class='row'></div>");
}
});
console.log("3");
if (videosContainer.children().length > 0){
$("#videos").append(videosContainer);
}
});
console.log("4");
}
if(sessionStorage.getItem("searchExecuted")){
console.log(sessionStorage.getItem("searchExecuted"));
videoSearch(API_KEY, sessionStorage.getItem("search"), 12, sessionStorage.getItem("videoDuration"));
document.getElementById("search").value = sessionStorage.getItem("search");
sessionStorage.clear();
console.log(sessionStorage.getItem("searchExecuted"));
}
});