perf(news): improved news page performance

This commit is contained in:
SethBurkart123
2024-11-29 14:52:26 +11:00
parent 430f158957
commit 5ed3a05f6a
+41 -37
View File
@@ -2528,72 +2528,76 @@ function createNewShortcut(link: any, icon: any, viewBox: any, title: any) {
document.getElementById('shortcuts')!.appendChild(shortcut) document.getElementById('shortcuts')!.appendChild(shortcut)
} }
export function SendNewsPage() { export async function SendNewsPage() {
setTimeout(function () {
// Sends the html data for the home page
console.info('[BetterSEQTA+] Started Loading News Page') console.info('[BetterSEQTA+] Started Loading News Page')
document.title = 'News ― SEQTA Learn' document.title = 'News ― SEQTA Learn'
var element = document.querySelector('[data-key=news]') await delay(100)
// Apply the active class to indicate clicked on home button const element = document.querySelector('[data-key=news]')
element!.classList.add('active') element!.classList.add('active')
// Remove all current elements in the main div to add new elements // Remove all current elements in the main div to add new elements
var main = document.getElementById('main') const main = document.getElementById('main')
main!.innerHTML = '' main!.innerHTML = ''
// Creates the root of the home page added to the main div const html = stringToHTML(/* html */`
var htmlStr = '<div class="home-root"><div class="home-container" id="news-container"><h1 class="border">Latest Headlines - ABC News</h1></div></div>' <div class="home-root">
<div class="home-container" id="news-container">
<h1 class="border">Latest Headlines - ABC News</h1>
</div>
</div>`)
var html = stringToHTML(htmlStr)
// Appends the html file to main div
// Note : firstChild of html is done due to needing to grab the body from the stringToHTML function
main!.append(html.firstChild!) main!.append(html.firstChild!)
const titlediv = document.getElementById('title')!.firstChild; const titlediv = document.getElementById('title')!.firstChild;
(titlediv! as HTMLElement).innerText = 'News' (titlediv! as HTMLElement).innerText = 'News'
AppendLoadingSymbol('newsloading', '#news-container') AppendLoadingSymbol('newsloading', '#news-container')
browser.runtime.sendMessage({ type: 'sendNews' }).then(function (response) { const response = await browser.runtime.sendMessage({ type: 'sendNews' })
let newsarticles = response.news.articles const newscontainer = document.querySelector('#news-container')
var newscontainer = document.querySelector('#news-container')
document.getElementById('newsloading')?.remove() document.getElementById('newsloading')?.remove()
for (let i = 0; i < newsarticles.length; i++) {
let newsarticle = document.createElement('a') // Create a document fragment to batch DOM operations
const fragment = document.createDocumentFragment()
// Map over articles to create elements
response.news.articles.forEach((article: any) => {
const newsarticle = document.createElement('a')
newsarticle.classList.add('NewsArticle') newsarticle.classList.add('NewsArticle')
newsarticle.href = newsarticles[i].url newsarticle.href = article.url
newsarticle.target = '_blank' newsarticle.target = '_blank'
let articleimage = document.createElement('div') const articleimage = document.createElement('div')
articleimage.classList.add('articleimage') articleimage.classList.add('articleimage')
if (newsarticles[i].urlToImage == 'null') { if (article.urlToImage == 'null') {
articleimage.style.backgroundImage = `url(${browser.runtime.getURL(LogoLightOutline)})` articleimage.style.cssText = `
articleimage.style.width = '20%' background-image: url(${browser.runtime.getURL(LogoLightOutline)});
articleimage.style.margin = '0 7.5%' width: 20%;
margin: 0 7.5%;
`
} else { } else {
articleimage.style.backgroundImage = `url(${newsarticles[i].urlToImage})` articleimage.style.backgroundImage = `url(${article.urlToImage})`
} }
let articletext = document.createElement('div') const articletext = document.createElement('div')
articletext.classList.add('ArticleText') articletext.classList.add('ArticleText')
let title = document.createElement('a')
title.innerText = newsarticles[i].title const title = document.createElement('a')
title.href = newsarticles[i].url title.innerText = article.title
title.href = article.url
title.target = '_blank' title.target = '_blank'
let description = document.createElement('p') const description = document.createElement('p')
description.innerHTML = newsarticles[i].description description.innerHTML = article.description
articletext.append(title) articletext.append(title, description)
articletext.append(description) newsarticle.append(articleimage, articletext)
fragment.append(newsarticle)
newsarticle.append(articleimage)
newsarticle.append(articletext)
newscontainer?.append(newsarticle)
}
}) })
}, 8)
// Single DOM update to append all articles
newscontainer?.append(fragment)
} }
async function CheckForMenuList() { async function CheckForMenuList() {