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

Better script running #194

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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
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