Skip to content

Commit

Permalink
Update chatbot_widget.js
Browse files Browse the repository at this point in the history
  • Loading branch information
naman1618 authored Nov 13, 2024
1 parent 9b62e97 commit e0c2b6e
Showing 1 changed file with 37 additions and 2 deletions.
39 changes: 37 additions & 2 deletions docs/chatbot_widget.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// chatbot_widget.js

document.addEventListener('DOMContentLoaded', function () {
// Create the chat icon button
const chatIcon = document.createElement('div');
Expand All @@ -16,11 +15,28 @@ document.addEventListener('DOMContentLoaded', function () {
<button id="chat-close">X</button>
</div>
<div id="chat-body">
<iframe src="https://chat-qa.cyverse.org/learning" id="chat-frame" width="100%" height="100%"></iframe>
<iframe
src="https://chat-qa.cyverse.org/learning"
id="chat-frame"
width="100%"
height="100%"
sandbox="allow-same-origin allow-scripts allow-popups allow-forms allow-top-navigation"
></iframe>
</div>
`;
document.body.appendChild(chatContainer);

// Add event listener for iframe messages
window.addEventListener('message', function(event) {
// Verify the origin of the message
if (event.origin === 'https://chat-qa.cyverse.org') {
if (event.data.type === 'link') {
// Open link in new tab
window.open(event.data.url, '_blank', 'noopener,noreferrer');
}
}
});

// Show or hide the chatbot window
chatIcon.addEventListener('click', () => {
chatContainer.style.display = chatContainer.style.display === 'block' ? 'none' : 'block';
Expand All @@ -30,4 +46,23 @@ document.addEventListener('DOMContentLoaded', function () {
document.getElementById('chat-close').addEventListener('click', () => {
chatContainer.style.display = 'none';
});

// Add event listener to handle links inside the iframe
const chatFrame = document.getElementById('chat-frame');
chatFrame.onload = function() {
try {
// Add click event listeners to all links in the iframe
const iframeLinks = chatFrame.contentDocument.getElementsByTagName('a');
Array.from(iframeLinks).forEach(link => {
link.setAttribute('target', '_blank');
link.setAttribute('rel', 'noopener noreferrer');
link.addEventListener('click', (e) => {
e.preventDefault();
window.open(link.href, '_blank', 'noopener,noreferrer');
});
});
} catch (e) {
console.log('Note: Cross-origin restrictions prevent direct iframe manipulation');
}
};
});

0 comments on commit e0c2b6e

Please sign in to comment.