-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.js
97 lines (93 loc) · 2.89 KB
/
main.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
// 导入所需库
const puppeteer = require("puppeteer");
// 初始化API参数
let textL = "蔚蓝";
let textR = "档案";
let x = "-15";
let y = "0";
let tp = false;
// Sleep函数
const sleep = (timeout) => {
return new Promise((resolve) => {
setTimeout(() => {
resolve();
}, timeout)
})
}
// 服务器配置
const express = require('express')
const app = express()
const port = 3000;
const cors = require('cors')
app.use(cors())
app.listen(port, () => {
console.log(`Server is running on http://127.0.0.1:${port}/`)
});
// 新建本地生成器
app.use(express.static('public'));
app.get('/', (req, res) => {
res.render('index');
});
/**
* 主程序,其中
* @param {string} l Logo左边文字
* @param {string} r Logo右边文字
* @param {string} x 光圈X坐标
* @param {string} y 光圈Y坐标
* @param {boolean} tp 是否透明背景
*/
let browser;
(async function () {
browser = await puppeteer.launch({
headless: "new",
args: ['--no-sandbox', '--disable-setuid-sandbox'],
});
})()
app.get('/balogo', async function (ctx, res) {
console.log(ctx.query);
let random = Math.random().toString(36).slice(-8);
textL = ctx.query.l || "蔚蓝";
textR = ctx.query.r || "档案";
x = ctx.query.x || "-15";
y = ctx.query.y || "0";
tp = ctx.query.tp || false;
const page = await browser.newPage();
await page.goto(`http://127.0.0.1:${port}/`);
const client = await page.target().createCDPSession();
await client.send('Page.setDownloadBehavior', {
behavior: 'allow',
downloadPath: `./pic/${random}/`
});
await page.waitForSelector('canvas');
if (tp === "true") {
await page.evaluate(() => {
document.querySelector("#transparent").click()
});
}
await page.evaluate(() => {
document.querySelector("#textL").value = "";
document.querySelector("#textR").value = "";
document.querySelector("#graphX").value = "";
document.querySelector("#graphY").value = "";
document.querySelector(".collapse.collapse-arrow.bg-base-100 input").click()
});
await page.type('#graphX', x);
await page.type('#graphY', y);
await page.type('#textL', textL);
await page.type('#textR', textR);
await page.waitForSelector('#loading.hidden');
await page.waitForSelector('#loading:not(.hidden)');
await page.waitForSelector('#loading.hidden');
await sleep(100);
const imgButton = await page.waitForSelector("#base64")
await imgButton.click();
await sleep(100);
const imgBase64 = await page.$eval('#base64', el => el.innerText);
await page.close();
// 将获取到的Base64编码转为Buffer
res.setHeader('Access-Control-Allow-Origin', '*')
const base64 = imgBase64.replace(/^data:image\/\w+;base64,/, "");
res.write(new Buffer.from(base64, 'base64'));
res.status(200);
res.end();
})