diff --git a/dist/404.html b/dist/404.html new file mode 100644 index 0000000..4cd37c4 --- /dev/null +++ b/dist/404.html @@ -0,0 +1,27 @@ + + + + + + + Blaze - 404 + + + +

404

+
+ Go back + + diff --git a/index.ts b/index.ts index c09b20b..1220dec 100644 --- a/index.ts +++ b/index.ts @@ -4,7 +4,7 @@ import got from "got"; import path from "path"; import { fileURLToPath } from "url"; import "dotenv/config"; -import { parseHTML } from "linkedom"; +import { parseHTML, parseJSON } from "linkedom"; // @ts-ignore import XHR2 from "xhr2"; const XMLHttpRequest = XHR2.XMLHttpRequest; @@ -111,7 +111,30 @@ app.get("/blazed", async (req, res) => { headers: { Accept: "text/html" }, }); const { document } = parseHTML(response.body); + + // TODO: missing handling of 404. The idea is to send the blaze 404 page, otherwise will show error page on client + if (!isProbablyReaderable(document)) { + // TODO: send minimalized version of the page instead the read mode + // implementation draft: + // document.querySelectorAll("link").forEach((l) => { + // l.remove(); + // }); + + // document.querySelectorAll("style").forEach((s) => { + // s.remove; + // }); + + // document.querySelectorAll("script").forEach((s) => { + // s.remove(); + // }); + + // // @ts-ignore + // const jsonDocument = document.toJSON(); + + // const cleanDocument = parseJSON(jsonDocument); + // return res.send(document.toString()); + return res.sendFile(path.join(__dirname, "/dist/not_blazed.html")); } @@ -135,7 +158,8 @@ app.get("/blazed", async (req, res) => { `; diff --git a/utils.ts b/utils.ts index 58c3bd0..1c57238 100644 --- a/utils.ts +++ b/utils.ts @@ -2,12 +2,26 @@ export const blazeUrl = process.env.DEV_MODE ? "http://localhost:8888" : "https://blaze.cyclic.app"; -export function injectBlazeToPageLinks(blazeUrl: string) { +export function injectBlazeToPageLinks(blazeUrl: string, currentUrl: string) { + const re = new RegExp("^(http|https)://", "i"); window.addEventListener("DOMContentLoaded", () => { const links = document.querySelectorAll("a"); console.log(links); links.forEach((link) => { - const originalHref = link.getAttribute("href"); + let originalHref = link.getAttribute("href"); + + if (!originalHref) { + return; + } + + const isAbsoluteLink = re.test(originalHref); + // TODO: is still buggy, probably some href with h,t,p,s inside are detected as false positives + // generating an original href like "https:///" + if (!isAbsoluteLink) { + const hostname = re.exec(currentUrl)![0]; + originalHref = `${hostname}${originalHref}`; + } + link.setAttribute("href", `${blazeUrl}/blazed?url=${originalHref}`); }); });