From 8fe29365d34eb2c1556967ba602b8d7da436ed79 Mon Sep 17 00:00:00 2001 From: Danny Spina Date: Wed, 21 Jun 2023 01:00:31 +0200 Subject: [PATCH] Add safesearch + Blaze injection --- index.ts | 14 ++++++++------ tsconfig.json | 3 ++- utils.ts | 14 ++++++++++++++ 3 files changed, 24 insertions(+), 7 deletions(-) create mode 100644 utils.ts diff --git a/index.ts b/index.ts index 8bdc407..c09b20b 100644 --- a/index.ts +++ b/index.ts @@ -9,6 +9,7 @@ import { parseHTML } from "linkedom"; import XHR2 from "xhr2"; const XMLHttpRequest = XHR2.XMLHttpRequest; import { minify } from "html-minifier"; +import { blazeUrl, injectBlazeToPageLinks } from "./utils.js"; const app = express(); const port = 8888; @@ -45,7 +46,7 @@ app.get("/", async (req, res) => { try { 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("X-Subscription-Token", key); @@ -60,15 +61,11 @@ app.get("/", async (req, res) => { } const data = JSON.parse(xhr.responseText); - const url = process.env.DEV_MODE - ? "http://localhost:8888/blazed" - : "https://blaze.cyclic.app/blazed"; - // @ts-ignore const results = data.web.results.map( (result: any) => `
- +

${result.title}

${result.meta_url.hostname} @@ -135,6 +132,11 @@ app.get("/blazed", async (req, res) => { ${article.content} + `; diff --git a/tsconfig.json b/tsconfig.json index 17b6959..c3d5359 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -104,7 +104,8 @@ "skipLibCheck": true /* Skip type checking all .d.ts files. */ }, "include": [ - "index.ts" + "index.ts", + "utils.ts" ], "exclude": [ "node_modules" diff --git a/utils.ts b/utils.ts new file mode 100644 index 0000000..58c3bd0 --- /dev/null +++ b/utils.ts @@ -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}`); + }); + }); +}