forked from ajmeese7/readme-ascii
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
122 lines (107 loc) · 4.21 KB
/
index.js
File metadata and controls
122 lines (107 loc) · 4.21 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
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
const puppeteer = require("puppeteer");
const express = require("express");
const path = require("path");
const PORT = process.env.PORT || 5000 // Heroku or local
var app = express();
app
.use(express.static(path.join(__dirname, "public")))
.set("views", path.join(__dirname, "views"))
.set("view engine", "ejs")
.get("/", (req, res) => res.render("index"))
.get("/index", (req, res) => res.render("index"))
.use((req, res, next) => {
// CORS headers
res.append("Access-Control-Allow-Origin", ["*"]);
res.append("Access-Control-Allow-Methods", "GET");
res.append("Access-Control-Allow-Headers", "Content-Type");
next();
})
.get("/generate", function (req, res) {
let text_param = req.query.text;
let textColor = req.query.textColor;
let backgroundColor = req.query.backgroundColor;
let shadow = req.query.shadow;
let ascii_url = "http://patorjk.com/software/taag/#p=display&f=Alpha&t=" + encodeURIComponent(text_param);
if (!text_param) return res.render("error");
(async () => {
let browser;
try {
browser = await puppeteer.launch({
args: ["--no-sandbox", "--disable-setuid-sandbox"],
headless: "new" // Use new headless mode for modern browsers
});
const page = await browser.newPage();
// Set a longer timeout for navigation
page.setDefaultNavigationTimeout(60000);
console.log("Navigating to ASCII generator...");
await page.goto(ascii_url);
// Wait for the output element to be available
await page.waitForSelector("#taag_output_text");
// Get the ASCII from the page
const ascii = await page.$eval("#taag_output_text", el => el.textContent);
if (!ascii) {
await browser.close();
return res.status(500).send("Failed to generate ASCII text");
}
console.log("Generating image from ASCII...");
// Check if background should be transparent
const isTransparent = backgroundColor === "rgba(255, 255, 255, 0)" ||
backgroundColor === "transparent" ||
backgroundColor.endsWith(", 0)") ||
backgroundColor.endsWith(",0)");
// Create an HTML page with the ASCII art styled according to parameters
const html = `
<!DOCTYPE html>
<html>
<head>
<style>
body {
margin: 0;
padding: 20px;
background-color: ${isTransparent ? "transparent" : backgroundColor || "white"};
}
pre {
font-family: monospace;
color: ${textColor || "black"};
font-weight: bold;
font-size: 12px;
line-height: 12px;
${shadow === "true" ? `text-shadow: -1ex 0.2pc 6px ${textColor || "black"};` : ""}
white-space: pre;
}
</style>
</head>
<body>
<pre>${ascii}</pre>
</body>
</html>
`;
// Set the HTML content
await page.setContent(html);
// Take a screenshot as base64
const screenshot = await page.screenshot({
encoding: "base64",
omitBackground: isTransparent,
fullPage: true,
type: "png"
});
// Create data URL from screenshot
const base64_url = `data:image/png;base64,${screenshot}`;
// Set content type to JSON and send the data URL
res.setHeader("Content-Type", "text/plain");
res.write(base64_url);
res.end();
} catch (error) {
console.error("Unexpected error:", error);
if (!res.headersSent) {
res.status(500).send("An unexpected error occurred while generating the image");
}
} finally {
if (browser) {
await browser.close();
}
}
})();
})
.get("*", (req, res) => res.render("error"))
.listen(PORT, () => console.log(`Listening on ${ PORT }`)) // localhost:5000