Skip to content

Commit

Permalink
Replace 404 page with general error page
Browse files Browse the repository at this point in the history
  • Loading branch information
mstieranka committed Oct 22, 2023
1 parent 7f2cf1b commit f2504d8
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 85 deletions.
115 changes: 61 additions & 54 deletions src/content/end.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { MessageType } from "../messages";
import { ExtensionSettings } from "../settings";
import { Course } from "./pages/Course/Course";
import { Err404 } from "./pages/Err404";
import { ErrorPage } from "./pages/Error/Error";
import { Exam } from "./pages/Exam";
import { Logged } from "./pages/Logged";
import { Login } from "./pages/Login";
Expand Down Expand Up @@ -38,65 +38,72 @@ const main = async (settings: ExtensionSettings) => {
document.body.removeAttribute("text");

let page: Page;
if (document.body.innerHTML == "") {
page = new Err404(settings);
} else if (document.querySelector("select[name=UID_UNIVERSITY]") != null) {
page = new Login();
} else if (args.has("X")) {
switch (args.get("X")) {
case "FAQ":
case "Preset":
case "CompilersDryRuns":
case "Extra":
case "KNT":
case "TaskGrp": {
page = new Logged(settings);
break;
}
case "KNTQ": {
page = new Exam(settings);
break;
}
case "Course": {
page = new Course(settings);
break;
}
case "Results": {
page = new Results(settings);
break;
}
case "Compiler":
case "DryRun":
case "Task":
case "TaskU": {
page = new Task(settings);
break;
}
case "Main": {
page = new Main(settings);
break;
}
default: {
// determine if site is really main
const navlink =
document.querySelector<HTMLSpanElement>("span.navlink"); // first time login
if (
document.querySelector(
'span.navLink > a.navLink[href="?X=Main"]',
) ||
(navlink && navlink.innerText.includes("Než"))
) {
try {
if (document.querySelector("select[name=UID_UNIVERSITY]") != null) {
page = new Login();
} else if (args.has("X")) {
switch (args.get("X")) {
case "FAQ":
case "Preset":
case "CompilersDryRuns":
case "Extra":
case "KNT":
case "TaskGrp": {
page = new Logged(settings);
} else {
break;
}
case "KNTQ": {
page = new Exam(settings);
break;
}
case "Course": {
page = new Course(settings);
break;
}
case "Results": {
page = new Results(settings);
break;
}
case "Compiler":
case "DryRun":
case "Task":
case "TaskU": {
page = new Task(settings);
break;
}
case "Main": {
page = new Main(settings);
break;
}
default: {
// determine if site is really main
const navlink =
document.querySelector<HTMLSpanElement>("span.navlink"); // first time login
if (
document.querySelector(
'span.navLink > a.navLink[href="?X=Main"]',
) ||
(navlink && navlink.innerText.includes("Než"))
) {
page = new Logged(settings);
} else {
page = new Main(settings);
}
}
}
} else {
page = new Main(settings);
}
} else {
page = new Main(settings);
}

await page.initialise();
await page.initialise();
} catch (e) {
if (e instanceof Error) {
page = new ErrorPage(e);
await page.initialise();
} else {
throw e;
}
}
return page;
};

Expand Down
31 changes: 0 additions & 31 deletions src/content/pages/Err404.ts

This file was deleted.

38 changes: 38 additions & 0 deletions src/content/pages/Error/Error.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<script lang="ts">
export let error: Error;
const goBack = () => {
history.back();
return false;
};
</script>

<main>
<h1>An unexpected error occurred</h1>
<p>The following error has occurred:</p>
<code>{error.toString()}</code>
{#if error.stack}
{#each error.stack.split("\n") as line}
<code>{line}</code>
{/each}
{/if}
<p>
Try <a href={document.referrer} on:click={goBack}>going back</a>, if
that doesn't help,
<a href="https://github.com/keombre/progtest-theme/issues"
>file an issue on GitHub</a
>.
</p>
</main>

<style>
code {
display: block;
}
main {
font-size: 14px;
max-width: 640px;
margin: 0 auto;
}
</style>
22 changes: 22 additions & 0 deletions src/content/pages/Error/Error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Page } from "../Page";
import ErrorComponent from "./Error.svelte";

export class ErrorPage implements Page {
constructor(private error: Error) {}

async initialise() {
document.title = "Error | ProgTest";

const center = document.querySelector<HTMLElement>("body > center");
if (center) {
center.style.display = "none";
}

const container = document.createElement("div");
document.body.insertBefore(container, center);
new ErrorComponent({
target: container,
props: { error: this.error },
});
}
}

0 comments on commit f2504d8

Please sign in to comment.