Fix bug link injection

This commit is contained in:
Danny Spina 2023-06-22 15:11:10 +02:00
parent 923f999ba1
commit 2f77ed66a2

View File

@ -3,10 +3,10 @@ export const blazeUrl = process.env.DEV_MODE
: "https://blaze.cyclic.app"; : "https://blaze.cyclic.app";
export function injectBlazeToPageLinks(blazeUrl: string, currentUrl: string) { export function injectBlazeToPageLinks(blazeUrl: string, currentUrl: string) {
const url = new URL(currentUrl);
const re = new RegExp("^(http|https)://", "i"); const re = new RegExp("^(http|https)://", "i");
window.addEventListener("DOMContentLoaded", () => { window.addEventListener("DOMContentLoaded", () => {
const links = document.querySelectorAll("a"); const links = document.querySelectorAll("a");
console.log(links);
links.forEach((link) => { links.forEach((link) => {
let originalHref = link.getAttribute("href"); let originalHref = link.getAttribute("href");
@ -15,11 +15,8 @@ export function injectBlazeToPageLinks(blazeUrl: string, currentUrl: string) {
} }
const isAbsoluteLink = re.test(originalHref); const isAbsoluteLink = re.test(originalHref);
// TODO: is still buggy, probably some href with h,t,p,s inside are detected as false positives
// generating an original href like "https:///"
if (!isAbsoluteLink) { if (!isAbsoluteLink) {
const hostname = re.exec(currentUrl)![0]; originalHref = `${url.protocol}//${url.hostname}${originalHref}`;
originalHref = `${hostname}${originalHref}`;
} }
link.setAttribute("href", `${blazeUrl}/blazed?url=${originalHref}`); link.setAttribute("href", `${blazeUrl}/blazed?url=${originalHref}`);