-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathITK Scraper.js
More file actions
78 lines (63 loc) · 2.12 KB
/
ITK Scraper.js
File metadata and controls
78 lines (63 loc) · 2.12 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
function scrape(index) {
let accordion = document.getElementsByClassName("accordion")[0];
let afdelinger = accordion.getElementsByClassName("accordion__item");
let ansatte = [];
for (const afdeling of afdelinger) {
let afdelingstitel = afdeling.getElementsByClassName("accordion__title")[0].innerText;
let rækker = afdeling.getElementsByTagName("tr");
for (const række of rækker) {
let info = række.children[1].innerText;
info = info.split("\n");
info = info.filter(s => s); //Remove empty strings
if (info.length < 2) {
continue;
}
let navn = info[0];
let job = info[1];
//If it's an email
if (job.includes("@")) {
job = "Jedi";
}
//Billede
let fileName;
let billedeContainer = række.children[0];
let billeder = billedeContainer.getElementsByTagName("img");
for (const billed of billeder) {
if (billed.width > 0) {
fileName = billed.src.replace(/(^.*\/)|(\?.*)/g, '');
downloadImage(billed.src, fileName);
}
}
//Output
let person = {
"Navn": navn,
"Stilling": job,
"Afdeling": afdelingstitel,
"image": `images/${fileName}`
}
ansatte.push(person);
}
}
return ansatte;
}
async function downloadImage(imageSrc, name) {
const image = await fetch(imageSrc)
const imageBlog = await image.blob()
const imageURL = URL.createObjectURL(imageBlog)
const link = document.createElement('a')
link.href = imageURL
link.download = name
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
sleep(100);
}
function sleep(milliseconds) {
let timeStart = new Date().getTime();
while (true) {
let elapsedTime = new Date().getTime() - timeStart;
if (elapsedTime > milliseconds) {
break;
}
}
}