Performance improved maybe?? Readded the body tag which apparently did not exist either ???????

This commit is contained in:
bingus_violet 2024-06-19 18:40:24 -05:00
parent f9a57722af
commit d26b0e12ed
5 changed files with 904 additions and 176 deletions

View file

@ -49,10 +49,6 @@ if (!fs.existsSync(path.join(cachePath, "emojis"))) {
fs.mkdirSync(path.join(cachePath, "emojis"))
}
app.get("/discHTML", (req, res) => {
res.send(pageUpdater.getActivities())
})
app.use(pageUpdater.middleWare)
process.on('uncaughtException', (err, origin) => {

802
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -20,6 +20,7 @@
"@node-minify/core": "^8.0.6",
"@node-minify/uglify-js": "^8.0.6",
"express": "^4.18.2",
"glob": "^10.4.2",
"himalaya": "^1.1.0",
"minify-html": "^0.0.2",
"ws": "^8.16.0",

View file

@ -6,7 +6,8 @@ const path = require('path'),
htmlMinifier = require("minify-html"),
activityToHTML = require("./overcomplicatedStatuses.js"),
randomThemer = require("./randomThemer.js"),
himalaya = require("himalaya")
himalaya = require("himalaya"),
glob = require("glob")
var constants = JSON.parse(fs.readFileSync(path.join(__dirname, 'constants.json')))
@ -22,6 +23,18 @@ var lanyardData = undefined
var uptime = Date.now()
var pregenFiles = []
var globResult = glob.globSync("**/static/**/*.html", { absolute: true })
for (var i = 0; i < globResult.length; i++) {
var result = globResult[i]
pregenFiles.push({
"absolutePath": result,
"path": result.substring(result.indexOf("static") + 7),
"html": undefined
})
}
function firstToUpper(str) {
return str.charAt(0).toUpperCase() + str.slice(1)
}
@ -40,102 +53,7 @@ function timeFormatter(seconds) {
}
function converter(html) {
var startTime = Date.now()
html = html
var config = JSON.parse(fs.readFileSync(path.join(__dirname, 'config/config.json')))
var bnchName = "Beta"
var bnchSub = "beta."
if (process.env.BRANCH == "dev") {
bnchName = "Stable"
bnchSub = ""
}
var replacers = {
"ALL_KEYWORDS": undefined,
"ALL_HIGHLIGHTS": Object.keys(highlightedWords).join(", "),
"BRANCH_NAME": bnchName,
"BRANCH_SUB": bnchSub,
"RANDOM_QUOTE": quotes[Math.floor(Math.random() * quotes.length)],
"COMMIT_COUNT": commitCount,
"QUOTE_COUNT": quotes.length,
"RANDOM_TITLE": titles[Math.floor(Math.random() * titles.length)],
"DISCORD_STATUS":
`<span style="color: ${constants.discStatuses[lanyardData.discord_status].color};" class="statusColor">${constants.discStatuses[lanyardData.discord_status].text}</span>` +
`<style>.pfp { border-color: ${constants.discStatuses[lanyardData.discord_status].color} }</style>`,
"UPTIME": uptime,
"TOPBAR": `<div id="topbar"><h3><a href="/socials">Socials</a></h3></div>`,
"CUSTOM_STATUS": () => {
if (lanyardData && lanyardData.activities[0] && lanyardData.activities[0].type == 4) {
var status = lanyardData.activities[0]
var addedHTML = "<hr/><p>"
if (status.emoji) {
if (status.emoji.id) {
addedHTML += `<img src="/emojis/${status.emoji.id}" title="${status.emoji.name}" class="emoji"/>`
} else {
addedHTML += status.emoji.name + " "
}
}
addedHTML += status.state
addedHTML += "</p>"
return addedHTML
}
return ""
},
"SELECTED_VIDEO": () => {
if (config.dailyVideoURL) {
return `<h2><hr/>Random video!</h2><p>I would call it random <em>daily</em> video but its not at all daily...</p>
<br/>
<video controls="true" src="${config.dailyVideoURL}"></video>`
}
return ``
},
"SPINCOUNT": globalSpins,
"UPTIME": timeFormatter((Date.now() - uptime) / 1000),
"LAST_LANYARD": timeFormatter((Date.now() - lastLanyardUpdate) / 1000),
"WEATHER_MODIFIER": randomThemer.returnTheme(),
"WEATHER_TEXT": "",
"ANNOUNCEMENT": fs.readFileSync(path.join(__dirname, "config/announcement.html")),
"ACTIVITIES": activityToHTML.activitiesToHTML(lanyardData),
"SOCIALS": () => {
var socials = lanyardData.socials
var html = ""
if (socials) {
var socialsTable = Object.keys(socials)
for (var i = 0; i < socialsTable.length; i++) {
var category = socialsTable[i]
var sites = socials[category]
var sitesTable = Object.keys(sites)
html += `<div class="grid-child"><div><h3>${category}</h3>`
for (var x = 0; x < sitesTable.length; x++) {
var siteName = sitesTable[x]
var siteData = sites[siteName]
if (siteData.url) {
html += `<a class="chip" href="${siteData.url}">${siteName}: ${siteData.name}</a>`
}
}
html += "</div></div>"
}
}
return html
}
}
// <div class="grid-child">
// <div>
// <h3>Chat</h3>
// <a class="chip" href="https://matrix.to/#/@bingus_violet:matrix.violets-purgatory.dev">Matrix: @bingus_violet:&ZeroWidthSpace;matrix.violets-purgatory.dev</a>
// <a class="chip">Discord: bingus_violet</a>
// <a class="chip">Revolt: Bingus{Violet}#5573</a>
// </div>
// </div>
replacers.ALL_KEYWORDS = "{" + Object.keys(replacers).join("}{") + "} "
function pathReplacer(html) {
while (html.includes("{PATH_")) {
var pagePath = html.substring(html.indexOf("{PATH_"))
pagePath = pagePath.substring(6, pagePath.indexOf('}'))
@ -147,45 +65,34 @@ function converter(html) {
pageHTML = pageHTML.substring(pageHTML.indexOf('<main>') + 6, pageHTML.indexOf('</main>'))
html = html.replaceAll(stringIndex, pageHTML)
}
return html
}
var rpTable = Object.keys(replacers)
function highlighter(json, full=true) {
for (var i = 0; i < json.length; i++) {
var element = json[i]
if (element.type == "element") {
if (element.children.length > 0) {
element.children = highlighter(element.children, full)
}
} else if (element.type == "text") {
var highTable = Object.keys(highlightedWords)
for (let index = 0; index < rpTable.length; index++) {
const text = rpTable[index];
html = html.replaceAll(`{${text}}`, replacers[text])
}
for (let index = 0; index < highTable.length; index++) {
var term = highTable[index];
var termProps = highlightedWords[term]
if (html.includes("<body>")) {
var bodyHTML = htmlMinifier.minify(html.substring(html.indexOf("<body>") + 6, html.lastIndexOf("</body>")))
var parsedHTML = himalaya.parse(bodyHTML)
} else {
var parsedHTML = himalaya.parse(html)
}
function highlighter(json) {
for (var i = 0; i < json.length; i++) {
var element = json[i]
if (element.type == "element") {
if (element.children.length > 0) {
element.children = highlighter(element.children)
}
} else if (element.type == "text") {
var highTable = Object.keys(highlightedWords)
for (let index = 0; index < highTable.length; index++) {
var term = highTable[index];
var termProps = highlightedWords[term]
var reg = term
if (termProps.caseInsensitive) {
reg = new RegExp(`(${term})`, "gi")
}
element.content = element.content.replaceAll(`{${term}}`, "TEMPORARY_REPLACE")
element.content = element.content.replaceAll(reg, "{TERM" + index + "}")
element.content = element.content.replaceAll("TEMPORARY_REPLACE", `${term}`)
var reg = term
if (termProps.caseInsensitive) {
reg = new RegExp(`(${term})`, "gi")
}
element.content = element.content.replaceAll(`{${term}}`, "TEMPORARY_REPLACE")
element.content = element.content.replaceAll(reg, "{TERM" + index + "}")
element.content = element.content.replaceAll("TEMPORARY_REPLACE", `${term}`)
}
if (full) {
for (let index = 0; index < highTable.length; index++) {
var termKey = "{TERM" + index + "}"
var termProps = highlightedWords[highTable[index]]
@ -230,20 +137,130 @@ function converter(html) {
element.content = element.content.substring(0, spanStart) + replacement + element.content.substring(spanEnd)
}
}
// element.content = element.content.replaceAll(termKey, replacement)
}
// element.content = element.content.replaceAll(termKey, replacement)
}
}
return json
}
parsedHTML = highlighter(parsedHTML)
return json
}
parsedHTML = himalaya.stringify(parsedHTML)
function converter(html, dynamic=true) {
var startTime = Date.now()
var config = JSON.parse(fs.readFileSync(path.join(__dirname, 'config/config.json')))
var staticReplacers = {
"ALL_HIGHLIGHTS": Object.keys(highlightedWords).join(", "),
"BRANCH_NAME": () => {
if (process.env.BRANCH == "dev") {
return "Stable"
}
return "Beta"
},
"BRANCH_SUB": () => {
if (process.env.BRANCH == "dev") {
return ""
}
return "beta."
},
"COMMIT_COUNT": commitCount,
"QUOTE_COUNT": quotes.length,
"DISCORD_STATUS":
`<span style="color: ${constants.discStatuses[lanyardData.discord_status].color};" class="statusColor">${constants.discStatuses[lanyardData.discord_status].text}</span>` +
`<style>.pfp { border-color: ${constants.discStatuses[lanyardData.discord_status].color} }</style>`,
"TOPBAR": `<div id="topbar"><h3><a href="/socials">Socials</a></h3></div>`,
"CUSTOM_STATUS": () => {
if (lanyardData && lanyardData.activities[0] && lanyardData.activities[0].type == 4) {
var status = lanyardData.activities[0]
var addedHTML = "<hr/><p>"
if (status.emoji) {
if (status.emoji.id) {
addedHTML += `<img src="/emojis/${status.emoji.id}" title="${status.emoji.name}" class="emoji"/>`
} else {
addedHTML += status.emoji.name + " "
}
}
addedHTML += status.state
addedHTML += "</p>"
return addedHTML
}
return ""
},
"SELECTED_VIDEO": () => {
if (config.dailyVideoURL) {
return `<h2><hr/>Random video!</h2><p>I would call it random <em>daily</em> video but its not at all daily...</p>
<br/>
<video controls="true" src="${config.dailyVideoURL}"></video>`
}
return ``
},
"WEATHER_MODIFIER": randomThemer.returnTheme(),
"WEATHER_TEXT": "",
"ANNOUNCEMENT": fs.readFileSync(path.join(__dirname, "config/announcement.html")),
"SOCIALS": () => {
var socials = lanyardData.socials
var html = ""
if (socials) {
var socialsTable = Object.keys(socials)
for (var i = 0; i < socialsTable.length; i++) {
var category = socialsTable[i]
var sites = socials[category]
var sitesTable = Object.keys(sites)
html += `<div class="grid-child"><div><h3>${category}</h3>`
for (var x = 0; x < sitesTable.length; x++) {
var siteName = sitesTable[x]
var siteData = sites[siteName]
if (siteData.url) {
html += `<a class="chip" href="${siteData.url}">${siteName}: ${siteData.name}</a>`
}
}
html += "</div></div>"
}
}
return html
},
}
var realtimeReplacers = {
"ACTIVITIES": activityToHTML.activitiesToHTML(lanyardData),
"SPINCOUNT": globalSpins,
"UPTIME": timeFormatter((Date.now() - uptime) / 1000),
"LAST_LANYARD": timeFormatter((Date.now() - lastLanyardUpdate) / 1000),
"RANDOM_TITLE": titles[Math.floor(Math.random() * titles.length)],
"RANDOM_QUOTE": quotes[Math.floor(Math.random() * quotes.length)],
}
if (dynamic) {
var replacers = realtimeReplacers
} else {
var replacers = staticReplacers
}
html = pathReplacer(html)
var rpTable = Object.keys(replacers)
for (let index = 0; index < rpTable.length; index++) {
const text = rpTable[index];
html = html.replaceAll(`{${text}}`, replacers[text])
}
if (html.includes("<body>")) {
var bodyHTML = htmlMinifier.minify(html.substring(html.indexOf("<body>") + 6, html.lastIndexOf("</body>")))
var parsedHTML = himalaya.parse(bodyHTML)
} else {
var parsedHTML = himalaya.parse(html)
}
// console.log(parsedHTML)
parsedHTML = highlighter(parsedHTML, dynamic)
parsedHTML = himalaya.stringify(parsedHTML)
if (html.includes("<body>")) {
parsedHTML = "<body>" + parsedHTML + "</body>"
html = html.substring(0, html.indexOf("<body>")) + parsedHTML + html.substring(html.indexOf("</body>") + 7)
} else {
html = parsedHTML
@ -278,7 +295,13 @@ module.exports = {
res.contentType(path.basename(filePath))
if (filePath.includes(".html")) {
data = converter(data, req.query)
for (var i = 0; i < pregenFiles.length; i++) {
if (pregenFiles[i].html && pregenFiles[i].absolutePath == filePath) {
data = pregenFiles[i].html
}
}
data = converter(data, true)
// console.log(data)
}
@ -365,6 +388,11 @@ function socketeer() {
lanyardData = data.d
lastLanyardUpdate = Date.now()
for (var i = 0; i < pregenFiles.length; i++) {
// console.log(pregenFiles[i].absolutePath)
pregenFiles[i].html = converter(fs.readFileSync(pregenFiles[i].absolutePath).toString(), false)
}
for (var i = 0; i < lanyardData.activities.length; i++) {
var activity = lanyardData.activities[i]
if (activity.type == 4 && activity.emoji) {

View file

@ -0,0 +1 @@
{ACTIVITIES}