Fix blaze injection

This commit is contained in:
Danny Spina 2023-06-21 02:26:24 +02:00
parent 8fe29365d3
commit 76bf19e6a8
3 changed files with 69 additions and 4 deletions

27
dist/404.html vendored Normal file
View File

@ -0,0 +1,27 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<link rel="icon" type="image/x-icon" href="/favicon.svg" />
<title>Blaze - 404</title>
<style>
body {
font-family: sans-serif;
text-align: center;
display: flex;
align-items: center;
justify-content: center;
height: 100dvh;
margin: 0;
padding: 0;
flex-direction: column;
}
</style>
</head>
<body>
<h1>404</h1>
<br />
<a href="#" role="button" onclick="history.back()">Go back</a>
</body>
</html>

View File

@ -4,7 +4,7 @@ import got from "got";
import path from "path"; import path from "path";
import { fileURLToPath } from "url"; import { fileURLToPath } from "url";
import "dotenv/config"; import "dotenv/config";
import { parseHTML } from "linkedom"; import { parseHTML, parseJSON } from "linkedom";
// @ts-ignore // @ts-ignore
import XHR2 from "xhr2"; import XHR2 from "xhr2";
const XMLHttpRequest = XHR2.XMLHttpRequest; const XMLHttpRequest = XHR2.XMLHttpRequest;
@ -111,7 +111,30 @@ app.get("/blazed", async (req, res) => {
headers: { Accept: "text/html" }, headers: { Accept: "text/html" },
}); });
const { document } = parseHTML(response.body); 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)) { 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")); return res.sendFile(path.join(__dirname, "/dist/not_blazed.html"));
} }
@ -135,7 +158,8 @@ app.get("/blazed", async (req, res) => {
<script> <script>
${injectBlazeToPageLinks} ${injectBlazeToPageLinks}
const url = "${blazeUrl}" const url = "${blazeUrl}"
injectBlazeToPageLinks(url) const currentUrl = "${req.query.url}"
injectBlazeToPageLinks(url, currentUrl)
</script> </script>
</body></html> </body></html>
`; `;

View File

@ -2,12 +2,26 @@ export const blazeUrl = process.env.DEV_MODE
? "http://localhost:8888" ? "http://localhost:8888"
: "https://blaze.cyclic.app"; : "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", () => { window.addEventListener("DOMContentLoaded", () => {
const links = document.querySelectorAll("a"); const links = document.querySelectorAll("a");
console.log(links); console.log(links);
links.forEach((link) => { 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}`); link.setAttribute("href", `${blazeUrl}/blazed?url=${originalHref}`);
}); });
}); });