feat: validate login passed to background and implemented into add all injected button (#443)

* docs: proper captializing

* docs: confirmed that the for loop is entered but on first startup something else

* feat: turns out - validate login status - not in background - pass background now

* feat: kronk.gif

* Update UserScheduleMessages.ts

* Update addCourseByURL.ts

* chore: format

* chore: chore

---------

Co-authored-by: doprz <52579214+doprz@users.noreply.github.com>
This commit is contained in:
2025-01-20 18:42:15 -06:00
committed by GitHub
parent f425510e11
commit cd05e5e7fc
4 changed files with 31 additions and 7 deletions

View File

@@ -8,6 +8,7 @@ import renameSchedule from '@pages/background/lib/renameSchedule';
import switchSchedule from '@pages/background/lib/switchSchedule'; import switchSchedule from '@pages/background/lib/switchSchedule';
import type { UserScheduleMessages } from '@shared/messages/UserScheduleMessages'; import type { UserScheduleMessages } from '@shared/messages/UserScheduleMessages';
import { Course } from '@shared/types/Course'; import { Course } from '@shared/types/Course';
import { validateLoginStatus } from '@shared/util/checkLoginStatus';
import type { MessageHandler } from 'chrome-extension-toolkit'; import type { MessageHandler } from 'chrome-extension-toolkit';
const userScheduleHandler: MessageHandler<UserScheduleMessages> = { const userScheduleHandler: MessageHandler<UserScheduleMessages> = {
@@ -41,6 +42,9 @@ const userScheduleHandler: MessageHandler<UserScheduleMessages> = {
.then(res => (response === 'json' ? res.json() : res.text())) .then(res => (response === 'json' ? res.json() : res.text()))
.then(sendResponse); .then(sendResponse);
}, },
validateLoginStatus({ data, sendResponse }) {
validateLoginStatus(data.url).then(sendResponse);
},
exportSchedule({ data, sendResponse }) { exportSchedule({ data, sendResponse }) {
exportSchedule(data.scheduleId).then(sendResponse); exportSchedule(data.scheduleId).then(sendResponse);
}, },

View File

@@ -23,7 +23,9 @@ export async function addCourseByURL(activeSchedule: UserSchedule, link?: string
if (!link) link = prompt('Enter course link') || undefined; if (!link) link = prompt('Enter course link') || undefined;
// Exit if the user cancels the prompt // Exit if the user cancels the prompt
if (!link) return; if (!link) {
return;
}
try { try {
let htmlText: string; let htmlText: string;
@@ -46,17 +48,16 @@ export async function addCourseByURL(activeSchedule: UserSchedule, link?: string
const scrapedCourses = scraper.scrape(tableRows, false); const scrapedCourses = scraper.scrape(tableRows, false);
if (scrapedCourses.length !== 1) return; if (scrapedCourses.length !== 1) return;
const description = scraper.getDescription(doc); const description = scraper.getDescription(doc);
const row = scrapedCourses[0]!; const row = scrapedCourses[0]!;
const course = row.course!; const course = row.course!;
course.description = description; course.description = description;
if (activeSchedule.courses.every(c => c.uniqueId !== course.uniqueId)) { if (activeSchedule.courses.every(c => c.uniqueId !== course.uniqueId)) {
console.log('adding course'); console.log('Adding course');
await addCourse(activeSchedule.id, course); await addCourse(activeSchedule.id, course);
} else { } else {
console.log('course already exists'); console.log('Course already exists');
} }
} catch (error) { } catch (error) {
console.error('Error scraping course:', error); console.error('Error scraping course:', error);

View File

@@ -64,6 +64,14 @@ export interface UserScheduleMessages {
*/ */
renameSchedule: (data: { scheduleId: string; newName: string }) => string | undefined; renameSchedule: (data: { scheduleId: string; newName: string }) => string | undefined;
/**
* Checks the login status by making a request to the provided URL.
*
* @param data - The URL to check the login status against.
* @returns true if user was already logged into the provided URL, false otherwise
*/
validateLoginStatus: (data: { url: string }) => boolean;
/** /**
* Exports the current schedule to a JSON file for backing up and sharing * Exports the current schedule to a JSON file for backing up and sharing
* *

View File

@@ -1,4 +1,6 @@
import { addCourseByURL } from '@pages/background/lib/addCourseByURL'; import { addCourseByURL } from '@pages/background/lib/addCourseByURL';
import { background } from '@shared/messages';
import { validateLoginStatus } from '@shared/util/checkLoginStatus';
import { Button } from '@views/components/common/Button'; import { Button } from '@views/components/common/Button';
import ExtensionRoot from '@views/components/common/ExtensionRoot/ExtensionRoot'; import ExtensionRoot from '@views/components/common/ExtensionRoot/ExtensionRoot';
import useSchedules from '@views/hooks/useSchedules'; import useSchedules from '@views/hooks/useSchedules';
@@ -30,10 +32,19 @@ export default function InjectedButton(): JSX.Element | null {
// Make sure to remove duplicate anchorTags using set // Make sure to remove duplicate anchorTags using set
const uniqueAnchorTags = Array.from(new Set(anchorTags.map(a => a.href))); const uniqueAnchorTags = Array.from(new Set(anchorTags.map(a => a.href)));
// Make sure user is logged in
const loggedInToUT = await background.validateLoginStatus({
url: 'https://utdirect.utexas.edu/apps/registrar/course_schedule/utrp_login/',
});
if (loggedInToUT) {
for (const a of uniqueAnchorTags) { for (const a of uniqueAnchorTags) {
// eslint-disable-next-line no-await-in-loop // eslint-disable-next-line no-await-in-loop
await addCourseByURL(activeSchedule, a); await addCourseByURL(activeSchedule, a);
} }
} else {
window.alert('Logged into UT Registrar.');
}
}; };
useEffect(() => { useEffect(() => {