blaze/utils.ts

29 lines
938 B
TypeScript
Raw Normal View History

2023-06-20 23:00:31 +00:00
export const blazeUrl = process.env.DEV_MODE
? "http://localhost:8888"
: "https://blaze.cyclic.app";
2023-06-21 00:26:24 +00:00
export function injectBlazeToPageLinks(blazeUrl: string, currentUrl: string) {
const re = new RegExp("^(http|https)://", "i");
2023-06-20 23:00:31 +00:00
window.addEventListener("DOMContentLoaded", () => {
const links = document.querySelectorAll("a");
console.log(links);
links.forEach((link) => {
2023-06-21 00:26:24 +00:00
let originalHref = link.getAttribute("href");
if (!originalHref) {
return;
}
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) {
const hostname = re.exec(currentUrl)![0];
originalHref = `${hostname}${originalHref}`;
}
2023-06-20 23:00:31 +00:00
link.setAttribute("href", `${blazeUrl}/blazed?url=${originalHref}`);
});
});
}