import { addExtensionSettings, enableAnimatedBackground, GetThresholdOfColor, loadHomePage, SendNewsPage, setupSettingsButton } from "@/SEQTA"; import { updateBgDurations } from "./Animation"; import { appendBackgroundToUI } from "./ImageBackgrounds"; import stringToHTML from "@/seqta/utils/stringToHTML"; import { settingsState } from "@/seqta/utils/listeners/SettingsState"; import { updateAllColors } from "./colors/Manager"; import { delay } from "@/seqta/utils/delay"; let cachedUserInfo: any = null; async function getUserInfo() { if (cachedUserInfo) return cachedUserInfo; try { const response = await fetch(`${location.origin}/seqta/student/login`, { method: 'POST', headers: { 'Content-Type': 'application/json; charset=utf-8', }, body: JSON.stringify({ mode: 'normal', query: null, redirect_url: location.origin, }), }); const responseData = await response.json(); cachedUserInfo = responseData.payload; return cachedUserInfo; } catch (error) { console.error('Error fetching user info:', error); throw error; } } export async function AddBetterSEQTAElements() { if (settingsState.onoff) { initializeSettings(); if (settingsState.DarkMode) { document.documentElement.classList.add('dark'); } const fragment = document.createDocumentFragment(); const menu = document.getElementById('menu')!; const menuList = menu.firstChild as HTMLElement; createHomeButton(fragment, menuList); createNewsButton(fragment, menu); menuList.insertBefore(fragment, menuList.firstChild); try { await Promise.all([ appendBackgroundToUI(), handleUserInfo(), handleStudentData() ]); } catch (error) { console.error('Error initializing UI elements:', error); } setupEventListeners(); await addDarkLightToggle(); customizeMenuToggle(); } addExtensionSettings(); await createSettingsButton(); setupSettingsButton(); } function initializeSettings() { enableAnimatedBackground(); updateBgDurations(); } function createHomeButton(fragment: DocumentFragment, menuList: HTMLElement) { const container = document.getElementById('content')!; const div = document.createElement('div'); div.classList.add('titlebar'); container.append(div); const NewButton = stringToHTML('
  • '); if (NewButton.firstChild) { fragment.appendChild(NewButton.firstChild); } } async function handleUserInfo() { try { const info = await getUserInfo(); updateUserInfo(info); } catch (error) { console.error('Error fetching and processing student data:', error); } } function updateUserInfo(info: { basic: boolean; clientIP: string[] | null; email: string | null; id: number | null; lastAccessedTime: number | null; meta: { code: string | null; governmentID: string | null; }; personUUID: string | null; status: number | null; synergeticCommunityUrl: string | null; type: string | null; userCode: string | null; userDesc: string | null; userName: string | null; }) { const titlebar = document.getElementsByClassName('titlebar')[0]; const userInfo = stringToHTML(/* html */`
    `).firstChild; titlebar.append(userInfo!); const userinfo = stringToHTML(/* html */`

    ${info.userDesc}

    ${info.meta.code} // ${info.meta.governmentID}

    `).firstChild; titlebar.append(userinfo!); var logoutbutton = document.getElementsByClassName('logout')[0]; var userInfosvgdiv = document.getElementById('logouttooltip')!; userInfosvgdiv.appendChild(logoutbutton); } async function handleStudentData() { try { const response = await fetch(`${location.origin}/seqta/student/load/message/people`, { method: 'POST', headers: { 'Content-Type': 'application/json; charset=utf-8', }, body: JSON.stringify({ mode: 'student' }), }); const responseData = await response.json(); let students = responseData.payload; await updateStudentInfo(students); } catch (error) { console.error('Error fetching and processing student data:', error); } } async function updateStudentInfo(students: any) { const info = await getUserInfo(); var index = students.findIndex(function (person: any) { return ( person.firstname == info.userDesc.split(' ')[0] && person.surname == info.userDesc.split(' ')[1] ); }); let houseelement1 = document.getElementsByClassName('userInfohouse')[0]; const houseelement = houseelement1 as HTMLElement; if (students[index]?.house) { if (students[index]?.house_colour) { houseelement.style.background = students[index].house_colour; try { let colorresult = GetThresholdOfColor(students[index]?.house_colour); houseelement.style.color = colorresult && colorresult > 300 ? 'black' : 'white'; houseelement.innerText = students[index].year + students[index].house; } catch (error) { houseelement.innerText = students[index].house; } } } else { try { houseelement.innerText = students[index].year; } catch(err) { houseelement.innerText = 'N/A'; } } } function createNewsButton(fragment: DocumentFragment, menu: HTMLElement) { const NewsButtonStr = '
  • '; const NewsButton = stringToHTML(NewsButtonStr); if (NewsButton.firstChild) { fragment.appendChild(NewsButton.firstChild); } let iconCover = document.createElement('div'); iconCover.classList.add('icon-cover'); iconCover.id = 'icon-cover'; menu.appendChild(iconCover); } function setupEventListeners() { const menuCover = document.querySelector('#icon-cover'); const homebutton = document.getElementById('homebutton'); const newsbutton = document.getElementById('newsbutton'); homebutton?.addEventListener('click', function() { if (!homebutton.classList.contains('draggable') && !homebutton.classList.contains('active')) { loadHomePage(); } }); newsbutton?.addEventListener('click', function() { if (!newsbutton.classList.contains('draggable') && !newsbutton.classList.contains('active')) { SendNewsPage(); } }); menuCover?.addEventListener('click', function() { location.href = '../#?page=/home'; loadHomePage(); (document.getElementById('menu')!.firstChild! as HTMLElement).classList.remove('noscroll'); }); } async function createSettingsButton() { let SettingsButton = stringToHTML( /* html */` `); let ContentDiv = document.getElementById('content'); ContentDiv!.append(SettingsButton.firstChild!); } function GetLightDarkModeString() { if (settingsState.DarkMode) { return 'Switch to light theme' } else { return 'Switch to dark theme' } } async function addDarkLightToggle() { const tooltipString = GetLightDarkModeString(); const svgContent = settingsState.DarkMode ? /* html */`` : /* html */``; const LightDarkModeButton = stringToHTML(/* html */` `); let ContentDiv = document.getElementById('content'); ContentDiv!.append(LightDarkModeButton.firstChild!); updateAllColors(); document.getElementById('LightDarkModeButton')!.addEventListener('click', async () => { const darklightText = document.getElementById('darklighttooliptext'); if (settingsState.originalDarkMode != undefined && settingsState.selectedTheme) { darklightText!.innerText = 'Locked by current theme'; await delay(1000) darklightText!.innerText = GetLightDarkModeString(); return } settingsState.DarkMode = !settingsState.DarkMode; updateAllColors(); darklightText!.innerText = GetLightDarkModeString(); }); } function customizeMenuToggle() { const menuToggle = document.getElementById('menuToggle'); if (menuToggle) { menuToggle.innerHTML = ''; } for (let i = 0; i < 3; i++) { const line = document.createElement('div'); line.className = 'hamburger-line'; menuToggle!.appendChild(line); } }