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}`); + }); + }); +}