Skip to content

Commit 66700a2

Browse files
committed
[change] Relative URL for Notification links
1 parent b8af7eb commit 66700a2

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

openwisp_notifications/static/openwisp-notifications/js/notifications.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ function notificationWidget($) {
197197
function notificationListItem(elem) {
198198
let klass,
199199
datetime = dateTimeStampToDateTimeLocaleString(new Date(elem.timestamp));
200+
const target_url = new URL(elem.target_url);
200201

201202
if (!notificationReadStatus.has(elem.id)) {
202203
if (elem.unread) {
@@ -207,8 +208,22 @@ function notificationWidget($) {
207208
}
208209
klass = notificationReadStatus.get(elem.id);
209210

211+
function convertMessageWithRelativeURL(htmlString) {
212+
const parser = new DOMParser();
213+
const doc = parser.parseFromString(htmlString, "text/html");
214+
const links = doc.querySelectorAll("a");
215+
links.forEach((link) => {
216+
const absoluteUrl = link.getAttribute("href");
217+
if (absoluteUrl) {
218+
const url = new URL(absoluteUrl);
219+
link.setAttribute("href", url.pathname);
220+
}
221+
});
222+
return doc.body.innerHTML;
223+
}
224+
210225
return `<div class="ow-notification-elem ${klass}" id=ow-${elem.id}
211-
data-location="${elem.target_url}" role="link" tabindex="0">
226+
data-location="${target_url.pathname}" role="link" tabindex="0">
212227
<div class="ow-notification-inner">
213228
<div class="ow-notification-meta">
214229
<div class="ow-notification-level-wrapper">
@@ -217,7 +232,7 @@ function notificationWidget($) {
217232
</div>
218233
<div class="ow-notification-date">${datetime}</div>
219234
</div>
220-
${elem.message}
235+
${convertMessageWithRelativeURL(elem.message)}
221236
</div>
222237
</div>`;
223238
}

0 commit comments

Comments
 (0)