Add search bar in serp + Serve light pages when not blazed + Change some styling

This commit is contained in:
Danny Spina 2023-06-22 16:00:46 +02:00
parent 2f77ed66a2
commit a73a03833d
3 changed files with 100 additions and 49 deletions

2
dist/index.html vendored
View File

@ -42,7 +42,7 @@
</head> </head>
<body> <body>
<section> <section>
<h1>Blaze</h1> <h1>BLA⚡E</h1>
<input <input
name="u" name="u"
id="u" id="u"

View File

@ -9,7 +9,11 @@ import { parseHTML, parseJSON } from "linkedom";
import XHR2 from "xhr2"; import XHR2 from "xhr2";
const XMLHttpRequest = XHR2.XMLHttpRequest; const XMLHttpRequest = XHR2.XMLHttpRequest;
import { minify } from "html-minifier"; import { minify } from "html-minifier";
import { blazeUrl, injectBlazeToPageLinks } from "./utils.js"; import {
blazeFunctionality,
blazeUrl,
injectBlazeToPageLinks,
} from "./utils.js";
const app = express(); const app = express();
const port = 8888; const port = 8888;
@ -88,7 +92,19 @@ app.get("/", async (req, res) => {
</style> </style>
</head> </head>
<body> <body>
<header>
<label>
<a href="/"><strong>BLAZE</strong></a>
<input type="search" value="${query}" />
<button>Blaze it</button>
</label>
</header>
<hr/>
${results.join("")} ${results.join("")}
<script>
${blazeFunctionality}
blazeFunctionality('${blazeUrl}')
</script>
</body> </body>
</html> </html>
`; `;
@ -107,35 +123,60 @@ app.get("/blazed", async (req, res) => {
const pageToBlaze = req.query.url as string; const pageToBlaze = req.query.url as string;
try { try {
const response = await got(pageToBlaze, { const xhr = new XMLHttpRequest();
headers: { Accept: "text/html" }, xhr.open("GET", pageToBlaze, true);
}); xhr.setRequestHeader("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 xhr.onreadystatechange = async () => {
if (xhr.readyState !== 4) {
return;
}
if (xhr.status === 404) {
res.sendFile(path.join(__dirname, "/dist/404.html"));
return;
}
if (xhr.status !== 200) {
console.error("XHR request failed:", xhr.status, xhr.statusText);
return;
}
const response = xhr.responseText;
const { document } = parseHTML(response);
if (!isProbablyReaderable(document)) { if (!isProbablyReaderable(document)) {
// TODO: send minimalized version of the page instead the read mode // TODO: still a lot of bugs, must be refined to handle some cases, like
// implementation draft: // cookie banners, etc.
// document.querySelectorAll("link").forEach((l) => { document.querySelectorAll("link").forEach((l) => {
// l.remove(); l.remove();
// }); });
// document.querySelectorAll("style").forEach((s) => { document.querySelectorAll("style").forEach((s) => {
// s.remove; s.remove;
// }); });
// document.querySelectorAll("script").forEach((s) => { document.querySelectorAll("script").forEach((s) => {
// s.remove(); s.remove();
// }); });
// // @ts-ignore const blazeDisclaimer = document.createElement("div");
// const jsonDocument = document.toJSON(); blazeDisclaimer.style.width = "100dvw";
blazeDisclaimer.style.border = "1px solid red";
blazeDisclaimer.style.padding = "1rem";
blazeDisclaimer.innerHTML = `
<h2 style="text-align: center">BLAZE INFO</h2>
<p>
The page you are seeing <strong>could not be correctly blazed</strong> due to these webpage characteristics.
<strong>Blaze served anyway</strong> a lightweight version of the page.
Keep in mind that this kind of pages <strong>can be hard or even impossible to use, read or understand</strong>.
</p>
`;
// const cleanDocument = parseJSON(jsonDocument); const referenceElement = document.body.firstChild;
// return res.send(document.toString()); document.body.insertBefore(blazeDisclaimer, referenceElement);
return res.sendFile(path.join(__dirname, "/dist/not_blazed.html")); return res.send(document.toString());
} }
//TODO: find if there are more performant ways to remove images or evaluate if is the case to remove images //TODO: find if there are more performant ways to remove images or evaluate if is the case to remove images
@ -167,6 +208,8 @@ app.get("/blazed", async (req, res) => {
const minifiedBlazedPage = minify(blazedPage, minifierOptions); const minifiedBlazedPage = minify(blazedPage, minifierOptions);
res.send(minifiedBlazedPage); res.send(minifiedBlazedPage);
};
xhr.send();
} catch (err) { } catch (err) {
console.log(err); console.log(err);
} }

View File

@ -23,3 +23,11 @@ export function injectBlazeToPageLinks(blazeUrl: string, currentUrl: string) {
}); });
}); });
} }
export function blazeFunctionality(blazeUrl: string) {
const t = document.querySelector("button"),
c = document.querySelector("input");
t!.addEventListener("click", () => {
location.href = blazeUrl + "?q=" + encodeURI(c.value);
});
}