|
5 | 5 | <meta name="viewport" content="width=device-width, initial-scale=1.0">
|
6 | 6 | <title>ACCESS Q&A Bot Standalone Demo</title>
|
7 | 7 | <link rel="stylesheet" href="./build/static/css/main.css">
|
| 8 | + <!-- Load scripts in head for earlier initialization --> |
| 9 | + <script> |
| 10 | + // Determine if user is logged in by checking for a class on the body |
| 11 | + document.addEventListener('DOMContentLoaded', function() { |
| 12 | + window.isAnonymous = !document.querySelector('body').classList.contains('user-logged-in'); |
| 13 | + }); |
| 14 | + </script> |
| 15 | + <!-- Preload JavaScript bundles --> |
| 16 | + <script src="./build/static/js/main.js"></script> |
| 17 | + <script src="./build/static/js/453.chunk.js"></script> |
8 | 18 | <style>
|
9 | 19 | body {
|
10 | 20 | font-family: Arial, sans-serif;
|
@@ -61,43 +71,62 @@ <h2>Method 3: Explicitly Call JavaScript Function</h2>
|
61 | 71 | </div>
|
62 | 72 |
|
63 | 73 | <script>
|
64 |
| - // Determine if user is logged in by checking for a class on the body |
65 |
| - function isAnonymous() { |
66 |
| - return !document.querySelector('body').classList.contains('user-logged-in'); |
67 |
| - } |
68 |
| - window.isAnonymous = isAnonymous(); |
69 |
| - </script> |
| 74 | + // Initialize all methods once DOM is fully loaded and scripts are available |
| 75 | + document.addEventListener('DOMContentLoaded', function() { |
| 76 | + // Ensure window.qAndATool is available before proceeding |
| 77 | + function initializeQABots() { |
| 78 | + if (window.qAndATool) { |
| 79 | + // Method 1: Initialize the root element if not already done |
| 80 | + const qaBot = document.getElementById('qa-bot'); |
| 81 | + if (qaBot && !qaBot.hasChildNodes()) { |
| 82 | + console.log("Initializing Method 1 - Root QA Bot"); |
| 83 | + window.qAndATool({ |
| 84 | + target: qaBot, |
| 85 | + isLoggedIn: !window.isAnonymous, |
| 86 | + isAnonymous: window.isAnonymous |
| 87 | + }); |
| 88 | + } |
70 | 89 |
|
71 |
| - <!-- |
72 |
| - Load the main JavaScript bundle which includes: |
73 |
| - 1. The React components that power the Q&A tool |
74 |
| - 2. The globally exposed qAndATool function (from src/index.js) |
| 90 | + // Method 2: Initialize embedded-qa-bot elements |
| 91 | + const embeddedBots = document.querySelectorAll('.embedded-qa-bot'); |
| 92 | + embeddedBots.forEach(function(bot) { |
| 93 | + if (!bot.hasChildNodes()) { |
| 94 | + console.log("Initializing Method 2 - Embedded QA Bot"); |
| 95 | + window.qAndATool({ |
| 96 | + target: bot, |
| 97 | + embedded: true, |
| 98 | + isOpen: true, |
| 99 | + welcome: bot.getAttribute('data-welcome') || "Hello!", |
| 100 | + prompt: bot.getAttribute('data-prompt') || "Ask me a question...", |
| 101 | + isLoggedIn: !window.isAnonymous, |
| 102 | + isAnonymous: window.isAnonymous |
| 103 | + }); |
| 104 | + } |
| 105 | + }); |
75 | 106 |
|
76 |
| - Note: All three integration methods depend on window.qAndATool |
77 |
| - being exposed by the JavaScript bundle |
78 |
| - --> |
79 |
| - <script src="./build/static/js/main.js"></script> |
80 |
| - <script src="./build/static/js/453.chunk.js"></script> |
| 107 | + // Method 3: Initialize the custom-qa-bot element |
| 108 | + console.log("Initializing Method 3 - Custom QA Bot"); |
| 109 | + window.qAndATool({ |
| 110 | + target: document.getElementById('custom-qa-bot'), |
| 111 | + embedded: true, |
| 112 | + welcome: "This is configured using JavaScript!", |
| 113 | + prompt: "Try asking a question about ACCESS...", |
| 114 | + isLoggedIn: !window.isAnonymous, |
| 115 | + isAnonymous: window.isAnonymous, |
| 116 | + disabled: window.isAnonymous, |
| 117 | + isOpen: true |
| 118 | + }); |
| 119 | + } else { |
| 120 | + console.error("qAndATool function not found. Make sure the JS files are loaded properly."); |
| 121 | + } |
| 122 | + } |
81 | 123 |
|
82 |
| - <script> |
83 |
| - // Wait for window.qAndATool to be available |
84 |
| - window.addEventListener('load', function() { |
| 124 | + // Try to initialize immediately |
85 | 125 | if (window.qAndATool) {
|
86 |
| - // JavaScript function example |
87 |
| - window.qAndATool({ |
88 |
| - target: document.getElementById('custom-qa-bot'), |
89 |
| - embedded: true, |
90 |
| - welcome: "This is configured using JavaScript!", |
91 |
| - prompt: "Try asking a question about ACCESS...", |
92 |
| - isLoggedIn: !window.isAnonymous, |
93 |
| - disabled: window.isAnonymous, |
94 |
| - isOpen: true |
95 |
| - }); |
96 |
| - |
97 |
| - // Note: Method 1 uses the div with id="qa-bot" |
98 |
| - // and Method 2 is handled by the class-based selector |
| 126 | + initializeQABots(); |
99 | 127 | } else {
|
100 |
| - console.error("qAndATool function not found. Make sure the JS files are loaded properly."); |
| 128 | + // Or wait a bit for scripts to load if needed |
| 129 | + setTimeout(initializeQABots, 500); |
101 | 130 | }
|
102 | 131 | });
|
103 | 132 | </script>
|
|
0 commit comments