-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
98 lines (87 loc) · 2.4 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
const fs = require('fs')
const path = require('path')
const express = require('express')
const PImage = require('pureimage')
const app = express()
const port = process.env.PORT || 9000
app.listen(port, () => {
console.log('Example app listening on port http://localhost:' + port)
})
app.get('/', (req, res) => {
res.redirect('/000000/1')
})
app.get('/:color/:width', (req, res) => {
const color = req.params.color
const width = parseInt(req.params.width)
const code = `
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Separator for browser bookmarks</title>
<link rel="icon" type="image/png" href='/favicon/${color}/${width}' />
<style>
html, body {
width: 100%;
height: 100%;
overflow: hidden;
}
body {
margin: 0;
display: flex;
text-align: center;
align-items: center;
font-family: Verdana;
justify-content: center;
}
a {
text-decoration: none;
}
#drag {
color: #fff;
width: 200px;
height: 50px;
font-size: 20px;
line-height: 50px;
text-align: center;
border-radius: 5px;
display: inline-block;
background-color: #aaaaaa;
}
#drag:before {
content: 'DRAG ME';
}
#me {
margin-top: 50px;
}
</style>
</head>
<body>
<div>
<a id="drag" href='/${color}/${width}' style='background-color: #${color};'></a>
<br />
<div id="me">Me: <a href="https://twitter.com/ozgrozer" target="_blank">twitter.com/ozgrozer</a></div>
</div>
</body>
</html>
`
res.send(code.replace(/ {2}|\r\n|\n|\r/gm, ''))
})
app.get('/favicon/:color/:width', (req, res) => {
const color = req.params.color
const width = parseInt(req.params.width)
const filename = path.join(__dirname, 'favicons', `${color}-${width}.png`)
if (fs.existsSync(filename)) {
res.sendFile(filename)
} else {
const img = PImage.make(16, 16)
const ctx = img.getContext('2d')
ctx.fillStyle = '#' + color
ctx.fillRect((16 - width) / 2, 0, width, 16)
if (!fs.existsSync('favicons')) fs.mkdirSync('favicons')
PImage.encodePNGToStream(img, fs.createWriteStream(filename))
.then(() => {
res.sendFile(filename)
})
}
})