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>
<body>
<section>
<h1>Blaze</h1>
<h1>BLA⚡E</h1>
<input
name="u"
id="u"

View File

@ -9,7 +9,11 @@ import { parseHTML, parseJSON } from "linkedom";
import XHR2 from "xhr2";
const XMLHttpRequest = XHR2.XMLHttpRequest;
import { minify } from "html-minifier";
import { blazeUrl, injectBlazeToPageLinks } from "./utils.js";
import {
blazeFunctionality,
blazeUrl,
injectBlazeToPageLinks,
} from "./utils.js";
const app = express();
const port = 8888;
@ -88,7 +92,19 @@ app.get("/", async (req, res) => {
</style>
</head>
<body>
<header>
<label>
<a href="/"><strong>BLAZE</strong></a>
<input type="search" value="${query}" />
<button>Blaze it</button>
</label>
</header>
<hr/>
${results.join("")}
<script>
${blazeFunctionality}
blazeFunctionality('${blazeUrl}')
</script>
</body>
</html>
`;
@ -107,35 +123,60 @@ 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 (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)) {
// TODO: send minimalized version of the page instead the read mode
// implementation draft:
// document.querySelectorAll("link").forEach((l) => {
// l.remove();
// });
// TODO: still a lot of bugs, must be refined to handle some cases, like
// cookie banners, etc.
document.querySelectorAll("link").forEach((l) => {
l.remove();
});
// document.querySelectorAll("style").forEach((s) => {
// s.remove;
// });
document.querySelectorAll("style").forEach((s) => {
s.remove;
});
// document.querySelectorAll("script").forEach((s) => {
// s.remove();
// });
document.querySelectorAll("script").forEach((s) => {
s.remove();
});
// // @ts-ignore
// const jsonDocument = document.toJSON();
const blazeDisclaimer = document.createElement("div");
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);
// return res.send(document.toString());
const referenceElement = document.body.firstChild;
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
@ -167,6 +208,8 @@ app.get("/blazed", async (req, res) => {
const minifiedBlazedPage = minify(blazedPage, minifierOptions);
res.send(minifiedBlazedPage);
};
xhr.send();
} catch (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);
});
}