From a73a03833d845eeda2098bef4f666dec86b1af2e Mon Sep 17 00:00:00 2001 From: Danny Spina Date: Thu, 22 Jun 2023 16:00:46 +0200 Subject: [PATCH] Add search bar in serp + Serve light pages when not blazed + Change some styling --- dist/index.html | 2 +- index.ts | 139 +++++++++++++++++++++++++++++++----------------- utils.ts | 8 +++ 3 files changed, 100 insertions(+), 49 deletions(-) diff --git a/dist/index.html b/dist/index.html index 3a03ffb..f8a5bf7 100644 --- a/dist/index.html +++ b/dist/index.html @@ -42,7 +42,7 @@
-

Blaze

+

BLA⚡E

{ +
+ +
+
${results.join("")} + `; @@ -107,66 +123,93 @@ app.get("/blazed", async (req, res) => { const pageToBlaze = req.query.url as string; try { - const response = await got(pageToBlaze, { - headers: { Accept: "text/html" }, - }); - const { document } = parseHTML(response.body); + const xhr = new XMLHttpRequest(); + xhr.open("GET", pageToBlaze, true); + xhr.setRequestHeader("Accept", "text/html"); - // 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 (!isProbablyReaderable(document)) { - // TODO: send minimalized version of the page instead the read mode - // implementation draft: - // document.querySelectorAll("link").forEach((l) => { - // l.remove(); - // }); + if (xhr.status === 404) { + res.sendFile(path.join(__dirname, "/dist/404.html")); + return; + } - // document.querySelectorAll("style").forEach((s) => { - // s.remove; - // }); + if (xhr.status !== 200) { + console.error("XHR request failed:", xhr.status, xhr.statusText); + return; + } - // document.querySelectorAll("script").forEach((s) => { - // s.remove(); - // }); + const response = xhr.responseText; + const { document } = parseHTML(response); - // // @ts-ignore - // const jsonDocument = document.toJSON(); + if (!isProbablyReaderable(document)) { + // TODO: still a lot of bugs, must be refined to handle some cases, like + // cookie banners, etc. + document.querySelectorAll("link").forEach((l) => { + l.remove(); + }); - // const cleanDocument = parseJSON(jsonDocument); - // return res.send(document.toString()); + document.querySelectorAll("style").forEach((s) => { + s.remove; + }); - return res.sendFile(path.join(__dirname, "/dist/not_blazed.html")); - } + document.querySelectorAll("script").forEach((s) => { + s.remove(); + }); - //TODO: find if there are more performant ways to remove images or evaluate if is the case to remove images - document.querySelectorAll("img").forEach((img) => img.remove()); + const blazeDisclaimer = document.createElement("div"); + blazeDisclaimer.style.width = "100dvw"; + blazeDisclaimer.style.border = "1px solid red"; + blazeDisclaimer.style.padding = "1rem"; + blazeDisclaimer.innerHTML = ` +

BLAZE INFO

+

+ The page you are seeing could not be correctly blazed due to these webpage characteristics. + Blaze served anyway a lightweight version of the page. + Keep in mind that this kind of pages can be hard or even impossible to use, read or understand. +

+ `; - const reader = new Readability(document); - const article = reader.parse(); + const referenceElement = document.body.firstChild; + document.body.insertBefore(blazeDisclaimer, referenceElement); - if (!article) { - return res.send("Something went wrong"); - } + return res.send(document.toString()); + } - const blazedPage = ` - - - - - - ${article.content} - - - `; + //TODO: find if there are more performant ways to remove images or evaluate if is the case to remove images + document.querySelectorAll("img").forEach((img) => img.remove()); - const minifiedBlazedPage = minify(blazedPage, minifierOptions); + const reader = new Readability(document); + const article = reader.parse(); - res.send(minifiedBlazedPage); + if (!article) { + return res.send("Something went wrong"); + } + + const blazedPage = ` + + + + + + ${article.content} + + + `; + + const minifiedBlazedPage = minify(blazedPage, minifierOptions); + + res.send(minifiedBlazedPage); + }; + xhr.send(); } catch (err) { console.log(err); } diff --git a/utils.ts b/utils.ts index a1dd7d2..7dcbb99 100644 --- a/utils.ts +++ b/utils.ts @@ -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); + }); +}