-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript_for_background.js
32 lines (28 loc) · 1.09 KB
/
script_for_background.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
const owner = 'TeenAgeTechBD';
const repo = 'wallpapers';
const apiURL = `https://api.github.com/repos/${owner}/${repo}/contents/`;
const allowedExtensions = ['jpg', 'jpeg', 'png', 'webp'];
fetch(apiURL)
.then(response => {
if (!response.ok) throw new Error(`Error fetching repo: ${response.statusText}`);
return response.json();
})
.then(files => {
const imageFiles = files.filter(file => {
const fileExtension = file.name.split('.').pop().toLowerCase();
return allowedExtensions.includes(fileExtension);
});
if (imageFiles.length > 0) {
const randomImage = imageFiles[Math.floor(Math.random() * imageFiles.length)];
const img = document.createElement('img');
img.src = randomImage.download_url;
img.alt = 'Loading...';
document.body.appendChild(img);
} else {
alert('No images found in the repository.');
}
})
.catch(error => {
console.error('Error:', error);
alert('An error occurred while fetching images.');
});