From bc011e47be6083b35d8c0fa1e0a713274bb39f17 Mon Sep 17 00:00:00 2001 From: Daniel Egger Date: Fri, 8 Sep 2023 11:33:42 +0200 Subject: [PATCH] VBV-489: Open external links in new tab in assignment --- .../LearningContentPage.vue | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/client/src/pages/learningPath/learningContentPage/LearningContentPage.vue b/client/src/pages/learningPath/learningContentPage/LearningContentPage.vue index bc3a36f0..079a2a93 100644 --- a/client/src/pages/learningPath/learningContentPage/LearningContentPage.vue +++ b/client/src/pages/learningPath/learningContentPage/LearningContentPage.vue @@ -4,7 +4,7 @@ import { useCircleStore } from "@/stores/circle"; import type { LearningContent } from "@/types"; import * as log from "loglevel"; import type { Ref } from "vue"; -import { onMounted, ref, watch } from "vue"; +import { getCurrentInstance, onMounted, onUpdated, ref, watch } from "vue"; log.debug("LearningContentView created"); @@ -52,6 +52,24 @@ onMounted(async () => { ); await loadLearningContent(); }); + +onUpdated(() => { + const vueInstance = getCurrentInstance(); + if (vueInstance) { + // VBV-489: open external links in new tab + const rootElement: HTMLElement = vueInstance.proxy?.$el; + const anchors = rootElement.querySelectorAll("a"); + anchors.forEach((anchor: HTMLAnchorElement) => { + if ( + /^https?\:\/\//i.test(anchor.href) && + !anchor.href.includes(window.location.hostname) + ) { + anchor.setAttribute("target", "_blank"); + anchor.setAttribute("rel", "noopener noreferrer"); + } + }); + } +});