Performance improved maybe?? Readded the body tag which apparently did not exist either ???????
This commit is contained in:
parent
f9a57722af
commit
d26b0e12ed
5 changed files with 904 additions and 176 deletions
4
index.js
4
index.js
|
@ -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
802
package-lock.json
generated
File diff suppressed because it is too large
Load diff
|
@ -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",
|
||||
|
|
272
pageUpdater.js
272
pageUpdater.js
|
@ -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,34 +53,123 @@ function timeFormatter(seconds) {
|
|||
|
||||
}
|
||||
|
||||
function converter(html) {
|
||||
var startTime = Date.now()
|
||||
function pathReplacer(html) {
|
||||
while (html.includes("{PATH_")) {
|
||||
var pagePath = html.substring(html.indexOf("{PATH_"))
|
||||
pagePath = pagePath.substring(6, pagePath.indexOf('}'))
|
||||
|
||||
html = html
|
||||
var stringIndex = `{PATH_${pagePath}}`
|
||||
pagePath = pagePath.toLowerCase()
|
||||
|
||||
var config = JSON.parse(fs.readFileSync(path.join(__dirname, 'config/config.json')))
|
||||
var pageHTML = fs.readFileSync(path.join(__dirname, 'static', pagePath, 'index.html')).toString()
|
||||
pageHTML = pageHTML.substring(pageHTML.indexOf('<main>') + 6, pageHTML.indexOf('</main>'))
|
||||
html = html.replaceAll(stringIndex, pageHTML)
|
||||
}
|
||||
return html
|
||||
}
|
||||
|
||||
var bnchName = "Beta"
|
||||
var bnchSub = "beta."
|
||||
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)
|
||||
|
||||
if (process.env.BRANCH == "dev") {
|
||||
bnchName = "Stable"
|
||||
bnchSub = ""
|
||||
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}`)
|
||||
}
|
||||
|
||||
if (full) {
|
||||
for (let index = 0; index < highTable.length; index++) {
|
||||
var termKey = "{TERM" + index + "}"
|
||||
var termProps = highlightedWords[highTable[index]]
|
||||
while (element.content.includes(termKey)) {
|
||||
var termIndex = element.content.indexOf(termKey)
|
||||
|
||||
var spanEnd = element.content.indexOf(" ", termIndex)
|
||||
|
||||
if (spanEnd == -1) {
|
||||
spanEnd = element.content.length
|
||||
}
|
||||
|
||||
var endContent = element.content.substring(termIndex + termKey.length, spanEnd)
|
||||
|
||||
var spanStart = element.content.substring(0, termIndex).lastIndexOf(" ") + 1
|
||||
var startContent = element.content.substring(spanStart - 1, termIndex)
|
||||
|
||||
var style = termProps.style || ""
|
||||
var classes = termProps.classes || ""
|
||||
|
||||
if (termProps.color) {
|
||||
style += `color: ${termProps.color};`
|
||||
}
|
||||
|
||||
if (termProps.italicized) {
|
||||
style += "font-style: italic;"
|
||||
}
|
||||
|
||||
if (termProps.bold) {
|
||||
classes += "bold"
|
||||
}
|
||||
|
||||
if (style.length > 2) {
|
||||
style = `style="${style}"`
|
||||
}
|
||||
|
||||
if (classes.length > 2) {
|
||||
classes = `class="${classes}"`
|
||||
}
|
||||
|
||||
var replacement = `<span ${style} ${classes}>${startContent + highTable[index] + endContent}</span>`
|
||||
|
||||
element.content = element.content.substring(0, spanStart) + replacement + element.content.substring(spanEnd)
|
||||
}
|
||||
}
|
||||
|
||||
// element.content = element.content.replaceAll(termKey, replacement)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var replacers = {
|
||||
"ALL_KEYWORDS": undefined,
|
||||
return json
|
||||
}
|
||||
|
||||
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": bnchName,
|
||||
"BRANCH_SUB": bnchSub,
|
||||
"RANDOM_QUOTE": quotes[Math.floor(Math.random() * quotes.length)],
|
||||
"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,
|
||||
"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) {
|
||||
|
@ -94,13 +196,9 @@ function converter(html) {
|
|||
}
|
||||
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 = ""
|
||||
|
@ -122,32 +220,26 @@ function converter(html) {
|
|||
}
|
||||
}
|
||||
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:​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("}{") + "} "
|
||||
|
||||
while (html.includes("{PATH_")) {
|
||||
var pagePath = html.substring(html.indexOf("{PATH_"))
|
||||
pagePath = pagePath.substring(6, pagePath.indexOf('}'))
|
||||
|
||||
var stringIndex = `{PATH_${pagePath}}`
|
||||
pagePath = pagePath.toLowerCase()
|
||||
|
||||
var pageHTML = fs.readFileSync(path.join(__dirname, 'static', pagePath, 'index.html')).toString()
|
||||
pageHTML = pageHTML.substring(pageHTML.indexOf('<main>') + 6, pageHTML.indexOf('</main>'))
|
||||
html = html.replaceAll(stringIndex, pageHTML)
|
||||
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++) {
|
||||
|
@ -162,88 +254,13 @@ function converter(html) {
|
|||
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)
|
||||
// console.log(parsedHTML)
|
||||
|
||||
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}`)
|
||||
}
|
||||
|
||||
for (let index = 0; index < highTable.length; index++) {
|
||||
var termKey = "{TERM" + index + "}"
|
||||
var termProps = highlightedWords[highTable[index]]
|
||||
while (element.content.includes(termKey)) {
|
||||
var termIndex = element.content.indexOf(termKey)
|
||||
|
||||
var spanEnd = element.content.indexOf(" ", termIndex)
|
||||
|
||||
if (spanEnd == -1) {
|
||||
spanEnd = element.content.length
|
||||
}
|
||||
|
||||
var endContent = element.content.substring(termIndex + termKey.length, spanEnd)
|
||||
|
||||
var spanStart = element.content.substring(0, termIndex).lastIndexOf(" ") + 1
|
||||
var startContent = element.content.substring(spanStart - 1, termIndex)
|
||||
|
||||
var style = termProps.style || ""
|
||||
var classes = termProps.classes || ""
|
||||
|
||||
if (termProps.color) {
|
||||
style += `color: ${termProps.color};`
|
||||
}
|
||||
|
||||
if (termProps.italicized) {
|
||||
style += "font-style: italic;"
|
||||
}
|
||||
|
||||
if (termProps.bold) {
|
||||
classes += "bold"
|
||||
}
|
||||
|
||||
if (style.length > 2) {
|
||||
style = `style="${style}"`
|
||||
}
|
||||
|
||||
if (classes.length > 2) {
|
||||
classes = `class="${classes}"`
|
||||
}
|
||||
|
||||
var replacement = `<span ${style} ${classes}>${startContent + highTable[index] + endContent}</span>`
|
||||
|
||||
element.content = element.content.substring(0, spanStart) + replacement + element.content.substring(spanEnd)
|
||||
}
|
||||
|
||||
// element.content = element.content.replaceAll(termKey, replacement)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return json
|
||||
}
|
||||
|
||||
parsedHTML = highlighter(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) {
|
||||
|
|
1
static/discHTML/index.html
Normal file
1
static/discHTML/index.html
Normal file
|
@ -0,0 +1 @@
|
|||
{ACTIVITIES}
|
Loading…
Reference in a new issue