Skip to content

Commit

Permalink
migrate index to typescript and add regex
Browse files Browse the repository at this point in the history
  • Loading branch information
toni-santos committed Oct 7, 2024
1 parent 5921500 commit 72ab8fc
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 81 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,7 @@ yarn lint:fix
</td>
</tr>
</table>

## Components Page

Checkout the components page by adding `/components` to the end of any Sigarra URL.
79 changes: 0 additions & 79 deletions content-scripts/index.js

This file was deleted.

155 changes: 155 additions & 0 deletions content-scripts/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
import {
injectOverrideFunctions,
reverseDateDirection,
currentAccountPage,
addSortTableActions,
} from "./modules/initialize";
import { injectAllChanges, userPreferences } from "./modules/options";
import constructNewData from "./modules/utilities/constructNewData";
import { getStorage, setStorage } from "./modules/utilities/storage";
import { rememberLogin } from "./modules/login";
import { replaceIcons } from "./modules/icons";
import { teacherPage } from "./pages/teacher_page";
import { classPage } from "./pages/class_page";
import { improveSchedule } from "./modules/schedule";
import { changeCourseCards, changeProfileRow } from "./pages/profile_page";
import { courseUnitPage } from "./pages/course_unit_page";
import { fixPagination } from "./modules/pagination";
import { changeLayout } from "./modules/layout";
import { addStarIconToCard } from "./modules/favorite-course";
import { createComponentsPage } from "./pages/components_page";

const GENERIC_SIGARRA_URL: string = "^https://sigarra.up.pt/feup/(pt|en)/";
const BASE_SIGARRA_URL: string = GENERIC_SIGARRA_URL + "(?!.*/components)";
const COMPONENTS_URL: string = GENERIC_SIGARRA_URL + "(?=.*/components)";
const STUDENT_PAGE_URL: string =
BASE_SIGARRA_URL +
"fest_geral.(cursos_list|curso_percurso_academico_view|curso_posicao_plano_view|ucurr_inscricoes_list|estatutos_regimes_view|info_ingresso_view|info_pessoal_completa_view).*$";

/*--
- Docs: https://developer.chrome.com/docs/extensions/reference/storage/#synchronous-response-to-storage-updates
- Listen to Chrome Storage changes
- Inject styles in respond to changes
--*/
chrome.storage.onChanged.addListener((changes) => {
const newChangesData = constructNewData(changes);
rememberLogin();
injectAllChanges(newChangesData);
});

/*--
- Initializing function, runs once at start
- Get Chrome Storage and inject respective styles
--*/

const functionsToExecute: Array<{
name: string;
func: () => void;
regex: RegExp;
}> = [
{
name: "changeLayout",
func: changeLayout,
regex: new RegExp(BASE_SIGARRA_URL + ".*$"),
},
{
name: "reverseDateDirection",
func: reverseDateDirection,
regex: new RegExp(BASE_SIGARRA_URL + ".*$"),
},
{
name: "currentAccountPage",
func: currentAccountPage,
regex: new RegExp(
BASE_SIGARRA_URL +
"gpag_ccorrente_geral.conta_corrente_view?pct_cod=.*$",
),
},
{
name: "addSortTableActions",
func: addSortTableActions,
regex: new RegExp(BASE_SIGARRA_URL + ".*$"),
},
{
name: "replaceIcons",
func: replaceIcons,
regex: new RegExp(BASE_SIGARRA_URL + ".*$"),
},
{
name: "improveSchedule",
func: improveSchedule,
regex: new RegExp(BASE_SIGARRA_URL + "hor_geral.estudantes_view.*$"),
},
{
name: "changeProfileRow",
func: changeProfileRow,
regex: new RegExp(STUDENT_PAGE_URL),
},
{
name: "changeCourseCards",
func: changeCourseCards,
regex: new RegExp(STUDENT_PAGE_URL),
},
{
name: "fixPagination",
func: fixPagination,
regex: new RegExp(BASE_SIGARRA_URL + ".*$"),
},
{
name: "teacherPage",
func: teacherPage,
regex: new RegExp(BASE_SIGARRA_URL + "func_geral.formview.*$"),
},
{
name: "classPage",
func: classPage,
regex: new RegExp(
BASE_SIGARRA_URL + "it_listagem.lista_turma_disciplina.*$",
),
},
{
name: "courseUnitPage",
func: courseUnitPage,
regex: new RegExp(BASE_SIGARRA_URL + "ucurr_geral.ficha_uc_view.*$"),
},
{
name: "injectOverrideFunctions",
func: injectOverrideFunctions,
regex: new RegExp(BASE_SIGARRA_URL + ".*$"),
},
{
name: "addStarIconToCard",
func: addStarIconToCard,
regex: new RegExp(STUDENT_PAGE_URL),
},
{
name: "componentsPage",
func: createComponentsPage,
regex: new RegExp(COMPONENTS_URL),
},
];

const init = async (): Promise<void> => {
// Inject user preferences
const data = await getStorage(userPreferences);
injectAllChanges(data);

if (!(await getStorage("favorite_courses"))) {
await setStorage({ favorite_courses: "{}" }); //Insert empty object
}

functionsToExecute.forEach((f) => {
try {
if (f.regex.test(document.location.href)) f.func();
} catch (error) {
console.error(`Error running ${f.name} init function!\n`);
console.error(error);
}
});
// we run rememberLogin at last, because it's async
// TODO (luisd): make a better mechanism for functions that depend on previous
// steps and might be async
await rememberLogin(data);
};

init();
2 changes: 1 addition & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ const config = {
},
},
entry: {
"base/content-scripts": "./content-scripts/index.js",
"base/content-scripts": "./content-scripts/index.ts",
"base/background": "./background.js",
},
output: { path: path.resolve("dist"), filename: "[name].js" },
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3858,7 +3858,7 @@ __metadata:
spdy: "npm:^4.0.2"
uglify-js: "npm:^3.15.1"
bin:
serve: index.js
serve: index.ts
checksum: 10c0/7d713b8eac69222f7987929db844277948deb2684b39e96712c890165a34d5d76d96cd3690f7cfd7c4b68720b1f9e61b68daacb63994f2077140e9bbb8ce25d0
languageName: node
linkType: hard
Expand Down

0 comments on commit 72ab8fc

Please sign in to comment.