Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add schedule to the homepage #196

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions content-scripts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { fixPagination } from "./modules/pagination";
import { changeLayout } from "./modules/layout";
import { addStarIconToCard } from "./modules/favorite-course";
import { createComponentsPage } from "./pages/components_page";
import { addMainPageSchedule } from "./modules/homepage-schedule";

/*--
- Docs: https://developer.chrome.com/docs/extensions/reference/storage/#synchronous-response-to-storage-updates
Expand Down Expand Up @@ -51,6 +52,7 @@ const functionsToExecute = [
{ name: "injectOverrideFunctions", func: injectOverrideFunctions },
{ name: "addStarIconToCard", func: addStarIconToCard },
{ name: "componentsPage", func: createComponentsPage },
{ name: "addMainPageSchedule", func: addMainPageSchedule },
];

const init = async () => {
Expand Down
89 changes: 89 additions & 0 deletions content-scripts/modules/homepage-schedule.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import jsx from "texsaur";
import { getUP } from "./utilities/sigarra";
import { Lecture } from "../types";

// TODO: probably move this somewhere else
const getWeek: (date: Date) => number = function (date) {
const newYear = new Date(date.getFullYear(), 0, 1);
let day = newYear.getDay(); //the day of week the year begins on
day = day >= 0 ? day : day + 7;
const daynum =
Math.floor(
(date.getTime() -
newYear.getTime() -
(date.getTimezoneOffset() - newYear.getTimezoneOffset()) *
60000) /
86400000,
) + 1;
let weeknum;
//if the year starts before the middle of a week
if (day < 4) {
weeknum = Math.floor((daynum + day - 1) / 7) + 1;
if (weeknum > 52) {
const nYear = new Date(date.getFullYear() + 1, 0, 1);
let nday = nYear.getDay();
nday = nday >= 0 ? nday : nday + 7;
/*if the next year starts before the middle of
the week, it is week #1 of that year*/
weeknum = nday < 4 ? 1 : 53;
}
} else {
weeknum = Math.floor((daynum + day - 1) / 7);
}
return weeknum;
};

const Schedule: (up: string) => Element = (up) => {
const current_year: number = new Date().getFullYear();

const schedule_url: (up: string) => string = (up: string) =>
`https://sigarra.up.pt/feup/pt/hor_geral.estudantes_view?pv_num_unico=${up}&pv_ano_lectivo=${current_year}&pv_periodos=1`;

return (
<iframe
src={schedule_url(up)}
width="100%"
height="100%"
frameBorder="0"
scrolling="auto"
style="display: none;"
id="main-page-schedule"
/>
);
};

export const addMainPageSchedule = async () => {
const up = getUP();
const schedule = Schedule(up);
document.body.appendChild(schedule);

const scheduleIframe = document.getElementById(
"main-page-schedule",
) as HTMLIFrameElement;

const json = await new Promise((resolve) => {
scheduleIframe.onload = resolve;
return;
}).then(async () => {
const api_url = scheduleIframe?.contentWindow?.document
.getElementById("cal-shadow-container")
?.getAttribute("data-evt-source-url");

const res = await fetch(api_url as string);
return res.json();
});

const weeks_map: Map<number, Array<Lecture>> = new Map();
json.data.forEach((lecture: Lecture) => {
const start = getWeek(new Date(lecture.start));
if (weeks_map.has(start)) {
weeks_map.get(start)?.push(lecture);
} else {
weeks_map.set(start, [lecture]);
}
});

// TODO: create the actual schedule element
console.log(weeks_map);
};
6 changes: 6 additions & 0 deletions content-scripts/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@ export type AuthSession = {
name: string;
hasNotifications: boolean;
};

// TODO: Add more types as needed
export type Lecture = {
ucs: Array<object>;
start: string;
};