-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
157 lines (148 loc) · 6.14 KB
/
script.js
File metadata and controls
157 lines (148 loc) · 6.14 KB
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
const height = window.innerHeight;
const width = window.innerWidth;
var book_title = [], book_author = [], book_publisher = [], image_links = [], book_published_date = [], book_descript = [];
var url;
let search_button = document.getElementById("search-btn");
let reset_button = document.getElementById("reset-btn");
let user_input = document.getElementById("user_input");
let mark_btn=document.getElementById("xmark-btn");
user_input.addEventListener("keypress", function(event) {
if (event.key === 'Enter') {
performSearch();
}
});
search_button.addEventListener("click", function() {
performSearch();
});
function performSearch() {
let userInputValue = user_input.value.trim();
mark_btn.classList.add("fa-xmark");
if (!userInputValue) {
alert("Please enter a search term.");
return;
}
let safeInput = sanitizeInput(userInputValue);
url = "https://www.googleapis.com/books/v1/volumes?q=" + encodeURIComponent(safeInput);
fetch(url)
.then(response => response.json())
.then(data => {
for (let i = 0; i < 10; i++) {
if (!Wrong_terms(data.items[i].volumeInfo.title) && !Wrong_terms(data.items[i].volumeInfo.title)) {
let access_author_one = data.items[i].volumeInfo.authors[0];
book_author.push(escapeHtml(access_author_one));
book_title.push(escapeHtml(data.items[i].volumeInfo.title));
book_descript.push(escapeHtml(data.items[i].volumeInfo.description ? data.items[i].volumeInfo.description : 'No description available'));
image_links.push(data.items[i].volumeInfo.imageLinks ? data.items[i].volumeInfo.imageLinks.thumbnail : 'https://via.placeholder.com/150');
book_publisher.push(escapeHtml(data.items[i].volumeInfo.publisher ? data.items[i].volumeInfo.publisher : 'Unknown Publisher'));
book_published_date.push(escapeHtml(data.items[i].volumeInfo.publishedDate ? data.items[i].volumeInfo.publishedDate : 'Unknown'));
} else {
alert("Warning");
location.reload();
break;
}
}
document.getElementById("text").remove();
test_the_code(book_title, image_links, book_author, book_publisher, book_descript, book_published_date);
})
.catch(error => {
console.error('Error fetching data:', error);
alert('Error fetching data. Please try again later.');
location.reload();
});
}
//bt-->book title
//image-->book image
//ba-->book author
//bp-->book publisher
//bd-->book descript
//pd-->published date
let control_flow=0;
var card_div=document.createElement("div");
card_div.classList.add("card-div");
function test_the_code(bt,image,ba,bp,bd,pd){
let container=document.getElementById("container");
container.appendChild(card_div);
for(let i=0;i<bt.length;i++){
let div=document.createElement("div"); //reference--> i create a div tag
div.classList.add("card")
//reference--> i create a container as a section to append all of my cards
card_div.appendChild(div) //append the div to the container section
let img=document.createElement("img");
let bookTitle=document.createElement("span");//book title
bookTitle.classList.add("title");
let bookAuthor=document.createElement("span");//book author
bookAuthor.classList.add("author-name");
let BookPublisher=document.createElement("span");//book publisher
BookPublisher.classList.add("publisher");
let bookDate=document.createElement("span");
bookDate.classList.add("date")
let btn=document.createElement("button");
let btn_text=document.createTextNode("Description");
//console.log(anchor_tag);
img.src=image[i];
bookTitle.innerHTML=bt[i];//book title
div.appendChild(img);
div.appendChild(bookTitle);
btn.appendChild(btn_text);
div.appendChild(btn);
btn.addEventListener("click",()=>{
display_description(bt[i],image[i],ba[i],bp[i],pd[i],bd[i]);
})
}
}
reset_button.addEventListener("click",()=>{
location.reload();
})
function display_description(Title1,image1,author,publish,date,descript){
card_div.style.filter="blur(10px)";
let main_container=document.getElementById("container")
let display_div=document.createElement("div");
display_div.classList.add("popup-div");
let display_div1=document.createElement("div")
display_div1.classList.add("popup-container");
main_container.appendChild(display_div);
display_div.appendChild(display_div1)
// create a tag to display the content in div
let img=document.createElement("img");
img.classList.add("book-img");
let h1=document.createElement("h1");
h1.classList.add("author");
let pb=document.createElement("span");
pb.classList.add("publish")
let d=document.createElement("span");
let des=document.createElement("p");
let btn1=document.createElement("button");
btn1.classList.add("fa-solid");
btn1.classList.add("fa-xmark")
d.classList.add("date");
img.src=image1;
h1.innerHTML=`<span>Author:${author}</span>`;
pb.innerHTML=publish;
d.innerHTML=date;
des.innerHTML=descript;
display_div1.append(img, h1, pb, d, des, btn1);//append the element to popup div
btn1.addEventListener("click",()=>{
card_div.style.filter="blur(0px)";
display_div.remove();
})
}
//The below funnction is helps to protect the site
function Wrong_terms(text) {
const wrong_word = ['explicit', 'inappropriate', 'adult', 'violence', 'profanity','adult content'];
for (const term of wrong_word) {
if (text.toLowerCase().includes(term)) {
return true;
}
}
}
function sanitizeInput(input) {
return input.replace(/[^\w\s]/gi, '');
}
function escapeHtml(unsafe) {
return unsafe
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
}