diff --git a/src/routes/LyntContents.svelte b/src/routes/LyntContents.svelte index 8ae5fd8..980d83e 100644 --- a/src/routes/LyntContents.svelte +++ b/src/routes/LyntContents.svelte @@ -14,6 +14,7 @@ import Report from './Report.svelte'; import { createEventDispatcher } from 'svelte'; import DOMPurify from 'dompurify'; + import { page } from '$app/stores'; function getTimeElapsed(date: Date | string) { if (typeof date === 'string') date = new Date(date); @@ -84,7 +85,8 @@ let contentElement: HTMLSpanElement | null = null; content = content!; let clickingExternalLink = false; - let openExternalLink = false; + // I can just use this in the listener and that should work. + let externalLink: URL | null = null; const formattedDate = formatDateTooltip(createdAt); @@ -121,22 +123,30 @@ $: if (contentElement) { // this shouldn't have any XSS vulnerabilities. Or at least, hopefully... - const sanitizedContent: string = DOMPurify.sanitize(content); + const sanitizedContent: string = DOMPurify.sanitize(content, { + USE_PROFILES: { + html: true + }, + }); const links = sanitizedContent.match(/[A-z|A-Z|a-z]+:\/\/[^\s]+/gm); if (links) for (const link of links) { + const linkHTML = `${link}`; + console.log("link", link, "linkHTML", linkHTML); + // FIXME: it may "double replace" the link, which isn't good. contentElement.innerHTML = contentElement.innerHTML.replace( link, - `${link}` + linkHTML ); - const a = contentElement.querySelector("a"); - a?.addEventListener("click", async (e) => { - e.preventDefault(); - if (new URL(a.href).host !== window.location.host) { + console.log("innerHTML after replace", contentElement.innerHTML) + const a = contentElement.querySelectorAll('a'); + a.forEach(el => el.addEventListener("click", (e) => { + if (new URL(el.href).host !== $page.url.host) { + e.preventDefault(); + externalLink = new URL(link); clickingExternalLink = true; } - return false; - }) + })) } } @@ -149,7 +159,7 @@ {/if} {#if clickingExternalLink} - + This link leads to a external website. + Are you sure you want to open it? - openExternalLink = true}>Open - openExternalLink = false}>Return to Lyntr + { + if (!externalLink) + return console.error("externalLink is null"); + window.open(externalLink) + externalLink = null + }}>Open + { + clickingExternalLink = false + }}>Return to Lyntr