-
Notifications
You must be signed in to change notification settings - Fork 116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
issue fixed mini chatbot #370
issue fixed mini chatbot #370
Conversation
WalkthroughThe changes introduce a new web-based chat interface for a waste management chatbot through the addition of Changes
Poem
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
❌ Deploy Preview for manageyourwaste failed. Why did it fail? →
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 4
🧹 Outside diff range and nitpick comments (3)
index.html (2)
17-55
: LGTM! Consider adding focus styles for keyboard accessibility.The new CSS classes for the chatbot button are well-structured and provide a good user experience. The fixed positioning and z-index ensure the button is always visible, and the tooltip is a nice touch.
Consider adding focus styles to the
.chatbot-button
class to improve keyboard accessibility. For example:.chatbot-button:focus { outline: 2px solid #fff; outline-offset: 2px; }
493-503
: LGTM! Consider adding aria-label for screen readers.The new HTML structure for the chatbot button is well-organized and includes all necessary elements. The image and tooltip enhance the user experience, and the button is properly linked to the chatbot page.
To improve accessibility for screen readers, consider adding an
aria-label
to the button. For example:<button class="chatbot-button group" aria-label="Open chatbot">This will provide context for users relying on screen readers.
chatbot.html (1)
105-105
: Remove unnecessary background color stylingThe background color set for the body might have been used for development or testing purposes. If it's not intended for the final design, consider removing it to avoid unintended styling in production.
Apply this diff to remove the background color:
- background-color: #42c553; /* Optional: to visualize the centering */
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
⛔ Files ignored due to path filters (2)
bot.jpg
is excluded by!**/*.jpg
chatbot.gif
is excluded by!**/*.gif
📒 Files selected for processing (2)
- chatbot.html (1 hunks)
- index.html (2 hunks)
🧰 Additional context used
🪛 Gitleaks
chatbot.html
124-124: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.
(generic-api-key)
🔇 Additional comments (3)
index.html (2)
56-64
: LGTM! Good adjustments for visual consistency.The changes to the scroll-to-top button's position and color are appropriate. Moving it up accommodates the new chatbot button, and matching the color creates visual consistency.
Line range hint
1-553
: Overall, great implementation of the chatbot feature!The changes in this file successfully introduce the mini chatbot feature as outlined in the PR objectives. The new chatbot button is well-integrated into the existing page, with appropriate styling and positioning. The modifications to the scroll-to-top button ensure visual consistency and prevent overlap with the new chatbot button.
A few minor suggestions for accessibility improvements have been made, which would further enhance the user experience for all users.
chatbot.html (1)
170-196
: Check for unhandled exceptions during message generationWhile the try-catch block handles errors during message generation, consider ensuring that all possible exceptions are properly caught and logged. Additionally, verify that the user receives appropriate feedback in all error scenarios.
Run the following script to check for any unhandled promise rejections or exceptions in the codebase related to
sendMessage
function:This script will help identify any async functions that may not be properly handling exceptions.
if (message.startsWith("```") && message.endsWith("```")) { | ||
// Code block handling | ||
const codeContent = message.substring(3, message.length - 3); | ||
const escapedCode = codeContent | ||
.replace(/</g, "<") | ||
.replace(/>/g, ">"); | ||
msgDiv.innerHTML = `<pre><code class="hljs">${escapedCode}</code></pre>`; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Address potential XSS vulnerabilities due to insufficient input sanitization
When setting innerHTML
with user-generated content, there's a risk of Cross-Site Scripting (XSS) attacks. The current code replaces <
and >
characters, but this may not be sufficient to prevent XSS in all cases. Consider using a robust sanitization library like DOMPurify to ensure all malicious scripts are neutralized.
To fix this issue, you can include DOMPurify and sanitize the content before setting innerHTML
:
Add DOMPurify to your project:
<script src="https://cdn.jsdelivr.net/npm/dompurify@2.4.0/dist/purify.min.js"></script>
Update the code block handling in both AI and user messages:
- msgDiv.innerHTML = `<pre><code class="hljs">${escapedCode}</code></pre>`;
+ const safeCode = DOMPurify.sanitize(escapedCode);
+ msgDiv.innerHTML = `<pre><code class="hljs">${safeCode}</code></pre>`;
For regular messages:
- msgDiv.innerHTML = marked.parse(message);
+ const safeMessage = DOMPurify.sanitize(marked.parse(message));
+ msgDiv.innerHTML = safeMessage;
Also applies to: 260-266
if (message.startsWith("```") && message.endsWith("```")) { | ||
// Code block handling | ||
const codeContent = message.substring(3, message.length - 3); | ||
const escapedCode = codeContent | ||
.replace(/</g, "<") | ||
.replace(/>/g, ">"); | ||
msgDiv.innerHTML = `<pre><code class="hljs">${escapedCode}</code></pre>`; | ||
window.hljs.highlightBlock(msgDiv.querySelector("code")); | ||
|
||
// Add Copy button | ||
const copyButton = document.createElement("button"); | ||
copyButton.innerText = "Copy code"; | ||
copyButton.onclick = function () { | ||
navigator.clipboard.writeText(codeContent).then( | ||
() => { | ||
// Change text to show confirmation instead of using alert | ||
copyButton.innerText = "Copied!"; | ||
// Optional: revert the button text back to "Copy code" after 2 seconds | ||
setTimeout(() => { | ||
copyButton.innerText = "Copy code"; | ||
}, 2000); | ||
}, | ||
(err) => { | ||
console.error("Failed to copy text: ", err); | ||
} | ||
); | ||
}; | ||
|
||
msgDiv.appendChild(copyButton); | ||
} else { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Refactor duplicated code for code block handling
The code for handling code blocks is duplicated in both the AI and user message rendering logic. To enhance maintainability and follow the DRY (Don't Repeat Yourself) principle, consider extracting this logic into a reusable function.
You can create a helper function to handle code blocks:
function renderCodeBlock(codeContent) {
const escapedCode = codeContent.replace(/</g, "<").replace(/>/g, ">");
return `<pre><code class="hljs">${escapedCode}</code></pre>`;
}
function addCopyButton(msgDiv, codeContent) {
const copyButton = document.createElement("button");
copyButton.innerText = "Copy code";
copyButton.onclick = function () {
navigator.clipboard.writeText(codeContent).then(
() => {
copyButton.innerText = "Copied!";
setTimeout(() => {
copyButton.innerText = "Copy code";
}, 2000);
},
(err) => {
console.error("Failed to copy text: ", err);
}
);
};
msgDiv.appendChild(copyButton);
}
Then, update your displayMessage
function to use these helpers:
if (message.startsWith("```") && message.endsWith("```")) {
const codeContent = message.substring(3, message.length - 3);
msgDiv.innerHTML = renderCodeBlock(codeContent);
window.hljs.highlightBlock(msgDiv.querySelector("code"));
addCopyButton(msgDiv, codeContent);
} else {
msgDiv.innerHTML = DOMPurify.sanitize(marked.parse(message));
}
This approach reduces code duplication and makes future maintenance easier.
Also applies to: 260-283
displayMessage( | ||
"This content is not safe for display based on current settings. or an internal error.", | ||
"ai" | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Improve error message clarity for better user experience
The error message displayed to the user combines two different scenarios, which might be confusing. Consider providing a clearer and more user-friendly message when an error occurs during message generation.
Apply this diff to refine the error message:
- displayMessage(
- "This content is not safe for display based on current settings. or an internal error.",
- "ai"
- );
+ displayMessage(
+ "Sorry, an error occurred while processing your request. Please try again later.",
+ "ai"
+ );
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
displayMessage( | |
"This content is not safe for display based on current settings. or an internal error.", | |
"ai" | |
); | |
displayMessage( | |
"Sorry, an error occurred while processing your request. Please try again later.", | |
"ai" | |
); |
#360 Add mini chattbot issue fixed , This chatbot gives quike response to the users questions and runs smoothly , @GarimaSingh0109 Add gssoc-ext, Hactoberfest-accepted and level 3 to this PR ,
Screenrecording
Screen.Recording.2024-10-20.162809.mp4
Summary by CodeRabbit
New Features
Enhancements
These changes enhance user interaction and accessibility within the waste management application.