Yaoi-Spinner/pageUpdater.js

55 lines
1.4 KiB
JavaScript
Raw Normal View History

2024-12-19 02:13:15 -06:00
import * as path from "path"
import * as fs from "fs"
import { staticPath } from "./constants.js"
import { flavors, variety } from "./constants.js"
import { getYaoi } from "./scraper.js"
export async function middleware(req, res, next) {
var filePath = (req.baseUrl + req.path).trim()
if (!filePath.includes(".")) {
if (filePath.charAt(filePath.length - 1) != '/') {
res.redirect(filePath + '/')
return
}
filePath = path.join(filePath, '/index.html')
}
filePath = path.join(staticPath, filePath || 'index.html')
if (fs.existsSync(filePath) && filePath.includes(".html")) {
var data = fs.readFileSync(filePath).toString()
res.contentType(path.basename(filePath))
data = await converter(data)
res.send(data)
}
else {
next()
}
}
export async function converter(html) {
var replacers = {
"FLAVOR_COUNT": flavors.length,
"VARIETY": variety,
"FLAVORS": flavors.join(", ").replace(/,(?=[^,]+$)/, ' &'),
"SPIN_BUTTON": `<a href="/spin" id="yaoiButton"><div>S</div><div>P</div><div>I</div><div>N</div></a>`,
"YAOI": () => {
var yaoi = getYaoi()
if (yaoi) {
return `<div class="post">${yaoi.html}</div>`
}
}
}
for (var key in replacers) {
html = html.replaceAll(`{${key}}`, replacers[key])
}
return html
}