gpt-4o-2024-08-06· OpenAI
WildChat-4.8M (AI2)
Your task is to review a pull request. Instructions: - Write in name of g4f copilot.…
- User
Your task is to review a pull request. Instructions:
- Write in name of g4f copilot. Don't use placeholder.
- Write the review in GitHub Markdown format.
- Thank the author for contributing to the project.
Pull request author: None Pull request title: [pull] main from xtekky:main Pull request description:
See Commits and Changes for more details.
Created by pull[bot]
Can you help keep this open source service alive? 💖 Please sponsor : )
Diff:
diff --git a/g4f/gui/client/static/js/chat.v1.js b/g4f/gui/client/static/js/chat.v1.js index 9790b261d5..d5a1ffaa85 100644 --- a/g4f/gui/client/static/js/chat.v1.js +++ b/g4f/gui/client/static/js/chat.v1.js @@ -57,6 +57,25 @@ function filter_message(text) { ) } +function fallback_clipboard (text) { + var textBox = document.createElement("textarea"); + textBox.value = text; + textBox.style.top = "0"; + textBox.style.left = "0"; + textBox.style.position = "fixed"; + document.body.appendChild(textBox); + textBox.focus(); + textBox.select(); + try { + var success = document.execCommand('copy'); + var msg = success ? 'succeeded' : 'failed'; + console.log('Clipboard Fallback: Copying text command ' + msg); + } catch (e) { + console.error('Clipboard Fallback: Unable to copy', e); + } + document.body.removeChild(textBox); +} + hljs.addPlugin(new CopyButtonPlugin()); let typesetPromise = Promise.resolve(); const highlight = (container) => { @@ -88,18 +107,31 @@ const register_message_buttons = async () => { }) } }); + document.querySelectorAll(".message .fa-clipboard").forEach(async (el) => { if (!("click" in el.dataset)) { el.dataset.click = "true"; el.addEventListener("click", async () => { const message_el = el.parentElement.parentElement.parentElement; const copyText = await get_message(window.conversation_id, message_el.dataset.index); - navigator.clipboard.writeText(copyText); + + try { + if (!navigator.clipboard) { + throw new Error("navigator.clipboard: Clipboard API unavailable."); + } + await navigator.clipboard.writeText(copyText); + } catch (e) { + console.error(e); + console.error("Clipboard API writeText() failed! Fallback to document.exec(\"copy\")..."); + fallback_clipboard(copyText); + } + el.classList.add("clicked"); setTimeout(() => el.classList.remove("clicked"), 1000); }) } }); + document.querySelectorAll(".message .fa-volume-high").forEach(async (el) => { if (!("click" in el.dataset)) { el.dataset.click = "true"; @@ -1424,4 +1456,4 @@ if (SpeechRecognition) { recognition.start(); } }); -} \ No newline at end of file +} diff --git a/g4f/gui/client/static/js/highlightjs-copy.min.js b/g4f/gui/client/static/js/highlightjs-copy.min.js index ac11d33ec0..cd8ae9573b 100644 --- a/g4f/gui/client/static/js/highlightjs-copy.min.js +++ b/g4f/gui/client/static/js/highlightjs-copy.min.js @@ -1 +1,54 @@ -class CopyButtonPlugin{constructor(options={}){self.hook=options.hook;self.callback=options.callback}"after:highlightElement"({el,text}){let button=Object.assign(document.createElement("button"),{innerHTML:"Copy",className:"hljs-copy-button"});button.dataset.copied=false;el.parentElement.classList.add("hljs-copy-wrapper");el.parentElement.appendChild(button);el.parentElement.style.setProperty("--hljs-theme-background",window.getComputedStyle(el).backgroundColor);button.onclick=function(){if(!navigator.clipboard)return;let newText=text;if(hook&&typeof hook==="function"){newText=hook(text,el)||text}navigator.clipboard.writeText(newText).then(function(){button.innerHTML="Copied!";button.dataset.copied=true;let alert=Object.assign(document.createElement("div"),{role:"status",className:"hljs-copy-alert",innerHTML:"Copied to clipboard"});el.parentElement.appendChild(alert);setTimeout(()=>{button.innerHTML="Copy";button.dataset.copied=false;el.parentElement.removeChild(alert);alert=null},2e3)}).then(function(){if(typeof callback==="function")return callback(newText,el)})}}} \ No newline at end of file +class CopyButtonPlugin { + constructor(options = {}) { + self.hook = options.hook; + self.callback = options.callback + } + "after:highlightElement"({ + el, + text + }) { + let button = Object.assign(document.createElement("button"), { + innerHTML: "Copy", + className: "hljs-copy-button" + }); + button.dataset.copied = false; + el.parentElement.classList.add("hljs-copy-wrapper"); + el.parentElement.appendChild(button); + el.parentElement.style.setProperty("--hljs-theme-background", window.getComputedStyle(el).backgroundColor); + button.onclick = async () => { + let newText = text; + if (hook && typeof hook === "function") { + newText = hook(text, el) || text + } + try { + if (!navigator.clipboard) { + throw new Error("navigator.clipboard: Clipboard API unavailable."); + } + await navigator.clipboard.writeText(newText); + } catch (e) { + console.error(e); + console.error("Clipboard API writeText() failed! Fallback to document.exec(\"copy\")..."); + fallback_clipboard(newText); + } + button.innerHTML = "Copied!"; + button.dataset.copied = true; + let alert = Object.assign(document.createElement("div"), { + role: "status", + className: "hljs-copy-alert", + innerHTML: "Copied to clipboard" + }); + el.parentElement.appendChild(alert); + setTimeout(() => { + button.innerHTML = "Copy"; + button.dataset.copied = false; + el.parentElement.removeChild(alert); + alert = null + }, 2e3) + } + + + if (typeof callback === "function") return callback(newText, el); + + } + +} diff --git a/g4f/version.py b/g4f/version.py index eda2b8fe79..403ce37049 100644 --- a/g4f/version.py +++ b/g4f/version.py @@ -116,4 +116,4 @@ def check_version(self) -> None: except Exception as e: print(f'Failed to check g4f version: {e}') -utils = VersionUtils() \ No newline at end of file +utils = VersionUtils() - Assistant
Review by g4f copilot
Pull Request Summary
Thank you for your contribution to the project! This pull request introduces the following changes:
- Enhanced clipboard functionality with a fallback mechanism using
document.execCommand('copy')for when the Clipboard API is unavailable. - Modified the
CopyButtonPluginto include error handling for clipboard operations. - Added new utility methods for clipboard operations within the chat application's JavaScript file (
chat.v1.js).
Code Review
JavaScript Changes (
chat.v1.jsandhighlightjs-copy.min.js):-
Fallback Clipboard Functionality:
- The addition of the
fallback_clipboardfunction provides a robust solution for copying text when the Clipboard API is unavailable. This improves the user experience by ensuring clipboard functionality across different browsers and environments. - Code Structure: The function is well-structured with clear handling of potential exceptions during the execution of the
document.execCommand('copy').
- The addition of the
-
CopyButtonPluginEnhancements:- Error Handling: The adjustments to the
CopyButtonPluginincorporate proper error handling, ensuring the application logs any issues with clipboard operations. This aids in debugging and maintaining application reliability. - UI/UX Improvements: The plugin now makes use of alerts and status messages to inform users when text has been copied, which enhances user feedback.
- Error Handling: The adjustments to the
General Observations:
- Code Consistency: The updates maintain consistency with the existing codebase, using similar methods to manage DOM elements and implement new logic.
- Error Management: Well-placed error logging enables easier detection of issues but consider using a logging library or system for easier debugging of logged messages.
Recommendations
-
Testing Across Browsers:
- Ensure that the clipboard functionality works seamlessly across different browsers and platforms, given that some might have differing levels of support for clipboard operations.
-
Future Considerations:
- As browser support for JavaScript APIs evolves, revisit the implementation to ensure compatibility with future updates or deprecations.
-
Code Comments:
- More detailed comments could be added in the functions for future developers to understand the logic and flow of operations implemented.
Overall, these changes implement a significant improvement to the clipboard functionalities of the application. Thank you again for your valuable contribution to the project! If you have any questions or need further assistance, feel free to reach out.
- Enhanced clipboard functionality with a fallback mechanism using
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.