Enhance RocketChat livechat initialization and improve smooth scrolling in BasicScripts

- Refactored the RocketChat livechat widget initialization to prevent multiple initializations and ensure proper loading of the script, improving performance and user experience.
- Implemented a mechanism in the BasicScripts component to manage event listeners for smooth scrolling, allowing for cleanup of old handlers to prevent memory leaks.
This commit is contained in:
2025-11-06 12:16:25 +01:00
parent a2c3475850
commit c38120053c
2 changed files with 101 additions and 28 deletions

View File

@@ -175,11 +175,20 @@ import { UI } from 'astrowind:config';
onPageShow();
});
// Store smooth scrolling handlers to allow cleanup
let smoothScrollHandlers = new WeakMap();
// Handle smooth scrolling for anchor links across all pages
function setupSmoothScrolling() {
// Handle links that start with # (pure anchor links)
document.querySelectorAll('a[href^="#"]:not([href="#"])').forEach((anchor) => {
anchor.addEventListener('click', function (e) {
// Remove old handler if exists
const oldHandler = smoothScrollHandlers.get(anchor);
if (oldHandler) {
anchor.removeEventListener('click', oldHandler);
}
const handler = function (e) {
e.preventDefault();
const targetId = this.getAttribute('href').substring(1);
@@ -191,12 +200,21 @@ import { UI } from 'astrowind:config';
behavior: 'smooth',
});
}
});
};
anchor.addEventListener('click', handler);
smoothScrollHandlers.set(anchor, handler);
});
// Handle links that contain # but don't start with it (page + anchor)
document.querySelectorAll('a[href*="#"]:not([href^="#"])').forEach((anchor) => {
anchor.addEventListener('click', function (e) {
// Remove old handler if exists
const oldHandler = smoothScrollHandlers.get(anchor);
if (oldHandler) {
anchor.removeEventListener('click', oldHandler);
}
const handler = function (e) {
const href = this.getAttribute('href');
const isHashLink = this.getAttribute('data-hash-link') === 'true';
@@ -240,7 +258,10 @@ import { UI } from 'astrowind:config';
window.location.href = href;
}
}
});
};
anchor.addEventListener('click', handler);
smoothScrollHandlers.set(anchor, handler);
});
}

View File

@@ -88,40 +88,92 @@ const { language, textDirection } = I18N;
<CookieBanner />
<BackToTop />
<script type="text/javascript">
// Initialize RocketChat livechat widget without client-facing logs
(function(w, d, s, u) {
w.RocketChat = function(c) {
if (w.RocketChat._) {
w.RocketChat._.push(c);
} else {
w.RocketChat._ = [c];
}
};
if (!w.RocketChat._) {
w.RocketChat._ = [];
}
w.RocketChat.url = u;
function loadRocketChat() {
try {
const h = d.getElementsByTagName(s)[0];
if (!h) {
<script is:inline>
// Initialize RocketChat livechat widget
(function() {
// Prevent multiple initializations
if (window.rocketChatInitialized) {
return;
}
const j = d.createElement(s);
j.async = true;
j.src = 'https://chat.365devnet.eu/livechat/rocketchat-livechat.min.js?_=201903270000';
j.onerror = function() {};
h.parentNode.insertBefore(j, h);
} catch (_) {}
}
if (d.readyState === 'loading') {
d.addEventListener('DOMContentLoaded', loadRocketChat);
window.rocketChatInitialized = true;
// Initialize RocketChat API
window.RocketChat = function(c) {
if (window.RocketChat._) {
window.RocketChat._.push(c);
} else {
loadRocketChat();
window.RocketChat._ = [c];
}
})(window, document, 'script', 'https://chat.365devnet.eu/livechat');
};
if (!window.RocketChat._) {
window.RocketChat._ = [];
}
window.RocketChat.url = 'https://chat.365devnet.eu/livechat';
function loadRocketChatScript() {
// Check if script is already loaded
if (document.querySelector('script[src*="rocketchat-livechat"]')) {
return;
}
try {
const firstScript = document.getElementsByTagName('script')[0];
if (!firstScript || !firstScript.parentNode) {
return;
}
const script = document.createElement('script');
script.async = true;
script.src = 'https://chat.365devnet.eu/livechat/rocketchat-livechat.min.js?_=201903270000';
script.onerror = function() {
// Silently fail - don't show errors to users
window.rocketChatInitialized = false;
};
script.onload = function() {
// Script loaded successfully
};
firstScript.parentNode.insertBefore(script, firstScript);
} catch (e) {
// Silently fail - don't show errors to users
window.rocketChatInitialized = false;
}
}
// Load on initial page load
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', loadRocketChatScript);
} else {
loadRocketChatScript();
}
// Reinitialize after Astro View Transitions
document.addEventListener('astro:after-swap', function() {
// Reset initialization flag to allow reload
window.rocketChatInitialized = false;
// Small delay to ensure DOM is ready
setTimeout(function() {
if (window.RocketChat && window.RocketChat.livechat) {
// Widget already exists, just ensure it's visible
try {
window.RocketChat.livechat.maximizeWidget();
} catch (e) {
// If maximizeWidget fails, widget might need full reload
loadRocketChatScript();
}
} else {
// Widget doesn't exist, load it
loadRocketChatScript();
}
}, 100);
});
})();
</script>
<ImageModal />