Compare commits

..

1 Commits

Author SHA1 Message Date
Kamesuta
6f0ab3a130
Merge 6f9a5c65cc into ab7e3c301c 2023-10-22 18:22:31 +00:00

View File

@ -2,6 +2,8 @@
* Script for landing.ejs * Script for landing.ejs
*/ */
// Requirements // Requirements
const cp = require('child_process')
const crypto = require('crypto')
const { URL } = require('url') const { URL } = require('url')
const { join } = require('path') const { join } = require('path')
const { const {
@ -826,16 +828,6 @@ function showNewsAlert(){
$(newsButtonAlert).fadeIn(250) $(newsButtonAlert).fadeIn(250)
} }
async function digestMessage(str) {
const msgUint8 = new TextEncoder().encode(str)
const hashBuffer = await crypto.subtle.digest('SHA-1', msgUint8)
const hashArray = Array.from(new Uint8Array(hashBuffer))
const hashHex = hashArray
.map((b) => b.toString(16).padStart(2, '0'))
.join('')
return hashHex
}
/** /**
* Initialize News UI. This will load the news and prepare * Initialize News UI. This will load the news and prepare
* the UI accordingly. * the UI accordingly.
@ -843,93 +835,106 @@ async function digestMessage(str) {
* @returns {Promise.<void>} A promise which resolves when the news * @returns {Promise.<void>} A promise which resolves when the news
* content has finished loading and transitioning. * content has finished loading and transitioning.
*/ */
async function initNews(){ function initNews(){
setNewsLoading(true) return new Promise((resolve, reject) => {
setNewsLoading(true)
const news = await loadNews() let news = {}
loadNews().then(news => {
newsArr = news?.articles || null newsArr = news?.articles || null
if(newsArr == null){ if(newsArr == null){
// News Loading Failed // News Loading Failed
setNewsLoading(false) setNewsLoading(false)
await $('#newsErrorLoading').fadeOut(250).promise() $('#newsErrorLoading').fadeOut(250, () => {
await $('#newsErrorFailed').fadeIn(250).promise() $('#newsErrorFailed').fadeIn(250, () => {
resolve()
})
})
} else if(newsArr.length === 0) {
// No News Articles
setNewsLoading(false)
} else if(newsArr.length === 0) { ConfigManager.setNewsCache({
// No News Articles date: null,
setNewsLoading(false) content: null,
dismissed: false
})
ConfigManager.save()
ConfigManager.setNewsCache({ $('#newsErrorLoading').fadeOut(250, () => {
date: null, $('#newsErrorNone').fadeIn(250, () => {
content: null, resolve()
dismissed: false })
}) })
ConfigManager.save() } else {
// Success
setNewsLoading(false)
await $('#newsErrorLoading').fadeOut(250).promise() const lN = newsArr[0]
await $('#newsErrorNone').fadeIn(250).promise() const cached = ConfigManager.getNewsCache()
} else { let newHash = crypto.createHash('sha1').update(lN.content).digest('hex')
// Success let newDate = new Date(lN.date)
setNewsLoading(false) let isNew = false
const lN = newsArr[0] if(cached.date != null && cached.content != null){
const cached = ConfigManager.getNewsCache()
let newHash = await digestMessage(lN.content)
let newDate = new Date(lN.date)
let isNew = false
if(cached.date != null && cached.content != null){ if(new Date(cached.date) >= newDate){
if(new Date(cached.date) >= newDate){ // Compare Content
if(cached.content !== newHash){
isNew = true
showNewsAlert()
} else {
if(!cached.dismissed){
isNew = true
showNewsAlert()
}
}
// Compare Content } else {
if(cached.content !== newHash){
isNew = true
showNewsAlert()
} else {
if(!cached.dismissed){
isNew = true isNew = true
showNewsAlert() showNewsAlert()
} }
} else {
isNew = true
showNewsAlert()
} }
} else { if(isNew){
isNew = true ConfigManager.setNewsCache({
showNewsAlert() date: newDate.getTime(),
content: newHash,
dismissed: false
})
ConfigManager.save()
}
const switchHandler = (forward) => {
let cArt = parseInt(newsContent.getAttribute('article'))
let nxtArt = forward ? (cArt >= newsArr.length-1 ? 0 : cArt + 1) : (cArt <= 0 ? newsArr.length-1 : cArt - 1)
displayArticle(newsArr[nxtArt], nxtArt+1)
}
document.getElementById('newsNavigateRight').onclick = () => { switchHandler(true) }
document.getElementById('newsNavigateLeft').onclick = () => { switchHandler(false) }
$('#newsErrorContainer').fadeOut(250, () => {
displayArticle(newsArr[0], 1)
$('#newsContent').fadeIn(250, () => {
resolve()
})
})
} }
} else { })
isNew = true
showNewsAlert()
}
if(isNew){
ConfigManager.setNewsCache({
date: newDate.getTime(),
content: newHash,
dismissed: false
})
ConfigManager.save()
}
const switchHandler = (forward) => {
let cArt = parseInt(newsContent.getAttribute('article'))
let nxtArt = forward ? (cArt >= newsArr.length-1 ? 0 : cArt + 1) : (cArt <= 0 ? newsArr.length-1 : cArt - 1)
displayArticle(newsArr[nxtArt], nxtArt+1)
}
document.getElementById('newsNavigateRight').onclick = () => { switchHandler(true) }
document.getElementById('newsNavigateLeft').onclick = () => { switchHandler(false) }
await $('#newsErrorContainer').fadeOut(250).promise()
displayArticle(newsArr[0], 1)
await $('#newsContent').fadeIn(250).promise()
}
})
} }
/** /**