From e9ee272ece374215e1dc1a4925ef8a12886d70a8 Mon Sep 17 00:00:00 2001
From: Jonathan Weth <git@jonathanweth.de>
Date: Fri, 13 Jan 2023 20:27:51 +0100
Subject: [PATCH] Open external links within iframe in _blank target

---
 aleksis/core/templates/core/base.html | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)

diff --git a/aleksis/core/templates/core/base.html b/aleksis/core/templates/core/base.html
index 6c35b2b94..961d58306 100644
--- a/aleksis/core/templates/core/base.html
+++ b/aleksis/core/templates/core/base.html
@@ -83,6 +83,33 @@
       window.parent.postMessage({height: $(document).height()});
     };
     window.onresize = documentResizePostMessage;
+
+    function findLink(el) {
+      if (el.href) {
+        return el.href;
+      } else if (el.parentElement) {
+        return findLink(el.parentElement);
+      } else {
+        return null;
+      }
+    };
+
+    function clickCallback(e) {
+      const link = findLink(e.target);
+      if (link == null) {
+        return;
+      }
+
+      const newUrl = new URL(link);
+      const currentUrl = new URL(window.location.href);
+      if(newUrl.origin !== currentUrl.origin) {
+        console.debug("External link clicked. Redirecting to " + link + " in new tab.");
+        e.preventDefault();
+        window.open(link, '_blank');
+      }
+    };
+
+    document.addEventListener('click', clickCallback, false);
   })
 </script>
 </body>
-- 
GitLab