Enhance RocketChat livechat integration with improved error handling
- Refactored the script for initializing the RocketChat livechat widget to include error handling for script loading failures. - Ensured the RocketChat object is properly initialized and handles cases where it may not exist. - Added a check for DOM readiness before loading the livechat script, improving reliability and user experience.
This commit is contained in:
@@ -86,12 +86,51 @@ const { language, textDirection } = I18N;
|
||||
<BackToTop />
|
||||
|
||||
|
||||
<script type="text/javascript" defer>
|
||||
<script type="text/javascript">
|
||||
// Initialize RocketChat livechat widget with proper error handling
|
||||
(function(w, d, s, u) {
|
||||
w.RocketChat = function(c) { w.RocketChat._.push(c) }; w.RocketChat._ = []; w.RocketChat.url = u;
|
||||
const h = d.getElementsByTagName(s)[0], j = d.createElement(s);
|
||||
j.async = true; j.src = 'https://chat.365devnet.eu/livechat/rocketchat-livechat.min.js?_=201903270000';
|
||||
h.parentNode.insertBefore(j, h);
|
||||
// Initialize RocketChat object if it doesn't exist
|
||||
w.RocketChat = function(c) {
|
||||
if (w.RocketChat._) {
|
||||
w.RocketChat._.push(c);
|
||||
} else {
|
||||
w.RocketChat._ = [c];
|
||||
}
|
||||
};
|
||||
|
||||
if (!w.RocketChat._) {
|
||||
w.RocketChat._ = [];
|
||||
}
|
||||
|
||||
w.RocketChat.url = u;
|
||||
|
||||
// Wait for DOM to be ready before loading the script
|
||||
function loadRocketChat() {
|
||||
try {
|
||||
const h = d.getElementsByTagName(s)[0];
|
||||
if (!h) return;
|
||||
|
||||
const j = d.createElement(s);
|
||||
j.async = true;
|
||||
j.src = 'https://chat.365devnet.eu/livechat/rocketchat-livechat.min.js?_=201903270000';
|
||||
|
||||
// Add error handling for script loading
|
||||
j.onerror = function() {
|
||||
console.warn('RocketChat livechat script failed to load');
|
||||
};
|
||||
|
||||
h.parentNode.insertBefore(j, h);
|
||||
} catch (error) {
|
||||
console.warn('Error loading RocketChat livechat:', error);
|
||||
}
|
||||
}
|
||||
|
||||
// Load when DOM is ready
|
||||
if (d.readyState === 'loading') {
|
||||
d.addEventListener('DOMContentLoaded', loadRocketChat);
|
||||
} else {
|
||||
loadRocketChat();
|
||||
}
|
||||
})(window, document, 'script', 'https://chat.365devnet.eu/livechat');
|
||||
</script>
|
||||
|
||||
|
Reference in New Issue
Block a user