From 66700a216b048136972f657b05e41f1db8aae1f1 Mon Sep 17 00:00:00 2001 From: Dhanus Date: Fri, 8 Mar 2024 13:47:24 +0530 Subject: [PATCH] [change] Relative URL for Notification links --- .../js/notifications.js | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/openwisp_notifications/static/openwisp-notifications/js/notifications.js b/openwisp_notifications/static/openwisp-notifications/js/notifications.js index 6311f38a..b670a582 100644 --- a/openwisp_notifications/static/openwisp-notifications/js/notifications.js +++ b/openwisp_notifications/static/openwisp-notifications/js/notifications.js @@ -197,6 +197,7 @@ function notificationWidget($) { function notificationListItem(elem) { let klass, datetime = dateTimeStampToDateTimeLocaleString(new Date(elem.timestamp)); + const target_url = new URL(elem.target_url); if (!notificationReadStatus.has(elem.id)) { if (elem.unread) { @@ -207,8 +208,22 @@ function notificationWidget($) { } klass = notificationReadStatus.get(elem.id); + function convertMessageWithRelativeURL(htmlString) { + const parser = new DOMParser(); + const doc = parser.parseFromString(htmlString, "text/html"); + const links = doc.querySelectorAll("a"); + links.forEach((link) => { + const absoluteUrl = link.getAttribute("href"); + if (absoluteUrl) { + const url = new URL(absoluteUrl); + link.setAttribute("href", url.pathname); + } + }); + return doc.body.innerHTML; + } + return `
+ data-location="${target_url.pathname}" role="link" tabindex="0">
@@ -217,7 +232,7 @@ function notificationWidget($) {
${datetime}
- ${elem.message} + ${convertMessageWithRelativeURL(elem.message)}
`; }