-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
43 lines (34 loc) · 1.45 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
function generateString() {
// Generate String for random link
let strRandom = '';
let strChar = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
for (let i = 0; i < 5; i++) {
strRandom += strChar.charAt(Math.floor(Math.random() * strChar.length));
}
return strRandom;
}
function generateImages() {
// Add img in html and set link imgur
let boxImages = document.getElementById('box-images');
boxImages.innerText = '';
let amount = document.getElementById("number").value;
for (let i = 0; i < amount; i++) {
let gString = generateString();
let linkRandom = "https://i.imgur.com/" + gString + ".png";
let imgNew = document.createElement("img");
let pNew = document.createElement("p");
pNew.innerHTML = "https://i.imgur.com/" + gString + ".png"
imgNew.setAttribute('src', linkRandom);
// Case img == 161: generate new image, but 161px image is removed.png from imgur
imgNew.onload = function () {
if (this.width == 161) {
gString = generateString();
imgNew.setAttribute('src', "https://i.imgur.com/" + gString + ".png");
pNew.innerHTML = "https://i.imgur.com/" + gString + ".png";
} else {
boxImages.insertBefore(imgNew, boxImages.firstElementChild);
boxImages.insertBefore(pNew, boxImages.firstElementChild);
}
}
}
}