Add safesearch + Blaze injection

This commit is contained in:
Danny Spina 2023-06-21 01:00:31 +02:00
parent be660aff98
commit 8fe29365d3
3 changed files with 24 additions and 7 deletions

View File

@ -9,6 +9,7 @@ import { parseHTML } 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";
const app = express(); const app = express();
const port = 8888; const port = 8888;
@ -45,7 +46,7 @@ app.get("/", async (req, res) => {
try { try {
const xhr = new XMLHttpRequest(); const xhr = new XMLHttpRequest();
xhr.open("GET", `${searchEngine}?q=${query}`, true); xhr.open("GET", `${searchEngine}?q=${query}&safesearch=moderate`, true);
xhr.setRequestHeader("Accept", "*/*"); xhr.setRequestHeader("Accept", "*/*");
xhr.setRequestHeader("X-Subscription-Token", key); xhr.setRequestHeader("X-Subscription-Token", key);
@ -60,15 +61,11 @@ app.get("/", async (req, res) => {
} }
const data = JSON.parse(xhr.responseText); const data = JSON.parse(xhr.responseText);
const url = process.env.DEV_MODE
? "http://localhost:8888/blazed"
: "https://blaze.cyclic.app/blazed";
// @ts-ignore // @ts-ignore
const results = data.web.results.map( const results = data.web.results.map(
(result: any) => ` (result: any) => `
<article> <article>
<a href="${url}?url=${result.url}"> <a href="${blazeUrl}/blazed?url=${result.url}">
<h2>${result.title}</h2> <h2>${result.title}</h2>
</a> </a>
<span>${result.meta_url.hostname}</span> <span>${result.meta_url.hostname}</span>
@ -135,6 +132,11 @@ app.get("/blazed", async (req, res) => {
</head> </head>
<body> <body>
${article.content} ${article.content}
<script>
${injectBlazeToPageLinks}
const url = "${blazeUrl}"
injectBlazeToPageLinks(url)
</script>
</body></html> </body></html>
`; `;

View File

@ -104,7 +104,8 @@
"skipLibCheck": true /* Skip type checking all .d.ts files. */ "skipLibCheck": true /* Skip type checking all .d.ts files. */
}, },
"include": [ "include": [
"index.ts" "index.ts",
"utils.ts"
], ],
"exclude": [ "exclude": [
"node_modules" "node_modules"

14
utils.ts Normal file
View File

@ -0,0 +1,14 @@
export const blazeUrl = process.env.DEV_MODE
? "http://localhost:8888"
: "https://blaze.cyclic.app";
export function injectBlazeToPageLinks(blazeUrl: string) {
window.addEventListener("DOMContentLoaded", () => {
const links = document.querySelectorAll("a");
console.log(links);
links.forEach((link) => {
const originalHref = link.getAttribute("href");
link.setAttribute("href", `${blazeUrl}/blazed?url=${originalHref}`);
});
});
}