-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
123 lines (103 loc) · 2.91 KB
/
app.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
const express = require("express");
const puppeteer = require("puppeteer");
const multer = require("multer");
const app = express();
const upload = multer(); // for parsing multipart/form-data
const port = 3000;
//Middlewares
// for serving static files from the public directory
app.use(express.static("public"));
// for parsing request bodies
app.use(express.urlencoded({ extended: true }));
app.post("/screenshot", upload.none(), async (req, res) => {
const code = req.body.code.trim();
// escape HTML entities so that browser treats them as plain text
const escapedHTML = code.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
// get dynnamic height and width from the html code editor
const editorHeight = parseInt(req.body.editorHeight, 10);
const editorWidth = parseInt(req.body.editorWidth, 10);
// Launch a new browser instance
const browser = await puppeteer.launch();
const page = await browser.newPage();
const width = editorWidth ? editorWidth + 100 : 700;
const height = editorHeight ? editorHeight + 100 : 500;
// Set the viewport based on the textarea dimensions
await page.setViewport({ width, height });
// Load the code snippet template and set the code content
await page.setContent(`
<!DOCTYPE html>
<html>
<head>
<title>Code Screenshot</title>
<style>
body {
font-family: 'Menlo', monospace;
padding: 20px;
color: #fff;
}
pre {
overflow: auto;
}
.container {
border-radius: 10px;
overflow: auto;
background-color: #282c34;
padding: 30px 20px 20px;
position: relative;
}
code {
white-space: pre-wrap;
}
.window-lights {
position: absolute;
top: 10px;
left: 10px;
display: flex;
}
.window-light {
width: 12px;
height: 12px;
border-radius: 50%;
margin-right: 5px;
}
.w-red {
background-color: #ff5f56;
}
.w-yellow {
background-color: #ffbd2e;
}
.w-green {
background-color: #27c93f;
}
</style>
</head>
<body>
<div class="container">
<div class="window-lights">
<div class="window-light w-red"></div>
<div class="window-light w-yellow"></div>
<div class="window-light w-green"></div>
</div>
<pre><code>${escapedHTML}</code></pre>
</div>
</body>
</html>
`);
// Take a screenshot of the page
const screenshot = await page.screenshot();
// Close the browser instance
await browser.close();
// Set the appropriate headers for the response
res.setHeader("Content-Type", "image/png");
res.setHeader("Content-Disposition", 'attachment; filename="screenshot.png"');
// Send the screenshot as the response
res.send(screenshot);
});
// Start the server
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});