-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNASA-APOD-background-image.html
86 lines (75 loc) · 2 KB
/
NASA-APOD-background-image.html
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
<!DOCTYPE html>
<html>
<head>
<title>NASA APOD Background Scraper</title>
<style type="text/css">
body {
background-color: black;
width: 100vw;
height: 100vh;
margin: 0 auto;
}
main {
height: 100%;
width: 100%;
background-size: cover;
background-repeat: no-repeat;
background-position: center;
}
.credits h1 {
margin: 5px 0px 15px;
font-size: 200%;
letter-spacing: 0px;
}
.credits {
background:rgba(0, 0, 0, 0.5);
color: white;
position: absolute;
bottom: 10px;
right: 15px;
padding: 15px;
font-size: small;
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
letter-spacing: 0.05em;
}
.credits a {
color: white;
text-decoration: none;
}
</style>
</head>
<body>
<main id='main'></main>
<section class='credits'>
</section>
</body>
<script>
var baseUrl = 'http://apod.nasa.gov/apod/';
var proxy = 'http://cors.io/?u=';
function loadData() {
var xhttp;
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
parseLink (xhttp.responseText)
}
}
xhttp.open("GET", proxy + "http://apod.nasa.gov/apod/astropix.html", true);
xhttp.send();
}
function parseLink (data) {
var urlRe = /<br>\s*\n*<a\shref="(image.*)">/g;
var creditsRe = /<\/center>\n\n<center>\n((.*\n*)*?)<\/center>/g;
var url = urlRe.exec(data)[1];
var credits = creditsRe.exec(data)[1].split('<br>');
title = credits[0];
credits = credits[1].replace(':', ':<br>');
renderBackground (baseUrl + url, title, credits);
}
function renderBackground (image, title, credits) {
document.getElementsByClassName('credits')[0].innerHTML = '<h1>' + title + '</h1>' + credits;
document.getElementById('main').style.backgroundImage = 'url(' + image + ')';
}
loadData();
</script>
</html>