-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
68 lines (55 loc) · 1.96 KB
/
script.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
// Access key from Unsplash
const accesskey = "Here Enter Your Access Key";
// Variables
const searchbar = document.getElementById("search_bar");
const searchtext = document.getElementById("search_text");
const imageresult = document.getElementById("image_result");
const loadmore = document.getElementById("load_more");
// Using API
let keyword = "";
let page = 1;
async function searchImages(){
// Fetch images from Unsplash API based on user input
keyword = searchtext.value;
const url = `https:api.unsplash.com/search/photos?page=${page}&query=${keyword}&client_id=${accesskey}&per_page=15`;
const response = await fetch(url);
const data = await response.json();
if(page === 1){
imageresult.innerHTML = "";
}
const results = data.results;
results.map((result) => {
const image = document.createElement("img");
image.src = result.urls.small;
image.style.cursor = "pointer";
image.addEventListener("click", async () => {
try {
const response_img = await fetch(result.urls.full);
const file = await response_img.blob();
const link = document.createElement("a");
link.href = URL.createObjectURL(file);
link.download = 'Cosas_Learning_' + new Date().getTime();
link.click();
} catch {
alert("Failed to download the file!");
}
});
imageresult.appendChild(image);
});
}
loadmore.addEventListener("click", ()=> {
// Load more images when the "Load More" button is clicked
page++;
searchImages();
})
searchbar.addEventListener("submit", (e) => {
// Handle form submission when the user searches for images
e.preventDefault();
page = 1;
searchImages();
})
window.addEventListener("load", () => {
// Load random images when the page loads
searchtext.value = "Random Images";
searchImages();
});