forked from pepelsbey/playground
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
71 lines (63 loc) · 1.94 KB
/
index.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
69
70
71
import * as http from 'http';
import * as fs from 'fs';
import { colors } from './colors.js';
import { random } from './random.js';
const server = http.createServer();
server.listen(3000, error => {
if (error) {
return console.error(error);
}
});
server.on('request', (request, response) => {
if (request.url === '/') {
response.setHeader('Content-Type', 'text/html');
response.statusCode = 200;
response.end(`<!DOCTYPE html>
<html lang="ru">
<head>
<title>Цветная тема вкладки</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="">
<style>
body {
background-color: ${ random(colors) };
font-family: sans-serif;
}
button {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
padding: 0.4em 0.8em;
border-radius: 2em;
border: none;
background-color: ${ random(colors) };
color: ${ random(colors) };
font-size: 80px;
font-family: inherit;
}
</style>
</head>
<body>
<button>Крась!</button>
<script type="module">
import { colors } from './colors.js';
import { random } from './random.js';
const button = document.querySelector('button');
const theme = document.querySelector('meta[name=theme-color]');
button.addEventListener('click', function() {
theme.content = random(colors);
});
</script>
</body>
</html>`);
}
if (request.url.endsWith('.js')) {
fs.access(request.url.slice(1), fs.constants.F_OK, () => {
response.setHeader('Content-Type', 'text/javascript');
response.statusCode = 200;
fs.createReadStream(request.url.slice(1)).pipe(response);
});
}
});